| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodel.coalescent; |
| 27 |
|
| 28 |
import dr.evolution.coalescent.ConstantPopulation; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.util.Units; |
| 31 |
import dr.evomodel.tree.TreeModel; |
| 32 |
import dr.evomodelxml.coalescent.CoalescentLikelihoodParser; |
| 33 |
import dr.inference.model.Likelihood; |
| 34 |
import dr.inference.model.Parameter; |
| 35 |
import dr.util.Author; |
| 36 |
import dr.util.Citable; |
| 37 |
import dr.util.Citation; |
| 38 |
import dr.xml.*; |
| 39 |
|
| 40 |
import java.util.Collections; |
| 41 |
import java.util.List; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class SkylineLikelihood extends OldAbstractCoalescentLikelihood implements Citable { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public static final String SKYLINE_LIKELIHOOD = "skyLineLikelihood"; |
| 55 |
public static final String POPULATION_SIZES = "populationSizes"; |
| 56 |
|
| 57 |
public SkylineLikelihood(Tree tree, Parameter popSizeParameter) { |
| 58 |
super(SKYLINE_LIKELIHOOD); |
| 59 |
|
| 60 |
this.popSizeParameter = popSizeParameter; |
| 61 |
int tips = tree.getExternalNodeCount(); |
| 62 |
int params = popSizeParameter.getDimension(); |
| 63 |
if (tips - params != 1) { |
| 64 |
throw new IllegalArgumentException("Number of tips (" + tips + ") must be one greater than number of pop sizes (" + params + ")"); |
| 65 |
} |
| 66 |
|
| 67 |
this.tree = tree; |
| 68 |
if (tree instanceof TreeModel) { |
| 69 |
addModel((TreeModel)tree); |
| 70 |
} |
| 71 |
addVariable(popSizeParameter); |
| 72 |
setupIntervals(); |
| 73 |
|
| 74 |
addStatistic(new DeltaStatistic()); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public double calculateLogLikelihood() { |
| 86 |
|
| 87 |
if (!intervalsKnown) setupIntervals(); |
| 88 |
|
| 89 |
double logL = 0.0; |
| 90 |
|
| 91 |
double currentTime = 0.0; |
| 92 |
|
| 93 |
int popIndex=0; |
| 94 |
|
| 95 |
ConstantPopulation cp = new ConstantPopulation(Units.Type.YEARS); |
| 96 |
|
| 97 |
for (int j = 0; j < intervalCount; j++) { |
| 98 |
|
| 99 |
cp.setN0(popSizeParameter.getParameterValue(popIndex)); |
| 100 |
if (getIntervalType(j) == CoalescentEventType.COALESCENT) { |
| 101 |
popIndex += 1; |
| 102 |
} |
| 103 |
|
| 104 |
logL += calculateIntervalLikelihood(cp, intervals[j], currentTime, lineageCounts[j], getIntervalType(j)); |
| 105 |
|
| 106 |
|
| 107 |
int diff = getCoalescentEvents(j)-1; |
| 108 |
for (int k = 0; k < diff; k++) { |
| 109 |
cp.setN0(popSizeParameter.getParameterValue(popIndex)); |
| 110 |
logL += calculateIntervalLikelihood(cp, 0.0, currentTime, lineageCounts[j]-k-1, |
| 111 |
CoalescentEventType.COALESCENT); |
| 112 |
popIndex += 1; |
| 113 |
} |
| 114 |
|
| 115 |
currentTime += intervals[j]; |
| 116 |
|
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
return logL; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 128 |
|
| 129 |
public String getParserName() { return SKYLINE_LIKELIHOOD; } |
| 130 |
|
| 131 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 132 |
|
| 133 |
XMLObject cxo = (XMLObject)xo.getChild(POPULATION_SIZES); |
| 134 |
Parameter param = (Parameter)cxo.getChild(Parameter.class); |
| 135 |
|
| 136 |
cxo = (XMLObject)xo.getChild(CoalescentLikelihoodParser.POPULATION_TREE); |
| 137 |
TreeModel treeModel = (TreeModel)cxo.getChild(TreeModel.class); |
| 138 |
|
| 139 |
return new SkylineLikelihood(treeModel, param); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public String getParserDescription() { |
| 147 |
return "This element represents the likelihood of the tree given the population size vector."; |
| 148 |
} |
| 149 |
|
| 150 |
public Class getReturnType() { return Likelihood.class; } |
| 151 |
|
| 152 |
public XMLSyntaxRule[] getSyntaxRules() { return rules; } |
| 153 |
|
| 154 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 155 |
new ElementRule(POPULATION_SIZES, new XMLSyntaxRule[] { |
| 156 |
new ElementRule(Parameter.class) |
| 157 |
}), |
| 158 |
new ElementRule(CoalescentLikelihoodParser.POPULATION_TREE, new XMLSyntaxRule[] { |
| 159 |
new ElementRule(TreeModel.class) |
| 160 |
}), |
| 161 |
}; |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
Parameter popSizeParameter = null; |
| 166 |
|
| 167 |
@Override |
| 168 |
public Citation.Category getCategory() { |
| 169 |
return Citation.Category.TREE_PRIORS; |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public String getDescription() { |
| 174 |
return "Bayesian Skyline Coalescent"; |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public List<Citation> getCitations() { |
| 179 |
return Collections.singletonList(CITATION); |
| 180 |
} |
| 181 |
|
| 182 |
public static Citation CITATION = new Citation( |
| 183 |
new Author[]{ |
| 184 |
new Author("AJ", "Drummond"), |
| 185 |
new Author("A", "Rambaut"), |
| 186 |
new Author("B", "Shapiro"), |
| 187 |
new Author("OG", "Pybus") |
| 188 |
}, |
| 189 |
"Bayesian coalescent inference of past population dynamics from molecular sequences", |
| 190 |
2005, |
| 191 |
"Mol Biol Evol", |
| 192 |
22, 1185, 1192 |
| 193 |
); |
| 194 |
} |