Mercurial > repos > fabio > sbtas_se
comparison search.py @ 0:00d6e82d74e9 draft
Uploaded 20180122
author | fabio |
---|---|
date | Mon, 22 Jan 2018 16:41:50 -0500 |
parents | |
children | 4291c9d1ff07 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:00d6e82d74e9 |
---|---|
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 | |
7 import optparse | |
8 import requests | |
9 from requests_futures.sessions import FuturesSession | |
10 | |
11 #### UV0 #### | |
12 # proxy to uv0 | |
13 #service_url = "http://deputy.bx.psu.edu/"; | |
14 # url to query page | |
15 #query_url = service_url+"query.php"; | |
16 # url to echo page: just return 'it works!' | |
17 #echo_url = service_url+"echo.php"; | |
18 ############# | |
19 | |
20 #### NN14 #### | |
21 service_url = "http://nn14.galaxyproject.org:8080/"; | |
22 query_url = service_url+"tree/0/query"; | |
23 ############## | |
24 | |
25 ''' | |
26 # synchronous | |
27 def echo( options, args ): | |
28 # create a session | |
29 session = requests.Session() | |
30 # make a sync get request | |
31 resp = session.get(echo_url) | |
32 # check for response status code | |
33 resp_code = resp.status_code; | |
34 if resp_code == requests.codes.ok: | |
35 # get output file path | |
36 output_file_path = options.output; | |
37 # write response on the output file | |
38 with open(output_file_path, 'w') as out: | |
39 #out.write(resp.data); | |
40 out.write(resp.content); | |
41 return 0; | |
42 else: | |
43 return resp_code; | |
44 ''' | |
45 | |
46 # asynchronous | |
47 def async_request( options, args, payload ): | |
48 # add additional parameters to the payload | |
49 payload["tree_id"] = str(options.treeid); | |
50 payload["search_mode"] = str(options.search); | |
51 payload["exact_algorithm"] = str(options.exact); | |
52 payload["search_threshold"] = str(options.sthreshold); | |
53 # create a session | |
54 session = FuturesSession(); | |
55 # make an async post request with requests-futures | |
56 future_req = session.post(query_url, data=payload); | |
57 # wait for the request to complete, if it has not already | |
58 resp = future_req.result(); | |
59 # check for response status code | |
60 resp_code = resp.status_code; | |
61 # get output file path | |
62 output_file_path = options.output; | |
63 # write response on the output file | |
64 with open(output_file_path, 'w') as out: | |
65 #out.write(resp.data); | |
66 out.write(str(resp_code)+"\n"+str(resp.content)); | |
67 if resp_code == requests.codes.ok: | |
68 return 0; | |
69 else: | |
70 return resp_code; | |
71 | |
72 def srase_query( options, args ): | |
73 multiple_files = {}; | |
74 comma_sep_file_paths = options.files; | |
75 #print("files: "+str(comma_sep_file_paths)+" - "+str(type(comma_sep_file_paths))); | |
76 # check if options.files contains at least one file path | |
77 if comma_sep_file_paths is not None: | |
78 # split file paths | |
79 file_paths = comma_sep_file_paths.split(","); | |
80 # split file names | |
81 comma_sep_file_names = str(options.names); | |
82 #print("names: "+str(comma_sep_file_names)); | |
83 file_names = comma_sep_file_names.split(","); | |
84 # populate a dictionary with the files containing the sequences to query | |
85 for idx, file_path in enumerate(file_paths): | |
86 file_name = file_names[idx]; | |
87 with open(file_path, 'r') as content_file: | |
88 content = content_file.read() | |
89 multiple_files[file_name] = content; | |
90 #print(file_name+": "+content+"\n"); | |
91 if len(multiple_files) > 0: | |
92 return async_request( options, args, multiple_files ); | |
93 #return echo( options, args ); | |
94 else: | |
95 search_mode = str(options.search); | |
96 text_content = ""; | |
97 if search_mode == "0": | |
98 # try with the sequence in --sequence | |
99 text_content = options.sequences; | |
100 elif search_mode == "1": | |
101 # try with the fasta content in --fasta | |
102 text_content = options.fasta; | |
103 #print("sequences: "+text_content); | |
104 # check if options.sequences contains a list of sequences (one for each row) | |
105 if text_content is not None: | |
106 text_content = str(text_content); | |
107 if text_content.strip(): | |
108 if search_mode == "0": | |
109 # populate a dictionary with the files containing the sequences to query | |
110 seq_counter = 0; | |
111 sequences_arr = text_content.split("__cn__"); | |
112 for seq in sequences_arr: | |
113 seq_index = 'sequence'+str(seq_counter); | |
114 multiple_files[seq_index] = seq; | |
115 #print(str(seq_counter)+": "+seq); | |
116 seq_counter += 1; | |
117 elif search_mode == "1": | |
118 multiple_files["fasta"] = text_content; | |
119 return async_request( options, args, multiple_files ); | |
120 #return echo( options, args ); | |
121 else: | |
122 return -1; | |
123 return -1; | |
124 | |
125 def __main__(): | |
126 # Parse the command line options | |
127 usage = "Usage: search.py --files comma_sep_file_paths --names comma_seq_file_names --sequences sequences_text --search search_mode --exact exact_alg --sthreshold threshold --output output_file_path"; | |
128 parser = optparse.OptionParser(usage = usage); | |
129 parser.add_option("-i", "--treeid", type="string", | |
130 action="store", dest="treeid", help="string representing the tree id"); | |
131 parser.add_option("-f", "--files", type="string", | |
132 action="store", dest="files", help="comma separated files path"); | |
133 parser.add_option("-n", "--names", type="string", | |
134 action="store", dest="names", help="comma separated names associated to the files specified in --files"); | |
135 parser.add_option("-s", "--sequences", type="string", | |
136 action="store", dest="sequences", help="contains a list of sequences (one for each row)"); | |
137 parser.add_option("-a", "--fasta", type="string", | |
138 action="store", dest="fasta", help="contains the content of a fasta file"); | |
139 parser.add_option("-x", "--search", type="int", default=0, | |
140 action="store", dest="search", help="search mode"); | |
141 parser.add_option("-e", "--exact", type="int", default=0, | |
142 action="store", dest="exact", help="exact algorithm (required if search is 1 only)"); | |
143 parser.add_option("-t", "--sthreshold", type="string", | |
144 action="store", dest="sthreshold", help="threshold applied to the search algrithm"); | |
145 parser.add_option("-o", "--output", type="string", | |
146 action="store", dest="output", help="output file path"); | |
147 parser.add_option("-v", "--version", action="store_true", dest="version", | |
148 default=False, help="display version and exit"); | |
149 (options, args) = parser.parse_args(); | |
150 if options.version: | |
151 print __version__; | |
152 else: | |
153 srase_query( options, args ); | |
154 | |
155 if __name__ == "__main__": __main__() |