Esempio n. 1
0
    def test_get_score(self):
        aln = AlignIO.read('TreeConstruction/msa.phy', 'phylip')
        tree = Phylo.read('./TreeConstruction/upgma.tre', 'newick')
        scorer = ParsimonyScorer()
        score = scorer.get_score(tree, aln)
        self.assertEqual(score, 2 + 1 + 2 + 2 + 1 + 1 + 1 + 3)

        alphabet = ['A', 'T', 'C', 'G']
        step_matrix = [[0], [2.5, 0], [2.5, 1, 0], [1, 2.5, 2.5, 0]]
        matrix = _Matrix(alphabet, step_matrix)
        scorer = ParsimonyScorer(matrix)
        score = scorer.get_score(tree, aln)
        self.assertEqual(score, 3.5 + 2.5 + 3.5 + 3.5 + 2.5 + 1 + 2.5 + 4.5)

        alphabet = [
            'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P',
            'Q', 'R', '1', '2', 'T', 'V', 'W', 'Y', '*', '-'
        ]
        step_matrix = [
            [0], [2, 0], [1, 2,
                          0],
            [1, 2, 1, 0],
            [2, 1, 2, 2, 0],
            [1, 1, 1, 1, 2,
             0], [2, 2, 1, 2, 2, 2,
                  0],
            [2, 2, 2, 2, 1, 2,
             2, 0], [2, 2, 2, 1, 2, 2, 2, 1,
                     0], [2, 2, 2, 2, 1, 2, 1, 1, 2,
                          0],
            [2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
             0], [2, 2, 1, 2, 2, 2, 1, 1, 1, 2, 2,
                  0],
            [1, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2,
             2, 0], [2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 2, 1,
                     0], [2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1,
                          0], [1, 1, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 0],
            [2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1,
             2, 2, 1, 2, 0],
            [1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 0],
            [1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 0],
            [2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 3, 2, 2, 1, 1, 2, 2, 2, 0],
            [2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 0],
            [2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 0],
            [
                3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
                3, 0
            ]
        ]

        matrix = _Matrix(alphabet, step_matrix)
        scorer = ParsimonyScorer(matrix)
        score = scorer.get_score(tree, aln)
        self.assertEqual(score, 3 + 1 + 3 + 3 + 2 + 1 + 2 + 5)
 def _build_protein_matrix(self, subsmat):
     """Convert matrix from SubsMat format to _Matrix object (PRIVATE)."""
     protein_matrix = _Matrix(self.protein_alphabet)
     for k, v in subsmat.items():
         aa1, aa2 = k
         protein_matrix[aa1, aa2] = v
     return protein_matrix
Esempio n. 3
0
def main(argv):
    # Test table data and corresponding labels
    M_labels = [
        'Wuttagoonaspis', 'Romundina', 'Brindabellaspis', 'Eurycaraspis',
        'Entelognathus'
    ]
    print(M_labels)  #A through G
    M = np.loadtxt(open(argv[1], "rb"), delimiter=",")
    l = np.tril(M)
    temp = np.ones((5, 5))
    u = np.triu(temp)
    l += u
    np.fill_diagonal(l, 0)

    M = l.tolist()

    for j in range(0, 5):
        for i in range(0, 5):
            M[i] = list(filter(lambda a: a != 1, M[i]))

    m = _Matrix(M_labels, M)
    print(type(m))

    constructor = DistanceTreeConstructor()
    tree = constructor.upgma(m)
    Phylo.draw(tree)
    def test_get_score(self):
        aln = AlignIO.read('TreeConstruction/msa.phy', 'phylip')
        tree = Phylo.read('./TreeConstruction/upgma.tre', 'newick')
        scorer = ParsimonyScorer()
        score = scorer.get_score(tree, aln)
        self.assertEqual(score, 2 + 1 + 2 + 2 + 1 + 1 + 1 + 3)

        alphabet = ['A', 'T', 'C', 'G']
        step_matrix = [[0],
                       [2.5, 0],
                       [2.5, 1, 0],
                       [1, 2.5, 2.5, 0]]
        matrix = _Matrix(alphabet, step_matrix)
        scorer = ParsimonyScorer(matrix)
        score = scorer.get_score(tree, aln)
        self.assertEqual(score, 3.5 + 2.5 + 3.5 + 3.5 + 2.5 + 1 + 2.5 + 4.5)

        alphabet = ['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N',
                    'P', 'Q', 'R', '1', '2', 'T', 'V', 'W', 'Y', '*', '-']
        step_matrix = [[0],
                       [2, 0],
                       [1, 2, 0],
                       [1, 2, 1, 0],
                       [2, 1, 2, 2, 0],
                       [1, 1, 1, 1, 2, 0],
                       [2, 2, 1, 2, 2, 2, 0],
                       [2, 2, 2, 2, 1, 2, 2, 0],
                       [2, 2, 2, 1, 2, 2, 2, 1, 0],
                       [2, 2, 2, 2, 1, 2, 1, 1, 2, 0],
                       [2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 0],
                       [2, 2, 1, 2, 2, 2, 1, 1, 1, 2, 2, 0],
                       [1, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 0],
                       [2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 2, 1, 0],
                       [2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 0],
                       [1, 1, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 0],
                       [2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 1, 2, 0],
                       [1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 0],
                       [1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 0],
                       [2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 3, 2, 2, 1, 1, 2, 2, 2, 0],
                       [2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 0],
                       [2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 0],
                       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0]]

        matrix = _Matrix(alphabet, step_matrix)
        scorer = ParsimonyScorer(matrix)
        score = scorer.get_score(tree, aln)
        self.assertEqual(score, 3 + 1 + 3 + 3 + 2 + 1 + 2 + 5)
 def test_get_neighbors(self):
     tree = Phylo.read('./TreeConstruction/upgma.tre', 'newick')
     alphabet = ['A', 'T', 'C', 'G']
     step_matrix = [[0], [2.5, 0], [2.5, 1, 0], [1, 2.5, 2.5, 0]]
     matrix = _Matrix(alphabet, step_matrix)
     scorer = ParsimonyScorer(matrix)
     searcher = NNITreeSearcher(scorer)
     trees = searcher._get_neighbors(tree)
     self.assertEqual(len(trees), 2 * (5 - 3))
     Phylo.write(trees, './TreeConstruction/neighbor_trees.tre', 'newick')
 def test_get_neighbors(self):
     tree = Phylo.read("./TreeConstruction/upgma.tre", "newick")
     alphabet = ["A", "T", "C", "G"]
     step_matrix = [[0], [2.5, 0], [2.5, 1, 0], [1, 2.5, 2.5, 0]]
     matrix = _Matrix(alphabet, step_matrix)
     scorer = ParsimonyScorer(matrix)
     searcher = NNITreeSearcher(scorer)
     trees = searcher._get_neighbors(tree)
     self.assertEqual(len(trees), 2 * (5 - 3))
     Phylo.write(trees, os.path.join(temp_dir, "neighbor_trees.tre"), "newick")
 def test_get_neighbors(self):
     tree = Phylo.read('./TreeConstruction/upgma.tre', 'newick')
     alphabet = ['A', 'T', 'C', 'G']
     step_matrix = [[0],
                    [2.5,   0],
                    [2.5,   1,    0],
                    [  1, 2.5,  2.5, 0]]
     matrix = _Matrix(alphabet, step_matrix)
     scorer = ParsimonyScorer(matrix)
     searcher = NNITreeSearcher(scorer)
     trees = searcher._get_neighbors(tree)
     self.assertEqual(len(trees), 2 * (5 - 3))
     Phylo.write(trees, './TreeConstruction/neighbor_trees.tre', 'newick')
 def test_build_tree(self):
     aln = AlignIO.read('TreeConstruction/msa.phy', 'phylip')
     tree1 = Phylo.read('./TreeConstruction/upgma.tre', 'newick')
     tree2 = Phylo.read('./TreeConstruction/nj.tre', 'newick')
     alphabet = ['A', 'T', 'C', 'G']
     step_matrix = [[0], [2.5, 0], [2.5, 1, 0], [1, 2.5, 2.5, 0]]
     matrix = _Matrix(alphabet, step_matrix)
     scorer = ParsimonyScorer(matrix)
     searcher = NNITreeSearcher(scorer)
     constructor = ParsimonyTreeConstructor(searcher, tree1)
     best_tree = constructor.build_tree(aln)
     Phylo.write(best_tree, './TreeConstruction/pars1.tre', 'newick')
     constructor.starting_tree = tree2
     best_tree = constructor.build_tree(aln)
     Phylo.write(best_tree, './TreeConstruction/pars2.tre', 'newick')
     constructor.starting_tree = None
     best_tree = constructor.build_tree(aln)
     Phylo.write(best_tree, './TreeConstruction/pars3.tre', 'newick')
Esempio n. 9
0
 def test_build_tree(self):
     aln = AlignIO.read("TreeConstruction/msa.phy", "phylip")
     tree1 = Phylo.read("./TreeConstruction/upgma.tre", "newick")
     tree2 = Phylo.read("./TreeConstruction/nj.tre", "newick")
     alphabet = ["A", "T", "C", "G"]
     step_matrix = [[0], [2.5, 0], [2.5, 1, 0], [1, 2.5, 2.5, 0]]
     matrix = _Matrix(alphabet, step_matrix)
     scorer = ParsimonyScorer(matrix)
     searcher = NNITreeSearcher(scorer)
     constructor = ParsimonyTreeConstructor(searcher, tree1)
     best_tree = constructor.build_tree(aln)
     Phylo.write(best_tree, os.path.join(temp_dir, "pars1.tre"), "newick")
     constructor.starting_tree = tree2
     best_tree = constructor.build_tree(aln)
     Phylo.write(best_tree, os.path.join(temp_dir, "pars2.tre"), "newick")
     constructor.starting_tree = None
     best_tree = constructor.build_tree(aln)
     Phylo.write(best_tree, os.path.join(temp_dir, "pars3.tre"), "newick")
    def __init__(self, model='identity', skip_letters=None):
        """Initialize with a distance model."""
        # Shim for backward compatibility (#491)
        if skip_letters:
            self.skip_letters = skip_letters
        elif model == 'identity':
            self.skip_letters = ()
        else:
            self.skip_letters = ('-', '*')

        if model == 'identity':
            self.scoring_matrix = None
        elif model in self.dna_models:
            self.scoring_matrix = _Matrix(self.dna_alphabet,
                                          self.dna_matrices[model])
        elif model in self.protein_models:
            self.scoring_matrix = self._build_protein_matrix(
                self.protein_matrices[model])
        else:
            raise ValueError("Model not supported. Available models: " +
                             ", ".join(self.models))
 def test_build_tree(self):
     aln = AlignIO.read(open('TreeConstruction/msa.phy'), 'phylip')
     tree1 = Phylo.read('./TreeConstruction/upgma.tre', 'newick')
     tree2 = Phylo.read('./TreeConstruction/nj.tre', 'newick')
     alphabet = ['A', 'T', 'C', 'G']
     step_matrix = [[0],
                    [2.5,   0],
                    [2.5,   1,    0],
                    [  1, 2.5,  2.5, 0]]
     matrix = _Matrix(alphabet, step_matrix)
     scorer = ParsimonyScorer(matrix)
     searcher = NNITreeSearcher(scorer)
     constructor = ParsimonyTreeConstructor(searcher, tree1)
     best_tree = constructor.build_tree(aln)
     Phylo.write(best_tree, './TreeConstruction/pars1.tre', 'newick')
     constructor.starting_tree = tree2
     best_tree = constructor.build_tree(aln)
     Phylo.write(best_tree, './TreeConstruction/pars2.tre', 'newick')
     constructor.starting_tree = None
     best_tree = constructor.build_tree(aln)
     Phylo.write(best_tree, './TreeConstruction/pars3.tre', 'newick')
Esempio n. 12
0
    def test_get_score(self):
        aln = AlignIO.read("TreeConstruction/msa.phy", "phylip")
        tree = Phylo.read("./TreeConstruction/upgma.tre", "newick")
        scorer = ParsimonyScorer()
        score = scorer.get_score(tree, aln)
        self.assertEqual(score, 2 + 1 + 2 + 2 + 1 + 1 + 1 + 3)

        alphabet = ["A", "T", "C", "G"]
        step_matrix = [[0], [2.5, 0], [2.5, 1, 0], [1, 2.5, 2.5, 0]]
        matrix = _Matrix(alphabet, step_matrix)
        scorer = ParsimonyScorer(matrix)
        score = scorer.get_score(tree, aln)
        self.assertEqual(score, 3.5 + 2.5 + 3.5 + 3.5 + 2.5 + 1 + 2.5 + 4.5)

        alphabet = [
            "A",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H",
            "I",
            "K",
            "L",
            "M",
            "N",
            "P",
            "Q",
            "R",
            "1",
            "2",
            "T",
            "V",
            "W",
            "Y",
            "*",
            "-",
        ]
        step_matrix = [
            [0],
            [2, 0],
            [1, 2, 0],
            [1, 2, 1, 0],
            [2, 1, 2, 2, 0],
            [1, 1, 1, 1, 2, 0],
            [2, 2, 1, 2, 2, 2, 0],
            [2, 2, 2, 2, 1, 2, 2, 0],
            [2, 2, 2, 1, 2, 2, 2, 1, 0],
            [2, 2, 2, 2, 1, 2, 1, 1, 2, 0],
            [2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 0],
            [2, 2, 1, 2, 2, 2, 1, 1, 1, 2, 2, 0],
            [1, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 0],
            [2, 2, 2, 1, 2, 2, 1, 2, 1, 1, 2, 2, 1, 0],
            [2, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 0],
            [1, 1, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 0],
            [2, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 1, 2, 0],
            [1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 0],
            [1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 0],
            [2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 3, 2, 2, 1, 1, 2, 2, 2, 0],
            [2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 0],
            [2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 0],
            [
                3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
                3, 0
            ],
        ]

        matrix = _Matrix(alphabet, step_matrix)
        scorer = ParsimonyScorer(matrix)
        score = scorer.get_score(tree, aln)
        self.assertEqual(score, 3 + 1 + 3 + 3 + 2 + 1 + 2 + 5)