Beispiel #1
0
    def test_roundtrip_generators(self):
        # test that fasta and qual files can be streamed into memory and back
        # out to disk using generator reader and writer
        fps = list(map(lambda e: list(map(get_data_path, e)),
                       [('empty', 'empty'),
                        ('fasta_multi_seq_roundtrip',
                         'qual_multi_seq_roundtrip')]))

        for fasta_fp, qual_fp in fps:
            with open(fasta_fp, 'U') as fh:
                exp_fasta = fh.read()
            with open(qual_fp, 'U') as fh:
                exp_qual = fh.read()

            fasta_fh = StringIO()
            qual_fh = StringIO()
            _generator_to_fasta(_fasta_to_generator(fasta_fp, qual=qual_fp),
                                fasta_fh, qual=qual_fh)
            obs_fasta = fasta_fh.getvalue()
            obs_qual = qual_fh.getvalue()
            fasta_fh.close()
            qual_fh.close()

            self.assertEqual(obs_fasta, exp_fasta)
            self.assertEqual(obs_qual, exp_qual)
Beispiel #2
0
 def test_generator_to_fasta_sequence_lowercase_exception(self):
     seq = Sequence('ACgt', metadata={'id': ''})
     fh = StringIO()
     with self.assertRaisesRegexp(AttributeError,
                                  "lowercase specified but class Sequence "
                                  "does not support lowercase "
                                  "functionality"):
         _generator_to_fasta(SequenceCollection([seq]), fh,
                             lowercase='introns')
Beispiel #3
0
    def test_generator_to_fasta_no_qual(self):
        # test writing standalone fasta (i.e., without a qual file)
        for obj, kwargs, fp, _ in self.objs_fps:
            fh = StringIO()
            _generator_to_fasta(obj, fh, **kwargs)
            obs = fh.getvalue()
            fh.close()

            with open(fp, 'U') as fh:
                exp = fh.read()
            self.assertEqual(obs, exp)
Beispiel #4
0
    def test_generator_to_fasta(self):
        for obj, kwargs, fp in self.objs_fps:
            fh = StringIO()
            _generator_to_fasta(obj, fh, **kwargs)
            obs = fh.getvalue()
            fh.close()

            with open(fp, 'U') as fh:
                exp = fh.read()

            self.assertEqual(obs, exp)
Beispiel #5
0
    def test_generator_to_fasta_mixed_qual_scores(self):
        # test writing some sequences with qual scores and some without is
        # possible if no qual output file is specified
        fh = StringIO()
        _generator_to_fasta(self.mixed_qual_score_gen, fh, lowercase='introns')
        obs = fh.getvalue()
        fh.close()

        with open(get_data_path('fasta_mixed_qual_scores'), 'U') as fh:
            exp = fh.read()

        self.assertEqual(obs, exp)
Beispiel #6
0
    def test_roundtrip_generators(self):
        # test that a file can be streamed into memory and back out to disk
        # using generator reader and writer
        for fp in map(get_data_path, ['empty', 'fasta_multi_seq_roundtrip']):
            with open(fp, 'U') as fh:
                exp = fh.read()

            fh = StringIO()
            _generator_to_fasta(_fasta_to_generator(fp), fh)
            obs = fh.getvalue()
            fh.close()

            self.assertEqual(obs, exp)
Beispiel #7
0
    def test_generator_to_fasta_with_qual(self):
        # test writing fasta and qual files
        for obj, kwargs, fasta_fp, qual_fp in self.objs_fps:
            if qual_fp is not None:
                fasta_fh = StringIO()
                qual_fh = StringIO()
                _generator_to_fasta(obj, fasta_fh, qual=qual_fh, **kwargs)
                obs_fasta = fasta_fh.getvalue()
                obs_qual = qual_fh.getvalue()
                fasta_fh.close()
                qual_fh.close()

                with open(fasta_fp, 'U') as fh:
                    exp_fasta = fh.read()
                with open(qual_fp, 'U') as fh:
                    exp_qual = fh.read()

                self.assertEqual(obs_fasta, exp_fasta)
                self.assertEqual(obs_qual, exp_qual)
Beispiel #8
0
 def test_generator_to_fasta_invalid_input(self):
     for obj, kwargs, error_type, error_msg_regexp in self.invalid_objs:
         fh = StringIO()
         with self.assertRaisesRegexp(error_type, error_msg_regexp):
             _generator_to_fasta(obj, fh, **kwargs)
         fh.close()