Source of file MgfWriter.php
Size: 4,012 Bytes - Last Modified: 2019-05-10T12:24:09+01:00
src/Writer/MgfWriter.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344
Covered by 4 test(s):
45
Covered by 4 test(s):
46
Covered by 4 test(s):
47
Covered by 4 test(s):
48495051525354555657
Covered by 3 test(s):
58
Covered by 1 test(s):
59606162
Covered by 2 test(s):
63
Covered by 2 test(s):
64
Covered by 2 test(s):
65
Covered by 2 test(s):
66676869
Covered by 2 test(s):
70
Covered by 2 test(s):
7172
Covered by 2 test(s):
73
Covered by 1 test(s):
74
Covered by 1 test(s):
7576
Covered by 2 test(s):
77
Covered by 1 test(s):
787980
Covered by 1 test(s):
8182
Covered by 1 test(s):
8384
Covered by 2 test(s):
85
Covered by 2 test(s):
86
Covered by 2 test(s):
8788
Covered by 2 test(s):
89
Covered by 2 test(s):
90
Covered by 2 test(s):
9192
Covered by 2 test(s):
93
Covered by 2 test(s):
9495
Covered by 2 test(s):
96
Covered by 2 test(s):
979899100101102103
Covered by 2 test(s):
104
Covered by 2 test(s):
105
Covered by 2 test(s):
106
Covered by 2 test(s):
107
Covered by 2 test(s):
108109110111112113114
Covered by 1 test(s):
115
Covered by 1 test(s):
116117
| <?php /** * Copyright 2018 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\Writer; use pgb_liv\php_ms\Core\Spectra\PrecursorIon; /** * A file writer class for creating Mascot Generic Format (MGF) files. * * @author Andrew Collins */ class MgfWriter { private $fileHandle = null; /** * Creates a new instance of an MGF file writer. * A close() should be called once all entries have been written. * * @param string $path * The path to the file to save to * @param string $searchType * The type of search (Default: MIS) * @param string $massType * The mass type (Default: Monoisotopic) */ public function __construct($path, $searchType = 'MIS', $massType = 'Monoisotopic') { $this->fileHandle = fopen($path, 'w'); fwrite($this->fileHandle, 'SEARCH=' . $searchType . PHP_EOL); fwrite($this->fileHandle, 'MASS=' . $massType . PHP_EOL); } /** * Writes the precursor ion and it's associated fragments to the file associated with this instance * * @param PrecursorIon $precursor * The precursor ion to write */ public function write(PrecursorIon $precursor) { if (is_null($this->fileHandle)) { throw new \BadMethodCallException('The file handle is closed. Cannot write after close() has been called.'); } // TODO: Validate mandatory/optional fields fwrite($this->fileHandle, 'BEGIN IONS' . PHP_EOL); fwrite($this->fileHandle, 'TITLE=' . $precursor->getTitle() . PHP_EOL); fwrite($this->fileHandle, 'PEPMASS=' . $precursor->getMonoisotopicMassCharge()); if (! is_null($precursor->getIntensity())) { fwrite($this->fileHandle, ' ' . $precursor->getIntensity()); } fwrite($this->fileHandle, PHP_EOL); fwrite($this->fileHandle, 'CHARGE=' . $precursor->getCharge() . '+' . PHP_EOL); if (! is_null($precursor->getScan())) { fwrite($this->fileHandle, 'SCANS=' . $precursor->getScan() . PHP_EOL); } if (! is_null($precursor->getRetentionTime())) { if ($precursor->hasRetentionTimeWindow()) { fwrite($this->fileHandle, 'RTINSECONDS=' . implode(',', $precursor->getRetentionTimeWindow()) . PHP_EOL); } else { fwrite($this->fileHandle, 'RTINSECONDS=' . $precursor->getRetentionTime() . PHP_EOL); } } foreach ($precursor->getFragmentIons() as $ion) { fwrite($this->fileHandle, $ion->getMonoisotopicMassCharge() . ' '); fwrite($this->fileHandle, $ion->getIntensity()); if (! is_null($ion->getCharge()) && $ion->getCharge() != 1) { fwrite($this->fileHandle, ' ' . $ion->getCharge()); } fwrite($this->fileHandle, PHP_EOL); } fwrite($this->fileHandle, 'END IONS' . PHP_EOL); } /** * Closes the file handle for this instance and releases resources. */ public function close() { if (! is_null($this->fileHandle)) { fclose($this->fileHandle); $this->fileHandle = null; } } /** * Finalise method to ensure the instance is correctly closed. */ public function __destruct() { $this->close(); } } |