| 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.TreeUtils; |
| 29 |
import dr.evolution.util.Taxa; |
| 30 |
import dr.evolution.util.TaxonList; |
| 31 |
import dr.evomodel.coalescent.CoalescentLikelihood; |
| 32 |
import dr.evomodel.coalescent.DemographicModel; |
| 33 |
import dr.evomodel.coalescent.MultiLociTreeSet; |
| 34 |
import dr.evomodel.coalescent.OldAbstractCoalescentLikelihood; |
| 35 |
import dr.evomodel.tree.TreeModel; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
import java.util.ArrayList; |
| 39 |
import java.util.List; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class CoalescentLikelihoodParser extends AbstractXMLObjectParser { |
| 44 |
|
| 45 |
public static final String COALESCENT_LIKELIHOOD = "coalescentLikelihood"; |
| 46 |
public static final String MODEL = "model"; |
| 47 |
public static final String POPULATION_TREE = "populationTree"; |
| 48 |
public static final String POPULATION_FACTOR = "factor"; |
| 49 |
|
| 50 |
public static final String INCLUDE = "include"; |
| 51 |
public static final String EXCLUDE = "exclude"; |
| 52 |
|
| 53 |
public String getParserName() { |
| 54 |
return COALESCENT_LIKELIHOOD; |
| 55 |
} |
| 56 |
|
| 57 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 58 |
|
| 59 |
XMLObject cxo = xo.getChild(MODEL); |
| 60 |
DemographicModel demoModel = (DemographicModel) cxo.getChild(DemographicModel.class); |
| 61 |
|
| 62 |
List<TreeModel> trees = new ArrayList<TreeModel>(); |
| 63 |
List<Double> popFactors = new ArrayList<Double>(); |
| 64 |
MultiLociTreeSet treesSet = demoModel instanceof MultiLociTreeSet ? (MultiLociTreeSet) demoModel : null; |
| 65 |
|
| 66 |
for (int k = 0; k < xo.getChildCount(); ++k) { |
| 67 |
final Object child = xo.getChild(k); |
| 68 |
if (child instanceof XMLObject) { |
| 69 |
cxo = (XMLObject) child; |
| 70 |
if (cxo.getName().equals(POPULATION_TREE)) { |
| 71 |
final TreeModel t = (TreeModel) cxo.getChild(TreeModel.class); |
| 72 |
assert t != null; |
| 73 |
trees.add(t); |
| 74 |
|
| 75 |
popFactors.add(cxo.getAttribute(POPULATION_FACTOR, 1.0)); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
TreeModel treeModel = null; |
| 85 |
if (trees.size() == 1 && popFactors.get(0) == 1.0) { |
| 86 |
treeModel = trees.get(0); |
| 87 |
} else if (trees.size() > 1) { |
| 88 |
treesSet = new MultiLociTreeSet.Default(trees, popFactors); |
| 89 |
} else if (!(trees.size() == 0 && treesSet != null)) { |
| 90 |
throw new XMLParseException("Incorrectly constructed likelihood element"); |
| 91 |
} |
| 92 |
|
| 93 |
TaxonList includeSubtree = null; |
| 94 |
|
| 95 |
if (xo.hasChildNamed(INCLUDE)) { |
| 96 |
includeSubtree = (TaxonList) xo.getElementFirstChild(INCLUDE); |
| 97 |
} |
| 98 |
|
| 99 |
List<TaxonList> excludeSubtrees = new ArrayList<TaxonList>(); |
| 100 |
|
| 101 |
if (xo.hasChildNamed(EXCLUDE)) { |
| 102 |
cxo = xo.getChild(EXCLUDE); |
| 103 |
for (int i = 0; i < cxo.getChildCount(); i++) { |
| 104 |
excludeSubtrees.add((TaxonList) cxo.getChild(i)); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (treeModel != null) { |
| 109 |
try { |
| 110 |
return new CoalescentLikelihood(treeModel, includeSubtree, excludeSubtrees, demoModel); |
| 111 |
} catch (TreeUtils.MissingTaxonException mte) { |
| 112 |
throw new XMLParseException("treeModel missing a taxon from taxon list in " + getParserName() + " element"); |
| 113 |
} |
| 114 |
} else { |
| 115 |
if (includeSubtree != null || excludeSubtrees.size() > 0) { |
| 116 |
throw new XMLParseException("Include/Exclude taxa not supported for multi locus sets"); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
return new OldAbstractCoalescentLikelihood(treesSet, demoModel); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public String getParserDescription() { |
| 130 |
return "This element represents the likelihood of the tree given the demographic function."; |
| 131 |
} |
| 132 |
|
| 133 |
public Class getReturnType() { |
| 134 |
return CoalescentLikelihood.class; |
| 135 |
} |
| 136 |
|
| 137 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 138 |
return rules; |
| 139 |
} |
| 140 |
|
| 141 |
private final XMLSyntaxRule[] rules = { |
| 142 |
new ElementRule(MODEL, new XMLSyntaxRule[]{ |
| 143 |
new ElementRule(DemographicModel.class) |
| 144 |
}, "The demographic model which describes the effective population size over time"), |
| 145 |
|
| 146 |
new ElementRule(POPULATION_TREE, new XMLSyntaxRule[]{ |
| 147 |
AttributeRule.newDoubleRule(POPULATION_FACTOR, true), |
| 148 |
new ElementRule(TreeModel.class) |
| 149 |
}, "Tree(s) to compute likelihood for", 0, Integer.MAX_VALUE), |
| 150 |
|
| 151 |
new ElementRule(INCLUDE, new XMLSyntaxRule[]{ |
| 152 |
new ElementRule(Taxa.class) |
| 153 |
}, "An optional subset of taxa on which to calculate the likelihood (should be monophyletic)", true), |
| 154 |
|
| 155 |
new ElementRule(EXCLUDE, new XMLSyntaxRule[]{ |
| 156 |
new ElementRule(Taxa.class, 1, Integer.MAX_VALUE) |
| 157 |
}, "One or more subsets of taxa which should be excluded from calculate the likelihood (should be monophyletic)", true) |
| 158 |
}; |
| 159 |
} |