| 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.evomodelxml.coalescent; |
| 27 |
|
| 28 |
import dr.evolution.tree.Tree; |
| 29 |
import dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood; |
| 30 |
import dr.evomodel.coalescent.GMRFSkyrideLikelihood; |
| 31 |
import dr.evomodel.tree.TreeModel; |
| 32 |
import dr.inference.model.MatrixParameter; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.List; |
| 38 |
import java.util.logging.Logger; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class GMRFSkyrideLikelihoodParser extends AbstractXMLObjectParser { |
| 44 |
|
| 45 |
public static final String SKYLINE_LIKELIHOOD = "gmrfSkyrideLikelihood"; |
| 46 |
public static final String SKYRIDE_LIKELIHOOD = "skyrideLikelihood"; |
| 47 |
public static final String SKYGRID_LIKELIHOOD = "gmrfSkyGridLikelihood"; |
| 48 |
|
| 49 |
public static final String POPULATION_PARAMETER = "populationSizes"; |
| 50 |
public static final String GROUP_SIZES = "groupSizes"; |
| 51 |
public static final String PRECISION_PARAMETER = "precisionParameter"; |
| 52 |
public static final String POPULATION_TREE = "populationTree"; |
| 53 |
public static final String LAMBDA_PARAMETER = "lambdaParameter"; |
| 54 |
public static final String BETA_PARAMETER = "betaParameter"; |
| 55 |
public static final String SINGLE_BETA = "singleBeta"; |
| 56 |
public static final String COVARIATE_MATRIX = "covariateMatrix"; |
| 57 |
public static final String RANDOMIZE_TREE = "randomizeTree"; |
| 58 |
public static final String TIME_AWARE_SMOOTHING = "timeAwareSmoothing"; |
| 59 |
|
| 60 |
public static final String RESCALE_BY_ROOT_ISSUE = "rescaleByRootHeight"; |
| 61 |
public static final String GRID_POINTS = "gridPoints"; |
| 62 |
public static final String OLD_SKYRIDE = "oldSkyride"; |
| 63 |
public static final String NUM_GRID_POINTS = "numGridPoints"; |
| 64 |
public static final String CUT_OFF = "cutOff"; |
| 65 |
public static final String PHI_PARAMETER = "phiParameter"; |
| 66 |
public static final String PLOIDY = "ploidy"; |
| 67 |
public static final String COVARIATES = "covariates"; |
| 68 |
public static final String COLUMN_MAJOR = "columnMajor"; |
| 69 |
public static final String FIRST_OBSERVED_INDEX = "firstObservedIndex"; |
| 70 |
public static final String LAST_OBSERVED_INDEX = "lastObservedIndex"; |
| 71 |
public static final String COV_PREC_PARAM = "covariatePrecision"; |
| 72 |
public static final String COV_PREC_REC = "covariatePrecisionRecent"; |
| 73 |
public static final String COV_PREC_DIST = "covariatePrecisionDistant"; |
| 74 |
|
| 75 |
|
| 76 |
public String getParserName() { |
| 77 |
return SKYLINE_LIKELIHOOD; |
| 78 |
} |
| 79 |
|
| 80 |
public String[] getParserNames() { |
| 81 |
return new String[]{getParserName(), SKYRIDE_LIKELIHOOD, SKYGRID_LIKELIHOOD}; |
| 82 |
} |
| 83 |
|
| 84 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 85 |
|
| 86 |
XMLObject cxo = xo.getChild(POPULATION_PARAMETER); |
| 87 |
Parameter popParameter = (Parameter) cxo.getChild(Parameter.class); |
| 88 |
|
| 89 |
cxo = xo.getChild(PRECISION_PARAMETER); |
| 90 |
Parameter precParameter = (Parameter) cxo.getChild(Parameter.class); |
| 91 |
|
| 92 |
cxo = xo.getChild(POPULATION_TREE); |
| 93 |
|
| 94 |
List<Tree> treeList = new ArrayList<Tree>(); |
| 95 |
for (int i = 0; i < cxo.getChildCount(); i++) { |
| 96 |
Object testObject = cxo.getChild(i); |
| 97 |
if (testObject instanceof Tree) { |
| 98 |
treeList.add((TreeModel) testObject); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
cxo = xo.getChild(GROUP_SIZES); |
| 105 |
Parameter groupParameter = null; |
| 106 |
if (cxo != null) { |
| 107 |
groupParameter = (Parameter) cxo.getChild(Parameter.class); |
| 108 |
|
| 109 |
if (popParameter.getDimension() != groupParameter.getDimension()) |
| 110 |
throw new XMLParseException("Population and group size parameters must have the same length"); |
| 111 |
} |
| 112 |
|
| 113 |
Parameter lambda; |
| 114 |
if (xo.getChild(LAMBDA_PARAMETER) != null) { |
| 115 |
cxo = xo.getChild(LAMBDA_PARAMETER); |
| 116 |
lambda = (Parameter) cxo.getChild(Parameter.class); |
| 117 |
} else { |
| 118 |
lambda = new Parameter.Default(LAMBDA_PARAMETER, 1.0); |
| 119 |
} |
| 120 |
|
| 121 |
Parameter gridPoints = null; |
| 122 |
if (xo.getChild(GRID_POINTS) != null) { |
| 123 |
cxo = xo.getChild(GRID_POINTS); |
| 124 |
gridPoints = (Parameter) cxo.getChild(Parameter.class); |
| 125 |
} |
| 126 |
|
| 127 |
Parameter numGridPoints = null; |
| 128 |
if (xo.getChild(NUM_GRID_POINTS) != null) { |
| 129 |
cxo = xo.getChild(NUM_GRID_POINTS); |
| 130 |
numGridPoints = (Parameter) cxo.getChild(Parameter.class); |
| 131 |
} |
| 132 |
|
| 133 |
Parameter cutOff = null; |
| 134 |
if (xo.getChild(CUT_OFF) != null) { |
| 135 |
cxo = xo.getChild(CUT_OFF); |
| 136 |
cutOff = (Parameter) cxo.getChild(Parameter.class); |
| 137 |
} |
| 138 |
|
| 139 |
Parameter phi = null; |
| 140 |
if (xo.getChild(PHI_PARAMETER) != null) { |
| 141 |
cxo = xo.getChild(PHI_PARAMETER); |
| 142 |
phi = (Parameter) cxo.getChild(Parameter.class); |
| 143 |
} |
| 144 |
|
| 145 |
List<Parameter> firstObservedIndex = null; |
| 146 |
if (xo.hasChildNamed(FIRST_OBSERVED_INDEX)) { |
| 147 |
firstObservedIndex = new ArrayList<Parameter>(); |
| 148 |
cxo = xo.getChild(FIRST_OBSERVED_INDEX); |
| 149 |
final int numInd = cxo.getChildCount(); |
| 150 |
|
| 151 |
for(int i=0; i< numInd; ++i) { |
| 152 |
firstObservedIndex.add((Parameter) cxo.getChild(i)); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
List<Parameter> lastObservedIndex = null; |
| 157 |
if (xo.hasChildNamed(LAST_OBSERVED_INDEX)) { |
| 158 |
lastObservedIndex = new ArrayList<Parameter>(); |
| 159 |
cxo = xo.getChild(LAST_OBSERVED_INDEX); |
| 160 |
final int numObsInd = cxo.getChildCount(); |
| 161 |
|
| 162 |
for(int i=0; i< numObsInd; ++i) { |
| 163 |
lastObservedIndex.add((Parameter) cxo.getChild(i)); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
Parameter ploidyFactors = null; |
| 168 |
if (xo.getChild(PLOIDY) != null) { |
| 169 |
cxo = xo.getChild(PLOIDY); |
| 170 |
ploidyFactors = (Parameter) cxo.getChild(Parameter.class); |
| 171 |
} else { |
| 172 |
ploidyFactors = new Parameter.Default(PLOIDY, treeList.size()); |
| 173 |
for (int i = 0; i < treeList.size(); i++) { |
| 174 |
ploidyFactors.setParameterValue(i, 1.0); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
Parameter betaParameter = null; |
| 179 |
if (xo.hasChildNamed(SINGLE_BETA)) { |
| 180 |
betaParameter = (Parameter) xo.getElementFirstChild(SINGLE_BETA); |
| 181 |
} |
| 182 |
|
| 183 |
List<Parameter> betaList = null; |
| 184 |
if (xo.getChild(BETA_PARAMETER) != null) { |
| 185 |
betaList = new ArrayList<Parameter>(); |
| 186 |
cxo = xo.getChild(BETA_PARAMETER); |
| 187 |
final int numBeta = cxo.getChildCount(); |
| 188 |
for (int i = 0; i < numBeta; ++i) { |
| 189 |
betaList.add((Parameter) cxo.getChild(i)); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
MatrixParameter dMatrix = null; |
| 194 |
if (xo.getChild(COVARIATE_MATRIX) != null) { |
| 195 |
cxo = xo.getChild(COVARIATE_MATRIX); |
| 196 |
dMatrix = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 197 |
} |
| 198 |
|
| 199 |
boolean timeAwareSmoothing = GMRFSkyrideLikelihood.TIME_AWARE_IS_ON_BY_DEFAULT; |
| 200 |
if (xo.hasAttribute(TIME_AWARE_SMOOTHING)) { |
| 201 |
timeAwareSmoothing = xo.getBooleanAttribute(TIME_AWARE_SMOOTHING); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if (dMatrix != null) { |
| 208 |
if (dMatrix.getRowDimension() != popParameter.getDimension()) |
| 209 |
throw new XMLParseException("Design matrix row dimension must equal the population parameter length."); |
| 210 |
if (dMatrix.getColumnDimension() != betaParameter.getDimension()) |
| 211 |
throw new XMLParseException("Design matrix column dimension must equal the regression coefficient length."); |
| 212 |
} |
| 213 |
|
| 214 |
List<Parameter> covPrecParamRecent = null; |
| 215 |
List<Parameter> covPrecParamDistant = null; |
| 216 |
|
| 217 |
if(xo.hasChildNamed(COV_PREC_REC)){ |
| 218 |
covPrecParamRecent = new ArrayList<Parameter>(); |
| 219 |
cxo = xo.getChild(COV_PREC_REC); |
| 220 |
for(int i = 0; i < cxo.getChildCount(); ++i){ |
| 221 |
covPrecParamRecent.add((Parameter) cxo.getChild(i)); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if(xo.hasChildNamed(COV_PREC_DIST)){ |
| 226 |
covPrecParamDistant = new ArrayList<Parameter>(); |
| 227 |
cxo = xo.getChild(COV_PREC_DIST); |
| 228 |
for(int i = 0; i < cxo.getChildCount(); ++i){ |
| 229 |
covPrecParamDistant.add((Parameter) cxo.getChild(i)); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
if (xo.hasChildNamed(COV_PREC_PARAM)){ |
| 234 |
|
| 235 |
if(firstObservedIndex != null) { |
| 236 |
covPrecParamRecent = new ArrayList<Parameter>(); |
| 237 |
} |
| 238 |
if(lastObservedIndex != null) { |
| 239 |
covPrecParamDistant = new ArrayList<Parameter>(); |
| 240 |
} |
| 241 |
cxo = xo.getChild(COV_PREC_PARAM); |
| 242 |
|
| 243 |
for(int i=0; i < cxo.getChildCount(); ++i){ |
| 244 |
|
| 245 |
if(firstObservedIndex != null) { |
| 246 |
covPrecParamRecent.add((Parameter) cxo.getChild(i)); |
| 247 |
} |
| 248 |
if(lastObservedIndex != null) { |
| 249 |
covPrecParamDistant.add((Parameter) cxo.getChild(i)); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if((covPrecParamDistant == null && lastObservedIndex != null) || (covPrecParamDistant != null && lastObservedIndex == null)){ |
| 255 |
throw new XMLParseException("Must specify both lastObservedIndex and covariatePrecision"); |
| 256 |
} |
| 257 |
|
| 258 |
if((covPrecParamRecent == null && firstObservedIndex != null) || (covPrecParamRecent != null && firstObservedIndex == null)){ |
| 259 |
throw new XMLParseException("Must specify both firstObservedIndex and covariatePrecision"); |
| 260 |
} |
| 261 |
|
| 262 |
List<MatrixParameter> covariates = null; |
| 263 |
if (xo.hasChildNamed(COVARIATES)){ |
| 264 |
covariates = new ArrayList<MatrixParameter>(); |
| 265 |
cxo = xo.getChild(COVARIATES); |
| 266 |
final int numCov = cxo.getChildCount(); |
| 267 |
|
| 268 |
for (int i = 0; i < numCov; ++i) { |
| 269 |
covariates.add((MatrixParameter) cxo.getChild(i)); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if ((covariates != null && betaList == null) || |
| 274 |
(covariates == null && betaList != null)) |
| 275 |
throw new XMLParseException("Must specify both a set of regression coefficients and a design matrix."); |
| 276 |
|
| 277 |
|
| 278 |
if (xo.getAttribute(RANDOMIZE_TREE, false)) { |
| 279 |
for (Tree tree : treeList) { |
| 280 |
if (tree instanceof TreeModel) { |
| 281 |
GMRFSkyrideLikelihood.checkTree((TreeModel) tree); |
| 282 |
} else { |
| 283 |
throw new XMLParseException("Can not randomize a fixed tree"); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
boolean rescaleByRootHeight = xo.getAttribute(RESCALE_BY_ROOT_ISSUE, true); |
| 289 |
|
| 290 |
Logger.getLogger("dr.evomodel").info("The " + SKYLINE_LIKELIHOOD + " has " + |
| 291 |
(timeAwareSmoothing ? "time aware smoothing" : "uniform smoothing")); |
| 292 |
|
| 293 |
if (xo.getAttribute(OLD_SKYRIDE, true) && xo.getName().compareTo(SKYGRID_LIKELIHOOD) != 0) { |
| 294 |
return new GMRFSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, |
| 295 |
lambda, betaParameter, dMatrix, timeAwareSmoothing, rescaleByRootHeight); |
| 296 |
|
| 297 |
} else { |
| 298 |
if (xo.getChild(GRID_POINTS) != null) { |
| 299 |
return new GMRFMultilocusSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, |
| 300 |
lambda, betaParameter, dMatrix, timeAwareSmoothing, gridPoints, covariates, ploidyFactors, |
| 301 |
firstObservedIndex, lastObservedIndex, covPrecParamRecent, covPrecParamDistant, betaList); |
| 302 |
} else { |
| 303 |
return new GMRFMultilocusSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, |
| 304 |
lambda, betaParameter, dMatrix, timeAwareSmoothing, cutOff.getParameterValue(0), (int) numGridPoints.getParameterValue(0), phi, ploidyFactors); |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public String getParserDescription() { |
| 314 |
return "This element represents the likelihood of the tree given the population size vector."; |
| 315 |
} |
| 316 |
|
| 317 |
public Class getReturnType() { |
| 318 |
return GMRFSkyrideLikelihood.class; |
| 319 |
} |
| 320 |
|
| 321 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 322 |
return rules; |
| 323 |
} |
| 324 |
|
| 325 |
private final XMLSyntaxRule[] rules = { |
| 326 |
new ElementRule(POPULATION_PARAMETER, new XMLSyntaxRule[]{ |
| 327 |
new ElementRule(Parameter.class) |
| 328 |
}), |
| 329 |
new ElementRule(PRECISION_PARAMETER, new XMLSyntaxRule[]{ |
| 330 |
new ElementRule(Parameter.class) |
| 331 |
}), |
| 332 |
new ElementRule(PHI_PARAMETER, new XMLSyntaxRule[]{ |
| 333 |
new ElementRule(Parameter.class) |
| 334 |
}, true), |
| 335 |
new ElementRule(POPULATION_TREE, new XMLSyntaxRule[]{ |
| 336 |
new ElementRule(TreeModel.class, 1, Integer.MAX_VALUE) |
| 337 |
}), |
| 338 |
new ElementRule(GROUP_SIZES, new XMLSyntaxRule[]{ |
| 339 |
new ElementRule(Parameter.class) |
| 340 |
}, true), |
| 341 |
new ElementRule(SINGLE_BETA, new XMLSyntaxRule[] { |
| 342 |
new ElementRule(Parameter.class), |
| 343 |
}, true), |
| 344 |
AttributeRule.newBooleanRule(RESCALE_BY_ROOT_ISSUE, true), |
| 345 |
AttributeRule.newBooleanRule(RANDOMIZE_TREE, true), |
| 346 |
AttributeRule.newBooleanRule(TIME_AWARE_SMOOTHING, true), |
| 347 |
AttributeRule.newBooleanRule(OLD_SKYRIDE, true) |
| 348 |
}; |
| 349 |
|
| 350 |
} |