comparison search.py @ 0:854be3d51221 draft

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