# HG changeset patch # User proteore # Date 1620660634 0 # Node ID 19195d1a4063753716d048ee3f274b9bcc72e89e # Parent ef31b5ac28d7c1245ac9b0301079afdf825d8b6c "planemo upload commit a2de8dd8f2468046e787f83812d201bb191edab8-dirty" diff -r ef31b5ac28d7 -r 19195d1a4063 reactome_analysis.py --- a/reactome_analysis.py Fri Jan 24 10:45:56 2020 -0500 +++ b/reactome_analysis.py Mon May 10 15:30:34 2021 +0000 @@ -1,7 +1,12 @@ -import os, re, json, argparse, csv +import argparse +import csv +import json +import os +import re CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) + def id_valid(identifiers): """ Validate IDs if they contain special characters @@ -15,7 +20,8 @@ else: remove.append(id) return res, remove - + + def isnumber(format, n): """ Check if an variable is numeric @@ -32,6 +38,7 @@ else: return False + def data_json(identifiers): """ Submit IDs list to Reactome and return results in json format @@ -40,67 +47,70 @@ trash = [] if identifiers[1] == "list": ids = identifiers[0].split() - ids = [x.split(";") for x in ids] - ids = [item.strip() for sublist in ids for item in sublist if item != ''] - 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() + ids = [x.split(";") for x in ids] + ids = [item.strip() for sublist in ids for item in sublist if item != ''] # noqa 501 + 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() # noqa 501 if len(id_valid(identifiers[0].split())[1]) > 0: trash = id_valid(identifiers[0].split())[1] elif identifiers[1] == "file": header = identifiers[2] - with open(identifiers[0],"r") as mq : - file_content = csv.reader(mq,delimiter="\t") - file_content = list(file_content) #csv object to list + with open(identifiers[0], "r") as mq: + file_content = csv.reader(mq, delimiter="\t") + file_content = list(file_content) # csv object to list ncol = identifiers[3] if isnumber("int", ncol.replace("c", "")): if header == "true": - idens = [x for x in [line[int(ncol.replace("c", ""))-1].split(";") for line in file_content[1:]]] + idens = [x for x in [line[int(ncol.replace("c", ""))-1].split(";") for line in file_content[1:]]] # noqa 501 else: - idens = [x for x in [line[int(ncol.replace("c", ""))-1].split(";") for line in file_content]] - - idens = [item.strip() for sublist in idens for item in sublist if item != ''] #flat list of list of lists, remove empty items + idens = [x for x in [line[int(ncol.replace("c", ""))-1].split(";") for line in file_content]] # noqa 501 + # flat list of list of lists, remove empty items + idens = [item.strip() for sublist in idens for item in sublist if item != ''] # noqa 501 ids = "\n".join(id_valid(idens)[0]) - 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() + 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() # noqa 501 if len(id_valid(idens)[1]) > 0: trash = id_valid(idens)[1] - #print(json_string) + # print(json_string) j = json.loads(json_string) - print ("Identifiers not found: " + str(j["identifiersNotFound"])) - print ("Pathways found: " + str(j["pathwaysFound"])) + print("Identifiers not found: " + str(j["identifiersNotFound"])) + print("Pathways found: " + str(j["pathwaysFound"])) return json_string, trash + def write_output(filename, json_string, species, trash_file, trash): """ Replace json result in template and print to output """ template = open(os.path.join(CURRENT_DIR, "template.html")) output = open(filename, "w") - try: + try: for line in template: if "{token}" in line: line = line.replace("{species}", species) - line = line.replace("{token}", json.loads(json_string)["summary"]["token"]) + line = line.replace("{token}", json.loads(json_string)["summary"]["token"]) # noqa 501 output.write(line) except ValueError: - output.write("An error occurred due to unavailability of Reactome web service. Please return later.") + output.write("An error occurred due to unavailability of Reactome web service. Please return later.") # noqa 501 template.close() output.close() - + if trash: - #print(trash) + # print(trash) trash_out = open(trash_file, "w") trash_out.write("\n".join(trash)) trash_out.close() + def options(): parser = argparse.ArgumentParser() argument = parser.add_argument("--json", nargs="+", required=True) argument = parser.add_argument("--output", default="output.html") argument = parser.add_argument("--trash", default="trash.txt") - argument = parser.add_argument("--species", default="48887") + argument = parser.add_argument("--species", default="48887") # noqa 841 args = parser.parse_args() filename = args.output json_string, trash = data_json(args.json) write_output(filename, json_string, args.species, args.trash, trash) + if __name__ == "__main__": options() diff -r ef31b5ac28d7 -r 19195d1a4063 reactome_analysis.xml --- a/reactome_analysis.xml Fri Jan 24 10:45:56 2020 -0500 +++ b/reactome_analysis.xml Mon May 10 15:30:34 2021 +0000 @@ -1,4 +1,4 @@ - + [Reactome] @@ -65,7 +65,7 @@ - + Please click the button to execute the analysis:

-
+ diff -r ef31b5ac28d7 -r 19195d1a4063 test-data/Reactome_Analysis_Tools_invalid_ID.txt --- a/test-data/Reactome_Analysis_Tools_invalid_ID.txt Fri Jan 24 10:45:56 2020 -0500 +++ b/test-data/Reactome_Analysis_Tools_invalid_ID.txt Mon May 10 15:30:34 2021 +0000 @@ -1,1 +0,0 @@ -