Debian Codegraph @

src/dr/evomodelxml/speciation/SpeciesBindingsParser.java

raw symbols SCIP
1 /*
2 * SpeciesBindingsParser.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 /* Can't be tree because XML parser supports usage of global tags only as main tags */
84 ElementRule treeWithPloidy = new ElementRule(GTREE,
85 new XMLSyntaxRule[]{AttributeRule.newDoubleRule(PLOIDY),
86 new ElementRule(TreeModel.class)}, 0, Integer.MAX_VALUE);
87 //XMLSyntaxRule[] someTree = {new OrRule(new ElementRule(TreeModel.class), treeWithPloidy)};
88
89 public XMLSyntaxRule[] getSyntaxRules() {
90 return new XMLSyntaxRule[]{
91 new ElementRule(SpeciesBindings.SPinfo.class, 2, Integer.MAX_VALUE),
92 // new ElementRule(GENE_TREES, someTree, 1, Integer.MAX_VALUE )
93 new ElementRule(GENE_TREES,
94 new XMLSyntaxRule[]{
95 // start at 0 for only ploidy tree cases
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 }