| 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.speciation; |
| 27 |
|
| 28 |
import dr.evolution.tree.Tree; |
| 29 |
import dr.evolution.util.Taxa; |
| 30 |
import dr.evolution.util.Taxon; |
| 31 |
import dr.evolution.util.TaxonList; |
| 32 |
import dr.evomodel.speciation.CalibrationPoints; |
| 33 |
import dr.evomodel.speciation.SpeciationLikelihood; |
| 34 |
import dr.evomodel.speciation.SpeciationModel; |
| 35 |
import dr.inference.distribution.DistributionLikelihood; |
| 36 |
import dr.inference.model.Statistic; |
| 37 |
import dr.math.distributions.Distribution; |
| 38 |
import dr.xml.*; |
| 39 |
|
| 40 |
import java.util.ArrayList; |
| 41 |
import java.util.HashSet; |
| 42 |
import java.util.List; |
| 43 |
import java.util.Set; |
| 44 |
import java.util.logging.Logger; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class SpeciationLikelihoodParser extends AbstractXMLObjectParser { |
| 49 |
|
| 50 |
public static final String SPECIATION_LIKELIHOOD = "speciationLikelihood"; |
| 51 |
public static final String MODEL = "model"; |
| 52 |
public static final String TREE = "speciesTree"; |
| 53 |
public static final String INCLUDE = "include"; |
| 54 |
public static final String EXCLUDE = "exclude"; |
| 55 |
|
| 56 |
public static final String CALIBRATION = "calibration"; |
| 57 |
public static final String CORRECTION = "correction"; |
| 58 |
public static final String POINT = "point"; |
| 59 |
|
| 60 |
private final String EXACT = CalibrationPoints.CorrectionType.EXACT.toString(); |
| 61 |
private final String APPROX = CalibrationPoints.CorrectionType.APPROXIMATED.toString(); |
| 62 |
private final String PEXACT = CalibrationPoints.CorrectionType.PEXACT.toString(); |
| 63 |
private final String NONE = CalibrationPoints.CorrectionType.NONE.toString(); |
| 64 |
|
| 65 |
public static final String PARENT = dr.evomodelxml.tree.TMRCAStatisticParser.PARENT; |
| 66 |
|
| 67 |
public String getParserName() { |
| 68 |
return SPECIATION_LIKELIHOOD; |
| 69 |
} |
| 70 |
|
| 71 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 72 |
|
| 73 |
XMLObject cxo = xo.getChild(MODEL); |
| 74 |
final SpeciationModel specModel = (SpeciationModel) cxo.getChild(SpeciationModel.class); |
| 75 |
|
| 76 |
cxo = xo.getChild(TREE); |
| 77 |
final Tree tree = (Tree) cxo.getChild(Tree.class); |
| 78 |
|
| 79 |
Set<Taxon> excludeTaxa = null; |
| 80 |
|
| 81 |
if (xo.hasChildNamed(INCLUDE)) { |
| 82 |
excludeTaxa = new HashSet<Taxon>(); |
| 83 |
for (int i = 0; i < tree.getTaxonCount(); i++) { |
| 84 |
excludeTaxa.add(tree.getTaxon(i)); |
| 85 |
} |
| 86 |
|
| 87 |
cxo = xo.getChild(INCLUDE); |
| 88 |
for (int i = 0; i < cxo.getChildCount(); i++) { |
| 89 |
TaxonList taxonList = (TaxonList) cxo.getChild(i); |
| 90 |
for (int j = 0; j < taxonList.getTaxonCount(); j++) { |
| 91 |
excludeTaxa.remove(taxonList.getTaxon(j)); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (xo.hasChildNamed(EXCLUDE)) { |
| 97 |
excludeTaxa = new HashSet<Taxon>(); |
| 98 |
cxo = xo.getChild(EXCLUDE); |
| 99 |
for (int i = 0; i < cxo.getChildCount(); i++) { |
| 100 |
TaxonList taxonList = (TaxonList) cxo.getChild(i); |
| 101 |
for (int j = 0; j < taxonList.getTaxonCount(); j++) { |
| 102 |
excludeTaxa.add(taxonList.getTaxon(j)); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
if (excludeTaxa != null) { |
| 107 |
Logger.getLogger("dr.evomodel").info("Speciation model excluding " + excludeTaxa.size() + " taxa from prior - " + |
| 108 |
(tree.getTaxonCount() - excludeTaxa.size()) + " taxa remaining."); |
| 109 |
} |
| 110 |
|
| 111 |
final XMLObject cal = xo.getChild(CALIBRATION); |
| 112 |
if( cal != null ) { |
| 113 |
if( excludeTaxa != null ) { |
| 114 |
throw new XMLParseException("Sorry, not implemented: internal calibration prior + excluded taxa"); |
| 115 |
} |
| 116 |
|
| 117 |
List<Distribution> dists = new ArrayList<Distribution>(); |
| 118 |
List<Taxa> taxa = new ArrayList<Taxa>(); |
| 119 |
List<Boolean> forParent = new ArrayList<Boolean>(); |
| 120 |
Statistic userPDF = null; |
| 121 |
|
| 122 |
for(int k = 0; k < cal.getChildCount(); ++k) { |
| 123 |
final Object ck = cal.getChild(k); |
| 124 |
if ( DistributionLikelihood.class.isInstance(ck) ) { |
| 125 |
dists.add( ((DistributionLikelihood) ck).getDistribution() ); |
| 126 |
} else if ( Distribution.class.isInstance(ck) ) { |
| 127 |
dists.add((Distribution) ck); |
| 128 |
} else if ( Taxa.class.isInstance(ck) ) { |
| 129 |
final Taxa tx = (Taxa) ck; |
| 130 |
taxa.add(tx); |
| 131 |
forParent.add( tx.getTaxonCount() == 1 ); |
| 132 |
} else if ( Statistic.class.isInstance(ck) ) { |
| 133 |
if( userPDF != null ) { |
| 134 |
throw new XMLParseException("more than one userPDF correction???"); |
| 135 |
} |
| 136 |
userPDF = (Statistic) cal.getChild(Statistic.class); |
| 137 |
} |
| 138 |
else { |
| 139 |
XMLObject cko = (XMLObject) ck; |
| 140 |
assert cko.getChildCount() == 2; |
| 141 |
|
| 142 |
for(int i = 0; i < 2; ++i) { |
| 143 |
final Object chi = cko.getChild(i); |
| 144 |
if ( DistributionLikelihood.class.isInstance(chi) ) { |
| 145 |
dists.add( ((DistributionLikelihood) chi).getDistribution() ); |
| 146 |
} else if ( Distribution.class.isInstance(chi) ) { |
| 147 |
dists.add((Distribution) chi); |
| 148 |
} else if ( Taxa.class.isInstance(chi) ) { |
| 149 |
taxa.add((Taxa) chi); |
| 150 |
boolean fp = ((Taxa) chi).getTaxonCount() == 1; |
| 151 |
if( cko.hasAttribute(PARENT) ) { |
| 152 |
boolean ufp = cko.getBooleanAttribute(PARENT); |
| 153 |
if( fp && ! ufp ) { |
| 154 |
throw new XMLParseException("forParent==false for a single taxon?? (must be true)"); |
| 155 |
} |
| 156 |
fp = ufp; |
| 157 |
} |
| 158 |
forParent.add(fp); |
| 159 |
} else { |
| 160 |
assert false; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if( dists.size() != taxa.size() ) { |
| 167 |
throw new XMLParseException("Mismatch in number of distributions and taxa specs"); |
| 168 |
} |
| 169 |
|
| 170 |
try { |
| 171 |
final String correction = cal.getAttribute(CORRECTION, EXACT); |
| 172 |
|
| 173 |
final CalibrationPoints.CorrectionType type = correction.equals(EXACT) ? CalibrationPoints.CorrectionType.EXACT : |
| 174 |
(correction.equals(APPROX) ? CalibrationPoints.CorrectionType.APPROXIMATED : |
| 175 |
(correction.equals(NONE) ? CalibrationPoints.CorrectionType.NONE : |
| 176 |
(correction.equals(PEXACT) ? CalibrationPoints.CorrectionType.PEXACT : null))); |
| 177 |
|
| 178 |
if( cal.hasAttribute(CORRECTION) && type == null ) { |
| 179 |
throw new XMLParseException("correction type == " + correction + "???"); |
| 180 |
} |
| 181 |
|
| 182 |
final CalibrationPoints calib = |
| 183 |
new CalibrationPoints(tree, specModel.isYule(), dists, taxa, forParent, userPDF, type); |
| 184 |
final SpeciationLikelihood speciationLikelihood = new SpeciationLikelihood(tree, specModel, null, calib); |
| 185 |
return speciationLikelihood; |
| 186 |
} catch( IllegalArgumentException e ) { |
| 187 |
throw new XMLParseException( e.getMessage() ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return new SpeciationLikelihood(tree, specModel, excludeTaxa, null); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public String getParserDescription() { |
| 199 |
return "This element represents the likelihood of the tree given the speciation."; |
| 200 |
} |
| 201 |
|
| 202 |
public Class getReturnType() { |
| 203 |
return SpeciationLikelihood.class; |
| 204 |
} |
| 205 |
|
| 206 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 207 |
return rules; |
| 208 |
} |
| 209 |
|
| 210 |
private final XMLSyntaxRule[] calibrationPoint = { |
| 211 |
AttributeRule.newBooleanRule(PARENT, true), |
| 212 |
new XORRule( |
| 213 |
new ElementRule(Distribution.class), |
| 214 |
new ElementRule(DistributionLikelihood.class)), |
| 215 |
new ElementRule(Taxa.class) |
| 216 |
}; |
| 217 |
|
| 218 |
private final XMLSyntaxRule[] calibration = { |
| 219 |
|
| 220 |
|
| 221 |
AttributeRule.newStringRule(CORRECTION, true), |
| 222 |
new ElementRule(Statistic.class, true), |
| 223 |
new XORRule( |
| 224 |
new ElementRule(Distribution.class, 1, 100), |
| 225 |
new ElementRule(DistributionLikelihood.class, 1, 100)), |
| 226 |
new ElementRule(Taxa.class, 1, 100), |
| 227 |
new ElementRule("point", calibrationPoint, 0, 100) |
| 228 |
}; |
| 229 |
|
| 230 |
private final XMLSyntaxRule[] rules = { |
| 231 |
new ElementRule(MODEL, new XMLSyntaxRule[]{ |
| 232 |
new ElementRule(SpeciationModel.class) |
| 233 |
}), |
| 234 |
new ElementRule(TREE, new XMLSyntaxRule[]{ |
| 235 |
new ElementRule(Tree.class) |
| 236 |
}), |
| 237 |
|
| 238 |
new ElementRule(INCLUDE, new XMLSyntaxRule[]{ |
| 239 |
new ElementRule(Taxa.class, 1, Integer.MAX_VALUE) |
| 240 |
}, "One or more subsets of taxa which should be included from calculate the likelihood (the remaining taxa are excluded)", true), |
| 241 |
|
| 242 |
new ElementRule(EXCLUDE, new XMLSyntaxRule[]{ |
| 243 |
new ElementRule(Taxa.class, 1, Integer.MAX_VALUE) |
| 244 |
}, "One or more subsets of taxa which should be excluded from calculate the likelihood (which is calculated on the remaining subtree)", true), |
| 245 |
|
| 246 |
new ElementRule(CALIBRATION, calibration, true), |
| 247 |
}; |
| 248 |
|
| 249 |
} |