humann/quantify/modules.py
def compute_pathways_coverage(pathways_and_reactions_store,pathways_database):
    """
    Compute the coverage of pathways for each bug
    """

    pathways_coverage_store=store.Pathways()
    xipe_stdout_results={}
    xipe_stderr_results={}
    xipe_commands=[]
    for bug in pathways_and_reactions_store.bug_list():
    
        logger.debug("Compute pathway coverage for bug: " + bug)
        
        # Process through each pathway to compute coverage
        xipe_input=[]
        median_score_value=pathways_and_reactions_store.median_score(bug)
        
        for pathway in pathways_and_reactions_store.pathway_list(bug):
                
            reaction_scores=pathways_and_reactions_store.reaction_scores(bug,pathway)
            
            # Check if the pathways database is structured
            if pathways_database.is_structured():
                structure=pathways_database.get_structure_for_pathway(pathway)
                key_reactions=pathways_database.get_key_reactions_for_pathway(pathway)
                # Apply gap fill
                reaction_scores=gap_fill(key_reactions, reaction_scores)
                # Compute the structured pathway coverage
                coverage=compute_structured_pathway_abundance_or_coverage(structure,
                    key_reactions,reaction_scores,True,median_score_value)
            else:
                # Count the reactions with scores greater than the median
                count_greater_than_median=0
                for reaction, score in reaction_scores.items():
                    if score > median_score_value:
                       count_greater_than_median+=1
                
                # Compute coverage
                coverage=0
                total_reactions_for_pathway=len(pathways_database.find_reactions(pathway))
                if total_reactions_for_pathway:
                    coverage=count_greater_than_median/float(total_reactions_for_pathway)
            
            pathways_coverage_store.add(bug,pathway,coverage)
            xipe_input.append(config.xipe_delimiter.join([pathway,str(coverage)]))
        
        # Check config to determine if xipe should be run
        if config.xipe_toggle == "on":

            # Create temp file for input
            infile=utilities.unnamed_temp_file()
            
            # Write the input to xipe
            file_handle=open(infile,"w")
            file_handle.write("\n".join(xipe_input))
            file_handle.close()
            
            stdout_file, stderr_file, command = xipe_command(infile)
            
            xipe_commands.append(command)
            xipe_stdout_results[bug]=stdout_file
            xipe_stderr_results[bug]=stderr_file
            
    # Run xipe
    if xipe_commands:
        utilities.command_threading(config.threads,xipe_commands)
            
    # Process the xipe output
    for bug in xipe_stdout_results:
            
        try:
            xipe_stderr=open(xipe_stderr_results[bug],"rt")
            xipe_stdout=open(xipe_stdout_results[bug],"rt")
        except EnvironmentError:
            logger.debug("Empty results file from xipe")
            continue
            
        # Record the pathways to remove based on the xipe error messages
        pathways_to_remove=[]
        for line in xipe_stderr:
            data=line.strip().split(config.xipe_delimiter)
            if len(data) == 2:
                pathways_to_remove.append(data[1])
            
        # Keep some of the pathways to remove based on their xipe scores
        for line in xipe_stdout:
            data=line.strip().split(config.xipe_delimiter)
            if len(data) == 2:
                pathway, pathway_data = data
                if pathway in pathways_to_remove:
                    score, bin = pathway_data[1:-1].split(", ")
                    if float(score) >= config.xipe_probability and int(bin) == config.xipe_bin:
                        pathways_to_remove.remove(pathway)
                    
        xipe_stderr.close()
        xipe_stdout.close()        
            
        # Remove the selected pathways
        for pathway in pathways_to_remove:
            pathways_coverage_store.delete(bug,pathway)

    return pathways_coverage_store

    Compute the coverage of pathways for each bug
    
Compute the coverage of pathways for each bug
Compute the coverage of pathways for each bug
def compute_pathways_coverage(pathways_and_reactions_store,pathways_database):
    pathways_coverage_store=store.Pathways()
    xipe_stdout_results={}
    xipe_stderr_results={}
    xipe_commands=[]
    for bug in pathways_and_reactions_store.bug_list():
        logger.debug("Compute pathway coverage for bug: " + bug)
        xipe_input=[]
        median_score_value=pathways_and_reactions_store.median_score(bug)
        for pathway in pathways_and_reactions_store.pathway_list(bug):
            reaction_scores=pathways_and_reactions_store.reaction_scores(bug,pathway)
            if pathways_database.is_structured():
                structure=pathways_database.get_structure_for_pathway(pathway)
                key_reactions=pathways_database.get_key_reactions_for_pathway(pathway)
                reaction_scores=gap_fill(key_reactions, reaction_scores)
                coverage=compute_structured_pathway_abundance_or_coverage(structure,
                    key_reactions,reaction_scores,True,median_score_value)
            else:
                count_greater_than_median=0
                for reaction, score in reaction_scores.items():
                    if score > median_score_value:
                       count_greater_than_median+=1
                coverage=0
                total_reactions_for_pathway=len(pathways_database.find_reactions(pathway))
                if total_reactions_for_pathway:
                    coverage=count_greater_than_median/float(total_reactions_for_pathway)
            pathways_coverage_store.add(bug,pathway,coverage)
            xipe_input.append(config.xipe_delimiter.join([pathway,str(coverage)]))
        if config.xipe_toggle == "on":
            infile=utilities.unnamed_temp_file()
            file_handle=open(infile,"w")
            file_handle.write("\n".join(xipe_input))
            file_handle.close()
            stdout_file, stderr_file, command = xipe_command(infile)
            xipe_commands.append(command)
            xipe_stdout_results[bug]=stdout_file
            xipe_stderr_results[bug]=stderr_file
    if xipe_commands:
        utilities.command_threading(config.threads,xipe_commands)
    for bug in xipe_stdout_results:
        try:
            xipe_stderr=open(xipe_stderr_results[bug],"rt")
            xipe_stdout=open(xipe_stdout_results[bug],"rt")
        except EnvironmentError:
            logger.debug("Empty results file from xipe")
            continue
        pathways_to_remove=[]
        for line in xipe_stderr:
            data=line.strip().split(config.xipe_delimiter)
            if len(data) == 2:
                pathways_to_remove.append(data[1])
        for line in xipe_stdout:
            data=line.strip().split(config.xipe_delimiter)
            if len(data) == 2:
                pathway, pathway_data = data
                if pathway in pathways_to_remove:
                    score, bin = pathway_data[1:-1].split(", ")
                    if float(score) >= config.xipe_probability and int(bin) == config.xipe_bin:
                        pathways_to_remove.remove(pathway)
        xipe_stderr.close()
        xipe_stdout.close()        
        for pathway in pathways_to_remove:
            pathways_coverage_store.delete(bug,pathway)
    return pathways_coverage_store
def compute_pathways_coverage ( pathways_and_reactions_store , pathways_database ) : pathways_coverage_store = store . Pathways ( ) xipe_stdout_results = { } xipe_stderr_results = { } xipe_commands = [ ] for bug in pathways_and_reactions_store . bug_list ( ) : logger . debug ( "Compute pathway coverage for bug: " + bug ) xipe_input = [ ] median_score_value = pathways_and_reactions_store . median_score ( bug ) for pathway in pathways_and_reactions_store . pathway_list ( bug ) : reaction_scores = pathways_and_reactions_store . reaction_scores ( bug , pathway ) if pathways_database . is_structured ( ) : structure = pathways_database . get_structure_for_pathway ( pathway ) key_reactions = pathways_database . get_key_reactions_for_pathway ( pathway ) reaction_scores = gap_fill ( key_reactions , reaction_scores ) coverage = compute_structured_pathway_abundance_or_coverage ( structure , key_reactions , reaction_scores , True , median_score_value ) else : count_greater_than_median = 0 for reaction , score in reaction_scores . items ( ) : if score > median_score_value : count_greater_than_median += 1 coverage = 0 total_reactions_for_pathway = len ( pathways_database . find_reactions ( pathway ) ) if total_reactions_for_pathway : coverage = count_greater_than_median / float ( total_reactions_for_pathway ) pathways_coverage_store . add ( bug , pathway , coverage ) xipe_input . append ( config . xipe_delimiter . join ( [ pathway , str ( coverage ) ] ) ) if config . xipe_toggle == "on" : infile = utilities . unnamed_temp_file ( ) file_handle = open ( infile , "w" ) file_handle . write ( "\n" . join ( xipe_input ) ) file_handle . close ( ) stdout_file , stderr_file , command = xipe_command ( infile ) xipe_commands . append ( command ) xipe_stdout_results [ bug ] = stdout_file xipe_stderr_results [ bug ] = stderr_file if xipe_commands : utilities . command_threading ( config . threads , xipe_commands ) for bug in xipe_stdout_results : try : xipe_stderr = open ( xipe_stderr_results [ bug ] , "rt" ) xipe_stdout = open ( xipe_stdout_results [ bug ] , "rt" ) except EnvironmentError : logger . debug ( "Empty results file from xipe" ) continue pathways_to_remove = [ ] for line in xipe_stderr : data = line . strip ( ) . split ( config . xipe_delimiter ) if len ( data ) == 2 : pathways_to_remove . append ( data [ 1 ] ) for line in xipe_stdout : data = line . strip ( ) . split ( config . xipe_delimiter ) if len ( data ) == 2 : pathway , pathway_data = data if pathway in pathways_to_remove : score , bin = pathway_data [ 1 : - 1 ] . split ( ", " ) if float ( score ) >= config . xipe_probability and int ( bin ) == config . xipe_bin : pathways_to_remove . remove ( pathway ) xipe_stderr . close ( ) xipe_stdout . close ( ) for pathway in pathways_to_remove : pathways_coverage_store . delete ( bug , pathway ) return pathways_coverage_store
Compute the coverage of pathways for each bug
Compute the coverage of pathways for each bug
"""
    Compute the coverage of pathways for each bug
    """ # Process through each pathway to compute coverage # Check if the pathways database is structured # Apply gap fill # Compute the structured pathway coverage # Count the reactions with scores greater than the median # Compute coverage # Check config to determine if xipe should be run # Create temp file for input # Write the input to xipe # Run xipe # Process the xipe output # Record the pathways to remove based on the xipe error messages # Keep some of the pathways to remove based on their xipe scores # Remove the selected pathways
pathways_and_reactions_store
pathways_database