1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
|
#!/usr/bin/env python
from cogent.util.unit_test import TestCase, main
from cogent.parse.tree import DndParser
from cogent.seqsim.tree import RangeNode, balanced_breakpoints, BalancedTree, \
RandomTree, CombTree, StarTree, LineTree
from cogent.core.usage import DnaPairs
from copy import deepcopy
from operator import mul, or_, add
from numpy import array, average, diag
from numpy.random import random, randint
from cogent.seqsim.usage import Rates
__author__ = "Rob Knight"
__copyright__ = "Copyright 2007-2016, The Cogent Project"
__credits__ = ["Rob Knight", "Daniel McDonald"]
__license__ = "GPL"
__version__ = "1.9"
__maintainer__ = "Rob Knight"
__email__ = "rob@spot.colorado.edu"
__status__ = "Production"
class treeTests(TestCase):
"""Tests for top-level functions."""
def test_init(self):
"""Make sure keyword arguments are being passed to baseclass"""
node = RangeNode(LeafRange=1, Id=2, Name='foo', Length=42)
self.assertEqual(node.LeafRange, 1)
self.assertEqual(node.Id, 2)
self.assertEqual(node.Name, 'foo')
self.assertEqual(node.Length, 42)
def test_balanced_breakpoints(self):
"""balanced_breakpoints should produce expected arrays."""
self.assertRaises(ValueError, balanced_breakpoints, 1)
self.assertEqual(balanced_breakpoints(2), array([0]))
self.assertEqual(balanced_breakpoints(4), array([1,0,2]))
self.assertEqual(balanced_breakpoints(8), \
array([3,1,5,0,2,4,6]))
self.assertEqual(balanced_breakpoints(16), \
array([7,3,11,1,5,9,13,0,2,4,6,8,10,12,14]))
self.assertEqual(balanced_breakpoints(32), \
array([15,7,23,3,11,19,27,1,5,9,13,17,21,25,29,\
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]))
def test_BalancedTree(self):
"""BalancedTree should return a balanced tree"""
b = BalancedTree(4)
self.assertEqual(len(list(b.traverse())), 4)
b.assignIds()
self.assertEqual(str(b), '((0,1)4,(2,3)5)6')
def test_RandomTree(self):
"""RandomTree should return correct number of nodes
NOTE: all the work is done in breakpoints, which is thoroughly
tested indepndently. RandomTree just makes permutations.
"""
d = {}
for i in range(10):
r = RandomTree(100)
self.assertEqual(len(list(r.traverse())), 100)
r.assignIds()
#make sure we get different trees each time...
s = str(r)
assert s not in d
d[s] = None
def test_CombTree(self):
"""CombTree should return correct topology"""
c = CombTree(4)
c.assignIds()
self.assertEqual(str(c), '(0,(1,(2,3)4)5)6')
c = CombTree(4, deepest_first=False)
c.assignIds()
self.assertEqual(str(c), '(((0,1)4,2)5,3)6')
def test_StarTree(self):
"""StarTree should return correct star topology and # nodes"""
t = StarTree(5)
self.assertEqual(len(t.Children), 5)
for c in t.Children:
assert c.Parent is t
def test_LineTree(self):
"""LineTree should return correct number of nodes"""
t = LineTree(5)
depth = 1
curr = t
while curr.Children:
self.assertEqual(len(curr.Children), 1)
depth += 1
curr = curr.Children[0]
self.assertEqual(depth, 5)
class RangeTreeTests(TestCase):
"""Tests of the RangeTree class."""
def setUp(self):
"""Make some standard objects to test."""
#Notes on sample string:
#
#1. trailing zeros are stripped in conversion to/from float, so result
# is only exactly the same without them.
#
#2. trailing chars (e.g. semicolon) are not recaptured in the output,
# so were deleted from original Newick-format string.
#
#3. whitespace is stripped, but is handy for formatting, so is stripped
# from original string before comparisons.
self.sample_tree_string = """
(
(
xyz:0.28124,
(
def:0.24498,
mno:0.03627)
A:0.1771)
B:0.0487,
abc:0.05925,
(
ghi:0.06914,
jkl:0.13776)
C:0.09853)
"""
self.t = DndParser(self.sample_tree_string, RangeNode)
self.i = self.t.indexByAttr('Name')
self.sample_string_2 = '((((a,b),c),(d,e)),((f,g),h))'
self.t2 = DndParser(self.sample_string_2, RangeNode)
self.i2 = self.t2.indexByAttr('Name')
self.sample_string_3 = '(((a,b),c),(d,e))'
self.t3 = DndParser(self.sample_string_3, RangeNode)
def test_str(self):
"""RangeNode should round-trip Newick string corrrectly."""
r = RangeNode()
self.assertEqual(str(r), '()')
#should work for tree with branch lengths set
t = DndParser(self.sample_tree_string, RangeNode)
expected = self.sample_tree_string.replace('\n', '')
expected = expected.replace(' ', '')
self.assertEqual(str(t), expected)
#self.assertEqual(t.getNewick(with_distances=True), expected)
#should also work for tree w/o branch lengths
t2 = DndParser(self.sample_string_2, RangeNode)
self.assertEqual(str(t2), self.sample_string_2)
def test_traverse(self):
"""RangeTree traverse should visit all nodes in correct order"""
t = self.t
i = self.i
#first, check that lengths are correct
#naked traverse() only does leaves; should be 6.
self.assertEqual(len(list(t.traverse())), 6)
#traverse() with self_before should count all nodes.
self.assertEqual(len(list(t.traverse(self_before=True))), 10)
#traverse() with self_after should have same count as self_before
self.assertEqual(len(list(t.traverse(self_after=True))), 10)
#traverse() with self_before and self_after should visit internal
#nodes multiple times
self.assertEqual(len(list(t.traverse(True,True))), 14)
#now, check that items are in correct order
exp = ['xyz','def','mno','abc','ghi','jkl']
obs = [i.Name for i in t.traverse()]
self.assertEqual(obs, exp)
exp = [None, 'B', 'xyz', 'A', 'def', 'mno', 'abc', 'C', 'ghi', 'jkl']
obs = [i.Name for i in t.traverse(self_before=True)]
self.assertEqual(obs, exp)
exp = ['xyz', 'def', 'mno', 'A', 'B', 'abc', 'ghi', 'jkl', 'C', None]
obs = [i.Name for i in t.traverse(self_after=True)]
self.assertEqual(obs, exp)
exp = [None, 'B', 'xyz', 'A', 'def', 'mno', 'A', 'B', 'abc', 'C', \
'ghi', 'jkl', 'C', None]
obs = [i.Name for i in t.traverse(self_before=True, self_after=True)]
self.assertEqual(obs, exp)
def test_indexByAttr(self):
"""RangeNode indexByAttr should make index using correct attr"""
t = self.t
i = self.i
#check that we got the right number of elements
#all elements unique, so should be same as num nodes
self.assertEqual(len(i), len(list(t.traverse(self_before=True))))
#check that we got everything
i_keys = i.keys()
i_vals = i.values()
for node in t.traverse(self_before=True):
assert node.Name in i_keys
assert node in i_vals
#can't predict which node will have None as the key
if node.Name is not None:
assert i[node.Name] is node
#check that it works when elements are not unique
t = self.t3
for node in t.traverse(self_before=True):
node.X = 'b'
for node in t.traverse():
node.X = 'a'
result = t.indexByAttr('X', multiple=True)
self.assertEqual(len(result), 2)
self.assertEqual(len(result['a']), 5)
self.assertEqual(len(result['b']), 4)
for n in t.traverse():
assert n in result['a']
assert not n in result['b']
def test_indexbyFunc(self):
"""RangeNode indexByFunc should make index from function"""
t = self.t
def f(n):
try:
return n.Name.isupper()
except AttributeError:
return None
i = self.i
f_i = t.indexByFunc(f)
self.assertEqual(len(f_i), 3)
self.assertEqual(f_i[True], [i['B'], i['A'], i['C']])
self.assertEqual(f_i[False], [i['xyz'], i['def'], i['mno'], \
i['abc'], i['ghi'], i['jkl']])
self.assertEqual(f_i[None], [i[None]])
def test_assignIds(self):
"""RangeNode assignIds should work as expected"""
t = self.t2
index = self.i2
t.assignIds()
#check that ids were set correctly on the leaves
for i, a in enumerate('abcdefgh'):
self.assertEqual(index[a].Id, i)
#check that ranges were set correctly on the leaves
for i, a in enumerate('abcdefgh'):
self.assertEqual(index[a].LeafRange, (i, i+1))
#check that internal ids were set correctly
obs = [i.Id for i in t.traverse(self_after=True)]
exp = [0,1,8,2,9,3,4,10,11,5,6,12,7,13,14]
self.assertEqual(obs, exp)
#check that internal ranges were set correctly
obs = [i.LeafRange for i in t.traverse(self_after=True)]
exp = [(0,1),(1,2),(0,2),(2,3),(0,3),(3,4),(4,5),(3,5),(0,5), \
(5,6),(6,7),(5,7),(7,8),(5,8),(0,8)]
self.assertEqual(obs, exp)
def test_propagateAttr(self):
"""RangeNode propagateAttr should send attr down tree, unless set"""
t = self.t
i = self.i
for n in t.traverse(self_before=True):
assert not hasattr(n, 'XYZ')
t.XYZ = 3
t.propagateAttr('XYZ')
for n in t.traverse(self_before=True):
self.assertEqual(n.XYZ, 3)
#shouldn't overwrite internal nodes by default
a_children = list(i['A'].traverse(self_before=True))
i['A'].GHI = 5
t.GHI = 1
t.propagateAttr('GHI')
for n in t.traverse(self_before=True):
if n in a_children:
self.assertEqual(n.GHI, 5)
else:
self.assertEqual(n.GHI, 1)
t.GHI = 0
t.propagateAttr('GHI', overwrite=True)
for n in t.traverse(self_before=True):
self.assertEqual(n.GHI, 0)
def test_delAttr(self):
"""RangeNode delAttr should delete attr from self and children"""
t = self.t2
for n in t.traverse(self_before=True):
assert hasattr(n, 'Name')
t.delAttr('Name')
for n in t.traverse(self_before=True):
assert not hasattr(n, 'Name')
def test_accumulateAttr(self):
"""RangeNode accumulateAttr should accumulate attr in right direction"""
t = self.t3
#test towards_leaves (the default)
f = lambda a, b: b + 1
for n in t.traverse(self_before=True):
n.Level = 0
t.accumulateAttr('Level', f=f)
levels = [i.Level for i in t.traverse(self_before=True)]
self.assertEqual(levels, [0,1,2,3,3,2,1,2,2])
for n in t.traverse(self_before=True):
n.Level=0
#test away from leaves
f = lambda a, b : max(a, b+1)
for n in t.traverse(self_before=True):
n.Level=0
t.accumulateAttr('Level', towards_leaves=False,f=f)
levels = [i.Level for i in t.traverse(self_before=True)]
self.assertEqual(levels, [3,2,1,0,0,0,1,0,0])
def test_accumulateChildAttr(self):
"""RangeNode accumulateChildAttr should work as expected"""
t = self.t2
i = self.i2
i['a'].x = 3
i['b'].x = 4
i['d'].x = 0
i['f'].x = 1
i['g'].x = 1
i['h'].x = 1
t.accumulateChildAttr('x', f=mul)
self.assertEqual([i.__dict__.get('x', None) for i in \
t.traverse(self_after=True)],
[3, 4, 12, None, 12, 0, None, 0, 0, 1, 1, 1, 1, 1, 0])
t.accumulateChildAttr('x', f=add)
self.assertEqual([i.__dict__.get('x', None) for i in \
t.traverse(self_after=True)],
[3, 4, 7, None, 7, 0, None, 0, 7, 1, 1, 2, 1, 3, 10])
def test_assignLevelsFromRoot(self):
"""RangeNode assignLevelsFromRoot should match hand-calculated levels"""
t = self.t3
t.assignLevelsFromRoot()
levels = [i.Level for i in t.traverse(self_before=True)]
self.assertEqual(levels, [0,1,2,3,3,2,1,2,2])
def test_assignLevelsFromLeaves(self):
"""RangeNode assignLevelsFromLeaves should match hand-calculated levels"""
t = self.t3
t.assignLevelsFromLeaves()
levels = [i.Level for i in t.traverse(self_before=True)]
self.assertEqual(levels, [3,2,1,0,0,0,1,0,0])
t.assignLevelsFromLeaves(use_min=True)
levels = [i.Level for i in t.traverse(self_before=True)]
self.assertEqual(levels, [2,1,1,0,0,0,1,0,0])
def test_attrToList(self):
"""RangeNode attrToList should return correct list of attr"""
t = self.t3
t.assignIds()
t.assignLevelsFromRoot()
#make sure nodes are in the order we expect
self.assertEqual([n.Id for n in t.traverse(self_before=True)],
[8,6,5,0,1,2,7,3,4])
#by default, should return list containing all nodes
obs = t.attrToList('Level')
self.assertEqual(obs, [3,3,2,2,2,2,1,1,0])
#should be able to do leaves only if specified
obs = t.attrToList('Level', leaves_only=True)
self.assertEqual(obs, [3,3,2,2,2])
#should be able to specify larger size
obs=t.attrToList('Level', size=12)
self.assertEqual(obs, [3,3,2,2,2,2,1,1,0,None,None,None])
#should be able to set default
obs=t.attrToList('Level', default='x', size=12)
self.assertEqual(obs, [3,3,2,2,2,2,1,1,0,'x','x','x'])
def test_attrFromList(self):
"""RangeNode attrFromList should set values correctly"""
t = self.t3
t.assignIds()
#by default, should set all nodes from array
t.attrFromList('Level', [3,3,2,2,2,2,1,1,0])
self.assertEqual([n.Level for n in t.traverse(self_before=True)], \
[0,1,2,3,3,2,1,2,2])
#should also work if we choose to set only the leaves (rest should
#stay at default values)
t.Level = -1
t.propagateAttr('Level', overwrite=True)
t.attrFromList('Level', [3,3,2,2,2,2,1,1,0], leaves_only=True)
self.assertEqual([n.Level for n in t.traverse(self_before=True)], \
[-1,-1,-1,3,3,2,-1,2,2])
def test_toBreakpoints(self):
"""RangeNode toBreakpoints should give expected list"""
t = self.t2
t.assignIds()
self.assertEqual(t.toBreakpoints(), [4,2,1,0,3,6,5])
def test_fromBreakpoints(self):
"""RangeNode fromBreakpoints should have correct topology"""
breakpoints = [4,2,1,0,3,6,5]
t = RangeNode.fromBreakpoints(breakpoints)
#check number of leaves
self.assertEqual(len(list(t.traverse())), 8)
self.assertEqual(len(list(t.traverse(self_before=True))), 15)
#check that leaves were created in right order
self.assertEqual([i.Id for i in t.traverse()], range(8))
#check that whole toplogy is right wrt ids...
nodes = list(t.traverse(self_before=True))
obs = [i.Id for i in nodes]
exp = [8, 9, 11, 13, 0, 1, 2, 12, 3, 4, 10, 14, 5, 6, 7]
self.assertEqual(obs, exp)
#...and ranges
obs = [i.LeafRange for i in nodes]
exp = [(0,8),(0,5),(0,3),(0,2),(0,1),(1,2),(2,3),(3,5),(3,4),(4,5), \
(5,8),(5,7),(5,6),(6,7),(7,8)]
self.assertEqual(obs, exp)
def test_leafLcaDepths(self):
"""RangeNode leafLcaDepths should return expected depths"""
t = self.t3
result = t.leafLcaDepths()
self.assertEqual(result, array([[0,1,2,3,3],
[1,0,2,3,3],
[2,2,0,3,3],
[3,3,3,0,1],
[3,3,3,1,0]]))
def test_randomNode(self):
"""RandomNode should hit all nodes equally"""
t = self.t3
result = {}
for i in range(100):
ans = id(t.randomNode())
if ans not in result:
result[ans] = 0
result[ans] += 1
self.assertEqual(len(result), 9)
for node in t.traverse(self_before=True):
assert id(node) in result
def test_randomLeaf(self):
"""RandomLeaf should hit all leaf nodes equally"""
t = self.t3
result = {}
for i in range(100):
ans = id(t.randomLeaf())
if ans not in result:
result[ans] = 0
result[ans] += 1
self.assertEqual(len(result), 5)
for node in t.traverse():
assert id(node) in result
def test_randomNodeWithNLeaves(self):
"""RandomNodeWithNLeaves should return node with correct # leaves"""
t = self.t3
#check that only the root gets selected with 5 leaves
result = {}
for i in range(20):
ans = id(t.randomNodeWithNLeaves(5))
if ans not in result:
result[ans] = 0
result[ans] += 1
self.assertEqual(len(result), 1)
assert id(t) in result
#check that nothing has 6 or 4 (for this tree) leaves
self.assertRaises(KeyError, t.randomNodeWithNLeaves, 6)
self.assertRaises(KeyError, t.randomNodeWithNLeaves, 4)
#check that it works with fewer than 5 leaves
#SINGLE LEAF:
result = {}
for i in range(40):
ans = id(t.randomNodeWithNLeaves(1))
if ans not in result:
result[ans] = 0
result[ans] += 1
self.assertEqual(len(result), 5)
self.assertEqual(sum(result.values()), 40)
#TWO LEAVES:
result = {}
for i in range(20):
ans = id(t.randomNodeWithNLeaves(2))
if ans not in result:
result[ans] = 0
result[ans] += 1
self.assertEqual(len(result), 2)
self.assertEqual(sum(result.values()), 20)
#THREE LEAVES:
result = {}
for i in range(20):
ans = id(t.randomNodeWithNLeaves(3))
if ans not in result:
result[ans] = 0
result[ans] += 1
self.assertEqual(len(result), 1)
self.assertEqual(sum(result.values()), 20)
def test_randomNodeAtLevel(self):
"""RangeNode randomNodeAtLevel should return random node at correct level"""
t = self.t3
#LEAVES:
result = {}
for i in range(40):
ans = id(t.randomNodeAtLevel(0))
if ans not in result:
result[ans] = 0
result[ans] += 1
self.assertEqual(len(result), 5)
self.assertEqual(sum(result.values()), 40)
#BACK ONE LEVEL:
result = {}
for i in range(20):
ans = id(t.randomNodeAtLevel(1))
if ans not in result:
result[ans] = 0
result[ans] += 1
self.assertEqual(len(result), 2)
self.assertEqual(sum(result.values()), 20)
#BACK TWO LEVELS:
result = {}
for i in range(20):
ans = id(t.randomNodeAtLevel(2))
if ans not in result:
result[ans] = 0
result[ans] += 1
self.assertEqual(len(result), 1)
self.assertEqual(sum(result.values()), 20)
#BACK THREE LEVELS (to root):
result = {}
for i in range(20):
ans = id(t.randomNodeAtLevel(3))
if ans not in result:
result[ans] = 0
result[ans] += 1
self.assertEqual(len(result), 1)
self.assertEqual(sum(result.values()), 20)
self.assertEqual(result.keys()[0], id(t))
def test_outgroupLast(self):
"""RangeNode outgroupLast should reorder nodes to put outgroup last"""
t = self.t3
a, b, c, d, e = t.traverse()
self.assertEqual(t.outgroupLast(c,a,b), (a, b, c))
self.assertEqual(t.outgroupLast(c,b,a), (b, a, c))
self.assertEqual(t.outgroupLast(b,d,a), (b, a, d))
self.assertEqual(t.outgroupLast(c,d,e), (d, e, c))
self.assertEqual(t.outgroupLast(a,d,e), (d, e, a))
self.assertEqual(t.outgroupLast(a,d,b), (a, b, d))
#check that it works if we suppress the cache
self.assertEqual(t.outgroupLast(c,a,b, False), (a, b, c))
self.assertEqual(t.outgroupLast(c,b,a, False), (b, a, c))
self.assertEqual(t.outgroupLast(b,d,a, False), (b, a, d))
self.assertEqual(t.outgroupLast(c,d,e, False), (d, e, c))
self.assertEqual(t.outgroupLast(a,d,e, False), (d, e, a))
self.assertEqual(t.outgroupLast(a,d,b, False), (a, b, d))
def test_filter(self):
"""RangeNode filter should keep or omit selected nodes."""
t_orig = self.t2
t = deepcopy(t_orig)
idx = t.indexByAttr('Name')
to_keep = map(idx.__getitem__, 'abch')
curr_leaves = list(t.traverse())
t.filter(to_keep)
curr_leaves = list(t.traverse())
for i in to_keep:
assert i in curr_leaves
for i in map(idx.__getitem__, 'defg'):
assert i not in curr_leaves
#note that it collapses one-child nodes
self.assertEqual(str(t), '(((a,b),c),h)')
#test same thing but omitting
t = deepcopy(t_orig)
idx = t.indexByAttr('Name')
to_omit = map(idx.__getitem__, 'abch')
t.filter(to_omit, keep=False)
curr_leaves = list(t.traverse())
for i in to_omit:
assert i not in curr_leaves
for i in map(idx.__getitem__, 'defg'):
assert i in curr_leaves
#note that it collapses one-child nodes
self.assertEqual(str(t), '((d,e),(f,g))')
#test that it works with internal nodes
t = deepcopy(t_orig)
idx = t.indexByAttr('Name')
to_omit = [idx['a'].Parent.Parent]
t.filter(to_omit, keep=False)
self.assertEqual(str(t), '((d,e),((f,g),h))')
#test that it adds branch lengths
t = deepcopy(t_orig)
idx = t.indexByAttr('Name')
for i in t.traverse(self_after=True):
i.BranchLength = 1
to_omit = map(idx.__getitem__, 'abdefg')
t.filter(to_omit, keep=False)
self.assertEqual(str(t), '(c,h)')
#test that it got rid of the temporary '_selected' attribute
for node in t.traverse(self_before=True):
assert not hasattr(node, '_selected')
#if nothing valid in to_keep, should return empty tree
t = deepcopy(t_orig)
idx = t.indexByAttr('Name')
to_keep = []
t.filter(to_keep, keep=True)
curr_leaves = list(t.traverse())
assert len(curr_leaves), 0
#if nothing valid in to_keep, should return empty tree
t = deepcopy(t_orig)
idx = t.indexByAttr('Name')
to_keep = list('abcde') #note: just labels, not nodes
t.filter(to_keep, keep=True)
curr_leaves = list(t.traverse())
assert len(curr_leaves), 0
def test_addChildren(self):
"""RangeNode add_children should add specified # children to list"""
t = RangeNode()
t2 = RangeNode(Parent=t)
t.addChildren(5)
self.assertEqual(len(t.Children), 6)
assert t.Children[0] is t2
for c in t.Children:
assert c.Parent is t
class OldPhyloNodeTests(TestCase):
"""Tests of the PhyloNode class -- these are all now methods of RangeNode."""
def setUp(self):
"""Make a couple of standard trees"""
self.t1 = DndParser('((a,(b,c)),(d,e))', RangeNode)
#selt.t1 indices: ((0,(1,2)5)6,(3,4)7)8
def test_makeIdIndex(self):
"""RangeNode makeIdIndex should assign ids to every node"""
self.t1.makeIdIndex()
result = self.t1.IdIndex
nodes = list(self.t1.traverse(self_before=True))
#check we got an entry for each node
self.assertEqual(len(result), len(nodes))
#check the ids are in the result
for i in nodes:
assert hasattr(i, 'Id')
assert i.Id in result
def test_assignQ_single_passed(self):
"""RangeNode assignQ should propagate single Q param down tree"""
#should work if Q explicitly passed
t = self.t1
Q = ['a']
t.assignQ(Q)
for node in t.traverse(self_before=True):
assert node.Q is Q
def test_assignQ_single_set(self):
"""RangeNode assignQ should propagate single Q if set"""
t = self.t1
Q = ['a']
assert not hasattr(t, 'Q')
t.Q = Q
t.assignQ()
for node in t.traverse(self_before=True):
assert node.Q is Q
def test_assignQ_single_overwrite(self):
"""RangeNode assignQ should overwrite root Q if new Q passed"""
t = self.t1
Q = ['a']
Q2 = ['b']
t.Q = Q
t.assignQ(Q2)
for node in t.traverse(self_before=True):
assert node.Q is Q2
assert not node.Q is Q
def test_assignQ_multiple(self):
"""RangeNode assignQ should propagate multiple Qs"""
t = self.t1
Q1 = ['a']
Q2 = ['b']
Q3 = ['c']
t.makeIdIndex()
t.IdIndex[7].Q = Q1
t.IdIndex[5].Q = Q2
t.assignQ(Q3)
result = [i.Q for i in t.traverse(self_after=True)]
assert t.Q is Q3
self.assertEqual(result, [Q3,Q2,Q2,Q2,Q3,Q1,Q1,Q1,Q3])
def test_assignQ_multiple_overwrite(self):
"""RangeNode assignQ should allow overwrite"""
t = self.t1
Q1 = ['a']
Q2 = ['b']
Q3 = ['c']
t.makeIdIndex()
t.IdIndex[7].Q = Q1
t.IdIndex[5].Q = Q2
t.assignQ(Q3, overwrite=True)
for i in t.traverse(self_after=True):
assert i.Q is Q3
def test_assignQ_special(self):
"""RangeNode assignQ should work with special Qs"""
t = self.t1
Q1 = 'a'
Q2 = 'b'
Q3 = 'c'
t.makeIdIndex()
special = {7:Q1, 1:Q2}
#won't work if no Q at root
self.assertRaises(ValueError, t.assignQ, special_qs=special)
t.assignQ(Q3, special_qs=special)
result = [i.Q for i in t.traverse(self_after=True)]
self.assertEqual(result, ['c','b','c','c','c','a','a','a','c'])
def test_assignLength(self):
"""RangeNode assignLength should set branch length"""
t = self.t1
t.assignLength(0.3)
for i in t.traverse(self_before=True):
self.assertEqual(i.Length, 0.3)
def test_evolve(self):
"""RangeNode evolve should work on a starting vector"""
t = self.t1
t.Q = Rates.random(DnaPairs)
t.assignQ()
t.assignLength(0.1)
t.assignP()
start = array([1,0,2,1,0,0,2,1,2,0,1,2,1,0,2,0,0,3,0,2,1,0,3,1,0,2,0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,3])
t.evolve(start)
for i in t.traverse():
self.assertEqual(len(i.Sequence), len(start))
self.assertNotEqual(i.Sequence, start)
#WARNING: Doesn't test base freqs etc. at this point, but those aren't
#really evolve()'s responsibity (tested as self.P.mutate(seq) once
#P is set, which we've already demonstrated works.)
def test_assignPs(self):
"""RangeNode assignPs should assign multiple scaled P matrices"""
t = self.t1
for i in t.traverse(self_before=True):
i.Length = random() * 0.5 #range 0 to 0.5
t.Q = Rates.random(DnaPairs)
t.assignQ()
t.assignPs([1, 0.5, 0.25])
t.assignIds()
for node in t.traverse(self_after=True):
if node.Parent is not None:
self.assertEqual(len(node.Ps), 3)
self.assertFloatEqual(average(1-diag(node.Ps[0]._data), axis=0), \
node.Length)
self.assertFloatEqual(average(1-diag(node.Ps[1]._data), axis=0), \
0.5*node.Length)
self.assertFloatEqual(average(1-diag(node.Ps[2]._data), axis=0), \
0.25*node.Length)
def test_evolveSeqs(self):
"""PhlyoNode evolveSeqs should evolve multiple sequences"""
t = self.t1
for i in t.traverse(self_before=True):
i.Length = 0.5
t.Q = Rates.random(DnaPairs)
t.assignQ()
t.assignPs([1, 1, 0.1])
t.assignIds()
orig_seqs = [array(i) for i in [randint(0,4,200), randint(0,4,200), \
randint(0,4,200)]]
t.evolveSeqs(orig_seqs)
for node in t.traverse(): #only look at leaves
if node.Parent is not None:
self.assertEqual(len(node.Sequences), 3)
for orig, new in zip(orig_seqs, node.Sequences):
self.assertEqual(len(orig), len(new))
self.assertNotEqual(orig, new)
assert sum(orig_seqs[1]!=node.Sequences[1]) > \
sum(orig_seqs[2]!=node.Sequences[2])
if __name__ == "__main__":
main()
|