Mercurial > repos > proteore > proteore_reactome
comparison reactome_analysis.py @ 0:19d8daa1eb2e draft
planemo upload commit 170560760f17fd1b77efe8bb95fedf3eb2433f0b-dirty
| author | proteore |
|---|---|
| date | Mon, 26 Nov 2018 04:48:25 -0500 |
| parents | |
| children | 8200968789c1 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:19d8daa1eb2e |
|---|---|
| 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(r"^[-]?[1-9][0-9]*.?[0-9]+$") | |
| 27 int_format = re.compile(r"^[-]?[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 if trash: | |
| 85 print(trash) | |
| 86 trash_out = open(trash_file, "w") | |
| 87 trash_out.write("\n".join(trash)) | |
| 88 trash_out.close() | |
| 89 | |
| 90 def options(): | |
| 91 parser = argparse.ArgumentParser() | |
| 92 argument = parser.add_argument("--json", nargs="+", required=True) | |
| 93 argument = parser.add_argument("--output", default="output.html") | |
| 94 argument = parser.add_argument("--trash", default="trash.txt") | |
| 95 args = parser.parse_args() | |
| 96 filename = args.output | |
| 97 json_string, trash = data_json(args.json) | |
| 98 write_output(filename, json_string, args.trash, trash) | |
| 99 | |
| 100 if __name__ == "__main__": | |
| 101 options() |
