Exemplo n.º 1
0
    def testCandidates(self):
        tree = '((((HUMAN:0.006969,CHIMP:0.009727)Anc7:0.025291,BABOON:0.044568)Anc6:0.11,(MOUSE:0.072818,RAT:0.081244)Anc5:0.260342)Anc4:0.023260,((DOG:0.07,CAT:0.07)Anc3:0.087381,(PIG:0.06,COW:0.06)Anc2:0.104728)Anc1:0.04)Anc0;'
        mcTree = MultiCactusTree(NXNewick().parseString(tree, addImpliedRoots = False))
        mcTree.computeSubtreeRoots()
        og = GreedyOutgroup()
        og.importTree(mcTree)
        candidates = set(['HUMAN', 'CHIMP', 'RAT'])
        og.greedy(candidateSet=candidates, candidateChildFrac=0.5)
        assert og.ogMap['Anc1'][0][0] == 'Anc4'
        assert og.ogMap['Anc2'][0][0] == 'Anc4'
        assert og.ogMap['Anc3'][0][0] == 'Anc4'
        assert 'Anc4' not in og.ogMap
        assert og.ogMap['Anc5'][0][0] in ['HUMAN', 'CHIMP', 'Anc6', 'Anc7']
        assert og.ogMap['Anc6'][0][0] in ['Anc5', 'MOUSE', 'RAT']
        assert og.ogMap['Anc7'][0][0] in ['Anc5', 'MOUSE', 'RAT']

        og = GreedyOutgroup()
        og.importTree(mcTree)
        candidates = set(['HUMAN', 'CHIMP', 'RAT'])
        candidateFrac = 1
        og.greedy(candidateSet=candidates, candidateChildFrac=1.0)
        assert og.ogMap['Anc1'][0][0] == 'Anc7'
        assert og.ogMap['Anc2'][0][0] == 'Anc7'
        assert og.ogMap['Anc3'][0][0] == 'Anc7'
        assert 'Anc4' not in og.ogMap
        assert og.ogMap['Anc5'][0][0] in ['HUMAN', 'CHIMP', 'Anc7']
        assert og.ogMap['Anc6'][0][0] == 'RAT'
        assert og.ogMap['Anc7'][0][0] == 'RAT'
Exemplo n.º 2
0
 def testMultipleOutgroupsOnRandomTrees(self):
     for tree in self.mcTrees:
         og = GreedyOutgroup()
         og.importTree(tree)
         og.greedy(candidateChildFrac=0.5, maxNumOutgroups=3)
         # make sure all entries have <= 3 outgroups.
         assert all(map(lambda x: len(x) <= 3, og.ogMap.values()))
         # and for all entries, the closest must be first.
         assert all(map(lambda x: x == sorted(x, key=itemgetter(1)),
                        og.ogMap.values()))
Exemplo n.º 3
0
 def testHeightTable(self):
     """Make sure the height-table is calculated correctly."""
     og = GreedyOutgroup()
     og.importTree(self.borMcTree)
     htable = og.heightTable()
     self.assertEquals(htable[self.borMcTree.getNodeId('HUMAN')], 0)
     self.assertEquals(htable[self.borMcTree.getNodeId('PIG')], 0)
     self.assertEquals(htable[self.borMcTree.getNodeId('RAT')], 0)
     self.assertEquals(htable[self.borMcTree.getNodeId('Anc7')], 1)
     self.assertEquals(htable[self.borMcTree.getNodeId('Anc1')], 2)
     self.assertEquals(htable[self.borMcTree.getNodeId('Anc0')], 4)
Exemplo n.º 4
0
 def testHeightTable(self):
     """Make sure the height-table is calculated correctly."""
     og = GreedyOutgroup()
     og.importTree(self.borMcTree)
     htable = og.heightTable()
     self.assertEquals(htable[self.borMcTree.getNodeId('HUMAN')], 0)
     self.assertEquals(htable[self.borMcTree.getNodeId('PIG')], 0)
     self.assertEquals(htable[self.borMcTree.getNodeId('RAT')], 0)
     self.assertEquals(htable[self.borMcTree.getNodeId('Anc7')], 1)
     self.assertEquals(htable[self.borMcTree.getNodeId('Anc1')], 2)
     self.assertEquals(htable[self.borMcTree.getNodeId('Anc0')], 4)
Exemplo n.º 5
0
 def testJustLeaves(self):
     og = GreedyOutgroup()
     og.importTree(self.borMcTree)
     candidates = set([self.borMcTree.getName(x) for x in self.borMcTree.getLeaves()])
     og.greedy(candidateSet=candidates, candidateChildFrac=2.)
     assert og.ogMap['Anc1'][0][0] == 'HUMAN'
     assert og.ogMap['Anc2'][0][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc3'][0][0] in ['PIG', 'COW']
     assert og.ogMap['Anc4'][0][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc5'][0][0] == 'HUMAN'
     assert og.ogMap['Anc6'][0][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc7'][0][0] == 'BABOON'
def fillInOutgroups(mcProj, outgroupNames, config, alignmentRootId):
    """
    Determines the outgroups for a MultiCactusProject using the strategy from the config.
    """
    mcProj.outgroup = None
    if config.getOutgroupStrategy() == 'greedy':
        # use the provided outgroup candidates, or use all outgroups
        # as candidates if none are given
        mcProj.outgroup = GreedyOutgroup()
        mcProj.outgroup.importTree(mcProj.mcTree, alignmentRootId)
        mcProj.outgroup.greedy(threshold=config.getOutgroupThreshold(),
                               candidateSet=outgroupNames,
                               candidateChildFrac=config.getOutgroupAncestorQualityFraction(),
                               maxNumOutgroups=config.getMaxNumOutgroups())
    elif config.getOutgroupStrategy() == 'greedyLeaves':
        # use all leaves as outgroups, unless outgroup candidates are given
        mcProj.outgroup = GreedyOutgroup()
        mcProj.outgroup.importTree(mcProj.mcTree, alignmentRootId)
        mcProj.outgroup.greedy(threshold=config.getOutgroupThreshold(),
                               candidateSet=outgroupNames,
                               candidateChildFrac=2.0,
                               maxNumOutgroups=config.getMaxNumOutgroups())
    elif config.getOutgroupStrategy() == 'greedyPreference':
        # prefer the provided outgroup candidates, if any, but use
        # other nodes as "filler" if we can't find enough.
        mcProj.outgroup = GreedyOutgroup()
        mcProj.outgroup.importTree(mcProj.mcTree, alignmentRootId)
        mcProj.outgroup.greedy(threshold=config.getOutgroupThreshold(),
                               candidateSet=outgroupNames,
                               candidateChildFrac=config.getOutgroupAncestorQualityFraction(),
                               maxNumOutgroups=config.getMaxNumOutgroups())
        mcProj.outgroup.greedy(threshold=config.getOutgroupThreshold(),
                               candidateSet=None,
                               candidateChildFrac=config.getOutgroupAncestorQualityFraction(),
                               maxNumOutgroups=config.getMaxNumOutgroups())
    elif config.getOutgroupStrategy() == 'dynamic':
        # dynamic programming algorithm that exactly optimizes probability
        # that base in target node aligns to at least one base in the
        # outgroup set.  Caveats are that it only returns leaves, and
        # the model used for optimization is super naive. Still, it does
        # some things better than greedy approaches such as properly account
        # for phylogenetic redundancy, as well as try to factor assembly
        # size/quality automatically.
        mcProj.outgroup = DynamicOutgroup()
        mcProj.outgroup.importTree(mcProj.mcTree, mcProj.inputSequenceMap, alignmentRootId,
                                   candidateSet=outgroupNames)
        mcProj.outgroup.compute(maxNumOutgroups=config.getMaxNumOutgroups())
    elif config.getOutgroupStrategy() != 'none':
        raise RuntimeError("Could not understand outgroup strategy %s" % config.getOutgroupStrategy())
Exemplo n.º 7
0
    def testGeneralConstrainedBetterThanLeaves(self):
        for tree in self.mcTrees:
            og1 = GreedyOutgroup()
            og1.importTree(tree)
            candidates = set([tree.getName(x) for x in tree.getLeaves()])
            og1.greedy(candidateSet=candidates, candidateChildFrac=2.)
            og2 = GreedyOutgroup()
            og2.importTree(tree)
            og2.greedy(candidateSet=None, threshold=2)

            for i in og1.ogMap:
                assert i in og2.ogMap
                dist1 = og1.ogMap[i][0][1]
                dist2 = og2.ogMap[i][0][1]
                assert dist2 <= dist1
Exemplo n.º 8
0
 def testJustLeaves(self):
     tree = '((((HUMAN:0.006969,CHIMP:0.009727)Anc7:0.025291,BABOON:0.044568)Anc6:0.11,(MOUSE:0.072818,RAT:0.081244)Anc5:0.260342)Anc4:0.023260,((DOG:0.07,CAT:0.07)Anc3:0.087381,(PIG:0.06,COW:0.06)Anc2:0.104728)Anc1:0.04)Anc0;'
     mcTree = MultiCactusTree(NXNewick().parseString(tree, addImpliedRoots = False))
     mcTree.computeSubtreeRoots()
     og = GreedyOutgroup()
     og.importTree(mcTree)
     candidates = set([mcTree.getName(x) for x in mcTree.getLeaves()])
     og.greedy(candidateSet=candidates, candidateChildFrac=2.)
     assert og.ogMap['Anc1'][0][0] == 'HUMAN'
     assert og.ogMap['Anc2'][0][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc3'][0][0] in ['PIG', 'COW']
     assert og.ogMap['Anc4'][0][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc5'][0][0] == 'HUMAN'
     assert og.ogMap['Anc6'][0][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc7'][0][0] == 'BABOON'
Exemplo n.º 9
0
 def testNoOutgroupIsADescendantOfAnother(self):
     """No two outgroups should be on the same path to the root."""
     for tree in self.mcTrees:
         tree.nameUnlabeledInternalNodes()
         og = GreedyOutgroup()
         og.importTree(tree)
         og.greedy(maxNumOutgroups=3)
         for source in og.ogMap:
             for (sink1, _) in og.ogMap[source]:
                 for (sink2, _) in og.ogMap[source]:
                     if sink1 != sink2:
                         sink1Id = tree.nameToId[sink1]
                         sink2Id = tree.nameToId[sink2]
                         assert sink1Id not in tree.postOrderTraversal(sink2Id)
                         assert sink2Id not in tree.postOrderTraversal(sink1Id)
Exemplo n.º 10
0
 def testMultipleOutgroups(self):
     og = GreedyOutgroup()
     og.importTree(self.borMcTree)
     og.greedy(candidateChildFrac=0.5, maxNumOutgroups=3)
     # make sure all entries have <= 3 outgroups.
     assert all(map(lambda x: len(x) <= 3, og.ogMap.values()))
     # and for all entries, the closest must be first.
     assert all(map(lambda x: x == sorted(x, key=itemgetter(1)),
                    og.ogMap.values()))
     # ordering is important!
     assert map(itemgetter(0), og.ogMap['Anc4']) == ['Anc1']
     assert map(itemgetter(0), og.ogMap['Anc7']) == ['BABOON', 'Anc1',
                                                     'Anc5']
     # We avoid cycles, and choose post-order first, so this only
     # uses leaves.
     assert map(itemgetter(0), og.ogMap['Anc1']) == ['HUMAN', 'CHIMP',
                                                     'BABOON']
Exemplo n.º 11
0
 def testMultipleOutgroupsJustLeaves(self):
     og = GreedyOutgroup()
     og.importTree(self.borMcTree)
     candidates = set([self.borMcTree.getName(x) for x in self.borMcTree.getLeaves()])
     og.greedy(candidateSet=candidates, candidateChildFrac=2.,
               maxNumOutgroups=3)
     # make sure all entries have <= 3 outgroups.
     assert all(map(lambda x: len(x) <= 3, og.ogMap.values()))
     # and for all entries, the closest must be first.
     assert all(map(lambda x: x == sorted(x, key=itemgetter(1)),
                    og.ogMap.values()))
     # ordering is important!
     assert map(itemgetter(0), og.ogMap['Anc1']) == ['HUMAN', 'CHIMP',
                                                     'BABOON']
     assert og.ogMap['Anc7'][0][0] == 'BABOON'
     assert og.ogMap['Anc7'][1][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc7'][2][0] in ['CAT', 'DOG']
Exemplo n.º 12
0
 def testMultipleOutgroupsJustLeaves(self):
     tree = '((((HUMAN:0.006969,CHIMP:0.009727)Anc7:0.025291,BABOON:0.044568)Anc6:0.11,(MOUSE:0.072818,RAT:0.081244)Anc5:0.260342)Anc4:0.023260,((DOG:0.07,CAT:0.07)Anc3:0.087381,(PIG:0.06,COW:0.06)Anc2:0.104728)Anc1:0.04)Anc0;'
     mcTree = MultiCactusTree(NXNewick().parseString(tree, addImpliedRoots = False))
     mcTree.computeSubtreeRoots()
     og = GreedyOutgroup()
     og.importTree(mcTree)
     candidates = set([mcTree.getName(x) for x in mcTree.getLeaves()])
     og.greedy(candidateSet=candidates, candidateChildFrac=2.,
               maxNumOutgroups=3)
     # make sure all entries have <= 3 outgroups.
     assert all(map(lambda x: len(x) <= 3, og.ogMap.values()))
     # and for all entries, the closest must be first.
     assert all(map(lambda x: x == sorted(x, key=itemgetter(1)),
                    og.ogMap.values()))
     # ordering is important!
     assert map(itemgetter(0), og.ogMap['Anc1']) == ['HUMAN', 'CHIMP',
                                                     'BABOON']
     assert og.ogMap['Anc7'][0][0] == 'BABOON'
     assert og.ogMap['Anc7'][1][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc7'][2][0] in ['CAT', 'DOG']
Exemplo n.º 13
0
 def testMultipleOutgroups(self):
     tree = '((((HUMAN:0.006969,CHIMP:0.009727)Anc7:0.025291,BABOON:0.044568)Anc6:0.11,(MOUSE:0.072818,RAT:0.081244)Anc5:0.260342)Anc4:0.023260,((DOG:0.07,CAT:0.07)Anc3:0.087381,(PIG:0.06,COW:0.06)Anc2:0.104728)Anc1:0.04)Anc0;'
     mcTree = MultiCactusTree(NXNewick().parseString(tree, addImpliedRoots = False))
     mcTree.computeSubtreeRoots()
     og = GreedyOutgroup()
     og.importTree(mcTree)
     og.greedy(candidateChildFrac=0.5, maxNumOutgroups=3)
     # make sure all entries have <= 3 outgroups.
     assert all(map(lambda x: len(x) <= 3, og.ogMap.values()))
     # and for all entries, the closest must be first.
     assert all(map(lambda x: x == sorted(x, key=itemgetter(1)),
                    og.ogMap.values()))
     # ordering is important!
     assert map(itemgetter(0), og.ogMap['Anc4']) == ['Anc1']
     assert map(itemgetter(0), og.ogMap['Anc7']) == ['BABOON', 'Anc1',
                                                     'Anc5']
     # We avoid cycles, and choose post-order first, so this only
     # uses leaves.
     assert map(itemgetter(0), og.ogMap['Anc1']) == ['HUMAN', 'CHIMP',
                                                     'BABOON']
Exemplo n.º 14
0
 def testZeroThreshold(self):
     """A threshold of 0 should produce outgroup sets that cause no additional depth in the resulting schedule."""
     tree = self.backboneTree
     og = GreedyOutgroup()
     og.importTree(tree)
     og.greedy(candidateSet=set(['Homo_sapiens', 'Mus_musculus']),
               threshold=0,
               maxNumOutgroups=3,
               candidateChildFrac=0.75)
     og.greedy(threshold=0, maxNumOutgroups=3, candidateChildFrac=0.75)
     htable = og.heightTable()
     for node, outgroups in og.ogMap.items():
         for outgroup, _ in outgroups:
             # For the outgroup assignment to create no
             # additional dependencies, each outgroup must have
             # a height lower than the node it's outgroup to
             # (or be a leaf)
             self.assertTrue(htable[tree.getNodeId(outgroup)] < htable[tree.getNodeId(node)] \
                             or htable[tree.getNodeId(outgroup)] == 0)
Exemplo n.º 15
0
 def testZeroThreshold(self):
     """A threshold of 0 should produce outgroup sets that cause no additional depth in the resulting schedule."""
     tree = self.backboneTree
     og = GreedyOutgroup()
     og.importTree(tree)
     og.greedy(candidateSet=set(['Homo_sapiens', 'Mus_musculus']),threshold=0, maxNumOutgroups=3, candidateChildFrac=0.75)
     og.greedy(threshold=0, maxNumOutgroups=3, candidateChildFrac=0.75)
     htable = og.heightTable()
     for node, outgroups in og.ogMap.items():
         for outgroup, _ in outgroups:
             # For the outgroup assignment to create no
             # additional dependencies, each outgroup must have
             # a height lower than the node it's outgroup to
             # (or be a leaf)
             self.assertTrue(htable[tree.getNodeId(outgroup)] < htable[tree.getNodeId(node)] \
                             or htable[tree.getNodeId(outgroup)] == 0)
Exemplo n.º 16
0
    def testCandidates(self):
        og = GreedyOutgroup()
        og.importTree(self.borMcTree)
        candidates = set(['HUMAN', 'CHIMP', 'RAT'])
        og.greedy(candidateSet=candidates, candidateChildFrac=0.5)
        assert og.ogMap['Anc1'][0][0] == 'Anc4'
        assert og.ogMap['Anc2'][0][0] == 'Anc4'
        assert og.ogMap['Anc3'][0][0] == 'Anc4'
        assert 'Anc4' not in og.ogMap
        assert og.ogMap['Anc5'][0][0] in ['HUMAN', 'CHIMP', 'Anc6', 'Anc7']
        assert og.ogMap['Anc6'][0][0] in ['Anc5', 'MOUSE', 'RAT']
        assert og.ogMap['Anc7'][0][0] in ['Anc5', 'MOUSE', 'RAT']

        og = GreedyOutgroup()
        og.importTree(self.borMcTree)
        candidates = set(['HUMAN', 'CHIMP', 'RAT'])
        og.greedy(candidateSet=candidates, candidateChildFrac=1.0)
        assert og.ogMap['Anc1'][0][0] == 'Anc7'
        assert og.ogMap['Anc2'][0][0] == 'Anc7'
        assert og.ogMap['Anc3'][0][0] == 'Anc7'
        assert 'Anc4' not in og.ogMap
        assert og.ogMap['Anc5'][0][0] in ['HUMAN', 'CHIMP', 'Anc7']
        assert og.ogMap['Anc6'][0][0] == 'RAT'
        assert og.ogMap['Anc7'][0][0] == 'RAT'
Exemplo n.º 17
0
 def testMultipleOutgroupsOnRandomTrees(self):
     for tree in self.mcTrees:
         og = GreedyOutgroup()
         og.importTree(tree)
         og.greedy(candidateChildFrac=0.5, maxNumOutgroups=3)
         # make sure all entries have <= 3 outgroups.
         assert all(map(lambda x: len(x) <= 3, og.ogMap.values()))
         # and for all entries, the closest must be first.
         assert all(
             map(lambda x: x == sorted(x, key=itemgetter(1)),
                 og.ogMap.values()))
Exemplo n.º 18
0
 def testJustLeaves(self):
     og = GreedyOutgroup()
     og.importTree(self.borMcTree)
     candidates = set(
         [self.borMcTree.getName(x) for x in self.borMcTree.getLeaves()])
     og.greedy(candidateSet=candidates, candidateChildFrac=2.)
     assert og.ogMap['Anc1'][0][0] == 'HUMAN'
     assert og.ogMap['Anc2'][0][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc3'][0][0] in ['PIG', 'COW']
     assert og.ogMap['Anc4'][0][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc5'][0][0] == 'HUMAN'
     assert og.ogMap['Anc6'][0][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc7'][0][0] == 'BABOON'
Exemplo n.º 19
0
 def testNoOutgroupIsADescendantOfAnother(self):
     """No two outgroups should be on the same path to the root."""
     for tree in self.mcTrees:
         tree.nameUnlabeledInternalNodes()
         og = GreedyOutgroup()
         og.importTree(tree)
         og.greedy(maxNumOutgroups=3)
         for source in og.ogMap:
             for (sink1, _) in og.ogMap[source]:
                 for (sink2, _) in og.ogMap[source]:
                     if sink1 != sink2:
                         sink1Id = tree.nameToId[sink1]
                         sink2Id = tree.nameToId[sink2]
                         assert sink1Id not in tree.postOrderTraversal(
                             sink2Id)
                         assert sink2Id not in tree.postOrderTraversal(
                             sink1Id)
Exemplo n.º 20
0
 def testMultipleOutgroups(self):
     og = GreedyOutgroup()
     og.importTree(self.borMcTree)
     og.greedy(candidateChildFrac=0.5, maxNumOutgroups=3)
     # make sure all entries have <= 3 outgroups.
     assert all(map(lambda x: len(x) <= 3, og.ogMap.values()))
     # and for all entries, the closest must be first.
     assert all(
         map(lambda x: x == sorted(x, key=itemgetter(1)),
             og.ogMap.values()))
     # ordering is important!
     assert map(itemgetter(0), og.ogMap['Anc4']) == ['Anc1']
     assert map(itemgetter(0),
                og.ogMap['Anc7']) == ['BABOON', 'Anc1', 'Anc5']
     # We avoid cycles, and choose post-order first, so this only
     # uses leaves.
     assert map(itemgetter(0),
                og.ogMap['Anc1']) == ['HUMAN', 'CHIMP', 'BABOON']
Exemplo n.º 21
0
 def testMultipleOutgroupsJustLeaves(self):
     og = GreedyOutgroup()
     og.importTree(self.borMcTree)
     candidates = set(
         [self.borMcTree.getName(x) for x in self.borMcTree.getLeaves()])
     og.greedy(candidateSet=candidates,
               candidateChildFrac=2.,
               maxNumOutgroups=3)
     # make sure all entries have <= 3 outgroups.
     assert all(map(lambda x: len(x) <= 3, og.ogMap.values()))
     # and for all entries, the closest must be first.
     assert all(
         map(lambda x: x == sorted(x, key=itemgetter(1)),
             og.ogMap.values()))
     # ordering is important!
     assert map(itemgetter(0),
                og.ogMap['Anc1']) == ['HUMAN', 'CHIMP', 'BABOON']
     assert og.ogMap['Anc7'][0][0] == 'BABOON'
     assert og.ogMap['Anc7'][1][0] in ['CAT', 'DOG']
     assert og.ogMap['Anc7'][2][0] in ['CAT', 'DOG']
Exemplo n.º 22
0
    def testCandidates(self):
        og = GreedyOutgroup()
        og.importTree(self.borMcTree)
        candidates = set(['HUMAN', 'CHIMP', 'RAT'])
        og.greedy(candidateSet=candidates, candidateChildFrac=0.5)
        assert og.ogMap['Anc1'][0][0] == 'Anc4'
        assert og.ogMap['Anc2'][0][0] == 'Anc4'
        assert og.ogMap['Anc3'][0][0] == 'Anc4'
        assert 'Anc4' not in og.ogMap
        assert og.ogMap['Anc5'][0][0] in ['HUMAN', 'CHIMP', 'Anc6', 'Anc7']
        assert og.ogMap['Anc6'][0][0] in ['Anc5', 'MOUSE', 'RAT']
        assert og.ogMap['Anc7'][0][0] in ['Anc5', 'MOUSE', 'RAT']

        og = GreedyOutgroup()
        og.importTree(self.borMcTree)
        candidates = set(['HUMAN', 'CHIMP', 'RAT'])
        og.greedy(candidateSet=candidates, candidateChildFrac=1.0)
        assert og.ogMap['Anc1'][0][0] == 'Anc7'
        assert og.ogMap['Anc2'][0][0] == 'Anc7'
        assert og.ogMap['Anc3'][0][0] == 'Anc7'
        assert 'Anc4' not in og.ogMap
        assert og.ogMap['Anc5'][0][0] in ['HUMAN', 'CHIMP', 'Anc7']
        assert og.ogMap['Anc6'][0][0] == 'RAT'
        assert og.ogMap['Anc7'][0][0] == 'RAT'
Exemplo n.º 23
0
 def testPreferredCandidateSets(self):
     """Test that running greedy() multiple times with different candidate
     sets will behave properly, i.e. keep all the existing outgroup
     assignments and fill in more on the second run."""
     for tree in self.mcTrees:
         ogOnce = GreedyOutgroup()
         ogOnce.importTree(tree)
         nodes = [j for j in tree.postOrderTraversal()]
         candidateSet = set([
             tree.getName(i)
             for i in random.sample(nodes, min(20, len(nodes)))
         ])
         ogOnce.greedy(candidateSet=candidateSet, maxNumOutgroups=3)
         ogTwice = GreedyOutgroup()
         ogTwice.importTree(tree)
         ogTwice.greedy(candidateSet=candidateSet, maxNumOutgroups=3)
         ogTwice.greedy(maxNumOutgroups=3)
         # make sure all entries have <= 3 outgroups.
         assert all(map(lambda x: len(x) <= 3, ogTwice.ogMap.values()))
         # and for all entries, the closest must be first.
         assert all(
             map(lambda x: x == sorted(x, key=itemgetter(1)),
                 ogTwice.ogMap.values()))
         for node in ogTwice.ogMap:
             if node in ogOnce.ogMap:
                 # the ogMap entry in ogOnce should be a subset of the ogMap entry for ogTwice
                 oneRunOutgroups = ogOnce.ogMap[node]
                 twoRunOutgroups = ogTwice.ogMap[node]
                 assert len(twoRunOutgroups) >= len(oneRunOutgroups)
                 for i in oneRunOutgroups:
                     assert i in twoRunOutgroups
Exemplo n.º 24
0
 def testMultipleIdenticalRunsProduceSameResult(self):
     """The code now allows for multiple greedy() calls with different
     candidate sets, so that some outgroups can be 'preferred' over
     others without being the only candidates.
     Check that running greedy() multiple times with the same
     parameters gives the same result as running it once.
     """
     for tree in self.mcTrees:
         ogOnce = GreedyOutgroup()
         ogOnce.importTree(tree)
         ogOnce.greedy(maxNumOutgroups=3)
         ogMultipleTimes = GreedyOutgroup()
         ogMultipleTimes.importTree(tree)
         ogMultipleTimes.greedy(maxNumOutgroups=3)
         ogMultipleTimes.greedy(maxNumOutgroups=3)
         ogMultipleTimes.greedy(maxNumOutgroups=3)
         # make sure all entries have <= 3 outgroups.
         assert all(
             map(lambda x: len(x) <= 3, ogMultipleTimes.ogMap.values()))
         # and for all entries, the closest must be first.
         assert all(
             map(lambda x: x == sorted(x, key=itemgetter(1)),
                 ogMultipleTimes.ogMap.values()))
         # Check that the maps are equal. Can't compare them
         # directly since python will convert them to ordered
         # association lists.
         assert len(ogOnce.ogMap) == len(ogMultipleTimes.ogMap)
         for i in ogOnce.ogMap:
             assert i in ogMultipleTimes.ogMap
             assert ogOnce.ogMap[i] == ogMultipleTimes.ogMap[i]
Exemplo n.º 25
0
 def testMultipleIdenticalRunsProduceSameResult(self):
     """The code now allows for multiple greedy() calls with different
     candidate sets, so that some outgroups can be 'preferred' over
     others without being the only candidates.
     Check that running greedy() multiple times with the same
     parameters gives the same result as running it once.
     """
     for tree in self.mcTrees:
         ogOnce = GreedyOutgroup()
         ogOnce.importTree(tree)
         ogOnce.greedy(maxNumOutgroups=3)
         ogMultipleTimes = GreedyOutgroup()
         ogMultipleTimes.importTree(tree)
         ogMultipleTimes.greedy(maxNumOutgroups=3)
         ogMultipleTimes.greedy(maxNumOutgroups=3)
         ogMultipleTimes.greedy(maxNumOutgroups=3)
         # make sure all entries have <= 3 outgroups.
         assert all(map(lambda x: len(x) <= 3, ogMultipleTimes.ogMap.values()))
         # and for all entries, the closest must be first.
         assert all(map(lambda x: x == sorted(x, key=itemgetter(1)),
                        ogMultipleTimes.ogMap.values()))
         # Check that the maps are equal. Can't compare them
         # directly since python will convert them to ordered
         # association lists.
         assert len(ogOnce.ogMap) == len(ogMultipleTimes.ogMap)
         for i in ogOnce.ogMap:
             assert i in ogMultipleTimes.ogMap
             assert ogOnce.ogMap[i] == ogMultipleTimes.ogMap[i]
Exemplo n.º 26
0
 def testPreferredCandidateSets(self):
     """Test that running greedy() multiple times with different candidate
     sets will behave properly, i.e. keep all the existing outgroup
     assignments and fill in more on the second run."""
     for tree in self.mcTrees:
         ogOnce = GreedyOutgroup()
         ogOnce.importTree(tree)
         nodes = [j for j in tree.postOrderTraversal()]
         candidateSet = set([tree.getName(i) for i in random.sample(nodes, min(20, len(nodes)))])
         ogOnce.greedy(candidateSet=candidateSet, maxNumOutgroups=3)
         ogTwice = GreedyOutgroup()
         ogTwice.importTree(tree)
         ogTwice.greedy(candidateSet=candidateSet, maxNumOutgroups=3)
         ogTwice.greedy(maxNumOutgroups=3)
         # make sure all entries have <= 3 outgroups.
         assert all(map(lambda x: len(x) <= 3, ogTwice.ogMap.values()))
         # and for all entries, the closest must be first.
         assert all(map(lambda x: x == sorted(x, key=itemgetter(1)),
                        ogTwice.ogMap.values()))
         for node in ogTwice.ogMap:
             if node in ogOnce.ogMap:
                 # the ogMap entry in ogOnce should be a subset of the ogMap entry for ogTwice
                 oneRunOutgroups = ogOnce.ogMap[node]
                 twoRunOutgroups = ogTwice.ogMap[node]
                 assert len(twoRunOutgroups) >= len(oneRunOutgroups)
                 for i in oneRunOutgroups:
                     assert i in twoRunOutgroups
def createMCProject(tree, experiment, config, options):
    mcTree = MultiCactusTree(tree, config.getSubtreeSize())
    mcTree.nameUnlabeledInternalNodes(config.getDefaultInternalNodePrefix())
    mcTree.computeSubtreeRoots()
    mcProj = MultiCactusProject()
    mcProj.mcTree = mcTree
    mcProj.inputSequences = experiment.getSequences()[:]
    if config.getDoSelfAlignment():
        mcTree.addSelfEdges()
    for name in mcProj.mcTree.getSubtreeRootNames():
        expPath = "%s/%s/%s_experiment.xml" % (options.path, name, name)
        mcProj.expMap[name] = os.path.abspath(expPath)
    alignmentRootId = mcProj.mcTree.getRootId()
    if options.root is not None:
        try:
            alignmentRootId = mcProj.mcTree.getNodeId(options.root)
        except:
            raise RuntimeError("Specified root name %s not found in tree" %
                               options.root)
    mcProj.outgroup = None
    if config.getOutgroupStrategy() == 'greedy':
        # use the provided outgroup candidates, or use all outgroups
        # as candidates if none are given
        mcProj.outgroup = GreedyOutgroup()
        mcProj.outgroup.importTree(mcProj.mcTree, alignmentRootId)
        mcProj.outgroup.greedy(
            threshold=config.getOutgroupThreshold(),
            candidateSet=options.outgroupNames,
            candidateChildFrac=config.getOutgroupAncestorQualityFraction(),
            maxNumOutgroups=config.getMaxNumOutgroups())
    elif config.getOutgroupStrategy() == 'greedyLeaves':
        # use all leaves as outgroups, unless outgroup candidates are given
        mcProj.outgroup = GreedyOutgroup()
        mcProj.outgroup.importTree(mcProj.mcTree, alignmentRootId)
        ogSet = options.outgroupNames
        if ogSet is None:
            ogSet = set(
                [mcProj.mcTree.getName(x) for x in mcProj.mcTree.getLeaves()])
        mcProj.outgroup.greedy(threshold=config.getOutgroupThreshold(),
                               candidateSet=ogSet,
                               candidateChildFrac=2.0,
                               maxNumOutgroups=config.getMaxNumOutgroups())
    elif config.getOutgroupStrategy() == 'greedyPreference':
        # prefer the provided outgroup candidates, if any, but use
        # other nodes as "filler" if we can't find enough.
        mcProj.outgroup = GreedyOutgroup()
        mcProj.outgroup.importTree(mcProj.mcTree, alignmentRootId)
        mcProj.outgroup.greedy(
            threshold=config.getOutgroupThreshold(),
            candidateSet=options.outgroupNames,
            candidateChildFrac=config.getOutgroupAncestorQualityFraction(),
            maxNumOutgroups=config.getMaxNumOutgroups())
        mcProj.outgroup.greedy(
            threshold=config.getOutgroupThreshold(),
            candidateSet=None,
            candidateChildFrac=config.getOutgroupAncestorQualityFraction(),
            maxNumOutgroups=config.getMaxNumOutgroups())
    elif config.getOutgroupStrategy() == 'dynamic':
        # dynamic programming algorithm that exactly optimizes probability
        # that base in target node aligns to at least one base in the
        # outgroup set.  Caveats are that it only returns leaves, and
        # the model used for optimization is super naive. Still, it does
        # some things better than greedy approaches such as properly account
        # for phylogenetic redundancy, as well as try to factor assembly
        # size/quality automatically.
        mcProj.outgroup = DynamicOutgroup()
        mcProj.outgroup.importTree(mcProj.mcTree,
                                   mcProj.getInputSequenceMap(),
                                   alignmentRootId,
                                   candidateSet=options.outgroupNames)
        mcProj.outgroup.compute(maxNumOutgroups=config.getMaxNumOutgroups())
    elif config.getOutgroupStrategy() != 'none':
        raise RuntimeError("Could not understand outgroup strategy %s" %
                           config.getOutgroupStrategy())

    # if necessary, we reroot the tree at the specified alignment root id.  all leaf genomes
    # that are no longer in the tree, but still used as outgroups, are moved into special fields
    # so that we can remember to, say, get their paths for preprocessing.
    specifyAlignmentRoot(mcProj, alignmentRootId)
    return mcProj
Exemplo n.º 28
0
    def testGeneralConstrainedBetterThanLeaves(self):
        for tree in self.mcTrees:
            og1 = GreedyOutgroup()
            og1.importTree(tree)
            candidates = set([tree.getName(x) for x in tree.getLeaves()])
            og1.greedy(candidateSet=candidates, candidateChildFrac=2.)
            og2 = GreedyOutgroup()
            og2.importTree(tree)
            og2.greedy(candidateSet=None, threshold=2)

            for i in og1.ogMap:
                assert i in og2.ogMap
                dist1 = og1.ogMap[i][0][1]
                dist2 = og2.ogMap[i][0][1]
                assert dist2 <= dist1