Mercurial > repos > proteore > proteore_reactome
annotate reactome_analysis.py @ 11:19195d1a4063 draft default tip
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
author | proteore |
---|---|
date | Mon, 10 May 2021 15:30:34 +0000 |
parents | a58dc5d4b8cd |
children |
rev | line source |
---|---|
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
1 import argparse |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
2 import csv |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
3 import json |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
4 import os |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
5 import re |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
6 |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
7 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
8 |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
9 |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
10 def id_valid(identifiers): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
11 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
12 Validate IDs if they contain special characters |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
13 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
14 res = [] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
15 remove = [] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
16 for id in identifiers: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
17 id = id.split(";")[0] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
18 if re.match("^[A-Za-z0-9_-]*$", id): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
19 res.append(id) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
20 else: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
21 remove.append(id) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
22 return res, remove |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
23 |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
24 |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
25 def isnumber(format, n): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
26 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
27 Check if an variable is numeric |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
28 """ |
4
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
29 float_format = re.compile(r"^[-]?[1-9][0-9]*.?[0-9]+$") |
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
30 int_format = re.compile(r"^[-]?[1-9][0-9]*$") |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
31 test = "" |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
32 if format == "int": |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
33 test = re.match(int_format, n) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
34 elif format == "float": |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
35 test = re.match(float_format, n) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
36 if test: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
37 return True |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
38 else: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
39 return False |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
40 |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
41 |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
42 def data_json(identifiers): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
43 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
44 Submit IDs list to Reactome and return results in json format |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
45 Return error in HTML format if web service is not available |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
46 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
47 trash = [] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
48 if identifiers[1] == "list": |
8
a58dc5d4b8cd
planemo upload commit 4ba1ebe7b3f5e3fabf78b5fed7ed0b92e2cbf9e5-dirty
proteore
parents:
6
diff
changeset
|
49 ids = identifiers[0].split() |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
50 ids = [x.split(";") for x in ids] |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
51 ids = [item.strip() for sublist in ids for item in sublist if item != ''] # noqa 501 |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
52 json_string = os.popen("curl -H \"Content-Type: text/plain\" -d \"$(printf '%s')\" -X POST --url www.reactome.org/AnalysisService/identifiers/\?pageSize\=1\&page\=1" % ids).read() # noqa 501 |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
53 if len(id_valid(identifiers[0].split())[1]) > 0: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
54 trash = id_valid(identifiers[0].split())[1] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
55 elif identifiers[1] == "file": |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
56 header = identifiers[2] |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
57 with open(identifiers[0], "r") as mq: |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
58 file_content = csv.reader(mq, delimiter="\t") |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
59 file_content = list(file_content) # csv object to list |
8
a58dc5d4b8cd
planemo upload commit 4ba1ebe7b3f5e3fabf78b5fed7ed0b92e2cbf9e5-dirty
proteore
parents:
6
diff
changeset
|
60 ncol = identifiers[3] |
a58dc5d4b8cd
planemo upload commit 4ba1ebe7b3f5e3fabf78b5fed7ed0b92e2cbf9e5-dirty
proteore
parents:
6
diff
changeset
|
61 if isnumber("int", ncol.replace("c", "")): |
a58dc5d4b8cd
planemo upload commit 4ba1ebe7b3f5e3fabf78b5fed7ed0b92e2cbf9e5-dirty
proteore
parents:
6
diff
changeset
|
62 if header == "true": |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
63 idens = [x for x in [line[int(ncol.replace("c", ""))-1].split(";") for line in file_content[1:]]] # noqa 501 |
8
a58dc5d4b8cd
planemo upload commit 4ba1ebe7b3f5e3fabf78b5fed7ed0b92e2cbf9e5-dirty
proteore
parents:
6
diff
changeset
|
64 else: |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
65 idens = [x for x in [line[int(ncol.replace("c", ""))-1].split(";") for line in file_content]] # noqa 501 |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
66 # flat list of list of lists, remove empty items |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
67 idens = [item.strip() for sublist in idens for item in sublist if item != ''] # noqa 501 |
8
a58dc5d4b8cd
planemo upload commit 4ba1ebe7b3f5e3fabf78b5fed7ed0b92e2cbf9e5-dirty
proteore
parents:
6
diff
changeset
|
68 ids = "\n".join(id_valid(idens)[0]) |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
69 json_string = os.popen("curl -H \"Content-Type: text/plain\" -d \"$(printf '%s')\" -X POST --url www.reactome.org/AnalysisService/identifiers/\?pageSize\=1\&page\=1 2> stderr" % ids).read() # noqa 501 |
8
a58dc5d4b8cd
planemo upload commit 4ba1ebe7b3f5e3fabf78b5fed7ed0b92e2cbf9e5-dirty
proteore
parents:
6
diff
changeset
|
70 if len(id_valid(idens)[1]) > 0: |
a58dc5d4b8cd
planemo upload commit 4ba1ebe7b3f5e3fabf78b5fed7ed0b92e2cbf9e5-dirty
proteore
parents:
6
diff
changeset
|
71 trash = id_valid(idens)[1] |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
72 # print(json_string) |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
73 j = json.loads(json_string) |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
74 print("Identifiers not found: " + str(j["identifiersNotFound"])) |
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
75 print("Pathways found: " + str(j["pathwaysFound"])) |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
76 return json_string, trash |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
77 |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
78 |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
79 def write_output(filename, json_string, species, trash_file, trash): |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
80 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
81 Replace json result in template and print to output |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
82 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
83 template = open(os.path.join(CURRENT_DIR, "template.html")) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
84 output = open(filename, "w") |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
85 try: |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
86 for line in template: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
87 if "{token}" in line: |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
88 line = line.replace("{species}", species) |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
89 line = line.replace("{token}", json.loads(json_string)["summary"]["token"]) # noqa 501 |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
90 output.write(line) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
91 except ValueError: |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
92 output.write("An error occurred due to unavailability of Reactome web service. Please return later.") # noqa 501 |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
93 template.close() |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
94 output.close() |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
95 |
4
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
96 if trash: |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
97 # print(trash) |
4
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
98 trash_out = open(trash_file, "w") |
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
99 trash_out.write("\n".join(trash)) |
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
100 trash_out.close() |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
101 |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
102 |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
103 def options(): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
104 parser = argparse.ArgumentParser() |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
105 argument = parser.add_argument("--json", nargs="+", required=True) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
106 argument = parser.add_argument("--output", default="output.html") |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
107 argument = parser.add_argument("--trash", default="trash.txt") |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
108 argument = parser.add_argument("--species", default="48887") # noqa 841 |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
109 args = parser.parse_args() |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
110 filename = args.output |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
111 json_string, trash = data_json(args.json) |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
112 write_output(filename, json_string, args.species, args.trash, trash) |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
113 |
11
19195d1a4063
"planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty"
proteore
parents:
8
diff
changeset
|
114 |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
115 if __name__ == "__main__": |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
116 options() |