Spaces:
Paused
Paused
Erva Ulusoy commited on
Commit ·
e0fbc94
1
Parent(s): 9fb2870
updated node titles to contain node name instead of id
Browse files1234542 unmodified lines48495051525380 unmodified lines1341351361371381398 unmodified lines14814915015115215315441 unmodified lines19619719819920020120220322 unmodified lines2262272282292302312322332342352361 unmodified line238239240241242243244245246247248249250251252253from pyvis.network import Networkimport osNODE_TYPE_COLORS = {'Disease': '#079dbb',42 unmodified lines'Cellular Component': 'GO_term_C'}def _gather_protein_edges(data, protein_id):protein_idx = data['Protein']['id_mapping'][protein_id]80 unmodified linesdef visualize_protein_subgraph(data, protein_id, prediction_df, limit=10):protein_edges = _gather_protein_edges(data, protein_id)visualized_edges = _filter_edges(protein_id, protein_edges, prediction_df, limit)print(f'Edges to be visualized: {visualized_edges}')8 unmodified lines}# Convert groups_config to a JSON-compatible stringimport jsongroups_json = json.dumps(groups_config)# Configure physics options with settings for better clustering41 unmodified lines"groups": """ + groups_json + "}")# Add the main protein nodenet.add_node(protein_id,label=f"{protein_id} (Protein)",color={'background': 'white', 'border': '#c1121f'},borderWidth=4,shape="dot",22 unmodified lines# Add source node if not presentif source_str not in added_nodes:net.add_node(source_str,label= source_str,shape="dot",font={'color': '#000000', 'size': 12},title=f"{source_str} ({NODE_LABEL_TRANSLATION[source_type] if source_type in NODE_LABEL_TRANSLATION else source_type})",group=source_type,size=15,mass=1.5)1 unmodified line# Add target node if not presentif target_str not in added_nodes:net.add_node(target_str,label= target_str,shape="dot",font={'color': '#000000', 'size': 12},title=f"{target_str} ({NODE_LABEL_TRANSLATION[target_type] if target_type in NODE_LABEL_TRANSLATION else target_type})",group=target_type,size=15,mass=1.5)added_nodes.add(target_str)# Add edge with relationship type and probability as labeledge_label = f"{relation_type}"if probability is not None:123456742 unmodified lines5051525354555657585960616263646566676869707172737475767778798081828384858687888980 unmodified lines1701711721731741751761771781798 unmodified lines18818919019119219341 unmodified lines23523623723823924024124224324424524624724824922 unmodified lines2722732742752762772782792802812822832842852862872882892901 unmodified line292293294295296297298299300301302303304305306307308309310311312313314from pyvis.network import Networkimport osimport jsonimport gzipNODE_TYPE_COLORS = {'Disease': '#079dbb',42 unmodified lines'Cellular Component': 'GO_term_C'}def get_node_url(node_type, node_id):"""Get the URL for a node based on its type and ID"""if node_type.startswith('GO_term'):return f"https://www.ebi.ac.uk/QuickGO/term/{node_id}"elif node_type == 'Protein':return f"https://www.uniprot.org/uniprotkb/{node_id}/entry"elif node_type == 'Disease':if ':' in node_id:ontology = node_id.split(':')[0]if ontology == 'EFO':return f"http://www.ebi.ac.uk/efo/EFO_{node_id.split(':')[1]}"elif ontology == 'MONDO':return f'http://purl.obolibrary.org/obo/MONDO_{node_id.split(":")[1]}'elif ontology == 'Orphanet':return f"http://www.orpha.net/ORDO/Orphanet_{node_id.split(':')[1]}"else:return f"https://www.genome.jp/entry/{node_id}"elif node_type == 'HPO':return f"https://hpo.jax.org/browse/term/{node_id}"elif node_type == 'Drug':return f"https://go.drugbank.com/drugs/{node_id}"elif node_type == 'Compound':return f"https://www.ebi.ac.uk/chembl/explore/compound/{node_id}"elif node_type == 'Domain':return f"https://www.ebi.ac.uk/interpro/entry/InterPro/{node_id}"elif node_type == 'Pathway':return f"https://reactome.org/content/detail/{node_id}"elif node_type == 'kegg_Pathway':return f"https://www.genome.jp/pathway/{node_id}"elif node_type == 'EC_number':return f"https://enzyme.expasy.org/EC/{node_id}"else:return Nonedef _gather_protein_edges(data, protein_id):protein_idx = data['Protein']['id_mapping'][protein_id]80 unmodified linesdef visualize_protein_subgraph(data, protein_id, prediction_df, limit=10):with gzip.open('data/name_info.json.gz', 'rt', encoding='utf-8') as file:name_info = json.load(file)protein_edges = _gather_protein_edges(data, protein_id)visualized_edges = _filter_edges(protein_id, protein_edges, prediction_df, limit)print(f'Edges to be visualized: {visualized_edges}')8 unmodified lines}# Convert groups_config to a JSON-compatible stringgroups_json = json.dumps(groups_config)# Configure physics options with settings for better clustering41 unmodified lines"groups": """ + groups_json + "}")# Add the main protein nodequery_node_url = get_node_url('Protein', protein_id)node_name = name_info['Protein'][protein_id]query_node_title = f"{node_name} (Query Protein)"if query_node_url:query_node_title = f'<a href="{query_node_url}" target="_blank">{query_node_title}</a>'net.add_node(protein_id,label=protein_id,title=query_node_title,color={'background': 'white', 'border': '#c1121f'},borderWidth=4,shape="dot",22 unmodified lines# Add source node if not presentif source_str not in added_nodes:if not source_type.startswith('GO_term'):node_name = name_info[source_type][source_str]else:node_name = name_info['GO_term'][source_str]url = get_node_url(source_type, source_str)title = f"{node_name} ({NODE_LABEL_TRANSLATION[source_type] if source_type in NODE_LABEL_TRANSLATION else source_type})"if url:title = f'<a href="{url}" target="_blank">{title}</a>'net.add_node(source_str,label=source_str,shape="dot",font={'color': '#000000', 'size': 12},title=title,group=source_type,size=15,mass=1.5)1 unmodified line# Add target node if not presentif target_str not in added_nodes:if not target_type.startswith('GO_term'):node_name = name_info[target_type][target_str]else:node_name = name_info['GO_term'][target_str]url = get_node_url(target_type, target_str)title = f"{node_name} ({NODE_LABEL_TRANSLATION[target_type] if target_type in NODE_LABEL_TRANSLATION else target_type})"if url:title = f'<a href="{url}" target="_blank">{title}</a>'net.add_node(target_str,label=target_str,shape="dot",font={'color': '#000000', 'size': 12},title=title,group=target_type,size=15,mass=1.5)added_nodes.add(target_str)# Add edge with relationship type and probability as labeledge_label = f"{relation_type}"if probability is not None:
visualize_kg.py+68-7