Debian Codegraph @

src/dr/evomodelxml/speciation/ModelAveragingSpeciationLikelihoodParser.java

raw symbols SCIP
1 /*
2 * ModelAveragingSpeciationLikelihoodParser.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.evolution.tree.Tree;
29 import dr.evomodel.speciation.MaskableSpeciationModel;
30 import dr.evomodel.speciation.ModelAveragingSpeciationLikelihood;
31 import dr.inference.model.Parameter;
32 import dr.inference.model.Variable;
33 import dr.xml.*;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.logging.Logger;
38
39 /**
40 * @author Alexei Drummond
41 * @author Walter Xie
42 */
43 public class ModelAveragingSpeciationLikelihoodParser extends AbstractXMLObjectParser {
44 public static final String MODEL_AVE_SPECIATION_LIKELIHOOD = "modelAveragingSpeciationLikelihood";
45 public static final String MODEL = "sM";
46 public static final String TREE = "sT";
47 public static final String INDEX = "modelIndex";
48 public static final String MAX_INDEX = "maxIndex";
49
50 public String getParserName() {
51 return MODEL_AVE_SPECIATION_LIKELIHOOD;
52 }
53
54 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
55 List<Tree> trees = new ArrayList<Tree>();
56 List<MaskableSpeciationModel> models = new ArrayList<MaskableSpeciationModel>();
57 Variable<Integer> index;
58
59 System.out.println("id = " + xo.getId());
60
61 XMLObject cxo = xo.getChild(MODEL);
62 for (int m = 0; m < cxo.getChildCount(); m++) {
63 final MaskableSpeciationModel specModel = (MaskableSpeciationModel) cxo.getChild(m);
64 models.add(specModel);
65 }
66
67 cxo = xo.getChild(TREE);
68 for (int t = 0; t < cxo.getChildCount(); t++) {
69 final Tree tree = (Tree) cxo.getChild(t);
70 trees.add(tree);
71 }
72
73 // cxo = xo.getChild(INDEX);
74 index = (Variable<Integer>) xo.getElementFirstChild(INDEX); // integer index parameter size = real size - 1
75 Parameter maxIndex = (Parameter) xo.getElementFirstChild(MAX_INDEX);
76 // System.out.println(index.getClass());
77 // for (int i=0; i<index.getSize(); i++) {
78 // System.out.println(index.getValue(i).getClass());
79 // index.setValue(i, 0);
80 // }
81
82 int indexLength = models.size();
83 if (indexLength < 1 || trees.size() < 1) {
84 throw new XMLParseException("It requires at least one tree or one speciation model.");
85 } else if (indexLength != trees.size()) {
86 throw new XMLParseException("The number of trees and the number of speciation models should be equal.");
87 } else if (indexLength != index.getSize() + 1) { // integer index parameter size = real size - 1
88 throw new XMLParseException("Index parameter must be same size as the number of trees.");
89 }
90
91
92 Logger.getLogger("dr.evomodel").info("Speciation model excluding " + " taxa remaining.");
93
94 return new ModelAveragingSpeciationLikelihood(trees, models, index, maxIndex, xo.getId());
95 }
96
97 //************************************************************************
98 // AbstractXMLObjectParser implementation
99 //************************************************************************
100
101 public String getParserDescription() {
102 return "Model Averaging Speciation Likelihood.";
103 }
104
105 public Class getReturnType() {
106 return ModelAveragingSpeciationLikelihood.class;
107 }
108
109 public XMLSyntaxRule[] getSyntaxRules() {
110 return rules;
111 }
112
113 private final XMLSyntaxRule[] rules = {
114 new ElementRule(MODEL, new XMLSyntaxRule[]{
115 new ElementRule(MaskableSpeciationModel.class, 1, Integer.MAX_VALUE)
116 }),
117 new ElementRule(TREE, new XMLSyntaxRule[]{
118 new ElementRule(Tree.class, 1, Integer.MAX_VALUE)
119 }),
120 new ElementRule(INDEX, new XMLSyntaxRule[]{
121 new ElementRule(Variable.class)
122 }),
123 new ElementRule(MAX_INDEX, new XMLSyntaxRule[]{
124 new ElementRule(Parameter.class)
125 }, true),
126 };
127
128 }