"""Functions for constructing a PPI PPIGraphConfig from STRINGdb and BIOGRID."""# %%# Graphein# Author: Arian Jamasb <arian@jamasb.io>, Ramon Vinas# License: MIT# Project Website: https://github.com/a-r-j/graphein# Code Repository: https://github.com/a-r-j/grapheinimportloggingfromtypingimportCallable,List,Optionalimportnetworkxasnxfromgraphein.ppi.configimportPPIGraphConfigfromgraphein.utils.utilsimport(annotate_edge_metadata,annotate_graph_metadata,annotate_node_metadata,compute_edges,)log=logging.getLogger(__name__)EDGE_COLOR_MAPPING={"string":"r","biogrid":"b"}
[docs]defparse_kwargs_from_config(config:PPIGraphConfig)->PPIGraphConfig:""" If configs for STRING and BIOGRID are provided in the Global :ref:`~graphein.ppi.config.PPIGraphConfig`, we update the kwargs :param config: PPI graph configuration object. :type config: PPIGraphConfig :return: config with updated config.kwargs :rtype: PPIGraphConfig """ifconfig.string_configisnotNone:string_config_dict={f"STRING_{k}":vfork,vindict(config.string_config.items())}config.kwargs=config.kwargs.update(string_config_dict)ifconfig.biogrid_configisnotNone:biogrid_config_dict={f"BIOGRID_{k}":vfork,vindict(config.biogrid_config.items())}config.kwargs=config.kwargs.update(biogrid_config_dict)returnconfig
[docs]defcompute_ppi_graph(protein_list:List[str],edge_construction_funcs:List[Callable],graph_annotation_funcs:Optional[List[Callable]]=None,node_annotation_funcs:Optional[List[Callable]]=None,edge_annotation_funcs:Optional[List[Callable]]=None,config:Optional[PPIGraphConfig]=None,)->nx.Graph:""" Computes a PPI Graph from a list of protein IDs. This is the core function for PPI graph construction. :param protein_list: List of protein identifiers :type protein_list: List[str] :param edge_construction_funcs: List of functions to construct edges with :type edge_construction_funcs: List[Callable], optional :param graph_annotation_funcs: List of functions to annotate graph metadata :type graph_annotation_funcs: List[Callable], optional :param node_annotation_funcs: List of functions to annotate node metadata :type node_annotation_funcs: List[Callable], optional :param edge_annotation_funcs: List of function to annotate edge metadata :type edge_annotation_funcs: List[Callable], optional :param config: Config object specifying additional parameters for STRING and BIOGRID API calls :type config: PPIGraphConfig, optional :return: ``nx.Graph`` of PPI network :rtype: nx.Graph """# Load default config if none suppliedifconfigisNone:config=PPIGraphConfig()# Parse kwargs from configconfig=parse_kwargs_from_config(config)# Create graph and add proteins as nodesG=nx.Graph(protein_list=protein_list,sources=[],ncbi_taxon_id=config.ncbi_taxon_id,)G.add_nodes_from(protein_list)log.debug(f"Added {len(protein_list)} nodes to graph")nx.set_node_attributes(G,dict(zip(protein_list,protein_list)),"protein_id",)# Annotate additional graph metadataifgraph_annotation_funcsisnotNone:G=annotate_graph_metadata(G,graph_annotation_funcs)# Annotate additional node metadataifnode_annotation_funcsisnotNone:G=annotate_node_metadata(G,node_annotation_funcs)# Add edgesG=compute_edges(G,edge_construction_funcs)# Annotate additional edge metadataifedge_annotation_funcsisnotNone:G=annotate_edge_metadata(G,edge_annotation_funcs)returnG