Debian Codegraph @

src/dr/evomodel/tree/NodeTraitLogger.java

raw symbols SCIP
1 /*
2 * NodeTraitLogger.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.evomodel.tree;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeTraitProvider;
31 import dr.inference.model.Parameter;
32 import dr.xml.*;
33
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Map;
37
38 /**
39 * @author Marc A. Suchard
40 */
41 public class NodeTraitLogger extends TreeTraitProvider.Helper {
42
43 public static final String TRAIT_LOGGER = "logAllTraits";
44
45 private final TreeModel treeModel;
46
47 public NodeTraitLogger(TreeModel treeModel) {
48 this.treeModel = treeModel;
49
50 throw new RuntimeException("NodeTraitLogger has not been fully converted to using TreeTraitProvider");
51
52 // need to find all the traits here....
53 }
54
55 public static String[] getAllNodeTraitLabels(TreeModel tree) {
56
57 Map<String, Parameter> traits = tree.getTraitMap(tree.getRoot());
58 List<String> labels = new ArrayList<String>();
59 for (Map.Entry<String, Parameter> stringParameterEntry : traits.entrySet()) {
60
61 Parameter traitParameter = stringParameterEntry.getValue();
62 if (traitParameter.getDimension() == 1)
63 labels.add(stringParameterEntry.getKey());
64 else {
65 for (int i = 1; i <= traitParameter.getDimension(); i++)
66 labels.add(stringParameterEntry.getKey() + i);
67 }
68 }
69 return labels.toArray(new String[labels.size()]);
70 }
71
72 public static String[] getAllNodeTraitValues(TreeModel tree, NodeRef node) {
73
74 Map<String, Parameter> traits = tree.getTraitMap(node);
75 List<String> values = new ArrayList<String>();
76 for( Map.Entry<String, Parameter> stringParameterEntry : traits.entrySet() ) {
77
78 Parameter traitParameter = stringParameterEntry.getValue();
79
80 for (int i = 0; i < traitParameter.getDimension(); i++)
81 values.add(Double.toString(traitParameter.getParameterValue(i)));
82 }
83 return values.toArray(new String[values.size()]);
84 }
85
86 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
87
88 public String getParserName() {
89 return TRAIT_LOGGER;
90 }
91
92 /**
93 * @return an object based on the XML element it was passed.
94 */
95 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
96
97 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
98
99 try {
100
101 //Map<String, Parameter> traits = treeModel.getTraitMap(treeModel.getRoot());
102 return new NodeTraitLogger(treeModel);
103
104 } catch (IllegalArgumentException e) {
105
106 throw new XMLParseException("Tree " + treeModel.getId() + " contains no traits to log");
107 }
108
109
110 }
111
112 //************************************************************************
113 // AbstractXMLObjectParser implementation
114 //************************************************************************
115 public XMLSyntaxRule[] getSyntaxRules() {
116 return rules;
117 }
118
119 private final XMLSyntaxRule[] rules = {
120 new ElementRule(TreeModel.class, "The tree which is to be logged")
121 };
122
123 public String getParserDescription() {
124 return null;
125 }
126
127 public String getExample() {
128 return null;
129 }
130
131 public Class getReturnType() {
132 return TreeTraitProvider.class;
133 }
134 };
135 }