Source of file FastaReader.php

Size: 5,467 Bytes - Last Modified: 2019-05-10T12:24:09+01:00

src/Reader/FastaReader.php

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
Covered by 5 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testObjectCanBeConstructedForValidConstructorArguments
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
60
Covered by 5 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testObjectCanBeConstructedForValidConstructorArguments
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
6162636465666768697071
Covered by 4 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
7273747576
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
7778798081
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
82
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
8384
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
85
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
868788
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
89
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
9091929394
Covered by 4 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
95
Covered by 1 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
96
Covered by 1 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
9798
Covered by 4 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
99100
Covered by 4 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
101
Covered by 1 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
102
Covered by 1 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
103104
Covered by 4 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
105
Covered by 4 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
106
Covered by 4 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
  • pgb_liv\php_ms\Test\Unit\FastaWriterTest::testCanWritePeffEntry
107108109110
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
111
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
112113114
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
115116117118119120121122123124
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
125
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
126127128
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
129
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
130131
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
132133134135136137138139140141
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
142
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
143
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
144145
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
146147148149150151152153154155156157
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
158159
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
160
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
161162
Covered by 1 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
163164
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
165
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
166
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
167
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
168169
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
170
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
171
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
172173
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
174175
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
176
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
177178
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
179
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
180
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
181
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
182183184
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
185
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
186
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
187188
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
189
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
190191192193194
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
195
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
196197
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
198199
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
200201202203204205206207208209
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
210211
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
212
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
213214
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
215216
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
217
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
218219
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
220221222
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
223224225226
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRetrieveEntry
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanRewind
  • pgb_liv\php_ms\Test\Unit\FastaReaderTest::testCanSkipWhitespace
227228229
<?php
/**
 * Copyright 2019 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\Reader;

use Exception;
use pgb_liv\php_ms\Core\Protein;
use pgb_liv\php_ms\Reader\FastaEntry\FastaInterface;
use pgb_liv\php_ms\Reader\FastaEntry\PeffFastaEntry;
use pgb_liv\php_ms\Reader\FastaEntry\DefaultFastaEntry;
use pgb_liv\php_ms\Reader\FastaEntry\FastaEntryFactory;

/**
 * A FASTA parser that creates a new iterable object that will return a database
 * entry on each iteration.
 *
 * @author Andrew Collins
 */
class FastaReader implements \Iterator
{

    private $filePath;

    private $fileHandle;

    private $filePeek;

    /**
     * The current protein that will be returned by the current() method
     *
     * @var Protein
     */
    private $current;

    private $key = 0;

    /**
     * The FASTA format engine to use for parsing
     *
     * @var FastaInterface
     */
    private $format;

    public function __construct($filePath)
    {
        $this->filePath = $filePath;
    }

    /**
     *
     * {@inheritdoc}
     *
     * @see \Iterator::current()
     * @return Protein
     */
    public function current()
    {
        return $this->current;
    }

    public function key()
    {
        return $this->key;
    }

    public function next()
    {
        $this->current = null;
        if (! feof($this->fileHandle)) {
            try {
                $this->current = $this->parseEntry();
            } catch (\InvalidArgumentException $ex) {
                $this->next();
            }
        }
    }

    public function rewind()
    {
        // Reset file parsing to start
        if ($this->fileHandle != null) {
            fclose($this->fileHandle);
        }

        $this->fileHandle = fopen($this->filePath, 'r');

        if (stripos($this->peekLine(), '# PEFF') === 0) {
            $this->format = new PeffFastaEntry();
        }

        $this->key = 0;
        $this->next();
    }

    public function valid()
    {
        if ($this->current instanceof Protein) {
            return true;
        }

        return false;
    }

    /**
     * Gets the next line and increments the file iterator
     *
     * @return string The next line in the file
     */
    private function getLine()
    {
        if ($this->filePeek == null) {
            return fgets($this->fileHandle);
        }

        $ret = $this->filePeek;
        $this->filePeek = null;

        return $ret;
    }

    /**
     * Gets the next line, though does not move the file iterator
     *
     * @return string The next line in the file
     */
    private function peekLine()
    {
        if ($this->filePeek == null) {
            $this->filePeek = fgets($this->fileHandle);
        }

        return $this->filePeek;
    }

    /**
     * Parses the current chunk into a Protein object
     *
     * @return Protein
     */
    private function parseEntry()
    {
        // Scan to first entry
        do {
            $line = trim($this->peekLine());

            if (strpos($line, '>') === 0) {
                break;
            }
        } while ($this->getLine());

        $identifier = '';
        while ($line = $this->getLine()) {
            $line = trim($line);
            $identifier .= substr($line, 1);

            $nextLine = trim($this->peekLine());
            if (strpos($nextLine, '>') !== 0) {
                break;
            }
        }

        $description = '';
        $separator = strpos($identifier, ' ');

        if ($separator !== false) {
            $description = substr($identifier, $separator + 1);
            $identifier = substr($identifier, 0, $separator);
        }

        try {
            if ($this->format == null || $this->format instanceof DefaultFastaEntry) {
                $this->format = FastaEntryFactory::getParser($identifier);
            }

            $protein = $this->format->getProtein($identifier, $description);
        } catch (Exception $e) {
            $this->format = FastaEntryFactory::getParser($identifier);
            $protein = $this->format->getProtein($identifier, $description);
        }

        $protein->setIdentifier($identifier);
        $protein->setSequence($this->parseSequence());

        $this->key ++;

        return $protein;
    }

    /**
     * Parses the sequence block from the FASTA file and returns the sequence without any line ending or formatting
     *
     * @return string
     */
    private function parseSequence()
    {
        $sequence = '';

        while ($line = $this->getLine()) {
            $sequence .= trim($line);

            $nextLine = trim($this->peekLine());

            if (strpos($nextLine, '>') === 0) {
                break;
            }
        }

        // Remove stop codon in IRGSP FASTA
        if (strrpos($sequence, '*', - 1) !== false) {
            $sequence = substr($sequence, 0, - 1);
        }

        return $sequence;
    }
}