| 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.evomodel.speciation.SpeciesBindings; |
| 29 |
import dr.evomodel.tree.TreeModel; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.List; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class SpeciesBindingsParser extends AbstractXMLObjectParser { |
| 38 |
public static final String SPECIES = "species"; |
| 39 |
public static final String GENE_TREES = "geneTrees"; |
| 40 |
public static final String GTREE = "gtree"; |
| 41 |
public static final String PLOIDY = "ploidy"; |
| 42 |
|
| 43 |
|
| 44 |
public String getParserName() { |
| 45 |
return SPECIES; |
| 46 |
} |
| 47 |
|
| 48 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 49 |
|
| 50 |
List<SpeciesBindings.SPinfo> sp = new ArrayList<SpeciesBindings.SPinfo>(); |
| 51 |
for (int k = 0; k < xo.getChildCount(); ++k) { |
| 52 |
final Object child = xo.getChild(k); |
| 53 |
if (child instanceof SpeciesBindings.SPinfo) { |
| 54 |
sp.add((SpeciesBindings.SPinfo) child); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
final XMLObject xogt = xo.getChild(GENE_TREES); |
| 59 |
final int nTrees = xogt.getChildCount(); |
| 60 |
final TreeModel[] trees = new TreeModel[nTrees]; |
| 61 |
double[] popFactors = new double[nTrees]; |
| 62 |
|
| 63 |
for (int nt = 0; nt < trees.length; ++nt) { |
| 64 |
Object child = xogt.getChild(nt); |
| 65 |
if (!(child instanceof TreeModel)) { |
| 66 |
assert child instanceof XMLObject; |
| 67 |
popFactors[nt] = ((XMLObject) child).getDoubleAttribute(PLOIDY); |
| 68 |
child = ((XMLObject) child).getChild(TreeModel.class); |
| 69 |
|
| 70 |
} else { |
| 71 |
popFactors[nt] = -1; |
| 72 |
} |
| 73 |
trees[nt] = (TreeModel) child; |
| 74 |
} |
| 75 |
|
| 76 |
try { |
| 77 |
return new SpeciesBindings(sp.toArray(new SpeciesBindings.SPinfo[sp.size()]), trees, popFactors); |
| 78 |
} catch (Error e) { |
| 79 |
throw new XMLParseException(e.getMessage()); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
ElementRule treeWithPloidy = new ElementRule(GTREE, |
| 85 |
new XMLSyntaxRule[]{AttributeRule.newDoubleRule(PLOIDY), |
| 86 |
new ElementRule(TreeModel.class)}, 0, Integer.MAX_VALUE); |
| 87 |
|
| 88 |
|
| 89 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 90 |
return new XMLSyntaxRule[]{ |
| 91 |
new ElementRule(SpeciesBindings.SPinfo.class, 2, Integer.MAX_VALUE), |
| 92 |
|
| 93 |
new ElementRule(GENE_TREES, |
| 94 |
new XMLSyntaxRule[]{ |
| 95 |
|
| 96 |
new ElementRule(TreeModel.class, 0, Integer.MAX_VALUE), |
| 97 |
treeWithPloidy |
| 98 |
}), |
| 99 |
}; |
| 100 |
} |
| 101 |
|
| 102 |
public String getParserDescription() { |
| 103 |
return "Binds taxa in gene trees with species information."; |
| 104 |
} |
| 105 |
|
| 106 |
public Class getReturnType() { |
| 107 |
return SpeciesBindings.class; |
| 108 |
} |
| 109 |
|
| 110 |
} |