0
|
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
|
2
|
49 #payload["tree_id"] = str(options.treeid);
|
0
|
50 payload["search_mode"] = str(options.search);
|
2
|
51 payload["exact_algorithm"] = int(options.exact);
|
|
52 payload["search_threshold"] = float(options.sthreshold);
|
|
53 # set the content type to application/json
|
|
54 headers = {'Content-type': 'application/json'};
|
0
|
55 # create a session
|
|
56 session = FuturesSession();
|
|
57 # make an async post request with requests-futures
|
2
|
58 future_req = session.post(query_url, headers=headers, json=payload);
|
0
|
59 # wait for the request to complete, if it has not already
|
|
60 resp = future_req.result();
|
|
61 # check for response status code
|
|
62 resp_code = resp.status_code;
|
|
63 # get output file path
|
|
64 output_file_path = options.output;
|
|
65 # write response on the output file
|
|
66 with open(output_file_path, 'w') as out:
|
|
67 #out.write(resp.data);
|
2
|
68 out.write(str(resp.content));
|
0
|
69 if resp_code == requests.codes.ok:
|
|
70 return 0;
|
|
71 else:
|
|
72 return resp_code;
|
|
73
|
|
74 def srase_query( options, args ):
|
2
|
75 multiple_data = {};
|
0
|
76 comma_sep_file_paths = options.files;
|
|
77 #print("files: "+str(comma_sep_file_paths)+" - "+str(type(comma_sep_file_paths)));
|
|
78 # check if options.files contains at least one file path
|
|
79 if comma_sep_file_paths is not None:
|
|
80 # split file paths
|
|
81 file_paths = comma_sep_file_paths.split(",");
|
|
82 # split file names
|
|
83 comma_sep_file_names = str(options.names);
|
|
84 #print("names: "+str(comma_sep_file_names));
|
|
85 file_names = comma_sep_file_names.split(",");
|
|
86 # populate a dictionary with the files containing the sequences to query
|
2
|
87 sequences = [];
|
0
|
88 for idx, file_path in enumerate(file_paths):
|
2
|
89 #file_name = file_names[idx];
|
0
|
90 with open(file_path, 'r') as content_file:
|
|
91 content = content_file.read()
|
2
|
92 sequences.append(content.strip());
|
|
93 #multiple_data[file_name] = content;
|
0
|
94 #print(file_name+": "+content+"\n");
|
2
|
95 if len(sequences) > 0:
|
|
96 multiple_data['sequences'] = sequences;
|
|
97 return async_request( options, args, multiple_data );
|
0
|
98 #return echo( options, args );
|
2
|
99 else:
|
|
100 return -1;
|
0
|
101 else:
|
2
|
102 # try with the sequence in --sequence
|
|
103 text_content = options.sequences;
|
0
|
104 #print("sequences: "+text_content);
|
|
105 # check if options.sequences contains a list of sequences (one for each row)
|
|
106 if text_content is not None:
|
|
107 text_content = str(text_content);
|
|
108 if text_content.strip():
|
2
|
109 # populate a dictionary with the files containing the sequences to query
|
|
110 multiple_data['sequences'] = text_content.strip().split("__cn__");
|
|
111 return async_request( options, args, multiple_data );
|
0
|
112 #return echo( options, args );
|
|
113 else:
|
|
114 return -1;
|
|
115 return -1;
|
|
116
|
|
117 def __main__():
|
|
118 # Parse the command line options
|
|
119 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";
|
|
120 parser = optparse.OptionParser(usage = usage);
|
|
121 parser.add_option("-f", "--files", type="string",
|
|
122 action="store", dest="files", help="comma separated files path");
|
|
123 parser.add_option("-n", "--names", type="string",
|
|
124 action="store", dest="names", help="comma separated names associated to the files specified in --files");
|
|
125 parser.add_option("-s", "--sequences", type="string",
|
|
126 action="store", dest="sequences", help="contains a list of sequences (one for each row)");
|
|
127 parser.add_option("-a", "--fasta", type="string",
|
|
128 action="store", dest="fasta", help="contains the content of a fasta file");
|
2
|
129 parser.add_option("-x", "--search", type="string", default=0,
|
0
|
130 action="store", dest="search", help="search mode");
|
|
131 parser.add_option("-e", "--exact", type="int", default=0,
|
|
132 action="store", dest="exact", help="exact algorithm (required if search is 1 only)");
|
2
|
133 parser.add_option("-t", "--sthreshold", type="float",
|
0
|
134 action="store", dest="sthreshold", help="threshold applied to the search algrithm");
|
|
135 parser.add_option("-o", "--output", type="string",
|
|
136 action="store", dest="output", help="output file path");
|
|
137 (options, args) = parser.parse_args();
|
2
|
138 return srase_query( options, args );
|
0
|
139
|
|
140 if __name__ == "__main__": __main__()
|