コード例 #1
0
    def test_mk_piNotSumTo1_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2
        Q = np.array([[-0.556216, 0.278108, 0.278108],
                      [0.278108, -0.556216, 0.278108],
                      [0.278108, 0.278108, -0.556216]])

        pi = np.array([0.0, 0.0, 0.0])

        try:
            discrete.mk(tree, chars, Q, pi=pi)
            self.fail("Assertion error not raised")
        except AssertionError, e:
            self.assertEquals("values of given pi must sum to 1", e.message)
コード例 #2
0
    def test_mk_piWrongType_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2
        Q = np.array([[-0.556216, 0.278108, 0.278108],
                      [0.278108, -0.556216, 0.278108],
                      [0.278108, 0.278108, -0.556216]])

        pi = [0.0, 0.0, 1.0]

        try:
            discrete.mk(tree, chars, Q, pi=pi)
            self.fail("Assertion error not raised")
        except AssertionError, e:
            self.assertEquals("pi must be str or numpy array", e.message)
コード例 #3
0
    def test_mk_wronglengthpi_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2
        Q = np.array([[-0.556216, 0.278108, 0.278108],
                      [0.278108, -0.556216, 0.278108],
                      [0.278108, 0.278108, -0.556216]])

        pi = np.array([0.0, 0.0, 0.0, 1.0])

        try:
            discrete.mk(tree, chars, Q, pi=pi)
            self.fail("Assertion error not raised")
        except AssertionError, e:
            self.assertEquals("length of given pi does not match Q dimensions",
                              e.message)
コード例 #4
0
    def test_mk_piWrongType_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2
        Q = np.array([[-0.556216,0.278108,0.278108],
                                  [0.278108,-0.556216,0.278108],
                                  [0.278108,0.278108,-0.556216]])

        pi = [0.0,0.0,1.0]

        try:
            discrete.mk(tree, chars, Q, pi=pi)
            self.fail("Assertion error not raised")
        except AssertionError, e:
            self.assertEquals("pi must be str or numpy array",
                              e.message)
コード例 #5
0
    def test_mk_piNotSumTo1_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2
        Q = np.array([[-0.556216,0.278108,0.278108],
                                  [0.278108,-0.556216,0.278108],
                                  [0.278108,0.278108,-0.556216]])

        pi = np.array([0.0,0.0,0.0])

        try:
            discrete.mk(tree, chars, Q, pi=pi)
            self.fail("Assertion error not raised")
        except AssertionError, e:
            self.assertEquals("values of given pi must sum to 1",
                              e.message)
コード例 #6
0
    def test_mk_wronglengthpi_raisesAssertionError(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2
        Q = np.array([[-0.556216,0.278108,0.278108],
                                  [0.278108,-0.556216,0.278108],
                                  [0.278108,0.278108,-0.556216]])

        pi = np.array([0.0,0.0,0.0,1.0])

        try:
            discrete.mk(tree, chars, Q, pi=pi)
            self.fail("Assertion error not raised")
        except AssertionError, e:
            self.assertEquals("length of given pi does not match Q dimensions",
                              e.message)
コード例 #7
0
    def test_mkFlatroot_randtree10_matchesPhytools(self):
        charstates = self.randchars10
        tree = self.randTree10
        Q = self.randQ

        phytoolslogLikelihood = -8.298437
        calculatedLogLikelihood = discrete.mk(tree, charstates, Q)

        self.assertTrue(np.isclose(phytoolslogLikelihood, calculatedLogLikelihood))
コード例 #8
0
    def test_mkFlatroot_randtree5_matchesPhytools(self):
        charstates = self.randchars5
        tree = self.randTree5
        Q = self.randQ

        phytoolslogLikelihood = -6.223166
        calculatedLogLikelihood = discrete.mk(tree, charstates, Q)

        self.assertTrue(
            np.isclose(phytoolslogLikelihood, calculatedLogLikelihood))
コード例 #9
0
    def test_mkFlatroot_randtree10_matchesPhytools(self):
        charstates = self.randchars10
        tree = self.randTree10
        Q = self.randQ

        phytoolslogLikelihood = -8.298437
        calculatedLogLikelihood = discrete.mk(tree, charstates, Q)

        self.assertTrue(
            np.isclose(phytoolslogLikelihood, calculatedLogLikelihood))
コード例 #10
0
    def test_mkFlatroot_randtree5_matchesPhytools(self):
        charstates = self.randchars5
        tree = self.randTree5
        Q = self.randQ

        phytoolslogLikelihood = -6.223166
        calculatedLogLikelihood = discrete.mk(tree, charstates, Q)



        self.assertTrue(np.isclose(phytoolslogLikelihood, calculatedLogLikelihood))
コード例 #11
0
    def test_mk_fitzjohn_matchesDiversitree(self):
        charstates = [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1]
        tree =  ivy.tree.read("support/randtree100tipsscale2.newick")
        Q = np.array([[-2.09613850e-01, 1.204029e-01, 8.921095e-02],
                      [5.654382e-01, -5.65438217e-01, 1.713339e-08],
                      [2.415020e-06, 5.958744e-07, -3.01089440e-06]])

        expectedLikelihood = -32.79025

        calculatedLogLikelihood = discrete.mk(tree, charstates, Q,
                                                 pi ="Fitzjohn")
        self.assertTrue(np.isclose(expectedLikelihood, calculatedLogLikelihood))
コード例 #12
0
    def test_mkMultiRegime_sameQ_matchesmk(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        Q = np.array([[-0.556216,0.278108,0.278108],
                      [0.278108,-0.556216,0.278108],
                      [0.278108,0.278108,-0.556216]])

        Qs = np.array([Q,Q])

        locs = [[i for i,n in enumerate(tree.postiter()) if not n.isroot][:50],
                 [i for i,n in enumerate(tree.postiter()) if not n.isroot][50:]]

        single = discrete.mk(tree, chars, Q)
        multi = discrete.mk_multi_regime(tree, chars, Qs, locs, pi="Equal")

        self.assertEquals(single, multi)
コード例 #13
0
    def test_mkMultiRegime_sameQ_matchesmk(self):
        tree = self.randTree100Scale2
        chars = self.simChars100states3Scale2

        Q = np.array([[-0.556216, 0.278108, 0.278108],
                      [0.278108, -0.556216, 0.278108],
                      [0.278108, 0.278108, -0.556216]])

        Qs = np.array([Q, Q])

        locs = [[i for i, n in enumerate(tree.postiter())
                 if not n.isroot][:50],
                [i for i, n in enumerate(tree.postiter())
                 if not n.isroot][50:]]

        single = discrete.mk(tree, chars, Q)
        multi = discrete.mk_multi_regime(tree, chars, Qs, locs, pi="Equal")

        self.assertEquals(single, multi)
コード例 #14
0
    def test_mkFlatroot_3tiptreeSymmetricQ2x2_returnslikelihood(self):
        tree = self.threetiptree
        chars = self.charstates_011
        Q = self.Q2x2_sym

        # Manually calculated likelihood for expected output
        L0A = 1
        L1A = 0
        L0B = 0
        L1B = 1
        L0D = 0
        L1D = 1

        P00A = 0.90936538
        P01A = 0.09063462
        P11A = 0.90936538
        P10A = 0.09063462

        P00B = 0.90936538
        P01B = 0.09063462
        P11B = 0.90936538
        P10B = 0.09063462

        P00C = 0.90936538
        P01C = 0.09063462
        P11C = 0.90936538
        P10C = 0.09063462

        P00D = 0.83516002
        P01D = 0.16483998
        P11D = 0.83516002
        P10D = 0.16483998

        L0C = (P00A * L0A + P01A * L1A) * (P00B * L0B + P01B * L1B)
        L1C = (P10A * L0A + P11A * L1A) * (P10B * L0B + P11B * L1B)

        L0r = (P00C * L0C + P01C * L1C) * (P00D * L0D + P01D * L1D)
        L1r = (P10C * L0C + P11C * L1C) * (P10D * L0D + P11D * L1D)

        predictedLikelihood = math.log(L0r * 0.5 + L1r * 0.5)
        calculatedLikelihood = discrete.mk(tree, chars, Q)

        self.assertTrue(np.isclose(predictedLikelihood, calculatedLikelihood))
コード例 #15
0
    def test_mkFlatroot_2tiptreeSymmetricQ2x2difblens_returnslikelihood(self):
        charstates = self.charstates_01
        tree = self.simpletreedifblens

        for i, node in enumerate(tree.leaves()):
            node.charstate = charstates[i]

        Q = self.Q2x2_sym
        t = [ node.length for node in tree.descendants() ]
        t = np.array(t, dtype=np.double)
        p = cyexpokit.dexpm_tree(Q,t)

        for i, node in enumerate(tree.descendants()):
            node.pmat = p[i]
        node = tree

        predictedLikelihood = math.log(0.11279709097648889)
        calculatedLikelihood = discrete.mk(tree, charstates, Q)

        self.assertTrue(np.isclose(predictedLikelihood, calculatedLikelihood))
コード例 #16
0
    def test_mkFlatroot_2tiptreeSymmetricQ2x2difblens_returnslikelihood(self):
        charstates = self.charstates_01
        tree = self.simpletreedifblens

        for i, node in enumerate(tree.leaves()):
            node.charstate = charstates[i]

        Q = self.Q2x2_sym
        t = [node.length for node in tree.descendants()]
        t = np.array(t, dtype=np.double)
        p = cyexpokit.dexpm_tree(Q, t)

        for i, node in enumerate(tree.descendants()):
            node.pmat = p[i]
        node = tree

        predictedLikelihood = math.log(0.11279709097648889)
        calculatedLikelihood = discrete.mk(tree, charstates, Q)

        self.assertTrue(np.isclose(predictedLikelihood, calculatedLikelihood))
コード例 #17
0
    def test_mk_fitzjohn_matchesDiversitree(self):
        charstates = [
            0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0,
            0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1
        ]
        tree = ivy.tree.read("support/randtree100tipsscale2.newick")
        Q = np.array([[-2.09613850e-01, 1.204029e-01, 8.921095e-02],
                      [5.654382e-01, -5.65438217e-01, 1.713339e-08],
                      [2.415020e-06, 5.958744e-07, -3.01089440e-06]])

        expectedLikelihood = -32.79025

        calculatedLogLikelihood = discrete.mk(tree,
                                              charstates,
                                              Q,
                                              pi="Fitzjohn")
        self.assertTrue(np.isclose(expectedLikelihood,
                                   calculatedLogLikelihood))
コード例 #18
0
    def test_mkFlatroot_3tiptreeSymmetricQ2x2_returnslikelihood(self):
        tree = self.threetiptree
        chars = self.charstates_011
        Q = self.Q2x2_sym

        # Manually calculated likelihood for expected output
        L0A = 1;L1A = 0;L0B = 0;L1B = 1;L0D = 0;L1D = 1

        P00A = 0.90936538
        P01A = 0.09063462
        P11A = 0.90936538
        P10A = 0.09063462

        P00B = 0.90936538
        P01B = 0.09063462
        P11B = 0.90936538
        P10B = 0.09063462

        P00C = 0.90936538
        P01C = 0.09063462
        P11C = 0.90936538
        P10C = 0.09063462

        P00D = 0.83516002
        P01D = 0.16483998
        P11D = 0.83516002
        P10D = 0.16483998

        L0C = (P00A * L0A + P01A * L1A) * (P00B * L0B + P01B * L1B)
        L1C = (P10A * L0A + P11A * L1A) * (P10B * L0B + P11B * L1B)

        L0r = (P00C * L0C + P01C * L1C) * (P00D * L0D + P01D * L1D)
        L1r = (P10C * L0C + P11C * L1C) * (P10D * L0D + P11D * L1D)

        predictedLikelihood = math.log(L0r * 0.5 + L1r * 0.5)
        calculatedLikelihood = discrete.mk(tree, chars, Q)

        self.assertTrue(np.isclose(predictedLikelihood, calculatedLikelihood))
コード例 #19
0
ファイル: optimize_mk.py プロジェクト: ChriZiegler/ivy
                                1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1]

Q = np.array([[-2.09613850e-01, 1.204029e-01, 8.921095e-02],
              [5.654382e-01, -5.65438217e-01, 1.713339e-08],
              [2.415020e-06, 5.958744e-07, -3.01089440e-06]])

mk(tree, chars, Q, pi="Equal", returnPi=True)


q, lik = discrete.fitMk(tree, chars, Q="ARD", pi="Equal")

Q = np.array([[-2.09613850e-01, 1.204029e-01, 8.921095e-02],
              [5.654382e-01, -5.65438217e-01, 1.713339e-08],
              [2.415020e-06, 5.958744e-07, -3.01089440e-06]])
calculatedLikelihood = discrete.mk(tree, chars, Q,
                                         pi = "Fitzjohn")
calculatedLogLikelihood = math.log(calculatedLikelihood)

calculatedLogLikelihood



tree = ivy.tree.read("../support/randtree100tips.newick")

chars = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
                    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                    1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

q,l = discrete.fitMk(tree, chars, Q="Sym", pi="Equilibrium")
コード例 #20
0
ファイル: multi_regime_bayes.py プロジェクト: ChriZiegler/ivy
Q2avg = np.percentile(Q2, 50)

multiregime_Q = np.array([[[-Q1avg,Q1avg],
                           [Q1avg,-Q1avg]],
                           [[-Q2avg,Q2avg],
                             [Q2avg,-Q2avg]]])
multiregime_locs = discrete.locs_from_switchpoint(mr_tree, mr_tree[int(scipy.stats.mode(out["switch"])[0])])

multiregime_likelihood = discrete.mk_mr(mr_tree, mr_chars,
                         multiregime_Q, multiregime_locs,
                         pi="Equal")

singleregime_Q = np.array([[-Qavg,Qavg],
                             [Qavg,-Qavg]])

singleregime_likelihood = discrete.mk(mr_tree, mr_chars, singleregime_Q,
                                      pi="Equal")


multiregime_likelihood
singleregime_likelihood


# Likelihood ratio test: we can do this because the 1-regime model
# is a special case of the 2-regime model

lr = -2*singleregime_likelihood + 2*multiregime_likelihood

# pchisq(19.4675329194382, 1, lower.tail=FALSE)
# 1.023242e-05
# Strong support for two regimes
コード例 #21
0
ファイル: multi_regime_bayes.py プロジェクト: ChriZiegler/ivy
Q2avg = np.percentile(Q2, 50)

multiregime_Q = np.array([[[-Q1avg,Q1avg],
                           [Q1avg,-Q1avg]],
                           [[-Q2avg,Q2avg],
                             [Q2avg,-Q2avg]]])
multiregime_locs = discrete.locs_from_switchpoint(mr_tree, mr_tree[int(scipy.stats.mode(out["switch"])[0])])

multiregime_likelihood = discrete.mk_multi_regime(mr_tree, mr_chars,
                         multiregime_Q, multiregime_locs,
                         pi="Equal")

singleregime_Q = np.array([[-Qavg,Qavg],
                             [Qavg,-Qavg]])

singleregime_likelihood = discrete.mk(mr_tree, mr_chars, singleregime_Q,
                                      pi="Equal")


multiregime_likelihood
singleregime_likelihood


# Likelihood ratio test: we can do this because the 1-regime model
# is a special case of the 2-regime model

lr = -2*singleregime_likelihood + 2*multiregime_likelihood

# pchisq(19.4675329194382, 1, lower.tail=FALSE)
# 1.023242e-05
# Strong support for two regimes