humann/tools/merge_abundance.py
def merge_abundances(gene_table,pathways_to_genes,input_pathways,output,additional_gene_info,remove_taxonomy):
    """
    Read through the pathway abundances file
    Write the merged abundances
    """
    
    lines=util.process_gene_table_with_header(input_pathways, allow_for_missing_header=True)
    header=next(lines)
    
    # open the output file
    try:
        write_file_handle=open(output,"w")
    except EnvironmentError:
        sys.exit("ERROR: Unable to write to output file: " + output)
        
    if header:
        write_file_handle.write(header+"\n")
        
    # read through the pathways mapping file, then merge in the gene families information
    # read thorugh the file first to allow for unordered input files
    pathways={}
    pathways_by_bug={}
    pathway_names={}
    for line in lines:
        data=line.split(TABLE_DELIMITER)
        bug=""
        pathway=data[0]
        if BUG_DELIMITER in line:
            pathway,bug=data[0].split(BUG_DELIMITER)
        
        # remove pathway name if present
        if GENE_NAME_DELIMITER in pathway:
            info=pathway.split(GENE_NAME_DELIMITER)
            pathway = info[0]
            pathway_names[pathway]=" ".join(info[1:])
       
        try:
            data_point=float(data[-1])
        except ValueError:
            data_point=0
 
        if data_point > 0:
            if bug:
                if not pathway in pathways_by_bug:
                    pathways_by_bug[pathway]=[]
                pathways_by_bug[pathway].append([bug,data[-1]])
            else:
                pathways[pathway]=data[-1]

    for pathway in sorted(pathways):
        # check if the name should be added to the pathway
        pathway_with_name=pathway
        if pathway in pathway_names:
            pathway_with_name+=GENE_NAME_DELIMITER+pathway_names[pathway]
        
        write_file_handle.write(TABLE_DELIMITER.join([pathway_with_name,pathways[pathway]])+"\n")
        
        # do not write the taxonomy if set
        if remove_taxonomy:
            for gene in pathways_to_genes.get(pathway,[]):
                if gene in gene_table:
                    # check if additional information should be added to the gene
                    gene_with_info=gene
                    if gene in additional_gene_info:
                        gene_with_info+=GENE_NAME_DELIMITER+additional_gene_info[gene]
                    
                     # look for the overall gene abundance
                    abundance=gene_table[gene].get("all",0)
                    if abundance > 0:
                        output_line=TABLE_DELIMITER.join([BUG_DELIMITER.join([pathway,gene_with_info]),str(abundance)])
                        write_file_handle.write(output_line+"\n")
        else:
            for item in pathways_by_bug.get(pathway,[]):
                bug, data = item
                output_line=TABLE_DELIMITER.join([BUG_DELIMITER.join([pathway,bug]), data])
                write_file_handle.write(output_line+"\n")
                # check for gene families for this pathway for this bug
                for gene in pathways_to_genes.get(pathway,[]):
                    if gene in gene_table:
                        # check if additional information should be added to the gene
                        gene_with_info=gene
                        if gene in additional_gene_info:
                            gene_with_info+=GENE_NAME_DELIMITER+additional_gene_info[gene]
                    
                        abundance=gene_table[gene].get(bug,0)
                        if abundance > 0:
                            output_line=TABLE_DELIMITER.join([BUG_DELIMITER.join([pathway,bug,gene_with_info]),str(abundance)])
                            write_file_handle.write(output_line+"\n")
                            
    write_file_handle.close()

    Read through the pathway abundances file
    Write the merged abundances
    
Read through the pathway abundances file
Write the merged abundances
Read through the pathway abundances file Write the merged abundances
def merge_abundances(gene_table,pathways_to_genes,input_pathways,output,additional_gene_info,remove_taxonomy):
    lines=util.process_gene_table_with_header(input_pathways, allow_for_missing_header=True)
    header=next(lines)
    try:
        write_file_handle=open(output,"w")
    except EnvironmentError:
        sys.exit("ERROR: Unable to write to output file: " + output)
    if header:
        write_file_handle.write(header+"\n")
    pathways={}
    pathways_by_bug={}
    pathway_names={}
    for line in lines:
        data=line.split(TABLE_DELIMITER)
        bug=""
        pathway=data[0]
        if BUG_DELIMITER in line:
            pathway,bug=data[0].split(BUG_DELIMITER)
        if GENE_NAME_DELIMITER in pathway:
            info=pathway.split(GENE_NAME_DELIMITER)
            pathway = info[0]
            pathway_names[pathway]=" ".join(info[1:])
        try:
            data_point=float(data[-1])
        except ValueError:
            data_point=0
        if data_point > 0:
            if bug:
                if not pathway in pathways_by_bug:
                    pathways_by_bug[pathway]=[]
                pathways_by_bug[pathway].append([bug,data[-1]])
            else:
                pathways[pathway]=data[-1]
    for pathway in sorted(pathways):
        pathway_with_name=pathway
        if pathway in pathway_names:
            pathway_with_name+=GENE_NAME_DELIMITER+pathway_names[pathway]
        write_file_handle.write(TABLE_DELIMITER.join([pathway_with_name,pathways[pathway]])+"\n")
        if remove_taxonomy:
            for gene in pathways_to_genes.get(pathway,[]):
                if gene in gene_table:
                    gene_with_info=gene
                    if gene in additional_gene_info:
                        gene_with_info+=GENE_NAME_DELIMITER+additional_gene_info[gene]
                    abundance=gene_table[gene].get("all",0)
                    if abundance > 0:
                        output_line=TABLE_DELIMITER.join([BUG_DELIMITER.join([pathway,gene_with_info]),str(abundance)])
                        write_file_handle.write(output_line+"\n")
        else:
            for item in pathways_by_bug.get(pathway,[]):
                bug, data = item
                output_line=TABLE_DELIMITER.join([BUG_DELIMITER.join([pathway,bug]), data])
                write_file_handle.write(output_line+"\n")
                for gene in pathways_to_genes.get(pathway,[]):
                    if gene in gene_table:
                        gene_with_info=gene
                        if gene in additional_gene_info:
                            gene_with_info+=GENE_NAME_DELIMITER+additional_gene_info[gene]
                        abundance=gene_table[gene].get(bug,0)
                        if abundance > 0:
                            output_line=TABLE_DELIMITER.join([BUG_DELIMITER.join([pathway,bug,gene_with_info]),str(abundance)])
                            write_file_handle.write(output_line+"\n")
    write_file_handle.close()
def merge_abundances ( gene_table , pathways_to_genes , input_pathways , output , additional_gene_info , remove_taxonomy ) : lines = util . process_gene_table_with_header ( input_pathways , allow_for_missing_header = True ) header = next ( lines ) try : write_file_handle = open ( output , "w" ) except EnvironmentError : sys . exit ( "ERROR: Unable to write to output file: " + output ) if header : write_file_handle . write ( header + "\n" ) pathways = { } pathways_by_bug = { } pathway_names = { } for line in lines : data = line . split ( TABLE_DELIMITER ) bug = "" pathway = data [ 0 ] if BUG_DELIMITER in line : pathway , bug = data [ 0 ] . split ( BUG_DELIMITER ) if GENE_NAME_DELIMITER in pathway : info = pathway . split ( GENE_NAME_DELIMITER ) pathway = info [ 0 ] pathway_names [ pathway ] = " " . join ( info [ 1 : ] ) try : data_point = float ( data [ - 1 ] ) except ValueError : data_point = 0 if data_point > 0 : if bug : if not pathway in pathways_by_bug : pathways_by_bug [ pathway ] = [ ] pathways_by_bug [ pathway ] . append ( [ bug , data [ - 1 ] ] ) else : pathways [ pathway ] = data [ - 1 ] for pathway in sorted ( pathways ) : pathway_with_name = pathway if pathway in pathway_names : pathway_with_name += GENE_NAME_DELIMITER + pathway_names [ pathway ] write_file_handle . write ( TABLE_DELIMITER . join ( [ pathway_with_name , pathways [ pathway ] ] ) + "\n" ) if remove_taxonomy : for gene in pathways_to_genes . get ( pathway , [ ] ) : if gene in gene_table : gene_with_info = gene if gene in additional_gene_info : gene_with_info += GENE_NAME_DELIMITER + additional_gene_info [ gene ] abundance = gene_table [ gene ] . get ( "all" , 0 ) if abundance > 0 : output_line = TABLE_DELIMITER . join ( [ BUG_DELIMITER . join ( [ pathway , gene_with_info ] ) , str ( abundance ) ] ) write_file_handle . write ( output_line + "\n" ) else : for item in pathways_by_bug . get ( pathway , [ ] ) : bug , data = item output_line = TABLE_DELIMITER . join ( [ BUG_DELIMITER . join ( [ pathway , bug ] ) , data ] ) write_file_handle . write ( output_line + "\n" ) for gene in pathways_to_genes . get ( pathway , [ ] ) : if gene in gene_table : gene_with_info = gene if gene in additional_gene_info : gene_with_info += GENE_NAME_DELIMITER + additional_gene_info [ gene ] abundance = gene_table [ gene ] . get ( bug , 0 ) if abundance > 0 : output_line = TABLE_DELIMITER . join ( [ BUG_DELIMITER . join ( [ pathway , bug , gene_with_info ] ) , str ( abundance ) ] ) write_file_handle . write ( output_line + "\n" ) write_file_handle . close ( )
Read through the pathway abundances file
Write the merged abundances
Read through the pathway abundances file Write the merged abundances
"""
    Read through the pathway abundances file
    Write the merged abundances
    """ # open the output file # read through the pathways mapping file, then merge in the gene families information # read thorugh the file first to allow for unordered input files # remove pathway name if present # check if the name should be added to the pathway # do not write the taxonomy if set # check if additional information should be added to the gene # look for the overall gene abundance # check for gene families for this pathway for this bug # check if additional information should be added to the gene
gene_table
pathways_to_genes
input_pathways
output
additional_gene_info
remove_taxonomy