-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikipathways_converter.py
144 lines (100 loc) · 4.9 KB
/
wikipathways_converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from wikinetworks_mod import *
import networkx as nx
import argparse
import os
import requests
from bs4 import BeautifulSoup
import ast
from inputs import *
from create_graph import create_graph
from assign_nodes import interactive_search_wrapper
from create_subgraph import subgraph_prioritized_path_cs
from create_subgraph import subgraph_prioritized_path_pdp
from create_subgraph import subgraph_prioritized_path_guiding_term
from create_subgraph import user_defined_edge_exclusion
from graph_similarity_metrics import *
from constants import (
WIKIPATHWAYS_SUBFOLDER
)
def get_wikipathway_from_pfocr_url(pfocr_url):
response = requests.get(pfocr_url)
# Parse HTML
soup = BeautifulSoup(response.text, 'html.parser')
# Find the image tag
image_tag = soup.find('img', alt='WikiPathways')
# Extract the message attribute
if image_tag:
message_attr = image_tag.get('src')
# Extracting the part after 'message='
wikipathway = message_attr.split('message=')[1].split('&')[0]
print("Wikipathway:", wikipathway)
return(wikipathway)
else:
print("Wikipathway not found.")
def download_wikipathways_edgelist(w_dir,wikipathway):
if not os.path.exists(w_dir+"/"+wikipathway):
os.makedirs(w_dir+"/"+wikipathway)
os.chdir(w_dir+"/"+wikipathway)
__all__ = ["WikiPathways"]
s = WikiPathways()
graph = runParsePathway(s, wikipathway)
print(graph)
def convert_wikipathways_input(all_wikipathways_dir,wikipathway):
wikipathways_edgelist = pd.read_csv(all_wikipathways_dir + '/' + wikipathway + '/' + wikipathway + '_edgeList.csv')
edgelist_df = wikipathways_edgelist[['Source', 'Target']]
edgelist_df = edgelist_df.rename(columns={'Source' : 'source', 'Target': 'target'})
if not os.path.exists(all_wikipathways_dir+ '/annotated_diagram'):
os.makedirs(all_wikipathways_dir+ '/annotated_diagram')
examples_file = all_wikipathways_dir + '/annotated_diagram/' + wikipathway + '_example_input.csv'
edgelist_df.to_csv(examples_file,sep='|',index=False)
logging.info('Output example file for %s: %r',wikipathway,examples_file)
return examples_file
#Converts PFOCRs as a file or list into Wikipathway IDs, if provided
def get_wikipathways_list(wikipathways,pfocr_urls,pfocr_urls_file):
#Convert string input to list
if wikipathways:
wikipathways = ast.literal_eval(wikipathways)
elif pfocr_urls:
pfocr_urls = ast.literal_eval(pfocr_urls)
wikipathways = []
for p in pfocr_urls:
#Gets wikpathway from pfocr_url
wikipathways.append[get_wikipathway_from_pfocr_url(p)]
elif pfocr_urls_file:
fname = os.getcwd() + "/wikipathways_graphs/" + 'PFOCR_url_list.txt'
f = open(fname, "r")
data = f.read()
pfocr_urls = data.split("\n")
wikipathways = []
for p in pfocr_urls:
#Gets wikpathway from pfocr_url
wikipathways.append(get_wikipathway_from_pfocr_url(p))
if not os.path.isfile(fname):
raise Exception('Missing file in wikipathways_graphs directory: ' + fname)
logging.error('Missing file in wikipathways_graphs directory: ' + fname)
return wikipathways
def main():
kg_type,embedding_dimensions,weights,search_type, pdp_weight,input_type, cosine_similarity, pdp, guiding_term, input_substring,wikipathways,pfocr_urls,pfocr_urls_file,enable_skipping = generate_graphsim_arguments()
input_dir = os.getcwd() + '/' + WIKIPATHWAYS_SUBFOLDER
triples_list_file,labels_file = get_wikipathways_graph_files(input_dir,kg_type,input_type,guiding_term,input_substring)
print("Creating knowledge graph object from inputs.....")
g = create_graph(triples_list_file,labels_file, kg_type)
wikipathways = get_wikipathways_list(wikipathways,pfocr_urls,pfocr_urls_file)
all_wikipathways_dir = os.getcwd() + "/" + WIKIPATHWAYS_SUBFOLDER
for wikipathway in wikipathways:
output_dir = all_wikipathways_dir + '/' + wikipathway + '_output'
#Check for existence of output directory
if not os.path.exists(output_dir):
os.makedirs(output_dir)
#Downloads wikipathway diagrams as edgelists
download_wikipathways_edgelist(all_wikipathways_dir,wikipathway)
#Converts wikipathway diagram edgelists to format necessary for subgraph generation
examples_file = convert_wikipathways_input(all_wikipathways_dir,wikipathway)
#Index wikipathway diagram
print("Mapping between user inputs and KG nodes for " + wikipathway + ".......")
s = interactive_search_wrapper(g, [examples_file], output_dir, input_type,kg_type, enable_skipping)
if guiding_term:
guiding_term_df = interactive_search_wrapper(g, examples_file, output_dir, 'guiding_term', kg_type,input_dir)
print("Mapping complete")
if __name__ == '__main__':
main()