Mercurial > repos > proteore > proteore_reactome
comparison reactome_analysis.py @ 8:a58dc5d4b8cd draft
planemo upload commit 4ba1ebe7b3f5e3fabf78b5fed7ed0b92e2cbf9e5-dirty
author | proteore |
---|---|
date | Fri, 28 Jun 2019 05:17:11 -0400 |
parents | 9cc475dcd0f2 |
children | 19195d1a4063 |
comparison
equal
deleted
inserted
replaced
7:6c95f1b88627 | 8:a58dc5d4b8cd |
---|---|
1 import os | 1 import os, re, json, argparse, csv |
2 import re | |
3 import json | |
4 import argparse | |
5 | 2 |
6 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) | 3 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) |
7 | 4 |
8 def id_valid(identifiers): | 5 def id_valid(identifiers): |
9 """ | 6 """ |
40 Submit IDs list to Reactome and return results in json format | 37 Submit IDs list to Reactome and return results in json format |
41 Return error in HTML format if web service is not available | 38 Return error in HTML format if web service is not available |
42 """ | 39 """ |
43 trash = [] | 40 trash = [] |
44 if identifiers[1] == "list": | 41 if identifiers[1] == "list": |
45 ids = "\n".join(id_valid(identifiers[0].split())[0]) | 42 ids = identifiers[0].split() |
43 ids = [x.split(";") for x in ids] | |
44 ids = [item.strip() for sublist in ids for item in sublist if item != ''] | |
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() | 45 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 if len(id_valid(identifiers[0].split())[1]) > 0: | 46 if len(id_valid(identifiers[0].split())[1]) > 0: |
48 trash = id_valid(identifiers[0].split())[1] | 47 trash = id_valid(identifiers[0].split())[1] |
49 elif identifiers[1] == "file": | 48 elif identifiers[1] == "file": |
50 header = identifiers[2] | 49 header = identifiers[2] |
51 mq = open(identifiers[0]).readlines() | 50 with open(identifiers[0],"r") as mq : |
52 if isnumber("int", identifiers[3].replace("c", "")): | 51 file_content = csv.reader(mq,delimiter="\t") |
53 if header == "true": | 52 file_content = list(file_content) #csv object to list |
54 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq[1:]] | 53 ncol = identifiers[3] |
55 else: | 54 if isnumber("int", ncol.replace("c", "")): |
56 idens = [x.split("\t")[int(identifiers[3].replace("c", ""))-1] for x in mq] | 55 if header == "true": |
57 ids = "\n".join(id_valid(idens)[0]) | 56 idens = [x for x in [line[int(ncol.replace("c", ""))-1].split(";") for line in file_content[1:]]] |
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() | 57 else: |
59 if len(id_valid(idens)[1]) > 0: | 58 idens = [x for x in [line[int(ncol.replace("c", ""))-1].split(";") for line in file_content]] |
60 trash = id_valid(idens)[1] | 59 |
60 idens = [item.strip() for sublist in idens for item in sublist if item != ''] #flat list of list of lists, remove empty items | |
61 ids = "\n".join(id_valid(idens)[0]) | |
62 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() | |
63 if len(id_valid(idens)[1]) > 0: | |
64 trash = id_valid(idens)[1] | |
61 #print(json_string) | 65 #print(json_string) |
62 j = json.loads(json_string) | 66 j = json.loads(json_string) |
63 print ("Identifiers not found: " + str(j["identifiersNotFound"])) | 67 print ("Identifiers not found: " + str(j["identifiersNotFound"])) |
64 print ("Pathways found: " + str(j["pathwaysFound"])) | 68 print ("Pathways found: " + str(j["pathwaysFound"])) |
65 return json_string, trash | 69 return json_string, trash |