def test_positionalBases(self): """BaseUsage positionalBases should have copy of self at each position""" b = BaseUsage('A') p = b.positionalBases() for i in p: assert i is not b self.assertEqual(i, {'A':1,'U':0,'C':0,'G':0})
def test_setitem(self): """BaseUsage should map keys on setitem""" b = BaseUsage() b['t'] = 3 b['G'] = 3 b.normalize() i = b.items() i.sort() self.assertEqual(i, [('A',0.0),('C',0.0),('G',0.5),('U',0.5)])
def test_fromCartesian(self): """BaseUsage fromCartesian should init instance from x,y,z""" b = BaseUsage.fromCartesian(0.5,.5,.5) self.assertFloatEqual(b['A'], 0.25) self.assertFloatEqual(b['C'], 0.25) self.assertFloatEqual(b['G'], 0.25) self.assertFloatEqual(b['U'], 0.25) b = BaseUsage.fromCartesian(1/3.0, 1/3.0, 1/3.0) self.assertEqual(b['U'], 0) self.assertEqual(b['A'], 1/3.0)
def test_getitem(self): """BaseUsage should map key on getitem""" b = BaseUsage({'a':3, 'T':2, 'X':1}) self.assertEqual(b['X'], 1) self.assertEqual(b['A'], 3) self.assertEqual(b['U'], 2) self.assertEqual(b['a'], 3) self.assertEqual(b['t'], 2) b.normalize() assert 'X' not in b self.assertFloatEqual(b['A'], 0.6) self.assertFloatEqual(b['u'], 0.4)
def test_init_empty(self): """BaseUsage should init with empty freqs""" b = BaseUsage() self.assertEqual(len(b), 4) for nt in 'UTACGutacg': assert nt in b self.assertEqual(b[nt], 0) for nt in 'UCAG': assert nt in b.keys() items = list(iter(b)) items.sort() self.assertEqual(items, ['A', 'C', 'G', 'U'])
def test_distance(self): """BaseUsage distance() should return dist between two BUs""" #absolute numbers, will normalize to calculate distance self.assertFloatEqual(BaseUsage('GC').distance(BaseUsage('AU')),1) self.assertFloatEqual(BaseUsage('AU').distance(BaseUsage('GC')),1) self.assertFloatEqual(BaseUsage('GCAU').distance(BaseUsage('AUAU')),.5) #should work even against dict with 'T's self.assertFloatEqual(BaseUsage('GCAU').distance(\ BaseUsage({'A':2,'T':2,'C':0,'G':0,})),.5) #rounding error self.assertEqual(BaseUsage('ACG').distance(BaseUsage('CCGGAA')),0) #normalized - as in unit simplex ag = BaseUsage('AG') ag.normalize() uc = BaseUsage('UC') uc.normalize() self.assertFloatEqual(ag.distance(uc),1) self.assertFloatEqual(BaseUsage({'A':0.4,'G':0.1,'C':0.4,'U':0.1})\ .distance(BaseUsage({'A':0.1,'G':0.4,'U':0.4,'C':0.1})),0.6) self.assertFloatEqual(BaseUsage({'A':0.25,'G':0.25,'C':0.25,'U':0.25})\ .distance(BaseUsage({'A':0.25,'G':0.25,'U':0.25,'C':0.25})),0.0) self.assertFloatEqual(BaseUsage({'A':0.245,'G':0.255,'C':0.245,\ 'U':0.255}).distance(BaseUsage({'A':0.255,'G':0.245,'U':0.245,\ 'C':0.255})),0.02) self.assertFloatEqual(BaseUsage({'A':0.245,'G':0.255,'C':0.245,\ 'U':0.255}).distance(BaseUsage({'A':0.25,'G':0.25,'U':0.25,\ 'C':0.25})),0.01) self.assertFloatEqual(BaseUsage({'A':0.248,'G':0.252,'C':0.248,\ 'U':0.252}).distance(BaseUsage({'A':0.25,'G':0.25,'U':0.25,\ 'C':0.25})),0.004)
def test_aminoAcids(self): """BaseUsage aminoAcids should give the same results as the codons""" known_data = { 'AAA' : .6 * .6 * .6, 'AAU' : .6 * .6 * .4, 'AUA' : .6 * .4 * .6, 'AUU' : .6 * .4 * .4, 'UAA' : .4 * .6 * .6, 'UAU' : .4 * .6 * .4, 'UUA' : .4 * .4 * .6, 'UUU' : .4 * .4 * .4, } known = CodonUsage(known_data) b = BaseUsage({'a':3, 'T':2, 'X':1}) self.assertEqual(b.aminoAcids(), known.aminoAcids()) #check that the genetic code is passed through correctly all_g = GeneticCode('G'*64) self.assertEqual(b.aminoAcids(all_g), AminoAcidUsage({'G':1}))
def test_codons(self): """BaseUsage codons should return most likely codon freqs""" b = BaseUsage({'a':3, 'T':2, 'X':1}) c = b.codons() known = { 'AAA' : .6 * .6 * .6, 'AAU' : .6 * .6 * .4, 'AUA' : .6 * .4 * .6, 'AUU' : .6 * .4 * .4, 'UAA' : .4 * .6 * .6, 'UAU' : .4 * .6 * .4, 'UUA' : .4 * .4 * .6, 'UUU' : .4 * .4 * .4, } for codon in c: if codon in known: self.assertFloatEqual(c[codon], known[codon]) else: self.assertEqual(c[codon], 0)
def test_toCartesian(self): """BaseUsage toCartesian should return x, y, z from instance""" b = BaseUsage('ACGU') self.assertEqual(b.toCartesian(), (0.5,0.5,0.5)) b = BaseUsage('A') self.assertEqual(b.toCartesian(), (0,0,1)) b = BaseUsage('CGA') self.assertEqual(b.toCartesian(), (1/3.0,1/3.0,1/3.0))
def test_normalize(self): """BaseUsage normalize should work when empty""" b = BaseUsage() b.normalize() self.assertEqual(b, {'U':0,'C':0,'A':0,'G':0}) b = BaseUsage('AACG') b.normalize() self.assertEqual(b, {'U':0, 'C':0.25, 'A':0.5, 'G':0.25})
def test_content(self): """BaseUsage content should return sum of specified bases.""" b = BaseUsage('UUUUUCCCAG') #should work for combinations self.assertEqual(b.content('UCAG'), 10) self.assertEqual(b.content('GC'), 4) self.assertEqual(b.content('AU'), 6) #should map T to U self.assertEqual(b.content('AT'), 6) #should work for single bases self.assertEqual(b.content('U'), 5) #shouldn't complain about invalid bases self.assertEqual(b.content('X'), 0)
def test_bases(self): """BaseUsage bases() should return same object""" b = BaseUsage({'a':3, 'T':2, 'C':5, 'X':1}) c = b.bases() assert b is c
def test_copy(self): """BaseUsage copy should work correctly""" b = BaseUsage({'a':3, 'T':2, 'C':5, 'X':1}) c = b.copy() self.assertEqual(c['AT'], 0.5)
def test_init_data(self): """BaseUsage should init with arbitrary data""" b = BaseUsage('UUUUUGGGCA') self.assertEqual(b, {'U':5, 'G':3, 'C':1, 'A':1}) b.normalize() self.assertEqual(b, {'U':0.5, 'G':0.3, 'C':0.1, 'A':0.1})