Debian Codegraph @

src/dr/evomodel/antigenic/DriftedTraitsLogger.java

raw symbols SCIP
1 /*
2 * DriftedTraitsLogger.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.antigenic;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeTrait;
31 import dr.evolution.tree.TreeTraitProvider;
32 import dr.evolution.util.Taxon;
33 import dr.inference.model.*;
34 import dr.evomodel.continuous.AbstractMultivariateTraitLikelihood;
35 import dr.xml.*;
36
37 /**
38 * @author Trevor Bedford
39 * @author Andrew Rambaut
40 */
41 public class DriftedTraitsLogger implements TreeTraitProvider {
42
43 public static final String DRIFTED_TRAITS_LOGGER = "driftedTraits";
44
45 private final AbstractMultivariateTraitLikelihood multivariateTraits;
46 private final Parameter locationDriftParameter;
47 private TreeTrait[] treeTraits = null;
48 private double maxHeight = -1.0;
49
50 public DriftedTraitsLogger(AbstractMultivariateTraitLikelihood multivariateTraits, Parameter locationDriftParameter) {
51 this.multivariateTraits = multivariateTraits;
52 this.locationDriftParameter = locationDriftParameter;
53 }
54
55 @Override
56 public TreeTrait[] getTreeTraits() {
57 if (treeTraits == null) {
58 treeTraits = new TreeTrait[] {
59 new TreeTrait.DA() {
60 public String getTraitName() {
61 return multivariateTraits.getTraitName();
62 }
63
64 public Intent getIntent() {
65 return Intent.NODE;
66 }
67
68 public Class getTraitClass() {
69 return Double.class;
70 }
71
72 public double[] getTrait(Tree tree, NodeRef node) {
73 double t[] = multivariateTraits.getTraitForNode(tree, node, multivariateTraits.getTraitName());
74 computeMaxHeight(tree);
75
76 // drift first dimension
77 double nodeHeight = tree.getNodeHeight(node);
78 double offset = locationDriftParameter.getParameterValue(0) * (maxHeight-nodeHeight);
79 t[0] = t[0] + offset;
80
81 return t;
82 }
83 }
84 };
85 }
86 return treeTraits;
87 }
88
89 @Override
90 public TreeTrait getTreeTrait(String key) {
91 TreeTrait[] tts = getTreeTraits();
92 for (TreeTrait tt : tts) {
93 if (tt.getTraitName().equals(key)) {
94 return tt;
95 }
96 }
97 return null;
98 }
99
100 private void computeMaxHeight(Tree tree) {
101 if (maxHeight < 0) {
102 int m = tree.getTaxonCount();
103 for (int i = 0; i < m; i++) {
104 Taxon taxon = tree.getTaxon(i);
105 double height = taxon.getHeight();
106 if (height > maxHeight) {
107 maxHeight = height;
108 }
109 }
110 }
111 }
112
113 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
114
115 public String getParserName() {
116 return DRIFTED_TRAITS_LOGGER;
117 }
118
119 /**
120 * @return an object based on the XML element it was passed.
121 */
122 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
123
124 AbstractMultivariateTraitLikelihood multivariateTraits = (AbstractMultivariateTraitLikelihood) xo.getChild(AbstractMultivariateTraitLikelihood.class);
125 Parameter locationDrift = (Parameter) xo.getChild(Parameter.class);
126
127 return new DriftedTraitsLogger(multivariateTraits, locationDrift);
128 }
129
130 //************************************************************************
131 // AbstractXMLObjectParser implementation
132 //************************************************************************
133 public XMLSyntaxRule[] getSyntaxRules() {
134 return rules;
135 }
136
137 private final XMLSyntaxRule[] rules = {
138 new ElementRule(AbstractMultivariateTraitLikelihood.class, "The tree trait provider which is to be drifted."),
139 new ElementRule(Parameter.class, "The parameter specifying location drift rate.")
140 };
141
142 public String getParserDescription() {
143 return null;
144 }
145
146 public String getExample() {
147 return null;
148 }
149
150 public Class getReturnType() {
151 return TreeTraitProvider.class;
152 }
153 };
154
155 }