annotate search.py @ 1:ef234e9679ea draft default tip

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