humann/tools/build_custom_database.py
def filter_fasta_file(fasta_file, output_folder, genus, id_mapping):
    """ Read through each sequence in the fasta file filtering by genus """
    
    try:
        file_handle=open(fasta_file, "rt")
    except EnvironmentError:
        sys.exit("ERROR: Unable to read input fasta file: " + fasta_file)
        
    # Create a file in the output folder that is the filtered fasta file
    try:
        input_file_basename=os.path.splitext(os.path.basename(fasta_file))
        new_file=input_file_basename[0]+".filtered"+input_file_basename[1]
    except IndexError:
        new_file=os.path.basename(fasta_file)
        
    new_file=os.path.join(output_folder,new_file)
    
    # Try to open the new file to write
    try:
        file_handle_write=open(new_file,"w")
    except EnvironmentError:
        sys.exit("ERROR: Unable to write output fasta file: " + new_file)
    
    print("Writing filtered fasta file to " + new_file)
    write_sequence=False
    total_sequences_written=0
    for line in file_handle:
        if line[0] == FASTA_ID_START:
            # Get the possible mapping id
            mapping_id=line.split(FASTA_ID_DELIMITER)[0][1:]
            # Apply id mapping if included
            if mapping_id in id_mapping:
                sequence_information=id_mapping[mapping_id][-1].lower().split(WORD_DELIMITER)
            else:
                sequence_information=line[1:].lower().split(WORD_DELIMITER)
                
            # Determine if write sequence based on genus information
            write_sequence=False
            if genus:
                # Check if the sequence taxonomy is an included genus
                # The taxonomy information is a string of words and any word
                # can include the genus information
                if list(filter(lambda x: x in genus, sequence_information)):
                    write_sequence=True
            else:
                write_sequence=True
                
            if write_sequence:
                file_handle_write.write(line)
                total_sequences_written+=1
        else:
            if write_sequence:
                file_handle_write.write(line)
    
    file_handle.close()
    file_handle_write.close()
    print("Finished writing filtered fasta file")

    return new_file, total_sequences_written
 Read through each sequence in the fasta file filtering by genus 
Read through each sequence in the fasta file filtering by genus
Read through each sequence in the fasta file filtering by genus
def filter_fasta_file(fasta_file, output_folder, genus, id_mapping):
    try:
        file_handle=open(fasta_file, "rt")
    except EnvironmentError:
        sys.exit("ERROR: Unable to read input fasta file: " + fasta_file)
    try:
        input_file_basename=os.path.splitext(os.path.basename(fasta_file))
        new_file=input_file_basename[0]+".filtered"+input_file_basename[1]
    except IndexError:
        new_file=os.path.basename(fasta_file)
    new_file=os.path.join(output_folder,new_file)
    try:
        file_handle_write=open(new_file,"w")
    except EnvironmentError:
        sys.exit("ERROR: Unable to write output fasta file: " + new_file)
    print("Writing filtered fasta file to " + new_file)
    write_sequence=False
    total_sequences_written=0
    for line in file_handle:
        if line[0] == FASTA_ID_START:
            mapping_id=line.split(FASTA_ID_DELIMITER)[0][1:]
            if mapping_id in id_mapping:
                sequence_information=id_mapping[mapping_id][-1].lower().split(WORD_DELIMITER)
            else:
                sequence_information=line[1:].lower().split(WORD_DELIMITER)
            write_sequence=False
            if genus:
                if list(filter(lambda x: x in genus, sequence_information)):
                    write_sequence=True
            else:
                write_sequence=True
            if write_sequence:
                file_handle_write.write(line)
                total_sequences_written+=1
        else:
            if write_sequence:
                file_handle_write.write(line)
    file_handle.close()
    file_handle_write.close()
    print("Finished writing filtered fasta file")
    return new_file, total_sequences_written
def filter_fasta_file ( fasta_file , output_folder , genus , id_mapping ) : try : file_handle = open ( fasta_file , "rt" ) except EnvironmentError : sys . exit ( "ERROR: Unable to read input fasta file: " + fasta_file ) try : input_file_basename = os . path . splitext ( os . path . basename ( fasta_file ) ) new_file = input_file_basename [ 0 ] + ".filtered" + input_file_basename [ 1 ] except IndexError : new_file = os . path . basename ( fasta_file ) new_file = os . path . join ( output_folder , new_file ) try : file_handle_write = open ( new_file , "w" ) except EnvironmentError : sys . exit ( "ERROR: Unable to write output fasta file: " + new_file ) print ( "Writing filtered fasta file to " + new_file ) write_sequence = False total_sequences_written = 0 for line in file_handle : if line [ 0 ] == FASTA_ID_START : mapping_id = line . split ( FASTA_ID_DELIMITER ) [ 0 ] [ 1 : ] if mapping_id in id_mapping : sequence_information = id_mapping [ mapping_id ] [ - 1 ] . lower ( ) . split ( WORD_DELIMITER ) else : sequence_information = line [ 1 : ] . lower ( ) . split ( WORD_DELIMITER ) write_sequence = False if genus : if list ( filter ( lambda x : x in genus , sequence_information ) ) : write_sequence = True else : write_sequence = True if write_sequence : file_handle_write . write ( line ) total_sequences_written += 1 else : if write_sequence : file_handle_write . write ( line ) file_handle . close ( ) file_handle_write . close ( ) print ( "Finished writing filtered fasta file" ) return new_file , total_sequences_written
Read through each sequence in the fasta file filtering by genus
Read through each sequence in the fasta file filtering by genus
""" Read through each sequence in the fasta file filtering by genus """ # Create a file in the output folder that is the filtered fasta file # Try to open the new file to write # Get the possible mapping id # Apply id mapping if included # Determine if write sequence based on genus information # Check if the sequence taxonomy is an included genus # The taxonomy information is a string of words and any word # can include the genus information
fasta_file
output_folder
genus
id_mapping