Debian Codegraph @

src/dr/evomodelxml/treelikelihood/StratifiedTraitLoggerParser.java

raw symbols SCIP
1 /*
2 * StratifiedTraitLoggerParser.java
3 *
4 * Copyright (c) 2002-2016 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.treelikelihood;
27
28 import dr.evomodel.treelikelihood.utilities.TreeTraitLogger;
29 import dr.evolution.tree.TreeTrait;
30 import dr.evolution.tree.TreeTraitProvider;
31 import dr.xml.*;
32 import dr.evomodel.tree.TreeModel;
33
34 /**
35 * @author Marc A. Suchard
36 */
37
38 public class StratifiedTraitLoggerParser extends AbstractXMLObjectParser {
39
40 public static final String PARSER_NAME = "stratifiedTraitLogger";
41 public static final String TRAIT_NAME = "traitName";
42 public static final String PARTITION = "partition";
43 //public static final String LOG_FORMAT = "format";
44
45 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
46
47 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
48
49 TreeTrait trait = TraitLoggerParser.parseTreeTrait(xo, true);
50
51 boolean partition = xo.getAttribute(PARTITION, false);
52
53 if (trait.getTraitClass() == Double.class || trait.getTraitClass() == Integer.class || !partition) {
54 return new TreeTraitLogger( treeModel, new TreeTrait[] { trait });
55 }
56
57 int length;
58 Object obj = trait.getTrait(treeModel, treeModel.getNode(0));
59
60 if (obj instanceof double[]) {
61 length = ((double[])obj).length;
62 } else if (obj instanceof int[]) {
63 length = ((int[])obj).length;
64 } else {
65 throw new XMLParseException("Unknown trait type for partitioning");
66 }
67
68 TreeTrait[] partitionedTraits = new TreeTrait[length];
69 for (int i = 0; i < length; i++) {
70 partitionedTraits[i] = new TreeTrait.PickEntryD(trait, i);
71 }
72 return new TreeTraitLogger( treeModel, partitionedTraits );
73 }
74
75 private static final XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
76 AttributeRule.newStringRule(TRAIT_NAME),
77 AttributeRule.newBooleanRule(PARTITION, true),
78 new ElementRule(TreeModel.class),
79 new ElementRule(TreeTraitProvider.class),
80 };
81
82 public XMLSyntaxRule[] getSyntaxRules() {
83 return rules;
84 }
85
86 public String getParserDescription() {
87 return "A parser to stratify traits to be logged";
88 }
89
90 public Class getReturnType() {
91 return TreeTraitLogger.class;
92 }
93
94 public String getParserName() {
95 return PARSER_NAME;
96 }
97 }