Debian Codegraph @

src/dr/evomodel/antigenic/DriftedLocationsStatistic.java

raw symbols SCIP
1 /*
2 * DriftedLocationsStatistic.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.inference.model.*;
29 import dr.xml.*;
30
31 /**
32 * @author Trevor Bedford
33 * @author Marc A. Suchard
34 */
35 public class DriftedLocationsStatistic extends Statistic.Abstract implements VariableListener {
36
37 public static final String DRIFTED_LOCATIONS_STATISTIC = "driftedLocationsStatistic";
38
39 public DriftedLocationsStatistic(MatrixParameter locationsParameter, Parameter offsetsParameter, Parameter locationDriftParameter) {
40 this.locationsParameter = locationsParameter;
41 locationsParameter.addParameterListener(this);
42 this.offsetsParameter = offsetsParameter;
43 offsetsParameter.addParameterListener(this);
44 this.locationDriftParameter = locationDriftParameter;
45 locationDriftParameter.addParameterListener(this);
46 }
47
48 public int getDimension() {
49 return locationsParameter.getDimension();
50 }
51
52 // strain index
53 public int getColumnIndex(int dim) {
54 return dim / locationsParameter.getRowDimension();
55 }
56
57 // dimension index
58 public int getRowIndex(int dim) {
59 int x = getColumnIndex(dim);
60 return dim - x * locationsParameter.getRowDimension();
61 }
62
63 public double getStatisticValue(int dim) {
64
65 double val;
66
67 // x is location count, y is location dimension
68 int x = getColumnIndex(dim);
69 int y = getRowIndex(dim);
70 Parameter loc = locationsParameter.getParameter(x);
71
72 if (y == 0) {
73 val = loc.getParameterValue(y) + locationDriftParameter.getParameterValue(0) * offsetsParameter.getParameterValue(x);
74 }
75 else {
76 val = loc.getParameterValue(y);
77 }
78
79
80 return val;
81
82 }
83
84 public String getDimensionName(int dim) {
85 return locationsParameter.getDimensionName(dim);
86 }
87
88 public void variableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
89 // do nothing
90 }
91
92 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
93
94 public final static String LOCATIONS = "locations";
95 public final static String OFFSETS = "offsets";
96 public final static String LOCATION_DRIFT = "locationDrift";
97
98 public String getParserName() {
99 return DRIFTED_LOCATIONS_STATISTIC;
100 }
101
102 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
103
104 MatrixParameter locations = (MatrixParameter) xo.getElementFirstChild(LOCATIONS);
105 Parameter offset = (Parameter) xo.getElementFirstChild(OFFSETS);
106 Parameter locationDrift = (Parameter) xo.getElementFirstChild(LOCATION_DRIFT);
107 return new DriftedLocationsStatistic(locations, offset, locationDrift);
108
109 }
110
111 //************************************************************************
112 // AbstractXMLObjectParser implementation
113 //************************************************************************
114
115 public String getParserDescription() {
116 return "This element returns a statistic that shifts a matrix of locations by location drift in the first dimension.";
117 }
118
119 public Class getReturnType() {
120 return DriftedLocationsStatistic.class;
121 }
122
123 public XMLSyntaxRule[] getSyntaxRules() {
124 return rules;
125 }
126
127 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
128 new ElementRule(LOCATIONS, MatrixParameter.class),
129 new ElementRule(OFFSETS, Parameter.class),
130 new ElementRule(LOCATION_DRIFT, Parameter.class)
131 };
132 };
133
134 private MatrixParameter locationsParameter;
135 private Parameter offsetsParameter;
136 private Parameter locationDriftParameter;
137 }