Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    def parse_multi(self, back=[], anywhere=[], front=[]):
        """
        Parse all three types of commandline options that can be used to
        specify adapters. back, anywhere and front are lists of strings,
        corresponding to the respective commandline types (-a, -b, -g).

        Return a list of appropriate Adapter classes.
        """
        adapters = []
        for specs, cmdline_type in (back, 'back'), (anywhere,
                                                    'anywhere'), (front,
                                                                  'front'):
            for spec in specs:
                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]
                            adapters.append(
                                self.parse(record.sequence, name,
                                           cmdline_type))
                else:
                    adapters.append(
                        self.parse(spec=spec, cmdline_type=cmdline_type))
        return adapters
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
 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))
Exemplo n.º 6
0
 def load_from_fasta(self, fasta):
     """
     Returns a dict of seq:set(names)
     """
     close = False
     if isinstance(fasta, str):
         fasta = open(fasta, 'rt')
         close = True
     with FastaReader(fasta) as fasta:
         for record in fasta:
             name = record.name.split(None, 1)[0]
             seq = record.sequence
             self.add(name, seq)
     if close:
         fasta.close()
Exemplo n.º 7
0
 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'