Mercurial > repos > proteore > proteore_reactome
annotate reactome_analysis.py @ 7:6c95f1b88627 draft
planemo upload commit 89cd1b5f2ca7b135abfe2d9ed18bf2478a59e099-dirty
author | proteore |
---|---|
date | Tue, 05 Mar 2019 09:40:41 -0500 |
parents | 9cc475dcd0f2 |
children | a58dc5d4b8cd |
rev | line source |
---|---|
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
1 import os |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
2 import re |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
3 import json |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
4 import argparse |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
5 |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
6 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
7 |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
8 def id_valid(identifiers): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
9 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
10 Validate IDs if they contain special characters |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
11 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
12 res = [] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
13 remove = [] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
14 for id in identifiers: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
15 id = id.split(";")[0] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
16 if re.match("^[A-Za-z0-9_-]*$", id): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
17 res.append(id) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
18 else: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
19 remove.append(id) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
20 return res, remove |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
21 |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
22 def isnumber(format, n): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
23 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
24 Check if an variable is numeric |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
25 """ |
4
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
26 float_format = re.compile(r"^[-]?[1-9][0-9]*.?[0-9]+$") |
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
27 int_format = re.compile(r"^[-]?[1-9][0-9]*$") |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
28 test = "" |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
29 if format == "int": |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
30 test = re.match(int_format, n) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
31 elif format == "float": |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
32 test = re.match(float_format, n) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
33 if test: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
34 return True |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
35 else: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
36 return False |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
37 |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
38 def data_json(identifiers): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
39 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
40 Submit IDs list to Reactome and return results in json format |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
41 Return error in HTML format if web service is not available |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
42 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
43 trash = [] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
44 if identifiers[1] == "list": |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
45 ids = "\n".join(id_valid(identifiers[0].split())[0]) |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
46 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() |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
47 if len(id_valid(identifiers[0].split())[1]) > 0: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
48 trash = id_valid(identifiers[0].split())[1] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
49 elif identifiers[1] == "file": |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
50 header = identifiers[2] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
51 mq = open(identifiers[0]).readlines() |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
52 if isnumber("int", identifiers[3].replace("c", "")): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
53 if header == "true": |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
54 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq[1:]] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
55 else: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
56 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq] |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
57 ids = "\n".join(id_valid(idens)[0]) |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
58 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() |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
59 if len(id_valid(idens)[1]) > 0: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
60 trash = id_valid(idens)[1] |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
61 #print(json_string) |
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
62 j = json.loads(json_string) |
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
63 print ("Identifiers not found: " + str(j["identifiersNotFound"])) |
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
64 print ("Pathways found: " + str(j["pathwaysFound"])) |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
65 return json_string, trash |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
66 |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
67 def write_output(filename, json_string, species, trash_file, trash): |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
68 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
69 Replace json result in template and print to output |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
70 """ |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
71 template = open(os.path.join(CURRENT_DIR, "template.html")) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
72 output = open(filename, "w") |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
73 try: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
74 for line in template: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
75 if "{token}" in line: |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
76 line = line.replace("{species}", species) |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
77 line = line.replace("{token}", json.loads(json_string)["summary"]["token"]) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
78 output.write(line) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
79 except ValueError: |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
80 output.write("An error occurred due to unavailability of Reactome web service. Please return later.") |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
81 template.close() |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
82 output.close() |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
83 |
4
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
84 if trash: |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
85 #print(trash) |
4
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
86 trash_out = open(trash_file, "w") |
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
87 trash_out.write("\n".join(trash)) |
878128362e33
planemo upload commit 34edf9c3db61650a03d5a0d548b9697a94ecde34-dirty
proteore
parents:
0
diff
changeset
|
88 trash_out.close() |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
89 |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
90 def options(): |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
91 parser = argparse.ArgumentParser() |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
92 argument = parser.add_argument("--json", nargs="+", required=True) |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
93 argument = parser.add_argument("--output", default="output.html") |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
94 argument = parser.add_argument("--trash", default="trash.txt") |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
95 argument = parser.add_argument("--species", default="48887") |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
96 args = parser.parse_args() |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
97 filename = args.output |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
98 json_string, trash = data_json(args.json) |
6
9cc475dcd0f2
planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
proteore
parents:
4
diff
changeset
|
99 write_output(filename, json_string, args.species, args.trash, trash) |
0
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
100 |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
101 if __name__ == "__main__": |
216bd2a75b1d
planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
proteore
parents:
diff
changeset
|
102 options() |