comparison reactome_analysis.py @ 6:9cc475dcd0f2 draft

planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
author proteore
date Tue, 18 Dec 2018 10:03:50 -0500
parents 878128362e33
children a58dc5d4b8cd
comparison
equal deleted inserted replaced
5:7fbd66f985b7 6:9cc475dcd0f2
41 Return error in HTML format if web service is not available 41 Return error in HTML format if web service is not available
42 """ 42 """
43 trash = [] 43 trash = []
44 if identifiers[1] == "list": 44 if identifiers[1] == "list":
45 ids = "\n".join(id_valid(identifiers[0].split())[0]) 45 ids = "\n".join(id_valid(identifiers[0].split())[0])
46 #print(ids) 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()
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: 47 if len(id_valid(identifiers[0].split())[1]) > 0:
50 trash = id_valid(identifiers[0].split())[1] 48 trash = id_valid(identifiers[0].split())[1]
51 elif identifiers[1] == "file": 49 elif identifiers[1] == "file":
52 header = identifiers[2] 50 header = identifiers[2]
53 mq = open(identifiers[0]).readlines() 51 mq = open(identifiers[0]).readlines()
55 if header == "true": 53 if header == "true":
56 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq[1:]] 54 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq[1:]]
57 else: 55 else:
58 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq] 56 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq]
59 ids = "\n".join(id_valid(idens)[0]) 57 ids = "\n".join(id_valid(idens)[0])
60 #print(ids) 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()
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: 59 if len(id_valid(idens)[1]) > 0:
64 trash = id_valid(idens)[1] 60 trash = id_valid(idens)[1]
65 print(json_string) 61 #print(json_string)
62 j = json.loads(json_string)
63 print ("Identifiers not found: " + str(j["identifiersNotFound"]))
64 print ("Pathways found: " + str(j["pathwaysFound"]))
66 return json_string, trash 65 return json_string, trash
67 66
68 def write_output(filename, json_string, trash_file, trash): 67 def write_output(filename, json_string, species, trash_file, trash):
69 """ 68 """
70 Replace json result in template and print to output 69 Replace json result in template and print to output
71 """ 70 """
72 template = open(os.path.join(CURRENT_DIR, "template.html")) 71 template = open(os.path.join(CURRENT_DIR, "template.html"))
73 output = open(filename, "w") 72 output = open(filename, "w")
74 try: 73 try:
75 for line in template: 74 for line in template:
76 if "{token}" in line: 75 if "{token}" in line:
76 line = line.replace("{species}", species)
77 line = line.replace("{token}", json.loads(json_string)["summary"]["token"]) 77 line = line.replace("{token}", json.loads(json_string)["summary"]["token"])
78 output.write(line) 78 output.write(line)
79 except ValueError: 79 except ValueError:
80 output.write("An error occurred due to unavailability of Reactome web service. Please return later.") 80 output.write("An error occurred due to unavailability of Reactome web service. Please return later.")
81 template.close() 81 template.close()
82 output.close() 82 output.close()
83 83
84 if trash: 84 if trash:
85 print(trash) 85 #print(trash)
86 trash_out = open(trash_file, "w") 86 trash_out = open(trash_file, "w")
87 trash_out.write("\n".join(trash)) 87 trash_out.write("\n".join(trash))
88 trash_out.close() 88 trash_out.close()
89 89
90 def options(): 90 def options():
91 parser = argparse.ArgumentParser() 91 parser = argparse.ArgumentParser()
92 argument = parser.add_argument("--json", nargs="+", required=True) 92 argument = parser.add_argument("--json", nargs="+", required=True)
93 argument = parser.add_argument("--output", default="output.html") 93 argument = parser.add_argument("--output", default="output.html")
94 argument = parser.add_argument("--trash", default="trash.txt") 94 argument = parser.add_argument("--trash", default="trash.txt")
95 argument = parser.add_argument("--species", default="48887")
95 args = parser.parse_args() 96 args = parser.parse_args()
96 filename = args.output 97 filename = args.output
97 json_string, trash = data_json(args.json) 98 json_string, trash = data_json(args.json)
98 write_output(filename, json_string, args.trash, trash) 99 write_output(filename, json_string, args.species, args.trash, trash)
99 100
100 if __name__ == "__main__": 101 if __name__ == "__main__":
101 options() 102 options()