| 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.alloppnet.parsers; |
| 27 |
|
| 28 |
import dr.evomodel.alloppnet.speciation.AlloppSpeciesBindings; |
| 29 |
import dr.evomodel.tree.TreeModel; |
| 30 |
import dr.xml.*; |
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public class AlloppSpeciesBindingsParser extends AbstractXMLObjectParser { |
| 85 |
public static final String ALLOPPSPECIES = "alloppspecies"; |
| 86 |
public static final String GENE_TREES = "geneTrees"; |
| 87 |
public static final String GTREE = "gtree"; |
| 88 |
public static final String POPFACTOR = "popfactor"; |
| 89 |
public static final String MIN_GENENODE_HEIGHT = "minGeneNodeHeight"; |
| 90 |
|
| 91 |
|
| 92 |
public String getParserName() { |
| 93 |
return ALLOPPSPECIES; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 98 |
|
| 99 |
List<AlloppSpeciesBindings.ApSpInfo> apsp = new ArrayList<AlloppSpeciesBindings.ApSpInfo>(); |
| 100 |
for (int k = 0; k < xo.getChildCount(); ++k) { |
| 101 |
final Object child = xo.getChild(k); |
| 102 |
if (child instanceof AlloppSpeciesBindings.ApSpInfo) { |
| 103 |
apsp.add((AlloppSpeciesBindings.ApSpInfo) child); |
| 104 |
} |
| 105 |
} |
| 106 |
final double mingenenodeheight = xo.getDoubleAttribute(MIN_GENENODE_HEIGHT); |
| 107 |
final XMLObject xogt = xo.getChild(GENE_TREES); |
| 108 |
final int nTrees = xogt.getChildCount(); |
| 109 |
final TreeModel[] trees = new TreeModel[nTrees]; |
| 110 |
double[] popFactors = new double[nTrees]; |
| 111 |
|
| 112 |
for (int nt = 0; nt < trees.length; ++nt) { |
| 113 |
Object child = xogt.getChild(nt); |
| 114 |
if (!(child instanceof TreeModel)) { |
| 115 |
assert child instanceof XMLObject; |
| 116 |
popFactors[nt] = ((XMLObject) child).getDoubleAttribute(POPFACTOR); |
| 117 |
child = ((XMLObject) child).getChild(TreeModel.class); |
| 118 |
|
| 119 |
} else { |
| 120 |
popFactors[nt] = -1; |
| 121 |
} |
| 122 |
trees[nt] = (TreeModel) child; |
| 123 |
} |
| 124 |
|
| 125 |
try { |
| 126 |
return new AlloppSpeciesBindings(apsp.toArray(new AlloppSpeciesBindings.ApSpInfo[apsp.size()]), |
| 127 |
trees, mingenenodeheight, popFactors); |
| 128 |
} catch (Error e) { |
| 129 |
throw new XMLParseException(e.getMessage()); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
ElementRule treeWithPopFactors = new ElementRule(GTREE, |
| 138 |
new XMLSyntaxRule[]{AttributeRule.newDoubleRule(POPFACTOR), |
| 139 |
new ElementRule(TreeModel.class)}, 0, Integer.MAX_VALUE); |
| 140 |
|
| 141 |
|
| 142 |
@Override |
| 143 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 144 |
return new XMLSyntaxRule[]{ |
| 145 |
AttributeRule.newDoubleRule(MIN_GENENODE_HEIGHT), |
| 146 |
new ElementRule(AlloppSpeciesBindings.ApSpInfo.class, 2, Integer.MAX_VALUE), |
| 147 |
new ElementRule(GENE_TREES, |
| 148 |
new XMLSyntaxRule[]{ |
| 149 |
new ElementRule(TreeModel.class, 0, Integer.MAX_VALUE), |
| 150 |
treeWithPopFactors |
| 151 |
}), |
| 152 |
}; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
@Override |
| 157 |
public String getParserDescription() { |
| 158 |
return "Binds taxa to gene trees with information about possibly allopolyploid species."; |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public Class getReturnType() { |
| 163 |
return AlloppSpeciesBindings.class; |
| 164 |
} |
| 165 |
|
| 166 |
} |