Example #1
0
 def test_is_valid(self):
     """Enumeration is_valid should return True for valid sequence"""
     a = Enumeration("bca")
     self.assertEqual(a.is_valid(""), True)
     self.assertEqual(a.is_valid("bbb"), True)
     self.assertEqual(a.is_valid("bbbaac"), True)
     self.assertEqual(a.is_valid("bbd"), False)
     self.assertEqual(a.is_valid("d"), False)
     self.assertEqual(a.is_valid(["a", "b"]), True)
     self.assertEqual(a.is_valid(["a", None]), False)
Example #2
0
    def test_mul(self):
        """Enumeration mul should produce correct JointEnumeration"""
        a = DnaBases * RnaBases
        self.assertEqual(len(a), 16)
        self.assertEqual(a[0], ("T", "U"))
        self.assertEqual(a[-1], ("G", "G"))

        # check that it works with gaps
        a = Enumeration("ab-", "-")
        b = Enumeration("xz", "z")
        x = a * b
        self.assertEqual(x.gap, ("-", "z"))
        self.assertEqual(x.gap_index, 5)
        self.assertEqual(len(x), 6)
        self.assertEqual(x, (("a", "x"), ("a", "z"), ("b", "x"), ("b", "z"),
                             ("-", "x"), ("-", "z")))
        # check that it doesn't work when only one seq has gaps
        c = Enumeration("c")
        x = a * c
        self.assertEqual(x.gap, None)
Example #3
0
    def test_pow(self):
        """Enumeration pow should produce JointEnumeration with n copies"""
        a = AminoAcids**3
        self.assertEqual(a[0], (AminoAcids[0], ) * 3)
        self.assertEqual(a[-1], (AminoAcids[-1], ) * 3)
        self.assertEqual(len(a), len(AminoAcids)**3)
        self.assertEqual(a.array_type, uint16)

        # check that it works with gaps
        a = Enumeration("a-b", "-")
        b = a**3
        self.assertEqual(len(b), 27)
        self.assertEqual(b.gap, ("-", "-", "-"))
        self.assertEqual(b.gap_index, 13)
        self.assertEqual(b.array_type, uint8)

        # check that array type is set correctly if needed
        b = a**6  # too big to fit in char
        self.assertEqual(b.array_type, uint16)
Example #4
0
    def test_init(self):
        """Enumeration init should work from any sequence"""
        a = Enumeration("abc")
        self.assertEqual(a.index("a"), 0)
        self.assertEqual(a.index("b"), 1)
        self.assertEqual(a.index("c"), 2)
        self.assertEqual(a[0], "a")
        self.assertEqual(a[1], "b")
        self.assertEqual(a[2], "c")
        self.assertEqual(a.array_type, uint8)

        a = Enumeration("bca")
        self.assertEqual(a.index("b"), 0)
        self.assertEqual(a.index("c"), 1)
        self.assertEqual(a.index("a"), 2)
        self.assertEqual(a[0], "b")
        self.assertEqual(a[1], "c")
        self.assertEqual(a[2], "a")

        a = Enumeration([1, "2"])
        self.assertEqual(a.index(1), 0)
        self.assertEqual(a.index("2"), 1)
        self.assertRaises(KeyError, a.index, "1")

        # check that it works with gaps
        a = Enumeration("ab-", "-")
        self.assertEqual(a.gap, "-")
        self.assertEqual(a.gap_index, 2)

        a = Enumeration(list(range(257)))  # too big to fit in uint8
        self.assertEqual(a.array_type, uint16)
Example #5
0
 def test_from_indices(self):
     """Enumeration from_indices should return elements from indices"""
     a = Enumeration("bca")
     self.assertEqual(a.from_indices([]), [])
     self.assertEqual(a.from_indices([1, 1, 2, 0, 2, 1]), list("ccabac"))
Example #6
0
 def test_to_indices(self):
     """Enumeration to_indices should return indices from elements"""
     a = Enumeration("bca")
     self.assertEqual(a.to_indices(""), [])
     self.assertEqual(a.to_indices("ccabac"), [1, 1, 2, 0, 2, 1])
Example #7
0
 def test_getitem(self):
     """Enumeration[i] should return character at i"""
     a = Enumeration("bca")
     self.assertEqual(a[0], "b")
     self.assertEqual(a[1], "c")
     self.assertEqual(a[2], "a")
Example #8
0
 def test_index(self):
     """Enumeration index should return first index of item"""
     a = Enumeration("bca")
     self.assertEqual(a.index("b"), 0)
     self.assertEqual(a.index("c"), 1)
     self.assertEqual(a.index("a"), 2)