Example #1
0
    def test_custom_phred_offset(self):
        ascii_chars = '*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\'
        obs = _encode_phred_to_qual(list(range(51)), phred_offset=42)
        self.assertEqual(obs, ascii_chars)

        with self.assertRaises(ValueError) as cm:
            _encode_phred_to_qual([42, -1, 33], phred_offset=42)
        self.assertIn('-1', str(cm.exception))
        self.assertIn('[0, 84]', str(cm.exception))

        obs = npt.assert_warns(UserWarning, _encode_phred_to_qual,
                               [42, 255, 33], phred_offset=42)
        self.assertEqual(obs, 'T~K')
Example #2
0
    def test_custom_phred_offset(self):
        ascii_chars = '*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\'
        obs = _encode_phred_to_qual(list(range(51)), phred_offset=42)
        self.assertEqual(obs, ascii_chars)

        with self.assertRaises(ValueError) as cm:
            _encode_phred_to_qual([42, -1, 33], phred_offset=42)
        self.assertIn('-1', str(cm.exception))
        self.assertIn('[0, 84]', str(cm.exception))

        obs = npt.assert_warns(UserWarning,
                               _encode_phred_to_qual, [42, 255, 33],
                               phred_offset=42)
        self.assertEqual(obs, 'T~K')
Example #3
0
    def test_illumina18_variant(self):
        # test entire range of possible ascii chars for illumina1.8
        all_illumina18_ascii = ('!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKL'
                                'MNOPQRSTUVWXYZ[\\]^_')
        obs = _encode_phred_to_qual(list(range(63)), variant='illumina1.8')
        self.assertEqual(obs, all_illumina18_ascii)

        with self.assertRaises(ValueError) as cm:
            _encode_phred_to_qual([42, -1, 33], variant='illumina1.8')
        self.assertIn('-1', str(cm.exception))
        self.assertIn('[0, 62]', str(cm.exception))

        obs = npt.assert_warns(UserWarning, _encode_phred_to_qual,
                               [42, 63, 33], variant='illumina1.8')
        self.assertEqual(obs, 'K_B')
Example #4
0
    def test_sanger_variant(self):
        # test entire range of possible ascii chars for sanger
        all_sanger_ascii = ('!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOP'
                            'QRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~')
        obs = _encode_phred_to_qual(list(range(94)), variant='sanger')
        self.assertEqual(obs, all_sanger_ascii)

        with self.assertRaises(ValueError) as cm:
            _encode_phred_to_qual([42, -1, 33], variant='sanger')
        self.assertIn('-1', str(cm.exception))
        self.assertIn('[0, 93]', str(cm.exception))

        obs = npt.assert_warns(UserWarning, _encode_phred_to_qual,
                               [42, 94, 33], variant='sanger')
        self.assertEqual(obs, 'K~B')
Example #5
0
    def test_illumina13_variant(self):
        # test entire range of possible ascii chars for illumina1.3
        all_illumina13_ascii = ('@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijk'
                                'lmnopqrstuvwxyz{|}~')
        obs = _encode_phred_to_qual(list(range(63)), variant='illumina1.3')
        self.assertEqual(obs, all_illumina13_ascii)

        with self.assertRaises(ValueError) as cm:
            _encode_phred_to_qual([42, -1, 33], variant='illumina1.3')
        self.assertIn('-1', str(cm.exception))
        self.assertIn('[0, 62]', str(cm.exception))

        obs = npt.assert_warns(UserWarning, _encode_phred_to_qual,
                               [42, 63, 33], variant='illumina1.3')
        self.assertEqual(obs, 'j~a')
Example #6
0
    def test_illumina13_variant(self):
        # test entire range of possible ascii chars for illumina1.3
        all_illumina13_ascii = ('@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijk'
                                'lmnopqrstuvwxyz{|}~')
        obs = _encode_phred_to_qual(list(range(63)), variant='illumina1.3')
        self.assertEqual(obs, all_illumina13_ascii)

        with self.assertRaises(ValueError) as cm:
            _encode_phred_to_qual([42, -1, 33], variant='illumina1.3')
        self.assertIn('-1', str(cm.exception))
        self.assertIn('[0, 62]', str(cm.exception))

        obs = npt.assert_warns(UserWarning,
                               _encode_phred_to_qual, [42, 63, 33],
                               variant='illumina1.3')
        self.assertEqual(obs, 'j~a')
Example #7
0
    def test_sanger_variant(self):
        # test entire range of possible ascii chars for sanger
        all_sanger_ascii = ('!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOP'
                            'QRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~')
        obs = _encode_phred_to_qual(list(range(94)), variant='sanger')
        self.assertEqual(obs, all_sanger_ascii)

        with self.assertRaises(ValueError) as cm:
            _encode_phred_to_qual([42, -1, 33], variant='sanger')
        self.assertIn('-1', str(cm.exception))
        self.assertIn('[0, 93]', str(cm.exception))

        obs = npt.assert_warns(UserWarning,
                               _encode_phred_to_qual, [42, 94, 33],
                               variant='sanger')
        self.assertEqual(obs, 'K~B')
Example #8
0
    def test_illumina18_variant(self):
        # test entire range of possible ascii chars for illumina1.8
        all_illumina18_ascii = ('!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKL'
                                'MNOPQRSTUVWXYZ[\\]^_')
        obs = _encode_phred_to_qual(list(range(63)), variant='illumina1.8')
        self.assertEqual(obs, all_illumina18_ascii)

        with self.assertRaises(ValueError) as cm:
            _encode_phred_to_qual([42, -1, 33], variant='illumina1.8')
        self.assertIn('-1', str(cm.exception))
        self.assertIn('[0, 62]', str(cm.exception))

        obs = npt.assert_warns(UserWarning,
                               _encode_phred_to_qual, [42, 63, 33],
                               variant='illumina1.8')
        self.assertEqual(obs, 'K_B')
Example #9
0
def _generator_to_fastq(obj, fh, variant=None, phred_offset=None,
                        id_whitespace_replacement='_',
                        description_newline_replacement=' '):
    formatted_records = _format_fasta_like_records(
        obj, id_whitespace_replacement, description_newline_replacement, True)
    for header, seq_str, qual_scores in formatted_records:
        qual_str = _encode_phred_to_qual(qual_scores, variant=variant,
                                         phred_offset=phred_offset)
        fh.write('@')
        fh.write(header)
        fh.write('\n')
        fh.write(seq_str)
        fh.write('\n+\n')
        fh.write(qual_str)
        fh.write('\n')
Example #10
0
def _generator_to_fastq(obj, fh, variant=None, phred_offset=None,
                        id_whitespace_replacement='_',
                        description_newline_replacement=' ', lowercase=None):
    formatted_records = _format_fasta_like_records(
        obj, id_whitespace_replacement, description_newline_replacement, True,
        lowercase=lowercase)
    for header, seq_str, qual_scores in formatted_records:
        qual_str = _encode_phred_to_qual(qual_scores, variant=variant,
                                         phred_offset=phred_offset)
        fh.write('@')
        fh.write(header)
        fh.write('\n')
        fh.write(seq_str)
        fh.write('\n+\n')
        fh.write(qual_str)
        fh.write('\n')
Example #11
0
 def test_no_phred_scores(self):
     self.assertEqual(_encode_phred_to_qual([], variant='sanger'), '')
Example #12
0
 def test_unrecognized_variant(self):
     with self.assertRaises(ValueError) as cm:
         _encode_phred_to_qual([1, 2, 3], variant='illumina')
     self.assertIn('variant', str(cm.exception))
     self.assertIn("'illumina'", str(cm.exception))
Example #13
0
 def test_solexa_variant(self):
     with self.assertRaises(NotImplementedError) as cm:
         _encode_phred_to_qual([1, 2, 3], variant='solexa')
     self.assertIn('719', str(cm.exception))
Example #14
0
 def test_variant_and_phred_offset_provided(self):
     with self.assertRaises(ValueError) as cm:
         _encode_phred_to_qual([1, 2, 3], variant='sanger', phred_offset=64)
     self.assertIn('both', str(cm.exception))
     self.assertIn('`variant`', str(cm.exception))
     self.assertIn('`phred_offset`', str(cm.exception))
Example #15
0
 def test_variant_and_phred_offset_provided(self):
     with self.assertRaises(ValueError) as cm:
         _encode_phred_to_qual([1, 2, 3], variant='sanger', phred_offset=64)
     self.assertIn('both', str(cm.exception))
     self.assertIn('`variant`', str(cm.exception))
     self.assertIn('`phred_offset`', str(cm.exception))
Example #16
0
 def test_no_phred_scores(self):
     self.assertEqual(_encode_phred_to_qual([], variant='sanger'), '')
Example #17
0
 def test_unrecognized_variant(self):
     with self.assertRaises(ValueError) as cm:
         _encode_phred_to_qual([1, 2, 3], variant='illumina')
     self.assertIn('variant', str(cm.exception))
     self.assertIn("'illumina'", str(cm.exception))
Example #18
0
 def test_solexa_variant(self):
     with self.assertRaises(NotImplementedError) as cm:
         _encode_phred_to_qual([1, 2, 3], variant='solexa')
     self.assertIn('719', str(cm.exception))
Example #19
0
 def test_missing_variant_and_phred_offset(self):
     with self.assertRaises(ValueError) as cm:
         _encode_phred_to_qual([1, 2, 3])
     self.assertIn('`variant`', str(cm.exception))
     self.assertIn('`phred_offset`', str(cm.exception))
     self.assertIn('encode', str(cm.exception))
Example #20
0
 def test_missing_variant_and_phred_offset(self):
     with self.assertRaises(ValueError) as cm:
         _encode_phred_to_qual([1, 2, 3])
     self.assertIn('`variant`', str(cm.exception))
     self.assertIn('`phred_offset`', str(cm.exception))
     self.assertIn('encode', str(cm.exception))