humann/tools/merge_abundance.py
def read_mapping(gene_mapping_file,pathway_mapping_file):
    """
    Read the gene to reaction and then reaction to pathway mappings
    """
    
    # read the gene to reaction mappings
    reactions_to_genes={} 
    reactions_to_ecs={}
    try:
        if gene_mapping_file.endswith(".gz"):
            file_handle=gzip.open(gene_mapping_file, "rt")
            readlines = file_handle.readlines()
        elif gene_mapping_file.endswith(".bz2"):
            file_handle = bz2.BZ2File(gene_mapping_file, "r")
            readlines = [line.decode('utf-8') for line in file_handle.readlines()]
        else:
            file_handle=open(gene_mapping_file, "rt")
            readlines = file_handle.readlines()
    except EnvironmentError:
        sys.exit("ERROR: Unable to open the mapping file: " + gene_mapping_file)
            
    file_handle.close()
    
    # read through the mapping lines
    for line in readlines:
        data=line.rstrip().split(TABLE_DELIMITER)
        reaction=data.pop(0)
        ecs=data.pop(0).split(EC_DELIMITER)
        reactions_to_genes[reaction]=data
        reactions_to_ecs[reaction]=ecs
                
    # read the reaction to pathway mappings
    pathways_to_genes={}
    pathways_to_ecs={}
    try:
        file_handle=open(pathway_mapping_file, "rt")
    except EnvironmentError:
        sys.exit("ERROR: Unable to open the mapping file: " + pathway_mapping_file)
        
    for line in file_handle:
        info=line.rstrip().split(TABLE_DELIMITER)
        if len(info) == 2:
            pathway, structure = info
            # get the reactions from the structure
            genes=set()
            ecs=set()
            for item in structure.split(" "):
                genes.update(reactions_to_genes.get(item,[]))
                ecs.update(reactions_to_ecs.get(item,[]))
            pathways_to_genes[pathway]=genes
            pathways_to_ecs[pathway]=ecs
        
    file_handle.close()
  
    return pathways_to_genes,pathways_to_ecs

    Read the gene to reaction and then reaction to pathway mappings
    
Read the gene to reaction and then reaction to pathway mappings
Read the gene to reaction and then reaction to pathway mappings
def read_mapping(gene_mapping_file,pathway_mapping_file):
    reactions_to_genes={} 
    reactions_to_ecs={}
    try:
        if gene_mapping_file.endswith(".gz"):
            file_handle=gzip.open(gene_mapping_file, "rt")
            readlines = file_handle.readlines()
        elif gene_mapping_file.endswith(".bz2"):
            file_handle = bz2.BZ2File(gene_mapping_file, "r")
            readlines = [line.decode('utf-8') for line in file_handle.readlines()]
        else:
            file_handle=open(gene_mapping_file, "rt")
            readlines = file_handle.readlines()
    except EnvironmentError:
        sys.exit("ERROR: Unable to open the mapping file: " + gene_mapping_file)
    file_handle.close()
    for line in readlines:
        data=line.rstrip().split(TABLE_DELIMITER)
        reaction=data.pop(0)
        ecs=data.pop(0).split(EC_DELIMITER)
        reactions_to_genes[reaction]=data
        reactions_to_ecs[reaction]=ecs
    pathways_to_genes={}
    pathways_to_ecs={}
    try:
        file_handle=open(pathway_mapping_file, "rt")
    except EnvironmentError:
        sys.exit("ERROR: Unable to open the mapping file: " + pathway_mapping_file)
    for line in file_handle:
        info=line.rstrip().split(TABLE_DELIMITER)
        if len(info) == 2:
            pathway, structure = info
            genes=set()
            ecs=set()
            for item in structure.split(" "):
                genes.update(reactions_to_genes.get(item,[]))
                ecs.update(reactions_to_ecs.get(item,[]))
            pathways_to_genes[pathway]=genes
            pathways_to_ecs[pathway]=ecs
    file_handle.close()
    return pathways_to_genes,pathways_to_ecs
def read_mapping ( gene_mapping_file , pathway_mapping_file ) : reactions_to_genes = { } reactions_to_ecs = { } try : if gene_mapping_file . endswith ( ".gz" ) : file_handle = gzip . open ( gene_mapping_file , "rt" ) readlines = file_handle . readlines ( ) elif gene_mapping_file . endswith ( ".bz2" ) : file_handle = bz2 . BZ2File ( gene_mapping_file , "r" ) readlines = [ line . decode ( 'utf-8' ) for line in file_handle . readlines ( ) ] else : file_handle = open ( gene_mapping_file , "rt" ) readlines = file_handle . readlines ( ) except EnvironmentError : sys . exit ( "ERROR: Unable to open the mapping file: " + gene_mapping_file ) file_handle . close ( ) for line in readlines : data = line . rstrip ( ) . split ( TABLE_DELIMITER ) reaction = data . pop ( 0 ) ecs = data . pop ( 0 ) . split ( EC_DELIMITER ) reactions_to_genes [ reaction ] = data reactions_to_ecs [ reaction ] = ecs pathways_to_genes = { } pathways_to_ecs = { } try : file_handle = open ( pathway_mapping_file , "rt" ) except EnvironmentError : sys . exit ( "ERROR: Unable to open the mapping file: " + pathway_mapping_file ) for line in file_handle : info = line . rstrip ( ) . split ( TABLE_DELIMITER ) if len ( info ) == 2 : pathway , structure = info genes = set ( ) ecs = set ( ) for item in structure . split ( " " ) : genes . update ( reactions_to_genes . get ( item , [ ] ) ) ecs . update ( reactions_to_ecs . get ( item , [ ] ) ) pathways_to_genes [ pathway ] = genes pathways_to_ecs [ pathway ] = ecs file_handle . close ( ) return pathways_to_genes , pathways_to_ecs
Read the gene to reaction and then reaction to pathway mappings
Read the gene to reaction and then reaction to pathway mappings
"""
    Read the gene to reaction and then reaction to pathway mappings
    """ # read the gene to reaction mappings # read through the mapping lines # read the reaction to pathway mappings # get the reactions from the structure
gene_mapping_file
pathway_mapping_file