def prepare_report_reactions(reactions=None): """ Prepares report of information about reactions for review. arguments: reactions (dict<dict>): information about reactions returns: (list<dict>): information about reactions raises: """ records = [] for reaction in reactions.values(): compartments = utility.collect_value_from_records( key="compartment", records=reaction["participants"]) metabolites = utility.collect_value_from_records( key="metabolite", records=reaction["participants"]) record = { "identifier": reaction["identifier"], "name": reaction["name"], "reversibility": reaction["reversibility"], "metabolites": metabolites, "compartments": compartments, "processes": reaction["processes"], "reference_gene": reaction["references"]["gene"], "reference_metanetx": reaction["references"]["metanetx"] } records.append(record) return records
def filter_reaction(reaction=None): """ Filters a reaction by relevance to contextual metabolic network. arguments: reaction (dict): information about a reaction returns: (tuple<bool, dict>): whether reaction passes filters, and report raises: """ # Name. # Biomass, protein assembly, and protein degradation are irrelevant. name = ((reaction["name"] == "Generic human biomass reaction") or (reaction["name"] == "Protein degradation") or ("Protein assembly" in reaction["name"])) # Compartment. # Boundary is irrelevant. compartments = utility.collect_value_from_records( key="compartment", records=reaction["participants"]) compartment_boundary = ("BOUNDARY" in compartments) # Extracellular region is irrelevant. compartment_exterior = ("MNXC2" in compartments) # Metabolite. # Biomass is irrelevant. metabolites = utility.collect_value_from_records( key="metabolite", records=reaction["participants"]) metabolite = ("BIOMASS" in metabolites) # Reference. # MetaNetX reaction MNXR01 is for a meaningless proton exchange. reference = "MNXR01" in reaction["references"]["metanetx"] # Determine whether reaction passes filters. filter = (name or compartment_boundary or compartment_exterior or metabolite or reference) # Prepare report. record = { "identifier_original": reaction["identifier"], "identifier_novel": "null", "name_original": reaction["name"], "name_novel": reaction["name"], "custom": False, } # Return information. return (filter, record)
def convert_networkx(nodes_reactions=None, nodes_metabolites=None, links=None): """ Converts information about network's nodes and links to format for NetworkX. Network is bipartite. Store information about separate groups of nodes for reactions and metabolites. arguments: nodes_reactions (dict<dict>): information about reactions' nodes nodes_metabolites (dict<dict>): information about metabolites' nodes links (dict<dict>): information about links between nodes for reactions and metabolites raises: returns: (dict<list<tuple>>): information about network's nodes and links """ nodes_networkx = convert_nodes_networkx( nodes_reactions=nodes_reactions, nodes_metabolites=nodes_metabolites) nodes_reactions_identifiers = utility.collect_value_from_records( key="identifier", records=nodes_reactions.values()) nodes_metabolites_identifiers = utility.collect_value_from_records( key="identifier", records=nodes_metabolites.values()) links_networkx = convert_links_networkx(links=links) # Compile and return information. return { "nodes": nodes_networkx, "nodes_reactions_identifiers": nodes_reactions_identifiers, "nodes_reactions": nodes_reactions, "nodes_metabolites_identifiers": nodes_metabolites_identifiers, "nodes_metabolites": nodes_metabolites, "links": links_networkx }
def determine_reaction_dispersal(reaction=None): """ Determines whether a reaction involves metabolites in multiple compartments arguments: reaction (dict): information about a reaction returns: (bool): whether the reaction involves participation of metabolites in multiple compartments raises: """ compartments = utility.collect_value_from_records( key="compartment", records=reaction["participants"]) return len(compartments) > 0
def access_reactions_summary(reactions_interest=None, reactions=None, directory=None): """ Accesses summary information about reactions of interest. arguments: reactions_interest (list<dict<str>>): identifiers of reactions of interest reactions (dict<dict>): information about reactions directory (str): path to directory for source and product files raises: returns: (list<dict<str>>): information about measurements and signals for all samples """ # Collect information about reactions of interest. identifiers = utility.collect_value_from_records( key="identifier", records=reactions_interest) identifiers_unique = utility.collect_unique_elements(identifiers) reactions_summary = [] for identifier in identifiers_unique: reaction = reactions[identifier] name = reaction["name"] metanetx = ";".join(reaction["references"]["metanetx"]) gene = ";".join(reaction["references"]["gene"]) enzyme = ";".join(reaction["references"]["enzyme"]) record_product = { "identifier": identifier, "name": name, "metanetx": metanetx, "gene": gene, "enzyme": enzyme } reactions_summary.append(record_product) # Return information. return reactions_summary
def define_reaction_node( reaction_candidacy_identifier=None, reactions_candidacy=None, reactions=None, metabolites=None, compartments=None, processes=None ): """ Defines information about a reaction's node. arguments: reaction_candidacy_identifier (str): identifier of a candidate reaction reactions_candidacy (dict<dict>): information about candidate reactions reactions (dict<dict>): information about reactions metabolites (dict<dict>): information about metabolites compartments (dict<dict>): information about compartments processes (dict<dict>): information about processes raises: returns: (dict): information about a reaction's node """ # Access information. reaction_candidacy = reactions_candidacy[reaction_candidacy_identifier] reaction = reactions[reaction_candidacy["reaction"]] # Compartments. compartments_reaction = utility.collect_value_from_records( key="compartment", records=reaction["participants"] ) compartments_unique = utility.collect_unique_elements( elements_original=compartments_reaction ) compartments_names = utility.collect_values_from_records_in_reference( key="name", identifiers=compartments_unique, reference=compartments ) # Processes. processes_names = utility.collect_values_from_records_in_reference( key="name", identifiers=reaction["processes"], reference=processes ) # Metabolites. metabolites_reaction = utility.collect_value_from_records( key="metabolite", records=reaction["participants"] ) metabolites_unique = utility.collect_unique_elements( elements_original=metabolites_reaction ) metabolites_names = utility.collect_values_from_records_in_reference( key="name", identifiers=metabolites_unique, reference=metabolites ) # Compile information. reaction_node = { "identifier": reaction_candidacy["identifier"], "type": "reaction", "entity": reaction["identifier"], "name": reaction_candidacy["name"], "reversibility": reaction_candidacy["reversibility"], "metabolites": ";".join(metabolites_names), "compartments": ";".join(compartments_names), "processes": ";".join(processes_names), "replicates": reaction_candidacy["replicates"], } # Return information. return reaction_node
def convert_reactions_export_text( reactions=None, metabolites=None, compartments=None, processes=None, ): """ Converts information about reactions to text format. Converts identifiers of metabolites, compartments, and processes to names. arguments: reactions (dict<dict>): information about reactions metabolites (dict<dict>): information about metabolites compartments (dict<dict>): information about compartments processes (dict<dict>): information about processes returns: (list<dict>): information about reactions raises: """ records = [] for reaction in reactions.values(): # Participants. # Write a function to compose identifier (name) human readable... # Compartments compartments_identifiers = utility.collect_value_from_records( key="compartment", records=reaction["participants"]) compartments_identifiers_unique = utility.collect_unique_elements( elements_original=compartments_identifiers) compartments_names = utility.collect_values_from_records_in_reference( key="name", identifiers=compartments_identifiers_unique, reference=compartments, ) # Processes processes_names = utility.collect_values_from_records_in_reference( key="name", identifiers=reaction["processes"], reference=processes, ) # Metabolites reactants_identifiers = utility.collect_reaction_participants_value( key="metabolite", criteria={"roles": ["reactant"]}, participants=reaction["participants"]) reactants_names = utility.collect_values_from_records_in_reference( key="name", identifiers=reactants_identifiers, reference=metabolites, ) products_identifiers = utility.collect_reaction_participants_value( key="metabolite", criteria={"roles": ["product"]}, participants=reaction["participants"]) products_names = utility.collect_values_from_records_in_reference( key="name", identifiers=products_identifiers, reference=metabolites, ) # Compile information. record = { "identifier": reaction["identifier"], "name": reaction["name"], "reactants": "; ".join(reactants_names), "products": "; ".join(products_names), "compartments": "; ".join(compartments_names), "processes": ";".join(processes_names), "reversibility": reaction["reversibility"], "reference_metanetx": ("; ".join(reaction["references"]["metanetx"])), "reference_recon2m2": ("; ".join(reaction["references"]["recon2m2"])), "reference_gene": "; ".join(reaction["references"]["gene"]), "reference_enzyme": "; ".join(reaction["references"]["enzyme"]), "reference_kegg": "; ".join(reaction["references"]["kegg"]), "reference_reactome": ("; ".join(reaction["references"]["reactome"])), "reference_metacyc": "; ".join(reaction["references"]["metacyc"]), "reference_bigg": "; ".join(reaction["references"]["bigg"]), } records.append(record) return records
def convert_reactions_text(reactions=None): """ Converts information about reactions to text format. arguments: reactions (dict<dict>): information about reactions returns: (list<dict>): information about reactions raises: """ records = [] for reaction in reactions.values(): # Participants. compartments = utility.collect_value_from_records( key="compartment", records=reaction["participants"]) compartments_unique = utility.collect_unique_elements( elements_original=compartments) metabolites = utility.collect_value_from_records( key="metabolite", records=reaction["participants"]) metabolites_unique = utility.collect_unique_elements( elements_original=metabolites) # Transports. transport_metabolites = utility.collect_value_from_records( key="metabolite", records=reaction["transports"]) transport_compartments = utility.collect_values_from_records( key="compartments", records=reaction["transports"]) transport_compartments_unique = utility.collect_unique_elements( elements_original=transport_compartments) # Compile information. record = { "identifier": reaction["identifier"], "name": reaction["name"], "equation": reaction["equation"], "metabolites": ";".join(metabolites_unique), "compartments": ";".join(compartments_unique), "processes": ";".join(reaction["processes"]), "reversibility": reaction["reversibility"], "conversion": reaction["conversion"], "dispersal": reaction["dispersal"], "transport": reaction["transport"], "transport_metabolites": ";".join(transport_metabolites), "transport_compartments": ";".join(transport_compartments_unique), "replication": reaction["replication"], "replicates": ";".join(reaction["replicates"]), "reference_metanetx": ";".join(reaction["references"]["metanetx"]), "reference_recon2m2": ";".join(reaction["references"]["recon2m2"]), "reference_gene": ";".join(reaction["references"]["gene"]), "reference_enzyme": ";".join(reaction["references"]["enzyme"]), "reference_kegg": ";".join(reaction["references"]["kegg"]), "reference_reactome": ";".join(reaction["references"]["reactome"]), "reference_metacyc": ";".join(reaction["references"]["metacyc"]), "reference_bigg": ";".join(reaction["references"]["bigg"]), "reference_rhea": ";".join(reaction["references"]["rhea"]), "reference_sabiork": ";".join(reaction["references"]["sabiork"]), "reference_seed": ";".join(reaction["references"]["seed"]) } records.append(record) return records