def test_init(self): """JointEnumeration init should work as expected""" #should work for alphabet object a = JointEnumeration([DnaBases, RnaBases]) self.assertEqual(len(a), 16) self.assertEqual(a.Shape, (4,4)) self.assertEqual(a[0], ('T','U')) self.assertEqual(a[-1], ('G','G')) self.assertEqual(a._sub_enum_factors, array([[4],[1]])) #should work for arbitrary sequences a = JointEnumeration(['TCAG', 'UCAG']) self.assertEqual(len(a), 16) self.assertEqual(a[0], ('T','U')) self.assertEqual(a[-1], ('G','G')) self.assertEqual(a._sub_enum_factors, array([[4],[1]])) #should work for different length sequences a = JointEnumeration(['TCA', 'UCAG']) self.assertEqual(a.Shape, (3,4)) self.assertEqual(len(a), 12) self.assertEqual(a[0], ('T','U')) self.assertEqual(a[-1], ('A','G')) self.assertEqual(a._sub_enum_factors, \ array([[4],[1]])) #note: _not_ [3,1]
def test_toString(self): """CharAlphabet toString should convert an input array to string""" r = CharAlphabet('UCAG') self.assertEqual(r.toString(array([[0,0,1],[0,3,2]], 'B')), 'UUC\nUGA') #should work with single seq self.assertEqual(r.toString(array([[0,0,1,0,3,2]], 'B')), 'UUCUGA') #should work with single seq self.assertEqual(r.toString(array([0,0,1,0,3,2], 'B')), 'UUCUGA') #should work with empty seq self.assertEqual(r.toString(array([], 'B')), '')
def test_init(self): """CharAlphabet init should make correct translation tables""" r = CharAlphabet('UCAG') i2c, c2i = r._indices_to_chars, r._chars_to_indices s = array([0,0,1,0,3,2], 'b').tostring() self.assertEqual(s.translate(i2c), 'UUCUGA') self.assertEqual('UUCUGA'.translate(c2i), '\000\000\001\000\003\002')
def test_unpackArrays(self): """JointEnumeration unpackArrays should return correct arrays.""" a = JointEnumeration(['xyz', 'abcd', 'ef']) v = [7, 15, 18, 0] result = a.unpackArrays(v) self.assertEqual(result, array([[0, 1, 2, 0], [3, 3, 1, 0], [1, 1, 0, 0]]))
def test_unpackArrays(self): """JointEnumeration unpackArrays should return correct arrays.""" a = JointEnumeration(['xyz', 'abcd', 'ef']) v = [7,15,18,0] result = a.unpackArrays(v) self.assertEqual(result, array([[0,1,2,0],[3,3,1,0], [1,1,0,0]]))
def test_toChars(self): """CharAlphabet toChars should convert an input array to chars""" r = CharAlphabet('UCAG') c = r.toChars(array([[0,0,1],[0,3,2]], 'B')) self.assertEqual(c, \ array(['UUC','UGA'], 'c'))
def test_fromArray(self): """CharAlphabet fromArray should return correct array""" r = CharAlphabet('UCAG') self.assertEqual(r.fromArray(array(['UUC','UGA'], 'c')), \ array([[0,0,1],[0,3,2]], 'B'))
def test_fromString(self): """CharAlphabet fromString should return correct array""" r = CharAlphabet('UCAG') self.assertEqual(r.fromString('UUCUGA'), array([0,0,1,0,3,2],'B'))
def test_counts(self): """Enumeration counts should count freqs in array""" a = DnaBases f = array([[0,0,1,0,0,3]]) self.assertEqual(a.counts(f), array([4,1,0,1])) #check that it works with byte array f = array([[0,0,1,0,0,3]], 'B') self.assertEqual(a.counts(f), array([4,1,0,1])) #should ignore out-of-bounds items g = [0,4] self.assertEqual(a.counts(g), array([1,0,0,0])) #make sure it works for long sequences, i.e. no wraparound at 255 h = [0, 3] * 70000 self.assertEqual(a.counts(h), array([70000,0,0,70000])) h2 = array(h).astype('B') self.assertEqual(a.counts(h2), array([70000,0,0,70000])) i = array([0,3] * 75000) self.assertEqual(a.counts(i), array([75000,0,0,75000])) #make sure it works for long _binary_ sequences, e.g. the results #of array comparisons. a = array([0,1,2,3]*10000) b = array([0,0,0,0]*10000) same = (a==b)