humann/quantify/modules.py
def identify_reactions_and_pathways(gene_scores, reactions_database, pathways_database):
    """
    Identify the reactions and then pathways from the hits found
    """
            
    if config.minpath_toggle == "on":
        # Write a flat reactions to pathways file
        logger.debug("Write flat reactions to pathways file for Minpath")
        pathways_database_file=utilities.unnamed_temp_file()
        file_handle=open(pathways_database_file,"w")
        file_handle.write(pathways_database.get_database())
        file_handle.close()
    
    # Create a store for the pathways and reactions by bug
    pathways_and_reactions_store=store.PathwaysAndReactions()
    reactions={}
    
    minpath_results={}
    minpath_commands=[]
    # Run through each of the score sets by bug
    for bug in gene_scores.bug_list():
        gene_scores_for_bug=gene_scores.scores_for_bug(bug)
        
        # Merge the gene scores to reaction scores   
        message="Compute reaction scores for bug: " + bug
        logger.info(message)
        
        reactions[bug]={}
        reactions_file_lines=[]
        if reactions_database:
            for reaction in sorted(reactions_database.reaction_list()):
                genes_list=reactions_database.find_genes(reaction)
                abundance=0
                # Add the scores for each gene to the total score for the reaction
                for gene in genes_list:
                    abundance+=gene_scores_for_bug.get(gene,0)  
                
                # Only write out reactions where the abundance is greater than 0
                if abundance>0: 
                    reactions_file_lines.append(reaction+config.output_file_column_delimiter
                        +str(abundance)+"\n")
                    # Store the abundance data to compile with the minpath pathways
                    reactions[bug][reaction]=abundance
        else:
            for gene in gene_scores_for_bug:
                score=gene_scores_for_bug[gene]
                
                if score>0:
                    reactions_file_lines.append(gene+config.output_file_column_delimiter
                        +str(score)+"\n")
                    # Store the abundance data to compile with the minpath pathways
                    reactions[bug][gene]=score
    
        pathways={}
        # Run minpath if toggle on and also if there is more than one reaction   
        if config.minpath_toggle == "on" and len(reactions_file_lines)>3:   
    
            # Create a temp file for the reactions results
            reactions_file=utilities.unnamed_temp_file()
            file_handle=open(reactions_file,"w")
            file_handle.write("".join(reactions_file_lines))
            file_handle.close()
        
            # Run minpath to identify the pathways
            logger.info("Run MinPath on " + bug)
                
            tmpfile, command=minpath_command(reactions_file, pathways_database_file)
            minpath_results[bug]=tmpfile
            minpath_commands.append(command)
            
    # Run through the minpath commands if minpath is to be run
    if minpath_commands:
        utilities.command_threading(config.threads,minpath_commands)
    
    # Link the pathways to reactions
    for bug in gene_scores.bug_list():
        pathways={}
        if bug in minpath_results:
            tmpfile=minpath_results[bug]
            # Process the minpath results
            if os.path.isfile(tmpfile):
        
                file_handle_read=open(tmpfile, "rt")
                line=file_handle_read.readline()
                    
                while line:
                    data=line.strip().split(config.minpath_pathway_delimiter)
                    if re.search(config.minpath_pathway_identifier,line):
                        current_pathway=data[config.minpath_pathway_index]
                    else:
                        current_reaction=data[config.minpath_reaction_index]
                        # store the pathway and reaction
                        pathways[current_reaction]=pathways.get(
                            current_reaction,[]) + [current_pathway]      
                    line=file_handle_read.readline()
                
                file_handle_read.close()
            else:
                message="Empty results file from MinPath run for bug: " + bug
                print(message)
                logger.warning(message)
        else:
            # Add all pathways associated with each reaction if not using minpath
            for current_reaction in reactions.get(bug,{}):
                pathways[current_reaction]=pathways.get(
                    current_reaction, []) + pathways_database.find_pathways(current_reaction) 
         
        # Store the pathway abundance for each reaction
        for current_reaction in reactions.get(bug,{}):
            # Find the pathways associated with reaction
            for current_pathway in pathways.get(current_reaction,[""]):
                # Only store data for items with pathway names
                if current_pathway:
                    pathways_and_reactions_store.add(bug,current_reaction, current_pathway, 
                        reactions[bug][current_reaction])
   
    return pathways_and_reactions_store

    Identify the reactions and then pathways from the hits found
    
Identify the reactions and then pathways from the hits found
Identify the reactions and then pathways from the hits found
def identify_reactions_and_pathways(gene_scores, reactions_database, pathways_database):
    if config.minpath_toggle == "on":
        logger.debug("Write flat reactions to pathways file for Minpath")
        pathways_database_file=utilities.unnamed_temp_file()
        file_handle=open(pathways_database_file,"w")
        file_handle.write(pathways_database.get_database())
        file_handle.close()
    pathways_and_reactions_store=store.PathwaysAndReactions()
    reactions={}
    minpath_results={}
    minpath_commands=[]
    for bug in gene_scores.bug_list():
        gene_scores_for_bug=gene_scores.scores_for_bug(bug)
        message="Compute reaction scores for bug: " + bug
        logger.info(message)
        reactions[bug]={}
        reactions_file_lines=[]
        if reactions_database:
            for reaction in sorted(reactions_database.reaction_list()):
                genes_list=reactions_database.find_genes(reaction)
                abundance=0
                for gene in genes_list:
                    abundance+=gene_scores_for_bug.get(gene,0)  
                if abundance>0: 
                    reactions_file_lines.append(reaction+config.output_file_column_delimiter
                        +str(abundance)+"\n")
                    reactions[bug][reaction]=abundance
        else:
            for gene in gene_scores_for_bug:
                score=gene_scores_for_bug[gene]
                if score>0:
                    reactions_file_lines.append(gene+config.output_file_column_delimiter
                        +str(score)+"\n")
                    reactions[bug][gene]=score
        pathways={}
        if config.minpath_toggle == "on" and len(reactions_file_lines)>3:   
            reactions_file=utilities.unnamed_temp_file()
            file_handle=open(reactions_file,"w")
            file_handle.write("".join(reactions_file_lines))
            file_handle.close()
            logger.info("Run MinPath on " + bug)
            tmpfile, command=minpath_command(reactions_file, pathways_database_file)
            minpath_results[bug]=tmpfile
            minpath_commands.append(command)
    if minpath_commands:
        utilities.command_threading(config.threads,minpath_commands)
    for bug in gene_scores.bug_list():
        pathways={}
        if bug in minpath_results:
            tmpfile=minpath_results[bug]
            if os.path.isfile(tmpfile):
                file_handle_read=open(tmpfile, "rt")
                line=file_handle_read.readline()
                while line:
                    data=line.strip().split(config.minpath_pathway_delimiter)
                    if re.search(config.minpath_pathway_identifier,line):
                        current_pathway=data[config.minpath_pathway_index]
                    else:
                        current_reaction=data[config.minpath_reaction_index]
                        pathways[current_reaction]=pathways.get(
                            current_reaction,[]) + [current_pathway]      
                    line=file_handle_read.readline()
                file_handle_read.close()
            else:
                message="Empty results file from MinPath run for bug: " + bug
                print(message)
                logger.warning(message)
        else:
            for current_reaction in reactions.get(bug,{}):
                pathways[current_reaction]=pathways.get(
                    current_reaction, []) + pathways_database.find_pathways(current_reaction) 
        for current_reaction in reactions.get(bug,{}):
            for current_pathway in pathways.get(current_reaction,[""]):
                if current_pathway:
                    pathways_and_reactions_store.add(bug,current_reaction, current_pathway, 
                        reactions[bug][current_reaction])
    return pathways_and_reactions_store
def identify_reactions_and_pathways ( gene_scores , reactions_database , pathways_database ) : if config . minpath_toggle == "on" : logger . debug ( "Write flat reactions to pathways file for Minpath" ) pathways_database_file = utilities . unnamed_temp_file ( ) file_handle = open ( pathways_database_file , "w" ) file_handle . write ( pathways_database . get_database ( ) ) file_handle . close ( ) pathways_and_reactions_store = store . PathwaysAndReactions ( ) reactions = { } minpath_results = { } minpath_commands = [ ] for bug in gene_scores . bug_list ( ) : gene_scores_for_bug = gene_scores . scores_for_bug ( bug ) message = "Compute reaction scores for bug: " + bug logger . info ( message ) reactions [ bug ] = { } reactions_file_lines = [ ] if reactions_database : for reaction in sorted ( reactions_database . reaction_list ( ) ) : genes_list = reactions_database . find_genes ( reaction ) abundance = 0 for gene in genes_list : abundance += gene_scores_for_bug . get ( gene , 0 ) if abundance > 0 : reactions_file_lines . append ( reaction + config . output_file_column_delimiter + str ( abundance ) + "\n" ) reactions [ bug ] [ reaction ] = abundance else : for gene in gene_scores_for_bug : score = gene_scores_for_bug [ gene ] if score > 0 : reactions_file_lines . append ( gene + config . output_file_column_delimiter + str ( score ) + "\n" ) reactions [ bug ] [ gene ] = score pathways = { } if config . minpath_toggle == "on" and len ( reactions_file_lines ) > 3 : reactions_file = utilities . unnamed_temp_file ( ) file_handle = open ( reactions_file , "w" ) file_handle . write ( "" . join ( reactions_file_lines ) ) file_handle . close ( ) logger . info ( "Run MinPath on " + bug ) tmpfile , command = minpath_command ( reactions_file , pathways_database_file ) minpath_results [ bug ] = tmpfile minpath_commands . append ( command ) if minpath_commands : utilities . command_threading ( config . threads , minpath_commands ) for bug in gene_scores . bug_list ( ) : pathways = { } if bug in minpath_results : tmpfile = minpath_results [ bug ] if os . path . isfile ( tmpfile ) : file_handle_read = open ( tmpfile , "rt" ) line = file_handle_read . readline ( ) while line : data = line . strip ( ) . split ( config . minpath_pathway_delimiter ) if re . search ( config . minpath_pathway_identifier , line ) : current_pathway = data [ config . minpath_pathway_index ] else : current_reaction = data [ config . minpath_reaction_index ] pathways [ current_reaction ] = pathways . get ( current_reaction , [ ] ) + [ current_pathway ] line = file_handle_read . readline ( ) file_handle_read . close ( ) else : message = "Empty results file from MinPath run for bug: " + bug print ( message ) logger . warning ( message ) else : for current_reaction in reactions . get ( bug , { } ) : pathways [ current_reaction ] = pathways . get ( current_reaction , [ ] ) + pathways_database . find_pathways ( current_reaction ) for current_reaction in reactions . get ( bug , { } ) : for current_pathway in pathways . get ( current_reaction , [ "" ] ) : if current_pathway : pathways_and_reactions_store . add ( bug , current_reaction , current_pathway , reactions [ bug ] [ current_reaction ] ) return pathways_and_reactions_store
Identify the reactions and then pathways from the hits found
Identify the reactions and then pathways from the hits found
"""
    Identify the reactions and then pathways from the hits found
    """ # Write a flat reactions to pathways file # Create a store for the pathways and reactions by bug # Run through each of the score sets by bug # Merge the gene scores to reaction scores    # Add the scores for each gene to the total score for the reaction # Only write out reactions where the abundance is greater than 0 # Store the abundance data to compile with the minpath pathways # Store the abundance data to compile with the minpath pathways # Run minpath if toggle on and also if there is more than one reaction    # Create a temp file for the reactions results # Run minpath to identify the pathways # Run through the minpath commands if minpath is to be run # Link the pathways to reactions # Process the minpath results # store the pathway and reaction # Add all pathways associated with each reaction if not using minpath # Store the pathway abundance for each reaction # Find the pathways associated with reaction # Only store data for items with pathway names
gene_scores
reactions_database
pathways_database