comparison reactome_analysis.py @ 0:216bd2a75b1d draft

planemo upload commit abb24d36c776520e73220d11386252d848173697-dirty
author proteore
date Sun, 26 Nov 2017 19:29:17 -0500
parents
children 878128362e33
comparison
equal deleted inserted replaced
-1:000000000000 0:216bd2a75b1d
1 import os
2 import re
3 import json
4 import argparse
5
6 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
7
8 def id_valid(identifiers):
9 """
10 Validate IDs if they contain special characters
11 """
12 res = []
13 remove = []
14 for id in identifiers:
15 id = id.split(";")[0]
16 if re.match("^[A-Za-z0-9_-]*$", id):
17 res.append(id)
18 else:
19 remove.append(id)
20 return res, remove
21
22 def isnumber(format, n):
23 """
24 Check if an variable is numeric
25 """
26 float_format = re.compile("^[\-]?[1-9][0-9]*\.?[0-9]+$")
27 int_format = re.compile("^[\-]?[1-9][0-9]*$")
28 test = ""
29 if format == "int":
30 test = re.match(int_format, n)
31 elif format == "float":
32 test = re.match(float_format, n)
33 if test:
34 return True
35 else:
36 return False
37
38 def data_json(identifiers):
39 """
40 Submit IDs list to Reactome and return results in json format
41 Return error in HTML format if web service is not available
42 """
43 trash = []
44 if identifiers[1] == "list":
45 ids = "\n".join(id_valid(identifiers[0].split())[0])
46 #print(ids)
47 #print("curl -H \"Content-Type: text/plain\" -d \"$(printf '%s')\" -X POST --url www.reactome.org/AnalysisService/identifiers/projection/\?pageSize\=1\&page\=1" % ids)
48 json_string = os.popen("curl -H \"Content-Type: text/plain\" -d \"$(printf '%s')\" -X POST --url www.reactome.org/AnalysisService/identifiers/projection/\?pageSize\=1\&page\=1" % ids).read()
49 if len(id_valid(identifiers[0].split())[1]) > 0:
50 trash = id_valid(identifiers[0].split())[1]
51 elif identifiers[1] == "file":
52 header = identifiers[2]
53 mq = open(identifiers[0]).readlines()
54 if isnumber("int", identifiers[3].replace("c", "")):
55 if header == "true":
56 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq[1:]]
57 else:
58 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq]
59 ids = "\n".join(id_valid(idens)[0])
60 #print(ids)
61 #print("curl -H \"Content-Type: text/plain\" -d \"$(printf '%s')\" -X POST --url www.reactome.org/AnalysisService/identifiers/projection/\?pageSize\=1\&page\=1" % ids)
62 json_string = os.popen("curl -H \"Content-Type: text/plain\" -d \"$(printf '%s')\" -X POST --url www.reactome.org/AnalysisService/identifiers/projection/\?pageSize\=1\&page\=1" % ids).read()
63 if len(id_valid(idens)[1]) > 0:
64 trash = id_valid(idens)[1]
65 print(json_string)
66 return json_string, trash
67
68 def write_output(filename, json_string, trash_file, trash):
69 """
70 Replace json result in template and print to output
71 """
72 template = open(os.path.join(CURRENT_DIR, "template.html"))
73 output = open(filename, "w")
74 try:
75 for line in template:
76 if "{token}" in line:
77 line = line.replace("{token}", json.loads(json_string)["summary"]["token"])
78 output.write(line)
79 except ValueError:
80 output.write("An error occurred due to unavailability of Reactome web service. Please return later.")
81 template.close()
82 output.close()
83
84 trash_out = open(trash_file, "w")
85 trash_out.write("\n".join(trash))
86 trash_out.close()
87
88 def options():
89 parser = argparse.ArgumentParser()
90 argument = parser.add_argument("--json", nargs="+", required=True)
91 argument = parser.add_argument("--output", default="output.html")
92 argument = parser.add_argument("--trash", default="trash.txt")
93 args = parser.parse_args()
94 filename = args.output
95 json_string, trash = data_json(args.json)
96 write_output(filename, json_string, args.trash, trash)
97
98 if __name__ == "__main__":
99 options()