def test_threshold_partition(self): jc = spectraltree.Jukes_Cantor() observations, taxa_meta = spectraltree.simulate_sequences( seq_len=1000, tree_model=self.reference_tree, seq_model=jc, mutation_rate=jc.p2t(0.95), rng=np.random.default_rng(321)) spectral_method = spectraltree.STDR(spectraltree.NeighborJoining, spectraltree.JC_similarity_matrix) tree_rec = spectral_method.deep_spectral_tree_reconstruction( observations, spectraltree.JC_similarity_matrix, taxa_metadata=taxa_meta, num_gaps=4, threshhold=35) RF, _ = spectraltree.compare_trees(tree_rec, self.reference_tree) self.assertEqual(RF, 0) tree_rec_b = spectral_method.deep_spectral_tree_reconstruction( observations, spectraltree.JC_similarity_matrix, taxa_metadata=taxa_meta, num_gaps=1, threshhold=35) RF_b, _ = spectraltree.compare_trees(tree_rec_b, self.reference_tree) self.assertEqual(RF_b, 0)
def test_jc(self): jc = spectraltree.Jukes_Cantor() observations, meta = spectraltree.simulate_sequences( seq_len=500, tree_model=self.reference_tree, seq_model=jc, mutation_rate=0.05, rng=np.random.default_rng(12345), alphabet='DNA') spectral_method = spectraltree.STDR(spectraltree.RAxML, spectraltree.JC_similarity_matrix) #spectral_method = spectraltree.STDR(spectraltree.NeighborJoining,spectraltree.JC_similarity_matrix) tree_rec = spectral_method.deep_spectral_tree_reconstruction( observations, spectraltree.JC_similarity_matrix, taxa_metadata=meta, threshhold=self.threshold, min_split=5, merge_method="least_square", verbose=False) self.assertTrue(spectraltree.topos_equal(self.reference_tree, tree_rec))
def test_jukes_cantor_similarity(self): observationsJC, metaJC = spectraltree.simulate_sequences( seq_len=400, tree_model=self.reference_tree, seq_model=spectraltree.Jukes_Cantor(), mutation_rate=0.1, rng=default_rng(345), alphabet="DNA") nj_jc = spectraltree.NeighborJoining(spectraltree.JC_similarity_matrix) self.assertTrue(spectraltree.topos_equal(self.reference_tree, nj_jc(observationsJC, metaJC)))
def test_jukes_cantor(self): observationsJC, metaJC = spectraltree.simulate_sequences( seq_len=500, tree_model=self.reference_tree, seq_model=spectraltree.Jukes_Cantor(), mutation_rate=0.1, rng=default_rng(234), alphabet="DNA") forrest = spectraltree.Forrest() self.assertTrue( spectraltree.topos_equal(self.reference_tree, forrest(observationsJC, metaJC)))
def test_jukes_cantor(self): # reference_tree = spectraltree.unrooted_birth_death_tree(num_taxa, birth_rate=1, rng=) # reference_tree = spectraltree.lopsided_tree(num_taxa) reference_tree = spectraltree.balanced_binary(8) jc = spectraltree.Jukes_Cantor(num_classes=2) observations, meta = spectraltree.simulate_sequences( seq_len=10_000, tree_model=reference_tree, seq_model=jc, mutation_rate=jc.p2t(0.98), rng=default_rng(678), alphabet="Binary") rg = spectraltree.RG(spectraltree.JC_distance_matrix) recoverd_tree = rg(observations, meta) print("(RF distance,F1 score):", spectraltree.compare_trees(reference_tree, recoverd_tree)) self.assertTrue(spectraltree.topos_equal( reference_tree, recoverd_tree)) # this doesn't work very well
def test_angle_least_square(self): # copied from test_deep_spectral_tree_reonstruction N = 1000 jc = spectraltree.Jukes_Cantor() mutation_rate = jc.p2t(0.95) num_itr = 2 #0 #reference_tree = spectraltree.balanced_binary(num_taxa) lopsided_reference_tree = spectraltree.lopsided_tree(128) # reference_tree = spectraltree.unrooted_birth_death_tree(num_taxa, birth_rate=1) # for x in reference_tree.preorder_edge_iter(): # x.length = 1 merging_method_list = ['least_square', 'angle'] RF = {'least_square': [], 'angle': []} F1 = {'least_square': [], 'angle': []} for merge_method in merging_method_list: for i in range(num_itr): observations, taxa_meta = spectraltree.simulate_sequences( seq_len=N, tree_model=lopsided_reference_tree, seq_model=jc, mutation_rate=mutation_rate, rng=np.random.default_rng(789)) spectral_method = spectraltree.STDR( spectraltree.NeighborJoining, spectraltree.JC_similarity_matrix) tree_rec = spectral_method.deep_spectral_tree_reconstruction( observations, spectraltree.JC_similarity_matrix, taxa_metadata=taxa_meta, threshhold=16, merge_method=merge_method) RF_i, F1_i = spectraltree.compare_trees( tree_rec, lopsided_reference_tree) RF[merge_method].append(RF_i) F1[merge_method].append(F1_i) self.assertEqual(np.mean(RF['angle']), 0) self.assertEqual(np.mean(RF['least_square']), 0)
import spectraltree def cherry_count_for_tree(tree): cherry_count = 0 for node in tree.leaf_nodes(): if node.parent_node.child_nodes()[0].is_leaf( ) and node.parent_node.child_nodes()[1].is_leaf(): cherry_count += 1 cherry_count = cherry_count / 2 return cherry_count N = 50 num_taxa = 64 jc = spectraltree.Jukes_Cantor() mutation_rate = [jc.p2t(0.95)] np.random.seed(0) reference_tree = spectraltree.unrooted_birth_death_tree(num_taxa, birth_rate=1) #reference_tree = spectraltree.lopsided_tree(num_taxa) #reference_tree = spectraltree.balanced_binary(num_taxa) for x in reference_tree.preorder_edge_iter(): x.length = 1 np.random.seed(0) cherry_count = cherry_count_for_tree(reference_tree) print("Orig Cherry count:", cherry_count) ############################################ observations, meta = spectraltree.simulate_sequences( N,
import spectraltree import spectraltree.compare_methods as compare_methods tree_list = [spectraltree.balanced_binary(32)] jc = spectraltree.Jukes_Cantor(num_classes=2) Ns = [100,200,300] mutation_rates = [jc.p2t(0.9)] snj = spectraltree.SpectralNeighborJoining(spectraltree.JC_similarity_matrix) nj = spectraltree.NeighborJoining(spectraltree.JC_similarity_matrix) RG = spectraltree.RG(spectraltree.JC_distance_matrix) CLRG = spectraltree.CLRG() methods = [nj,snj,RG,CLRG] num_reps = 1 results = compare_methods.experiment(tree_list = tree_list, sequence_model = jc, Ns = Ns, methods=methods, mutation_rates = mutation_rates, reps_per_tree=num_reps,savepath='balanced_binary_m_512.pkl',folder = './data/',overwrite=True) df = compare_methods.results2frame(results) print(df) a =1