0
|
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 res = []
|
|
10 remove = []
|
|
11 for id in identifiers:
|
|
12 id = id.split(";")[0]
|
|
13 if re.match("^[A-Za-z0-9_-]*$", id):
|
|
14 res.append(id)
|
|
15 else:
|
|
16 remove.append(id)
|
|
17 return res, remove
|
|
18
|
|
19 def isnumber(format, n):
|
|
20 float_format = re.compile("^[\-]?[1-9][0-9]*\.?[0-9]+$")
|
|
21 int_format = re.compile("^[\-]?[1-9][0-9]*$")
|
|
22 test = ""
|
|
23 if format == "int":
|
|
24 test = re.match(int_format, n)
|
|
25 elif format == "float":
|
|
26 test = re.match(float_format, n)
|
|
27 if test:
|
|
28 return True
|
|
29 else:
|
|
30 return False
|
|
31
|
|
32 def data_json(identifiers):
|
|
33 trash = []
|
|
34 if identifiers[1] == "list":
|
|
35 ids = "\n".join(id_valid(identifiers[0].split())[0])
|
|
36 #print(ids)
|
|
37 #print("curl -H \"Content-Type: text/plain\" -d \"$(printf '%s')\" -X POST --url www.reactome.org/AnalysisService/identifiers/projection/\?pageSize\=1\&page\=1" % ids)
|
|
38 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()
|
|
39 if len(id_valid(identifiers[0].split())[1]) > 0:
|
|
40 trash = id_valid(identifiers[0].split())[1]
|
|
41 #elif identifiers[1] == "file":
|
|
42 #file = open(identifiers[0]).readlines()
|
|
43 #ids = "\n".join(id_valid(file)[0])
|
|
44 #print(ids)
|
|
45 #print("curl -H \"Content-Type: text/plain\" -d \"$(printf '%s')\" -X POST --url www.reactome.org/AnalysisService/identifiers/projection/\?pageSize\=1\&page\=1" % ids)
|
|
46 #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()
|
|
47 #if len(id_valid(file)[1]) > 0:
|
|
48 #trash = id_valid(file)[1]
|
|
49 elif identifiers[1] == "mq_file":
|
|
50 header = identifiers[2]
|
|
51 mq = open(identifiers[0]).readlines()
|
|
52 if isnumber("int", identifiers[3].replace("c", "")):
|
|
53 if header == "true":
|
|
54 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq[1:]]
|
|
55 else:
|
|
56 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq]
|
|
57 ids = "\n".join(id_valid(idens)[0])
|
|
58 #print(ids)
|
|
59 #print("curl -H \"Content-Type: text/plain\" -d \"$(printf '%s')\" -X POST --url www.reactome.org/AnalysisService/identifiers/projection/\?pageSize\=1\&page\=1" % ids)
|
|
60 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()
|
|
61 if len(id_valid(idens)[1]) > 0:
|
|
62 trash = id_valid(idens)[1]
|
|
63 print(json_string)
|
|
64 return json_string, trash
|
|
65
|
|
66 def write_output(filename, json_string, trash_file, trash):
|
|
67 template = open(os.path.join(CURRENT_DIR, "template.html"))
|
|
68 output = open(filename, "w")
|
|
69 for line in template:
|
|
70 if "{token}" in line:
|
|
71 line = line.replace("{token}", json.loads(json_string)["summary"]["token"])
|
|
72 output.write(line)
|
|
73 template.close()
|
|
74 output.close()
|
|
75
|
|
76 trash_out = open(trash_file, "w")
|
|
77 trash_out.write("\n".join(trash))
|
|
78 trash_out.close()
|
|
79
|
|
80 def options():
|
|
81 parser = argparse.ArgumentParser()
|
|
82 argument = parser.add_argument("--json", nargs="+", required=True)
|
|
83 argument = parser.add_argument("--output", default="output.html")
|
|
84 argument = parser.add_argument("--trash", default="trash.txt")
|
|
85 args = parser.parse_args()
|
|
86 filename = args.output
|
|
87 json_string, trash = data_json(args.json)
|
|
88 write_output(filename, json_string, args.trash, trash)
|
|
89
|
|
90 if __name__ == "__main__":
|
|
91 options()
|