def test(self): with FastaReader("tests/data/simple.fasta") as f: reads = list(f) assert reads == simple_fasta fasta = StringIO( ">first_sequence\nSEQUENCE1\n>second_sequence\nSEQUENCE2\n") reads = list(FastaReader(fasta)) assert reads == simple_fasta
def parse(self, spec, cmdline_type='back'): """Parse an adapter specification not using ``file:`` notation and return an object of an appropriate Adapter class. The notation for anchored 5' and 3' adapters is supported. If the name parameter is None, then an attempt is made to extract the name from the specification (If spec is 'name=ADAPTER', name will be 'name'.) Args: spec: The adapter spec. name: The adapter name. If not provided, one is automatically generated. cmdline_type: describes which commandline parameter was used (``-a`` is 'back', ``-b`` is 'anywhere', and ``-g`` is 'front'). TODO: describe the adapter spec format Returns: An :class:`Adapter` instance. """ if spec.startswith('file:'): # read adapter sequences from a file with FastaReader(spec[5:]) as fasta: for record in fasta: name = record.name.split(None, 1)[0] yield self.parse_from_spec(record.sequence, cmdline_type, name) else: yield self.parse_from_spec(spec, cmdline_type)
def test_context_manager(self): filename = "tests/data/simple.fasta" with open(filename) as f: assert not f.closed reads = list(openseq(f)) assert not f.closed assert f.closed with FastaReader(filename) as sr: tmp_sr = sr assert not sr._file.closed reads = list(sr) assert not sr._file.closed assert tmp_sr._file is None # Open it a second time with FastaReader(filename) as sr: pass
def test_with_comments(self): fasta = StringIO( dedent(""" # a comment # another one >first_sequence SEQUENCE1 >second_sequence SEQUENCE2 """)) reads = list(FastaReader(fasta)) assert reads == simple_fasta
def test_wrong_format(self): with raises(FormatError): fasta = StringIO( dedent(""" # a comment # another one unexpected >first_sequence SEQUENCE1 >second_sequence SEQUENCE2 """)) reads = list(FastaReader(fasta))
def load_from_fasta(self, fasta): """Load adapter data from a FASTA file. Args: fasta: FASTA file. """ close = False if isinstance(fasta, str): fasta = open(fasta, 'rt') close = True num_records = None with FastaReader(fasta) as fasta: for num_records, record in enumerate(fasta, 1): name = record.name.split(None, 1)[0] seq = record.sequence self.add(name, seq) if close: fasta.close() return num_records
def test_fastareader_keeplinebreaks(self): with FastaReader("tests/data/simple.fasta", keep_linebreaks=True) as f: reads = list(f) assert reads[0] == simple_fasta[0] assert reads[1].sequence == 'SEQUEN\nCE2'