4
|
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 os, uuid, optparse, requests, json, time
|
|
7 #from requests_futures.sessions import FuturesSession
|
|
8
|
|
9 #### NN14 ####
|
10
|
10 SERVICE_URL = "http://nn14.galaxyproject.org:8080/";
|
4
|
11 #service_url = "http://127.0.0.1:8082/";
|
10
|
12 QUERY_URL = SERVICE_URL+"tree/0/query";
|
|
13 STATUS_URL = SERVICE_URL+"status/<task_id>";
|
4
|
14 ##############
|
6
|
15 # query delay in seconds
|
10
|
16 QUERY_DELAY = 30;
|
6
|
17 ##############
|
4
|
18
|
10
|
19 VALID_CHARS = '.-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '
|
|
20
|
4
|
21 def query_request( options, args, payload ):
|
|
22 # add additional parameters to the payload
|
|
23 #payload["tree_id"] = str(options.treeid);
|
|
24 payload["search_mode"] = str(options.search);
|
|
25 payload["exact_algorithm"] = int(options.exact);
|
|
26 payload["search_threshold"] = float(options.sthreshold);
|
|
27 # set the content type to application/json
|
|
28 headers = {'Content-type': 'application/json'};
|
|
29
|
|
30 # create a session
|
|
31 session = requests.Session();
|
|
32 # make a synchronous post request to the query route
|
10
|
33 req = session.post(QUERY_URL, headers=headers, json=payload);
|
4
|
34 resp_code = req.status_code;
|
6
|
35 print(str(req.content)+"\n\n");
|
4
|
36 if resp_code == requests.codes.ok:
|
|
37 resp_content = str(req.content);
|
|
38 # convert out to json
|
|
39 json_content = json.loads(resp_content);
|
|
40 # retrieve task id
|
|
41 task_id = json_content['task_id'];
|
|
42 task_processed = False;
|
|
43 # results json content
|
|
44 json_status_content = None;
|
|
45 task_status = None;
|
|
46 while task_processed is False:
|
|
47 # create a new session
|
|
48 session = requests.Session();
|
|
49 # make a synchronous get request to the status route
|
10
|
50 status_query_url = STATUS_URL.replace("<task_id>", task_id);
|
4
|
51 status_req = session.get(status_query_url);
|
|
52 status_resp_content = str(status_req.content);
|
6
|
53 print(status_resp_content+"\n\n");
|
4
|
54 # convert out to json
|
|
55 json_status_content = json.loads(status_resp_content);
|
|
56 # take a look at the state
|
|
57 # state attribute is always available
|
|
58 if json_status_content['state'] == 'SUCCESS':
|
|
59 task_processed = True;
|
|
60 break;
|
|
61 elif json_status_content['state'] in ['FAILURE', 'REVOKED']:
|
|
62 return "Task status: "+str(json_status_content['state']);
|
|
63 else:
|
10
|
64 time.sleep(QUERY_DELAY); # in seconds
|
4
|
65
|
|
66 # get output dir (collection) path
|
|
67 output_dir_path = options.outputdir;
|
|
68 if not os.path.exists(output_dir_path):
|
|
69 os.makedirs(output_dir_path);
|
10
|
70 out_file_format = "tabular";
|
4
|
71
|
|
72 for block in json_status_content['results']:
|
|
73 seq_id = block['sequence_id'];
|
|
74 accessions = block['accession_numbers'];
|
|
75 # put response block in the output collection
|
|
76 output_file_path = os.path.join(output_dir_path, seq_id + "_" + out_file_format);
|
|
77 accessions_list = "";
|
|
78 for accession_number in accessions:
|
|
79 accessions_list = accessions_list + accession_number + "\n";
|
|
80 with open(output_file_path, 'w') as out:
|
|
81 out.write(accessions_list.strip());
|
|
82 else:
|
|
83 return "Unable to query the remote server. Please try again in a while.";
|
|
84
|
|
85 def query( options, args ):
|
|
86 multiple_data = {};
|
|
87 comma_sep_file_paths = options.files;
|
|
88 #print("files: "+str(comma_sep_file_paths)+" - "+str(type(comma_sep_file_paths)));
|
|
89 # check if options.files contains at least one file path
|
|
90 if comma_sep_file_paths is not None:
|
|
91 # split file paths
|
|
92 file_paths = comma_sep_file_paths.split(",");
|
|
93 # split file names
|
|
94 comma_sep_file_names = str(options.names);
|
|
95 #print("names: "+str(comma_sep_file_names));
|
|
96 file_names = comma_sep_file_names.split(",");
|
|
97 for idx, file_path in enumerate(file_paths):
|
|
98 #file_name = file_names[idx];
|
|
99 with open(file_path, 'r') as content_file:
|
|
100 for line in content_file:
|
|
101 if line.strip() != "":
|
8
|
102 line_split = line.strip().split("\t"); # split on tab
|
4
|
103 if len(line_split) == 2: # 0:id , 1:seq , otherwise skip line
|
|
104 seq_id = line_split[0];
|
10
|
105 # fix seq_id using valid chars only
|
|
106 seq_id = ''.join(e for e in seq_id if e in VALID_CHARS)
|
4
|
107 seq_text = line_split[1];
|
|
108 if seq_id in multiple_data:
|
|
109 return "Error: the id '"+seq_id+"' is duplicated";
|
|
110 multiple_data[seq_id] = seq_text;
|
|
111 if len(multiple_data) > 0:
|
6
|
112 return query_request( options, args, multiple_data );
|
4
|
113 #return echo( options, args );
|
|
114 else:
|
|
115 return "An error has occurred. Please be sure that your input files are valid.";
|
|
116 else:
|
|
117 # try with the sequence in --sequence
|
|
118 text_content = options.sequences;
|
|
119 #print("sequences: "+text_content);
|
|
120 # check if options.sequences contains a list of sequences (one for each row)
|
|
121 if text_content is not None:
|
|
122 text_content = str(text_content);
|
|
123 if text_content.strip():
|
|
124 # populate a dictionary with the files containing the sequences to query
|
|
125 text_content = text_content.strip().split("__cn__"); # split on new line
|
|
126 for line in text_content:
|
|
127 if line.strip() != "":
|
|
128 line_split = line.strip().split("__tc__"); # split on tab
|
|
129 if len(line_split) == 2: # 0:id , 1:seq , otherwise skip line
|
|
130 seq_id = line_split[0];
|
10
|
131 # fix seq_id using valid chars only
|
|
132 seq_id = ''.join(e for e in seq_id if e in VALID_CHARS)
|
4
|
133 seq_text = line_split[1];
|
|
134 if seq_id in multiple_data:
|
|
135 return "Error: the id '"+seq_id+"' is duplicated";
|
|
136 multiple_data[seq_id] = seq_text;
|
|
137 if len(multiple_data) > 0:
|
6
|
138 return query_request( options, args, multiple_data );
|
4
|
139 #return echo( options, args );
|
|
140 else:
|
|
141 return "An error has occurred. Please be sure that your input files are valid.";
|
|
142 else:
|
|
143 return "You have to insert at least one row formatted as a tab delimited <id, sequence> touple";
|
|
144 return -1;
|
|
145
|
|
146 def __main__():
|
|
147 # Parse the command line options
|
|
148 usage = "Usage: query.py --files comma_sep_file_paths --names comma_seq_file_names --sequences sequences_text --search search_mode --exact exact_alg --sthreshold threshold --outputdir output_dir_path";
|
|
149 parser = optparse.OptionParser(usage = usage);
|
|
150 parser.add_option("-f", "--files", type="string",
|
|
151 action="store", dest="files", help="comma separated files path");
|
|
152 parser.add_option("-n", "--names", type="string",
|
|
153 action="store", dest="names", help="comma separated names associated to the files specified in --files");
|
|
154 parser.add_option("-s", "--sequences", type="string",
|
|
155 action="store", dest="sequences", help="contains a list of sequences (one for each row)");
|
|
156 parser.add_option("-a", "--fasta", type="string",
|
|
157 action="store", dest="fasta", help="contains the content of a fasta file");
|
|
158 parser.add_option("-x", "--search", type="string", default=0,
|
|
159 action="store", dest="search", help="search mode");
|
|
160 parser.add_option("-e", "--exact", type="int", default=0,
|
|
161 action="store", dest="exact", help="exact algorithm (required if search is 1 only)");
|
|
162 parser.add_option("-t", "--sthreshold", type="float",
|
|
163 action="store", dest="sthreshold", help="threshold applied to the search algrithm");
|
|
164 parser.add_option("-o", "--outputdir", type="string",
|
|
165 action="store", dest="outputdir", help="output directory (collection) path");
|
|
166
|
|
167 #parser.add_option("-k", "--outfile", type="string",
|
|
168 #action="store", dest="outfile", help="output file");
|
|
169
|
|
170 # TEST
|
|
171 #--search 'rrr'
|
|
172 #--sthreshold 0.5
|
|
173 #--exact 0
|
|
174 #--sequences 'id0__tc__CAATTAATGATAAATATTTTATAAGGTGCGGAAATAAAGTGAGGAATATCTTTTAAATTCAAGTTCAATTCTGAAAGC'
|
|
175 #--outputdir 'collection_content'
|
6
|
176 #sequences = 'NM_001169378.2__tc__atttcggatgctttggagggaggaactctagtgctgcattgattggggcgtgtgttaatgatattcccagttcgcatggcgagcatcgattcctggtacgtatgtgggccccttgactcccacttatcgcacttgtcgttcgcaatttgcatgaattccgcttcgtctgaaacgcacttgcgccagacttctccggctggtctgatctggtctgtgatccggtctggtggggcgccagttgcgtttcgagctcatcaccagtcactccgcagtcgcattctgccagaggtctccgatcaagagcgcttctccattcgagattcaaacgcagcgcggtctgacgccgccacatcgagtgaaatccatatcgatggccacattcacacaggacgagatcgacttcctgcgcagccatggcaacgagctgtgtgccaagacctggctgggattgtgggatccgaagcgggctgtgcaccagcaggagcagcgcgaactgatgatggacaagtatgagcggaagcgatactacctggagccggccagtcctcttaagtcgctggccaatgcggtcaacctgaagtcgtctgctccggcgacgaaccacactcagaatggccaccaaaatgggtatgccagcatccatttgacgcctcctgctgcccagcggacctcggccaatggattgcagaaggtggccaactcgtcgagtaactcttctggaaagacctcatcctcgatcagtaggccacactataatcaccagaacaacagccaaaacaacaatcacgatgcctttggcctgggtggcggattgagcagcctgaacagcgccggttccacatccactggagctctttccgacaccagcagttgtgctagcaatggcttcggtgcggactgcgactttgtggctgactttggctcggccaacattttcgacgccacatcggcgcgttccacaggatcgccggcggtgtcgtccgtgtcctcagtgggttccagcaatggctacgccaaggtgcagcccatccgggcagctcatctccagcagcaacagcagttgcagcagcagctgcatcagcagcagctcctcaatggcaatggtcatcagggcactgagaactttgccgacttcgatcacgctcccatctacaatgcagtggctccaccgacttttaacgattggatcagcgactggagcaggcggggcttccacgatcccttcgacgattgcgatgactcgccaccaggtgcccgccctccagcacctgcgccagctcctgctcaagttcccgcagtatcatcaccattgccaaccgtccgagaagaaccagagcttgcgtggaatttttgggaggacgagatgcgaatagaggcgcaggaaaaggagtcccaaactaaacagccggagttgggctactccttttcgattagtactactacgcccctttccccttcgaatcccttcctgccctaccttgtcagtgaggagcagcatcgaaatcatccagagaagccctccttttcgtattcgttgttcagctccatatcaaatagttcgcaagaagatcaggcggatgatcatgagatgaatgttttaaatgccaatttccatgatttctttacgtggagtgctcccttgcagaacggccatacgaccagtccgcccaagggcggaaatgcagcgatggcgcccagtgaggatcgatatgccgctcttaaggatctcgacgagcagctgcgagaactgaaggccagcgaaagcgccacagagacgcccacgcccaccagtggcaatgttcaggccacagatgcctttggtggagccctcaacaacaatccaaatcccttcaagggccagcaacagcagcagctcagcagccatgtggtgaatccattccagcagcagcaacagcagcagcaccagcagaatctctatggccagttgacgctcataccaaatgcctacggcagcagttcccagcagcagatggggcaccatctcctccagcagcagcagcagcaacagcagagcttcttcaacttcaacaacaacgggttcgccatctcgcagggtctgcccaacggctgcggcttcggcagcatgcaacccgctcctgtgatggccaacaatccctttgcagccagcggcgccatgaacaccaacaatccattcttatgagactcaacccgggagaatccgcctcgcgccacctggcagaggcgctgagccagcgaacaaagagcagacgcggaggaaccgaaccgaaattagtccattttactaacaatagcgttaatctatgtatacataatgcacgccggagagcactctttgtgtacatagcccaaatatgtacacccgaaaggctccacgctgacgctagtcctcgcggatggcggaggcggactggggcgttgatatattcttttacatggtaactctactctaacgtttacggatacggatatttgtatttgccgtttgccctagaactctatacttgtactaagcgcccatgaacacttcatccactaacatagctactaatcctcatcctagtggaggatgcagttggtccagacactctgttatttgttttatccatcctcgtacttgtctttgtcccatttagcactttcgttgcggataagaactttgtcagttattgattgtgtggccttaataagattataaaactaaatattataacgtacgactatacatatacggatacagatacagattcagacacagttagtacagatacagatatacatatacgcttttgtacctaatgaattgcttcttgtttccattgctaatcatctgcttttcgtgtgctaattttatacactagtacgtgcgatatcggccgtgcagatagattgctcagctcgcgagtcaagcctcttttggttgcacccacggcagacatttgtacatatactgtctgattgtaagcctcgtgtaatacctccattaacaccactcccccaccacccatccatcgaaccccgaatccatgactcaattcactgctcacatgtccatgcccatgccttaacgtgtcaaacattatcgaagccttaaagttatttaaaactacgaaatttcaataaaaacaaataagaacgctatc';
|
4
|
177 #print(sequences);
|
|
178 #(options, args) = parser.parse_args(['-x', 'rrr', '-t', 0.5, '-s', sequences, '-o', 'collection_content']);
|
|
179
|
|
180 (options, args) = parser.parse_args();
|
|
181 return query( options, args );
|
|
182
|
|
183 if __name__ == "__main__": __main__()
|