Ejemplo n.º 1
0
 def test_no_labels(self):
     """FastaParser should return empty list from file w/o seqs"""
     #should fail if strict (the default)
     self.assertRaises(RecordError, list,
                       FastaParser(self.labels, strict=True))
     #if not strict, should skip the records
     self.assertEqual(list(FastaParser(self.labels, strict=False)), [])
Ejemplo n.º 2
0
 def test_multiple_bad(self):
     """Parser should complain or skip bad records"""
     self.assertRaises(RecordError, list, FastaParser(self.twogood))
     f = list(FastaParser(self.twogood, strict=False))
     self.assertEqual(len(f), 2)
     a, b = f
     a, b = a[1], b[1]  #field 0 is name
     self.assertEqual((a.Name, a), ('abc', 'caggac'))
     self.assertEqual((b.Name, b), ('456', 'cg'))
Ejemplo n.º 3
0
    def test_single(self):
        """FastaParser should read single record as seq object"""
        f = list(FastaParser(self.oneseq))
        self.assertEqual(len(f), 1)
        a = f[0]
        self.assertEqual(a, ('abc', 'UCAG'))
        self.assertEqual(a[1].Name, 'abc')

        f = list(FastaParser(self.multiline))
        self.assertEqual(len(f), 1)
        a = f[0]
        self.assertEqual(a, ('xyz', 'UUUUCCAAAAAG'))
        self.assertEqual(a[1].Name, 'xyz')
Ejemplo n.º 4
0
    def test_multiple_constructor_bad(self):
        """Parser should complain or skip bad records w/ constructor"""
        def dnastrict(x, **kwargs):
            try:
                return Dna(x, check=True, **kwargs)
            except Exception as e:
                raise RecordError("Could not convert sequence")

        self.assertRaises(RecordError, list, FastaParser(self.oneX, dnastrict))
        f = list(FastaParser(self.oneX, dnastrict, strict=False))
        self.assertEqual(len(f), 2)
        a, b = f
        a, b = a[1], b[1]
        self.assertEqual((a.Name, a), ('abc', 'caggac'.upper()))
        self.assertEqual((b.Name, b), ('456', 'cg'.upper()))
Ejemplo n.º 5
0
    def test_single_constructor(self):
        """FastaParser should use constructors if supplied"""
        f = list(FastaParser(self.oneseq, Dna))
        self.assertEqual(len(f), 1)
        a = f[0]
        self.assertEqual(a, ('abc', 'TCAG'))
        self.assertEqual(a[1].Name, 'abc')

        def upper_abc(x):
            return None, {'ABC': x.upper()}

        f = list(FastaParser(self.multiline, Dna, upper_abc))
        self.assertEqual(len(f), 1)
        a = f[0]
        self.assertEqual(a, (None, 'TTTTCCAAAAAG'))
        self.assertEqual(a[1].Name, None)
        self.assertEqual(a[1].Info.ABC, 'XYZ')
Ejemplo n.º 6
0
 def test_multiple(self):
     """FastaParser should read multiline records correctly"""
     f = list(FastaParser(self.threeseq))
     self.assertEqual(len(f), 3)
     for i in f:
         assert isinstance(i[1], Sequence)
     a, b, c = f
     self.assertEqual((a[1].Name, a[1]), ('123', 'a'))
     self.assertEqual((b[1].Name, b[1]), ('abc', 'caggac'))
     self.assertEqual((c[1].Name, c[1]), ('456', 'cg'))
Ejemplo n.º 7
0
 def test_empty(self):
     """FastaParser should return empty list from 'file' w/o labels"""
     self.assertEqual(list(FastaParser(self.empty)), [])
     self.assertEqual(list(FastaParser(self.nolabels, strict=False)), [])
     self.assertRaises(RecordError, list, FastaParser(self.nolabels))
Ejemplo n.º 8
0
class FastaParserTests(GenericFastaTest):
    """Tests of FastaParser: returns sequence objects."""
    def test_empty(self):
        """FastaParser should return empty list from 'file' w/o labels"""
        self.assertEqual(list(FastaParser(self.empty)), [])
        self.assertEqual(list(FastaParser(self.nolabels, strict=False)), [])
        self.assertRaises(RecordError, list, FastaParser(self.nolabels))

    def test_no_labels(self):
        """FastaParser should return empty list from file w/o seqs"""
        #should fail if strict (the default)
        self.assertRaises(RecordError, list,
                          FastaParser(self.labels, strict=True))
        #if not strict, should skip the records
        self.assertEqual(list(FastaParser(self.labels, strict=False)), [])

    def test_single(self):
        """FastaParser should read single record as seq object"""
        f = list(FastaParser(self.oneseq))
        self.assertEqual(len(f), 1)
        a = f[0]
        self.assertEqual(a, ('abc', 'UCAG'))
        self.assertEqual(a[1].Name, 'abc')

        f = list(FastaParser(self.multiline))
        self.assertEqual(len(f), 1)
        a = f[0]
        self.assertEqual(a, ('xyz', 'UUUUCCAAAAAG'))
        self.assertEqual(a[1].Name, 'xyz')

    def test_single_constructor(self):
        """FastaParser should use constructors if supplied"""
        f = list(FastaParser(self.oneseq, Dna))
        self.assertEqual(len(f), 1)
        a = f[0]
        self.assertEqual(a, ('abc', 'TCAG'))
        self.assertEqual(a[1].Name, 'abc')

        def upper_abc(x):
            return None, {'ABC': x.upper()}

        f = list(FastaParser(self.multiline, Dna, upper_abc))
        self.assertEqual(len(f), 1)
        a = f[0]
        self.assertEqual(a, (None, 'TTTTCCAAAAAG'))
        self.assertEqual(a[1].Name, None)
        self.assertEqual(a[1].Info.ABC, 'XYZ')

    def test_multiple(self):
        """FastaParser should read multiline records correctly"""
        f = list(FastaParser(self.threeseq))
        self.assertEqual(len(f), 3)
        for i in f:
            assert isinstance(i[1], Sequence)
        a, b, c = f
        self.assertEqual((a[1].Name, a[1]), ('123', 'a'))
        self.assertEqual((b[1].Name, b[1]), ('abc', 'caggac'))
        self.assertEqual((c[1].Name, c[1]), ('456', 'cg'))

    def test_multiple_bad(self):
        """Parser should complain or skip bad records"""
        self.assertRaises(RecordError, list, FastaParser(self.twogood))
        f = list(FastaParser(self.twogood, strict=False))
        self.assertEqual(len(f), 2)
        a, b = f
        a, b = a[1], b[1]  #field 0 is name
        self.assertEqual((a.Name, a), ('abc', 'caggac'))
        self.assertEqual((b.Name, b), ('456', 'cg'))

    def test_multiple_constructor_bad(self):
        """Parser should complain or skip bad records w/ constructor"""
        def dnastrict(x, **kwargs):
            try:
                return Dna(x, check=True, **kwargs)
            except Exception, e:
                raise RecordError, "Could not convert sequence"

        self.assertRaises(RecordError, list, FastaParser(self.oneX, dnastrict))
        f = list(FastaParser(self.oneX, dnastrict, strict=False))
        self.assertEqual(len(f), 2)
        a, b = f
        a, b = a[1], b[1]
        self.assertEqual((a.Name, a), ('abc', 'caggac'.upper()))
        self.assertEqual((b.Name, b), ('456', 'cg'.upper()))