| 1 |
module Model where |
| 2 |
|
| 3 |
import Probability |
| 4 |
import Probability.Logger |
| 5 |
import Bio.Alphabet |
| 6 |
import Bio.Alignment |
| 7 |
import Bio.Sequence |
| 8 |
import MCMC (scaleGroupSlice) |
| 9 |
import Tree |
| 10 |
import Tree.Newick |
| 11 |
import SModel |
| 12 |
import System.Environment (getArgs) |
| 13 |
import System.FilePath |
| 14 |
|
| 15 |
{- Problems: |
| 16 |
|
| 17 |
1. We can explain the data ignoring the sample ages if we have high low mutation rates and deep coalescence times. |
| 18 |
- We want the prior to favor high mutation rates. |
| 19 |
- We want the prior to favor a recent root age => high coalescent rates => small population sizes. |
| 20 |
3. N/tau prints as tau |
| 21 |
|
| 22 |
-} |
| 23 |
|
| 24 |
smodel_prior nucleotides = do |
| 25 |
freqs <- sample $ symmetricDirichletOn (getLetters nucleotides) 1 |
| 26 |
kappa1 <- sample $ logNormal (log 2) (1/4) |
| 27 |
kappa2 <- sample $ logNormal (log 2) (1/4) |
| 28 |
|
| 29 |
let tn93_model = tn93' nucleotides kappa1 kappa2 freqs |
| 30 |
let loggers = ["kappa1" %=% kappa1, "kappa2" %=% kappa2, "frequencies" %=% freqs] |
| 31 |
|
| 32 |
return (tn93_model, loggers) |
| 33 |
|
| 34 |
|
| 35 |
tree_prior taxa = do |
| 36 |
|
| 37 |
popSize <- sample $ logNormal 3 2 -- Favor small population sizes -> high coalescent rates -> more recent root age. |
| 38 |
|
| 39 |
let taxonAges = getTaxonAges taxa "s(\\d+)$" Forward |
| 40 |
popSizes = [(0, popSize)] |
| 41 |
|
| 42 |
tree <- sample $ coalescentTree taxonAges popSizes |
| 43 |
|
| 44 |
let loggers = ["N_over_tau" %=% popSize] |
| 45 |
|
| 46 |
return (tree, loggers) |
| 47 |
|
| 48 |
|
| 49 |
model seqData@[seqData1,seqData2,seqData3] logTree = do |
| 50 |
|
| 51 |
let taxa = getTaxa seqData1 |
| 52 |
|
| 53 |
(tree , tree_loggers) <- tree_prior taxa |
| 54 |
|
| 55 |
(smodel, sloggers ) <- smodel_prior dna |
| 56 |
|
| 57 |
mu <- sample $ logNormal (-5) 1.25 -- Favor high mutation rates -> more recent root age. |
| 58 |
|
| 59 |
-- We can't inverse-scale mu because it is exp(modifiable), not directly modifiable. |
| 60 |
addMove 1 $ scaleGroupSlice [ nodeTime tree node | node <- internalNodes tree ] |
| 61 |
|
| 62 |
weights <- map (3*) <$> (sample $ symmetricDirichlet 3 3) |
| 63 |
|
| 64 |
let [mu1,mu2,mu3] = map (mu*) weights |
| 65 |
|
| 66 |
observe seqData1 $ phyloCTMC tree (alignmentLength seqData1) smodel mu1 |
| 67 |
|
| 68 |
observe seqData2 $ phyloCTMC tree (alignmentLength seqData2) smodel mu2 |
| 69 |
|
| 70 |
observe seqData3 $ phyloCTMC tree (alignmentLength seqData3) smodel mu3 |
| 71 |
|
| 72 |
addLogger $ logTree (addInternalLabels tree) |
| 73 |
|
| 74 |
let tlength = treeLength tree |
| 75 |
substs = map (parsimony tree (unitCostMatrix dna)) seqData |
| 76 |
rootAge = nodeTime tree (root tree) |
| 77 |
loggers = ["tree" %>% tree_loggers, |
| 78 |
"tn93" %>% sloggers, |
| 79 |
"|T|" %=% tlength, |
| 80 |
"mu*|T|" %=% tlength * mu, |
| 81 |
"rootAge" %=% rootAge, |
| 82 |
"weights" %=% weights, |
| 83 |
"mu" %=% mu, |
| 84 |
"mu1" %=% mu1, |
| 85 |
"mu2" %=% mu2, |
| 86 |
"mu3" %=% mu3, |
| 87 |
"#substs" %=% substs, |
| 88 |
"#total_substs" %=% sum substs] |
| 89 |
|
| 90 |
return loggers |
| 91 |
|
| 92 |
main logDir = do |
| 93 |
|
| 94 |
[filename] <- getArgs |
| 95 |
|
| 96 |
sequences <- loadSequences filename |
| 97 |
|
| 98 |
let seqData = [ mkAlignedCharacterData dna $ selectRange range sequences | range <- ["3-.\\3", "1-.\\3", "2-.\\3"]] |
| 99 |
|
| 100 |
logTree <- treeLogger $ logDir </> "C1.trees" |
| 101 |
|
| 102 |
return $ model seqData logTree |
| 103 |
|