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 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_wrong_format(self): fasta = StringIO( dedent(""" # a comment # another one unexpected >first_sequence SEQUENCE1 >second_sequence SEQUENCE2 """)) reads = list(FastaReader(fasta))
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 parse(self, spec, cmdline_type='back'): """ Parse an adapter specification and yield appropriate Adapter classes. This works like the _parse_no_file() function above, but also supports the ``file:`` notation for reading adapters from an external FASTA file. Since a file can contain multiple adapters, this function is a generator. """ 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(record.sequence, cmdline_type, name=name) else: yield self._parse(spec, cmdline_type, name=None)
def gather_adapters(back, anywhere, front): """ Yield (name, seq, where) tuples from which Adapter instances can be built. This generator deals with the notation for anchored 5'/3' adapters and also understands the ``file:`` syntax for reading adapters from an external FASTA file. """ for adapter_list, where in ((back, BACK), (anywhere, ANYWHERE), (front, FRONT)): for seq in adapter_list: if seq.startswith('file:'): # read adapter sequences from a file path = seq[5:] with FastaReader(path) as fasta: for record in fasta: name = record.name.split(None, 1)[0] seq, w = parse_adapter(record.sequence, where) yield (name, seq, w) else: name, seq = parse_adapter_name(seq) seq, w = parse_adapter(seq, where) yield (name, seq, w)
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'