1
2
16
17import msparser
18import sys
19import getopt
20import ntpath
21import time
22
23
24def ms_range(start, stop, step=1):
25 i = start
26 while i <= stop:
27 yield i
28 i += step
29
30
31msurl = ''
32mscmd = ''
33msmgf = ''
34mstaskID = ''
35msaccession = ''
36msdatabase = ''
37msusername = ''
38mspassword = ''
39
40
41usage = """
42Usage http_client.py --url=<URL> --cmd=<command> [options]
43
44 --help Print this message
45
46 --url Mascot Server URL of the form
47 http://your-server/mascot/cgi/
48 --cmd One of the following
49 --cmd=search to submit a Mascot search
50 --cmd=getresults to download the search results file
51 --cmd=getseq to save protein sequence to XML file
52 --mgf Path to an MGF peak list file
53 Required if --cmd=search
54 --taskID Mascot task ID for the search
55 Required if --cmd=getresults
56 --accession Accession string for protein
57 Required if --cmd=getseq
58 --database Mascot database name
59 Required if --cmd=getseq
60 --username Mascot Server user name
61 May be required if Mascot Security is enabled
62 --password Mascot Server password
63 May be required if Mascot Security is enabled
64"""
65
66# If no arguments supplied, print help and exit
67if len(sys.argv) < 2 :
68 print(usage)
69 sys.exit(1)
70
71
72try:
73 opts, args = getopt.getopt(sys.argv[1:],"",["help","url=","cmd=","mgf=","taskID=","accession=","database=","username=","password="])
74except getopt.GetoptError:
75 sys.exit(1)
76
77
78for opt, arg in opts:
79 if opt == '--help':
80 print(usage)
81 sys.exit(2)
82
83
84 elif opt in ("--url"):
85 msurl = arg
86 elif opt in ("--cmd"):
87 mscmd = arg
88 elif opt in ("--mgf"):
89 msmgf = arg
90 elif opt in ("--taskID"):
91 mstaskID = arg
92 elif opt in ("--accession"):
93 msaccession = arg
94 elif opt in ("--database"):
95 msdatabase = arg
96 elif opt in ("--username"):
97 msusername = arg
98 elif opt in ("--password"):
99 mspassword = arg
100
101
102if not msurl:
103 print(usage)
104 sys.exit(1)
105elif mscmd == "search":
106 if not msmgf:
107 print(usage)
108 sys.exit(1)
109elif mscmd == "getresults":
110 if not mstaskID:
111 print(usage)
112 sys.exit(1)
113elif mscmd == "getseq":
114 if not msaccession:
115 print(usage)
116 sys.exit(1)
117 elif not msdatabase:
118 print(usage)
119 sys.exit(1)
120
121
122
123
124
125objSettings = msparser.ms_connection_settings()
126objSettings.setUserAgent("PythonScriptTest/1.0 " + objSettings.getUserAgent())
127
128objSettings.setProxyServerType(msparser.ms_connection_settings.PROXY_TYPE_AUTO)
129
130
131objHttpClient = msparser.ms_http_client(msurl, objSettings)
132if not objHttpClient.isValid():
133 print("Connection to server %s is not valid" % msurl)
134 showErrorsAndExit(objHttpClient)
135
136
137objHttpClient.getErrorHandler().setLoggingFile("log.txt", msparser.ms_errs.sev_debug3)
138
139
140
141objSession = msparser.ms_http_client_session()
142loginReturnCode = objHttpClient.userLogin(msusername, mspassword, objSession)
143if loginReturnCode != msparser.ms_http_client.L_SUCCESS and loginReturnCode != msparser.ms_http_client.L_SECURITYDISABLED:
144 print("Failed to login to %s as user %s. Return code id %d" % (msurl, msusername, loginReturnCode))
145 sys.exit(1)
146else:
147 print("Logged in to %s with session: %s" % (msurl, objSession.sessionId()))
148
149def doGetSeq():
150 print("Get Sequence")
151 accessions = msparser.VectorString()
152 frames = msparser.vectori()
153 accessions.append(msaccession)
154 localFileName = "sequence.xml"
155 if objSession.getSequenceFile(msdatabase, accessions, frames, localFileName):
156 print("Successfully saved sequence to %s" % localFileName)
157 else:
158 print("Failed to get sequence or failed to save it to %s" % localFileName)
159 showErrorsAndExit(objSession)
160
161def doGetResults():
162 print("Get Results")
163 objSearch = msparser.ms_http_client_search(objSession, mstaskID)
164 if not objSearch.isValid():
165 print("Search is not valid")
166 showErrorsAndExit(objSearch)
167 ok, remoteFileName = objSearch.getResultsFileName()
168 print(remoteFileName)
169 if ok and remoteFileName:
170 print("Found filename for results file")
171 else:
172 print("Unable to find filename for results file")
173 sys.exit(1)
174
175 localFileName = ntpath.basename(remoteFileName)
176 print("Downloading remote results file %s to %s" % (remoteFileName, localFileName))
177 objProgress = msparser.ms_http_helper_progress()
178
179 if objSearch.downloadResultsFile(localFileName, objProgress):
180 print("Downloaded remote file to %s" % localFileName)
181 else:
182 print("Failed to download remote file to %s" % localFileName)
183 return False
184 return True
185
186def doSearch():
187 print("Searching" )
188
189 httpHeader = "Content-Type: multipart/mixed; boundary=---------FormBoundary4C9ByKKVofH"
190 prologue = """-----------FormBoundary4C9ByKKVofH
191Content-Disposition: form-data; name="FORMVER"
192
1931.01
194-----------FormBoundary4C9ByKKVofH
195Content-Disposition: form-data; name="SEARCH"
196
197MIS
198-----------FormBoundary4C9ByKKVofH
199Content-Disposition: form-data; name="COM"
200
201Python script test
202-----------FormBoundary4C9ByKKVofH
203Content-Disposition: form-data; name="DB"
204
205SwissProt
206-----------FormBoundary4C9ByKKVofH
207Content-Disposition: form-data; name="CLE"
208
209Trypsin/P
210-----------FormBoundary4C9ByKKVofH
211Content-Disposition: form-data; name="PFA"
212
2131
214-----------FormBoundary4C9ByKKVofH
215Content-Disposition: form-data; name="QUANTITATION"
216
217None
218-----------FormBoundary4C9ByKKVofH
219Content-Disposition: form-data; name="TAXONOMY"
220
221. . . . . . . . . . . . . . . . Homo sapiens (human)
222-----------FormBoundary4C9ByKKVofH
223Content-Disposition: form-data; name="MODS"
224
225Carbamidomethyl (C)
226-----------FormBoundary4C9ByKKVofH
227Content-Disposition: form-data; name="IT_MODS"
228
229Oxidation (M)
230-----------FormBoundary4C9ByKKVofH
231Content-Disposition: form-data; name="TOL"
232
23310
234-----------FormBoundary4C9ByKKVofH
235Content-Disposition: form-data; name="TOLU"
236
237ppm
238-----------FormBoundary4C9ByKKVofH
239Content-Disposition: form-data; name="PEP_ISOTOPE_ERROR"
240
2411
242-----------FormBoundary4C9ByKKVofH
243Content-Disposition: form-data; name="ITOL"
244
2450.1
246-----------FormBoundary4C9ByKKVofH
247Content-Disposition: form-data; name="ITOLU"
248
249Da
250-----------FormBoundary4C9ByKKVofH
251Content-Disposition: form-data; name="CHARGE"
252
2532+
254-----------FormBoundary4C9ByKKVofH
255Content-Disposition: form-data; name="MASS"
256
257Monoisotopic
258-----------FormBoundary4C9ByKKVofH
259Content-Disposition: form-data; name="FORMAT"
260
261Mascot generic
262-----------FormBoundary4C9ByKKVofH
263Content-Disposition: form-data; name="INSTRUMENT"
264
265ESI-TRAP
266-----------FormBoundary4C9ByKKVofH
267Content-Disposition: form-data; name="REPORT"
268
269AUTO
270-----------FormBoundary4C9ByKKVofH
271Content-Disposition: form-data; name="USERNAME"
272
273Mascot Parser Test
274-----------FormBoundary4C9ByKKVofH
275Content-Disposition: form-data; name="USEREMAIL"
276
277dcreasy@matrixscience.com
278-----------FormBoundary4C9ByKKVofH
279Content-Disposition: form-data; name="FILE"; filename="test.mgf"
280"""
281
282 epilogue = "-----------FormBoundary4C9ByKKVofH--"
283
284
285 objSearch = msparser.ms_http_client_search(objSession, mstaskID)
286 if not objSearch.isValid():
287 showErrorsAndExit(objSearch)
288
289 print("Submitting search")
290 objProgress = msparser.ms_http_helper_progress()
291 if objSession.submitSearch(objSearch, httpHeader, prologue, msmgf, epilogue, objProgress):
292 print("Search submitted. Task ID = %s" % objSearch.searchTaskId())
293 success = False
294 returnValue = 0
295 returnCode = msparser.ms_http_client_search.SS_UNKNOWN
296 complete = False
297 while not complete:
298 success, returnCode, returnValue = objSearch.getStatus()
299 if not success:
300 print("Unable to call getStatus()")
301 sys.exit(1)
302 if returnCode == msparser.ms_http_client_search.SS_UNKNOWN:
303 print("Unkown search id")
304 complete = False
305 elif returnCode == msparser.ms_http_client_search.SS_ASSIGNED:
306 print("Search not started")
307 complete = False
308 elif returnCode == msparser.ms_http_client_search.SS_QUEUED:
309 print("Search Queued")
310 complete = False
311 elif returnCode == msparser.ms_http_client_search.SS_RUNNING:
312 print("Search running: %s complete" % returnValue)
313 complete = False
314 elif returnCode == msparser.ms_http_client_search.SS_COMPLETE:
315 print("Search Complete")
316 complete = True
317 elif returnCode == msparser.ms_http_client_search.SS_ERROR:
318 print("Search Error : %s " % returnValue)
319 complete = True
320 elif returnCode == msparser.ms_http_client_search.SS_SEARCH_CONTROL_ERROR:
321 print("Search control error: %s " % returnValue)
322 complete = True
323 time.sleep(1)
324
325
326 ok, remoteFileName = objSearch.getResultsFileName()
327 if not ok:
328 print("Unable to get filename for results file")
329 showErrorsAndExit(objHttpClient)
330 else:
331 print("Search result report can be viewed at this URL:")
332 print(objHttpClient.baseUrl() + "master_results_2.pl?file=" + remoteFileName)
333 else:
334 print("Failed to submit search")
335 showErrorsAndExit(objSession)
336
337def showErrorsAndExit(ms_errs):
338 print("Error: %s" % ms_errs.getLastErrorString())
339 errs = ms_errs.getErrorHandler()
340 for i in ms_range(1, errs.getNumberOfErrors()):
341 print("Error Number: " + str(errs.getErrorNumber(i)) + " (" + str(errs.getErrorRepeats(i + 1)) + " times :")
342 sys.exit(1)
343
344if mscmd == "search":
345 doSearch()
346
347if mscmd == "getresults":
348 doGetResults()
349
350if mscmd == "getseq":
351 doGetSeq()
352
353
354objSession.logout()
355
356