def test_random_tree(self): t5 = trees.random_tree(5) t10 = trees.random_tree(10) self.assertIsInstance(t10, trees.Tree) self.assertIsInstance(t5, trees.Tree) self.assertEqual(len(trees.get_list_of_tree_nodes(t5)), 9) self.assertEqual(len(trees.get_list_of_tree_nodes(t10)), 19) nodes = trees.get_list_of_tree_nodes(t5) self.assertTrue(all(isinstance(node.length, float) for node in nodes))
def test_compile_histories(self): t10 = evolve.evolve_tree(models.Sequence(self.old_sequence), taxa=10, t=0.1, omega=1.1, kappa=1.5) histories = evolve.compile_histories(t10) self.assertIsInstance(histories, dict) for i in trees.get_list_of_tree_nodes(t10): self.assertIn(i.id, histories)
def compile_histories(tree): """ Args: tree: Tree instance with models.Sequence as value """ histories = {} nodes = trees.get_list_of_tree_nodes(tree) for t in nodes: histories[t.id] = t.value.history return histories
def benchmark_evolve_tree(): with open('../../data/split/Seq2_Sus', 'r') as f: old_sequence = f.read()[10:].replace('\n', '').lower() old_sequence = models.Sequence(old_sequence) for i in [3, 6, 9]: start_time = time.time() # new_sequences, mutations = evolve.evolve(old_sequence, taxa=10, log=True) tree = evolve.evolve_tree(old_sequence, taxa=i) nodes = len(trees.get_list_of_tree_nodes(tree)) print("taxa:{} (nodes={}), sec:{}".format(i, nodes, time.time() - start_time))
def evolve_tree(sequence, taxa=10, t=1e-2, omega=1.0, kappa=2.0, lmbda=1e-5, ti_td=0.1, codon_freq='F1x4', scale_q=True, **kwargs): """ Evolve a parent DNA sequence into a set of daughter sequences (taxa) by: 1. generating a random phylogenetic tree 2. intantiating a mutational model (e.g. Goldman-Yang-like by default) represented by Q-matrix, with indels 3. mutate sequence according to tree shape using model Args: sequence: a model.Sequence instance taxa: number of daughter sequences to evolve t: evolution time or branch length omega: dN/dS kappa: ratio of transition to transversion rates lmbda: probability of indel at codon ti_td: ratio of insertions to deletions codon_freq: codon frequency model, also know as equilibrium frequencies (default is F1x4) scale_q: scales Q so that the average rate of substitution at equilibrium equals 1. Branch lengths are thus expected number of nucleotide substitutions per codon. See Goldman (1994). Returns: tree instance populated with new sequence strings """ codon_freq = getattr(codon_frequencies, codon_freq)(sequence) q = models.goldman_Q(kappa=kappa, omega=omega, codon_freq=codon_freq, scale_q=scale_q, return_dict=False) tree = trees.random_tree(taxa) tree.value = sequence for node in trees.get_list_of_tree_nodes(tree)[1:]: node.value = evolve_sequence_with_q(node.parent.value, q, t=t, lmbda=lmbda, ti_td=ti_td) return tree
def evolve_tree(sequence, taxa=10, t=1e-2, omega=1.0, kappa=2.0, lmbda=1e-5, ti_td=0.1, codon_freq='F1x4', scale_q=True, **kwargs ): """ Evolve a parent DNA sequence into a set of daughter sequences (taxa) by: 1. generating a random phylogenetic tree 2. intantiating a mutational model (e.g. Goldman-Yang-like by default) represented by Q-matrix, with indels 3. mutate sequence according to tree shape using model Args: sequence: a model.Sequence instance taxa: number of daughter sequences to evolve t: evolution time or branch length omega: dN/dS kappa: ratio of transition to transversion rates lmbda: probability of indel at codon ti_td: ratio of insertions to deletions codon_freq: codon frequency model, also know as equilibrium frequencies (default is F1x4) scale_q: scales Q so that the average rate of substitution at equilibrium equals 1. Branch lengths are thus expected number of nucleotide substitutions per codon. See Goldman (1994). Returns: tree instance populated with new sequence strings """ codon_freq = getattr(codon_frequencies, codon_freq)(sequence) q = models.goldman_Q(kappa=kappa, omega=omega, codon_freq=codon_freq, scale_q=scale_q, return_dict=False) tree = trees.random_tree(taxa) tree.value = sequence for node in trees.get_list_of_tree_nodes(tree)[1:]: node.value = evolve_sequence_with_q(node.parent.value, q, t=t, lmbda=lmbda, ti_td=ti_td) return tree
def test_get_list_of_tree_nodes(self): t = trees.random_tree(10) tl = trees.get_list_of_tree_nodes(t) self.assertEqual(len(tl), 19)
def test_preorder_exec(self): t = trees.random_tree(10) tlist = trees.get_list_of_tree_nodes(t) self.assertTrue(all(i.get_value() is None for i in tlist)) trees.preorder_exec(t, function='set_value', arguments=['test']) self.assertTrue(all(i.get_value() == 'test' for i in tlist))
def test_get_two_random_orphans_returns_empty_list_if_less_than_two_orphans(self): t5 = trees.random_tree(5) nodes = trees.get_list_of_tree_nodes(t5) orphans = trees.get_two_random_orphans(nodes) self.assertEqual(orphans, [])