Source of file MascotSearch.php
Size: 15,226 Bytes - Last Modified: 2019-05-10T12:24:09+01:00
src/Search/MascotSearch.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
Covered by 6 test(s):
57
Covered by 6 test(s):
58
Covered by 6 test(s):
59
Covered by 6 test(s):
60616263
Covered by 3 test(s):
64
Covered by 3 test(s):
65
Covered by 3 test(s):
66
Covered by 3 test(s):
6768
Covered by 3 test(s):
6970717273
Covered by 4 test(s):
7475
Covered by 4 test(s):
76
Covered by 4 test(s):
7778
Covered by 4 test(s):
79
Covered by 2 test(s):
80
Covered by 2 test(s):
8182
Covered by 4 test(s):
8384
Covered by 4 test(s):
85
Covered by 4 test(s):
86
Covered by 4 test(s):
87
Covered by 4 test(s):
88
Covered by 1 test(s):
89
Covered by 1 test(s):
90
Covered by 1 test(s):
91
Covered by 4 test(s):
92
Covered by 1 test(s):
93
Covered by 1 test(s):
94
Covered by 1 test(s):
95
Covered by 1 test(s):
96
Covered by 1 test(s):
97
Covered by 1 test(s):
98
Covered by 4 test(s):
99
Covered by 4 test(s):
100101
Covered by 4 test(s):
102
Covered by 4 test(s):
103104
Covered by 4 test(s):
105
Covered by 4 test(s):
106107
Covered by 4 test(s):
108
Covered by 4 test(s):
109110
Covered by 4 test(s):
111
Covered by 1 test(s):
112
Covered by 1 test(s):
113
Covered by 1 test(s):
114
Covered by 1 test(s):
115116
Covered by 1 test(s):
117
Covered by 1 test(s):
118
Covered by 1 test(s):
119
Covered by 1 test(s):
120121
Covered by 1 test(s):
122123
Covered by 1 test(s):
124
Covered by 4 test(s):
125
Covered by 1 test(s):
126127
Covered by 1 test(s):
128
Covered by 1 test(s):
129
Covered by 1 test(s):
130
Covered by 1 test(s):
131
Covered by 1 test(s):
132
Covered by 4 test(s):
133
Covered by 4 test(s):
134135
Covered by 4 test(s):
136137
Covered by 4 test(s):
138139
Covered by 4 test(s):
140141142143144
Covered by 1 test(s):
145146
Covered by 1 test(s):
147
Covered by 1 test(s):
148
Covered by 1 test(s):
149
Covered by 1 test(s):
150151
Covered by 1 test(s):
152
Covered by 1 test(s):
153
Covered by 1 test(s):
154
Covered by 1 test(s):
155156
Covered by 1 test(s):
157
Covered by 1 test(s):
158159
Covered by 1 test(s):
160
Covered by 1 test(s):
161
Covered by 1 test(s):
162163
Covered by 1 test(s):
164165166167168
Covered by 5 test(s):
169
Covered by 5 test(s):
170
Covered by 5 test(s):
171
Covered by 5 test(s):
172
Covered by 5 test(s):
173
Covered by 5 test(s):
174
Covered by 5 test(s):
175
Covered by 5 test(s):
176
Covered by 5 test(s):
177178
Covered by 5 test(s):
179
Covered by 5 test(s):
180181
Covered by 5 test(s):
182
Covered by 5 test(s):
183
Covered by 5 test(s):
184
Covered by 5 test(s):
185186
Covered by 5 test(s):
187
Covered by 5 test(s):
188
Covered by 5 test(s):
189
Covered by 5 test(s):
190191192
Covered by 5 test(s):
193
Covered by 5 test(s):
194195
Covered by 5 test(s):
196
Covered by 5 test(s):
197
Covered by 5 test(s):
198199
Covered by 5 test(s):
200201
Covered by 5 test(s):
202
Covered by 5 test(s):
203
Covered by 5 test(s):
204205
Covered by 5 test(s):
206207208
Covered by 5 test(s):
209
Covered by 5 test(s):
210
Covered by 5 test(s):
211212
Covered by 5 test(s):
213
Covered by 2 test(s):
214
Covered by 2 test(s):
215
Covered by 2 test(s):
216217
Covered by 5 test(s):
218
Covered by 5 test(s):
219
Covered by 2 test(s):
220
Covered by 2 test(s):
221
Covered by 2 test(s):
222
Covered by 2 test(s):
223224
Covered by 2 test(s):
225
Covered by 2 test(s):
226
Covered by 5 test(s):
227228
Covered by 5 test(s):
229230
Covered by 5 test(s):
231232
Covered by 5 test(s):
233
Covered by 2 test(s):
234
Covered by 2 test(s):
235236237
Covered by 5 test(s):
238
Covered by 5 test(s):
239
Covered by 5 test(s):
240241
Covered by 5 test(s):
242243244245246
Covered by 5 test(s):
247
Covered by 5 test(s):
248
Covered by 5 test(s):
249
Covered by 5 test(s):
250
Covered by 5 test(s):
251
Covered by 5 test(s):
252
Covered by 5 test(s):
253
Covered by 5 test(s):
254
Covered by 5 test(s):
255256
Covered by 5 test(s):
257258
Covered by 5 test(s):
259
Covered by 5 test(s):
260
Covered by 5 test(s):
261
Covered by 4 test(s):
262
Covered by 4 test(s):
263
Covered by 4 test(s):
264
Covered by 4 test(s):
265
Covered by 5 test(s):
266267
Covered by 5 test(s):
268269270271272
Covered by 2 test(s):
273
Covered by 2 test(s):
274
Covered by 2 test(s):
275
Covered by 2 test(s):
276
Covered by 2 test(s):
277
Covered by 2 test(s):
278
Covered by 2 test(s):
279
Covered by 2 test(s):
280
Covered by 2 test(s):
281
Covered by 2 test(s):
282
Covered by 2 test(s):
283
Covered by 2 test(s):
284
Covered by 2 test(s):
285
Covered by 2 test(s):
286
Covered by 2 test(s):
287
Covered by 2 test(s):
288
Covered by 2 test(s):
289
Covered by 2 test(s):
290
Covered by 2 test(s):
291
Covered by 2 test(s):
292
Covered by 2 test(s):
293
Covered by 2 test(s):
294
Covered by 2 test(s):
295
Covered by 2 test(s):
296
Covered by 2 test(s):
297
Covered by 2 test(s):
298
Covered by 2 test(s):
299
Covered by 2 test(s):
300
Covered by 2 test(s):
301
Covered by 2 test(s):
302
Covered by 2 test(s):
303
Covered by 2 test(s):
304
Covered by 2 test(s):
305
Covered by 2 test(s):
306
Covered by 2 test(s):
307
Covered by 2 test(s):
308
Covered by 2 test(s):
309
Covered by 2 test(s):
310
Covered by 2 test(s):
311
Covered by 2 test(s):
312
Covered by 2 test(s):
313
Covered by 2 test(s):
314
Covered by 2 test(s):
315
Covered by 2 test(s):
316
Covered by 2 test(s):
317318
Covered by 2 test(s):
319320321
Covered by 2 test(s):
322
Covered by 2 test(s):
323
Covered by 2 test(s):
324325326327328
Covered by 2 test(s):
329
Covered by 2 test(s):
330
Covered by 2 test(s):
331
Covered by 2 test(s):
332
Covered by 2 test(s):
333
Covered by 2 test(s):
334
Covered by 2 test(s):
335
Covered by 2 test(s):
336
Covered by 2 test(s):
337
Covered by 2 test(s):
338
Covered by 2 test(s):
339
Covered by 2 test(s):
340
Covered by 2 test(s):
341
Covered by 2 test(s):
342
Covered by 2 test(s):
343
Covered by 2 test(s):
344
Covered by 2 test(s):
345
Covered by 2 test(s):
346
Covered by 2 test(s):
347
Covered by 2 test(s):
348
Covered by 2 test(s):
349
Covered by 2 test(s):
350
Covered by 2 test(s):
351
Covered by 2 test(s):
352
Covered by 2 test(s):
353
Covered by 2 test(s):
354
Covered by 2 test(s):
355
Covered by 2 test(s):
356
Covered by 2 test(s):
357
Covered by 2 test(s):
358
Covered by 2 test(s):
359
Covered by 2 test(s):
360
Covered by 2 test(s):
361
Covered by 2 test(s):
362363
Covered by 2 test(s):
364365
Covered by 2 test(s):
366
Covered by 2 test(s):
367
Covered by 2 test(s):
368369
Covered by 2 test(s):
370
Covered by 2 test(s):
371
Covered by 2 test(s):
372
Covered by 2 test(s):
373
Covered by 2 test(s):
374
Covered by 2 test(s):
375
Covered by 2 test(s):
376
Covered by 2 test(s):
377
Covered by 2 test(s):
378
Covered by 2 test(s):
379
Covered by 2 test(s):
380
Covered by 2 test(s):
381
Covered by 2 test(s):
382
Covered by 2 test(s):
383
Covered by 2 test(s):
384
Covered by 2 test(s):
385
Covered by 2 test(s):
386
Covered by 2 test(s):
387388
Covered by 2 test(s):
389390391392393
Covered by 1 test(s):
394
Covered by 1 test(s):
395
Covered by 1 test(s):
396
Covered by 1 test(s):
397
Covered by 1 test(s):
398
Covered by 1 test(s):
399
Covered by 1 test(s):
400
Covered by 1 test(s):
401
Covered by 1 test(s):
402
Covered by 1 test(s):
403
Covered by 1 test(s):
404
Covered by 1 test(s):
405
Covered by 1 test(s):
406
Covered by 1 test(s):
407
Covered by 1 test(s):
408
Covered by 1 test(s):
409
Covered by 1 test(s):
410
Covered by 1 test(s):
411
Covered by 1 test(s):
412
Covered by 1 test(s):
413
Covered by 1 test(s):
414
Covered by 1 test(s):
415
Covered by 1 test(s):
416
Covered by 1 test(s):
417
Covered by 1 test(s):
418
Covered by 1 test(s):
419
Covered by 1 test(s):
420
Covered by 1 test(s):
421
Covered by 1 test(s):
422
Covered by 1 test(s):
423
Covered by 1 test(s):
424
Covered by 1 test(s):
425
Covered by 1 test(s):
426
Covered by 1 test(s):
427
Covered by 1 test(s):
428429
Covered by 1 test(s):
430431432
Covered by 1 test(s):
433
Covered by 1 test(s):
434435
Covered by 1 test(s):
436437438
| <?php /** * Copyright 2016 University of Liverpool * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace pgb_liv\php_ms\Search; use pgb_liv\php_ms\Search\Parameters\MascotSearchParameters; /** * Client to perform Mascot search and results retrieval * * @author Andrew Collins */ class MascotSearch { private $host; private $port; private $path; private $cookies = array(); const FILE_NAME = 'filename'; const MIME_TYPE = 'mime'; const FILE_DATA = 'data'; /** * Create a new instance of this class. * Must supply server details. * * @param string $host * Server hostname or IP address * @param int $port * Server port * @param string $path * Path to base Mascot directory */ public function __construct($host, $port, $path) { $this->host = $host; $this->port = $port; $this->path = $path; } private function getCookieHeader() { $cookiePairs = array(); foreach ($this->cookies as $key => $value) { $cookiePairs[] = $key . '=' . $value; } return 'Cookie: ' . implode(';', $cookiePairs); } private function sendPost($path, $args) { $handle = fsockopen($this->host, $this->port); fwrite($handle, 'POST ' . $path . ' HTTP/1.1' . "\r\n"); fwrite($handle, 'Host: ' . $this->host . "\r\n"); if (! empty($this->cookies)) { fwrite($handle, $this->getCookieHeader() . "\r\n"); } $boundary = '---------------------------' . mt_rand(); $size = 0; foreach ($args as $key => $value) { $size += 2 + strlen($boundary) + 2; if (is_array($value) && $key == 'FILE') { $size += 38 + strlen($key) + 13 + strlen($value[MascotSearch::FILE_NAME]) + 3; $size += strlen($value[MascotSearch::MIME_TYPE]) + 4; $size += filesize($value[MascotSearch::FILE_DATA]) + 4; } elseif ($key == 'MODS' || $key == 'IT_MODS') { $size += 38 + strlen($key) + 5; foreach ($value as $modification) { $size += strlen($modification->getName()); $size += 3 + count($modification->getResidues()) + 2; } } else { $size += 38 + strlen($key) + 5; $size += strlen($value) + 2; } } $size += 2 + strlen($boundary) + 4; fwrite($handle, 'Content-Type: multipart/form-data; boundary=' . $boundary . "\r\n"); fwrite($handle, 'Content-Length: ' . $size . "\r\n\r\n"); foreach ($args as $key => $value) { fwrite($handle, '--' . $boundary . "\r\n"); if (is_array($value) && $key == 'FILE') { fwrite($handle, 'Content-Disposition: form-data; name="' . $key . '"; filename="' . $value[MascotSearch::FILE_NAME] . '"' . "\r\n"); fwrite($handle, $value[MascotSearch::MIME_TYPE] . "\r\n\r\n"); $fileHandle = fopen($value[MascotSearch::FILE_DATA], 'r'); while (! feof($fileHandle)) { fwrite($handle, fgets($fileHandle)); } fclose($fileHandle); fwrite($handle, "\r\n\r\n"); } elseif ($key == 'MODS' || $key == 'IT_MODS') { fwrite($handle, 'Content-Disposition: form-data; name="' . $key . '"' . "\r\n\r\n"); foreach ($value as $modification) { fwrite($handle, $modification->getName() . ' (' . implode('', $modification->getResidues()) . ")\r\n"); } } else { fwrite($handle, 'Content-Disposition: form-data; name="' . $key . '"' . "\r\n\r\n"); fwrite($handle, $value . "\r\n"); } } fwrite($handle, '--' . $boundary . "--\r\n"); return $this->readResponse($handle); } private function sendGet($path, $args) { $handle = fsockopen($this->host, $this->port); $queryString = ''; $prefix = '?'; foreach ($args as $key => $value) { $queryString .= $prefix . urlencode($key) . '=' . urlencode($value); if ($prefix == '?') { $prefix = '&'; } } fwrite($handle, 'GET ' . $path . $queryString . ' HTTP/1.1' . "\r\n"); fwrite($handle, 'Host: ' . $this->host . "\r\n"); if (! empty($this->cookies)) { fwrite($handle, $this->getCookieHeader() . "\r\n\r\n"); } return $this->readResponse($handle); } private function readResponse($socket) { $header = ''; $content = ''; $isHeader = true; $hasAttachment = false; $attachmentName = null; $tmpFile = null; $isChunked = false; $chunkSizeExpected = - 1; $chunkSize = 0; while (! feof($socket)) { $line = fgets($socket); if ($isChunked) { if ($chunkSize + strlen($line) - 2 == $chunkSizeExpected) { $line = substr($line, 0, - 2); } if ($chunkSize == $chunkSizeExpected) { $chunkSizeExpected = hexdec(trim($line)); $chunkSize = 0; continue; } $chunkSize += strlen($line); } if ($isHeader) { if ($line == "\r\n") { $isHeader = false; if ($isChunked) { // Reset for first chunk $chunkSizeExpected = 0; $chunkSize = 0; } continue; } if (stripos($line, 'Transfer-Encoding: chunked') === 0) { $isChunked = true; } if (stripos($line, 'Content-Disposition: attachment;') === 0) { $hasAttachment = true; $attachmentName = substr($line, 43, - 3); } $header .= $line; } elseif ($hasAttachment) { if (is_null($tmpFile)) { $tmpFile = tempnam(sys_get_temp_dir(), 'mascot_attach_'); $tmpFileHandle = fopen($tmpFile, 'w'); } fwrite($tmpFileHandle, $line); } else { $content .= $line; } } fclose($socket); if (! is_null($tmpFile)) { fclose($tmpFileHandle); } return array( 'header' => $header, 'content' => $content, 'attachmentName' => $attachmentName, 'attachmentFile' => $tmpFile ); } public function authenticate($username, $password) { $args = array(); $args['username'] = $username; $args['password'] = $password; $args['submit'] = 'Login'; $args['display'] = 'logout_prompt'; $args['savecookie'] = '1'; $args['action'] = 'login'; $args['userid'] = ''; $args['onerrdisplay'] = 'login_prompt'; $response = $this->sendPost($this->path . '/cgi/login.pl', $args); $this->cookies = array(); foreach (explode("\n", $response['header']) as $line) { if (stripos($line, 'Set-Cookie:') === 0) { $cookie = substr($line, 12, stripos($line, ';') - 12); $parts = explode('=', $cookie); $this->cookies[$parts[0]] = $parts[1]; } } return count($this->cookies) >= 3; } public function getXml($filePath) { $args = array(); $args['file'] = $filePath; $args['do_export'] = '1'; $args['prot_hit_num'] = '1'; $args['prot_acc'] = '1'; $args['pep_query'] = '1'; $args['pep_rank'] = '1'; $args['pep_isbold'] = '1'; $args['pep_isunique'] = '1'; $args['pep_exp_mz'] = '1'; $args['_showallfromerrortolerant'] = ''; $args['_onlyerrortolerant'] = ''; $args['_noerrortolerant'] = ''; $args['_show_decoy_report'] = '0'; $args['sessionid'] = ''; $args['export_format'] = 'XML'; $args['_sigthreshold'] = '0.05'; $args['_ignoreionsscorebelow'] = '0'; $args['use_homology'] = '0'; $args['report'] = 'AUTO'; $args['_server_mudpit_switch'] = '0.000000001'; $args['_showsubsets'] = '0'; $args['search_master'] = '1'; $args['show_header'] = '1'; $args['show_decoy'] = '1'; $args['show_mods'] = '1'; $args['show_params'] = '1'; $args['show_format'] = '1'; $args['protein_master'] = '1'; $args['prot_score'] = '1'; $args['prot_desc'] = '1'; $args['prot_mass'] = '1'; $args['prot_matches'] = '1'; $args['peptide_master'] = '1'; $args['pep_exp_mr'] = '1'; $args['pep_exp_z'] = '1'; $args['pep_calc_mr'] = '1'; $args['pep_calc_mr'] = '1'; $args['pep_delta'] = '1'; $args['pep_miss'] = '1'; $args['pep_score'] = '1'; $args['pep_expect'] = '1'; $args['pep_seq'] = '1'; $args['pep_var_mod'] = '1'; $args['pep_scan_title'] = '1'; $response = $this->sendPost($this->path . '/cgi/export_dat_2.pl', $args); return array( 'name' => $response['attachmentName'], 'path' => $response['attachmentFile'] ); } public function getSearches($limit) { $args = array(); $args['CalledFromForm'] = 1; $args['logfile'] = '../logs/searches.log'; $args['start'] = - 1; $args['howMany'] = $limit; $args['pathToData'] = ''; $args['column'] = 0; $args['s0'] = 1; $args['s1'] = 1; $args['s2'] = 1; $args['s3'] = 1; $args['s4'] = 1; $args['s7'] = 1; $args['s8'] = 1; $args['s9'] = 1; $args['s10'] = 1; $args['s11'] = 1; $args['s12'] = 1; $args['s14'] = 1; $args['f0'] = ''; $args['f1'] = ''; $args['f2'] = ''; $args['f3'] = ''; $args['f4'] = ''; $args['f5'] = ''; $args['f6'] = ''; $args['f7'] = ''; $args['f8'] = ''; $args['f9'] = ''; $args['f10'] = ''; $args['f11'] = ''; $args['f12'] = ''; $args['f13'] = ''; $args['f14'] = ''; $response = $this->sendGet($this->path . '/x-cgi/ms-review.exe', $args); $pattern = '/<TR>\s+<TD><A HREF="..\/cgi\/master_results_2.pl\?file=(?<filename>.*)">\s?(?<job>[0-9]+)<\/A><\/TD>\s+<TD>\s?(?<pid>[0-9]+)<\/TD>\s+<TD>(?<dbase>.+)<\/TD>\s+<TD>(?<username>.*)<\/TD>\s*<TD>(?<email>.*)<\/TD>\s+<TD>(?<ti>.*)<\/TD>\s+<TD>.*<\/TD>\s+<TD NOWRAP>(?<start_time>.+)<\/TD>\s+<TD>\s*(?<dur>[0-9]+)<\/TD>\s+<TD>(?<status>.+)<\/TD>\s+<TD>(?<pr>.+)<\/TD>\s+<TD>(?<typ>.+)<\/TD>\s+<TD>(?<enzyme>.+)<\/TD>\s+<TD>\s?(?<ip>[0-9]*)<\/TD>\s+<TD>\s?(?<userid>[0-9]+)<\/TD>/sU'; $matches = null; preg_match_all($pattern, $response['content'], $matches); $searchLog = array(); for ($i = 0; $i < count($matches['filename']); $i ++) { $searchLog[$i] = array(); $searchLog[$i]['filename'] = $matches['filename'][$i]; $searchLog[$i]['job'] = $matches['job'][$i]; $searchLog[$i]['pid'] = $matches['pid'][$i]; $searchLog[$i]['dbase'] = $matches['dbase'][$i]; $searchLog[$i]['username'] = $matches['username'][$i]; $searchLog[$i]['email'] = $matches['email'][$i]; $searchLog[$i]['ti'] = $matches['ti'][$i]; $searchLog[$i]['start_time'] = $matches['start_time'][$i]; $searchLog[$i]['dur'] = $matches['dur'][$i]; $searchLog[$i]['status'] = $matches['status'][$i]; $searchLog[$i]['typ'] = $matches['typ'][$i]; $searchLog[$i]['enzyme'] = $matches['enzyme'][$i]; $searchLog[$i]['ip'] = $matches['ip'][$i]; $searchLog[$i]['userid'] = $matches['userid'][$i]; } return $searchLog; } public function search(MascotSearchParameters $params) { $args = array(); $args['INTERMEDIATE'] = $params->getIntermediate(); $args['FORMVER'] = $params->getFormVersion(); $args['SEARCH'] = $params->getSearchType(); $args['PEAK'] = $params->getPeak(); $args['REPTYPE'] = $params->getRepType(); $args['ErrTolRepeat'] = $params->getErrorTolerantRepeat(); $args['SHOWALLMODS'] = $params->isShowAllModsEnabled(); $args['USERNAME'] = $params->getUserName(); $args['USEREMAIL'] = $params->getUserMail(); $args['COM'] = $params->getTitle(); $args['DB'] = $params->getDatabases(); $args['CLE'] = $params->getEnzyme(); $args['PFA'] = $params->getMissedCleavageCount(); $args['QUANTITATION'] = $params->getQuantitation(); $args['TAXONOMY'] = $params->getTaxonomy(); $args['MODS'] = $params->getFixedModifications(); $args['IT_MODS'] = $params->getVariableModifications(); $args['TOL'] = $params->getPrecursorTolerance()->getTolerance(); $args['TOLU'] = $params->getPrecursorTolerance()->getUnit(); $args['PEP_ISOTOPE_ERROR'] = $params->getPeptideIsotopeError(); $args['ITOL'] = $params->getFragmentTolerance()->getTolerance(); $args['ITOLU'] = $params->getFragmentTolerance()->getUnit(); $args['CHARGE'] = $params->getCharge(); $args['MASS'] = $params->getMassType(); $args['FILE'] = array( MascotSearch::FILE_NAME => basename($params->getSpectraPath()), MascotSearch::MIME_TYPE => 'Content-Type: application/octet-stream', MascotSearch::FILE_DATA => $params->getSpectraPath() ); $args['FORMAT'] = $params->getFileFormat(); $args['PRECURSOR'] = $params->getPrecursor(); $args['INSTRUMENT'] = $params->getInstrument(); $args['DECOY'] = $params->isDecoyEnabled(); $args['REPORT'] = $params->getReport(); $response = $this->sendPost($this->path . '/cgi/nph-mascot.exe?1', $args); // Extract .dat path $matches = null; preg_match('/master_results\\.pl\\?file=(.*[0-9]+\\/F[0-9]+\\.dat)/', $response['content'], $matches); return $matches[1]; } } |