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