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