annotate create.py @ 16:ba9d0fc8657f draft

Uploaded 20190118
author fabio
date Fri, 18 Jan 2019 10:12:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
1 #!/usr/bin/env python
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
2
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
3 # https://github.com/ross/requests-futures
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
4 # http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
5
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
6 import sys, os, uuid, optparse, requests, json, time
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
7 #from requests_futures.sessions import FuturesSession
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
8
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
9 #### NN14 ####
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
10 SERVICE_URL = "http://nn14.galaxyproject.org:8080/";
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
11 #service_url = "http://127.0.0.1:8082/";
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
12 CREATE_URL = SERVICE_URL+"tree/create";
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
13 STATUS_URL = SERVICE_URL+"status/<query_id>";
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
14 ##############
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
15 # query delay in seconds
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
16 QUERY_DELAY = 30;
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
17 ##############
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
18
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
19 __version__ = "1.0.0";
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
20 ERR_EXIT_CODE = 1;
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
21 OK_EXIT_CODE = 0;
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
22
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
23 def raiseException( exitcode, message, errorfilepath ):
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
24 with open(errorfilepath, 'w') as out:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
25 out.write(message);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
26 sys.exit(exitcode);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
27
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
28 def create_request( options, args, data ):
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
29 outfilepath = options.outfile;
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
30 cluster_id_2_query_id = { };
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
31
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
32 for cluster_id in data:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
33 payload = { };
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
34 payload["accessions"] = data[cluster_id];
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
35 # add additional parameters to the payload
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
36 payload["qualitycontrol"] = int(options.qualitycontrol);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
37 payload["qualitythreshold"] = float(options.qualitythreshold);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
38 payload["klen"] = int(options.klen);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
39 payload["minabundance"] = int(options.minabundance);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
40 # set the content type to application/json
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
41 headers = {'Content-type': 'application/json'};
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
42 # create a session
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
43 session = requests.Session();
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
44 # make a synchronous post request to the create route
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
45 req = session.post(CREATE_URL, headers=headers, json=payload);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
46 resp_code = req.status_code;
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
47 #print(str(req.content)+"\n\n");
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
48 if resp_code == requests.codes.ok:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
49 resp_content = str(req.content);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
50 # convert out to json
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
51 json_content = json.loads(resp_content);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
52 # retrieve query id
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
53 query_id = json_content['query_id'];
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
54 cluster_id_2_query_id[cluster_id] = query_id;
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
55 else:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
56 with open(outfilepath, 'a+') as outfile:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
57 outfile.write( "An error has occurred while submitting data to the /tree/create endpoint for the cluster " + cluster_id + "\n\n" );
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
58
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
59 build_flags = [ ]
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
60 while len(build_flags) < len(cluster_id_2_query_id):
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
61 for idx, cluster_id in enumerate( cluster_id_2_query_id ):
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
62 if cluster_id not in build_flags:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
63 query_id = cluster_id_2_query_id[ cluster_id ];
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
64 # create a new session
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
65 session = requests.Session();
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
66 # make a synchronous get request to the status route
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
67 status_query_url = STATUS_URL.replace("<query_id>", query_id);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
68 status_req = session.get(status_query_url);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
69 status_resp_content = str(status_req.content);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
70 #print(status_resp_content+"\n\n");
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
71 # convert out to json
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
72 json_status_content = json.loads(status_resp_content);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
73 # take a look at the state
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
74 # state attribute is always available
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
75 if json_status_content['state'] == 'SUCCESS':
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
76 build_flags.append( cluster_id );
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
77 built_tree_id = json_status_content['results']['tree_id'];
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
78 with open(outfilepath, 'a+') as outfile:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
79 outfile.write( "Query ID: " + str(query_id) + "\n" + "Query status: " + str(json_status_content['state']) + "\n" + "Cluster ID: " + cluster_id + "\n" + "Sequence Bloom Tree ID: " + built_tree_id + "\n\n" );
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
80 elif json_status_content['state'] in ['FAILURE', 'REVOKED']:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
81 build_flags.append( cluster_id );
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
82 with open(outfilepath, 'a+') as outfile:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
83 outfile.write( "Query ID: " + str(query_id) + "\n" + "Query status: " + str(json_status_content['state']) + "\n" + "Cluster ID: " + cluster_id + "\n\n" );
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
84 if len(build_flags) < len(cluster_id_2_query_id):
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
85 time.sleep(QUERY_DELAY); # in seconds
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
86 return sys.exit(OK_EXIT_CODE);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
87
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
88 def create( options, args ):
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
89 multiple_data = {};
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
90 experiment_list_file_path = options.explist;
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
91 with open(experiment_list_file_path) as explist:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
92 for line in explist:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
93 if line.strip() != "":
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
94 line_split = line.strip().split("\t"); # split on tab
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
95 if len(line_split) == 2: # 0:accession , 1:cluster_id , otherwise skip line
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
96 accession = line_split[0];
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
97 cluster_id = line_split[1];
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
98 if cluster_id in multiple_data:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
99 multiple_data[cluster_id].append( accession );
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
100 else:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
101 multiple_data[cluster_id] = [ accession ];
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
102 if len(multiple_data) > 0:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
103 return create_request( options, args, multiple_data );
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
104 else:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
105 return raiseException( ERR_EXIT_CODE, "An error has occurred. Please be sure that your input file is valid.", options.outfile );
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
106
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
107 def __main__():
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
108 # Parse the command line options
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
109 usage = "Usage: create.py --explist experiment_list --qualitycontrol quality_control --qualitythreshold quality_threshold --klen kmer_len --minabundance min_abundance --outfile output_file_path";
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
110 parser = optparse.OptionParser(usage = usage);
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
111 parser.add_option("-v", "--version", action="store_true", dest="version",
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
112 default=False, help="display version and exit")
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
113 parser.add_option("-l", "--explist", type="string",
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
114 action="store", dest="explist", help="tabular file with a list of SRA accessions and their cluster label");
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
115 parser.add_option("-q", "--qualitycontrol", type="int", default=0
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
116 action="store", dest="qualitycontrol", help="flag to enable or disable the experiment quality control");
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
117 parser.add_option("-t", "--qualitythreshold", type="float", default=0.0
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
118 action="store", dest="qualitythreshold", help="quality threshold, if quality control is enabled only");
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
119 parser.add_option("-k", "--klen", type="int", default=21,
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
120 action="store", dest="klen", help="k-mer length");
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
121 parser.add_option("-m", "--minabundance", type="int", default=2,
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
122 action="store", dest="minabundance", help="minimum abundance");
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
123 parser.add_option("-o", "--outfile", type="string", default="outfile_txt",
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
124 action="store", dest="outfile", help="output file path");
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
125
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
126 (options, args) = parser.parse_args();
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
127 if options.version:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
128 print __version__;
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
129 else:
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
130 return create( options, args );
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
131
ba9d0fc8657f Uploaded 20190118
fabio
parents:
diff changeset
132 if __name__ == "__main__": __main__()