Debian Codegraph @

src/dr/evomodel/tipstatesmodel/HypermutantErrorModel.java

raw symbols SCIP
1 /*
2 * HypermutantErrorModel.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.evomodel.tipstatesmodel;
27
28 import dr.evolution.alignment.HypermutantAlignment;
29 import dr.evolution.datatype.Nucleotides;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Statistic;
32 import dr.inference.model.Variable;
33 import dr.util.Author;
34 import dr.util.Citable;
35 import dr.util.Citation;
36 import dr.xml.*;
37
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.logging.Logger;
43
44 /**
45 * @author Andrew Rambaut
46 * @version $Id$
47 */
48 public class HypermutantErrorModel extends TipStatesModel implements Citable {
49
50 public static final String HYPERMUTANT_ERROR_MODEL = "hypermutantErrorModel";
51 public static final String HYPERMUTATION_RATE = "hypermutationRate";
52 public static final String HYPERMUTATION_INDICATORS = "hypermutationIndicators";
53 public static final String UNLINKED_RATES = "unlinkedRates";
54
55 public HypermutantErrorModel(HypermutantAlignment hypermutantAlignment, Parameter hypermutationRateParameter, Parameter hypermuationIndicatorParameter, boolean unlinkedRates) {
56 super(HYPERMUTANT_ERROR_MODEL, null, null);
57
58 this.hypermutantAlignment = hypermutantAlignment;
59
60 this.unlinkedRates = unlinkedRates;
61
62 this.hypermutationRateParameter = hypermutationRateParameter;
63 addVariable(this.hypermutationRateParameter);
64
65 this.hypermutationIndicatorParameter = hypermuationIndicatorParameter;
66
67 addVariable(this.hypermutationIndicatorParameter);
68
69 addStatistic(new TaxonHypermutatedStatistic());
70 addStatistic(new TaxonHypermutationRateStatistic());
71 addStatistic(new HypermutatedProportionStatistic());
72 }
73
74 protected void taxaChanged() {
75 if (hypermutationIndicatorParameter.getDimension() <= 1) {
76 this.hypermutationIndicatorParameter.setDimension(tree.getExternalNodeCount());
77 }
78 if (unlinkedRates && hypermutationRateParameter.getDimension() <= 1) {
79 this.hypermutationRateParameter.setDimension(tree.getExternalNodeCount());
80 }
81 }
82
83 @Override
84 public Type getModelType() {
85 return Type.PARTIALS;
86 }
87
88 @Override
89 public void getTipStates(int nodeIndex, int[] tipStates) {
90 throw new IllegalArgumentException("This model emits only tip partials");
91 }
92
93 @Override
94 public void getTipPartials(int nodeIndex, double[] partials) {
95 int[] states = this.states[nodeIndex];
96 boolean isHypermutated = hypermutationIndicatorParameter.getParameterValue(nodeIndex) > 0.0;
97
98 double rate = (unlinkedRates ? hypermutationRateParameter.getParameterValue(nodeIndex) : hypermutationRateParameter.getParameterValue(0));
99
100 int k = 0;
101 for (int j = 0; j < patternCount; j++) {
102
103 switch (states[j]) {
104 case Nucleotides.A_STATE: // is an A
105 partials[k] = 1.0;
106 partials[k + 1] = 0.0;
107 partials[k + 2] = 0.0;
108 partials[k + 3] = 0.0;
109 break;
110 case Nucleotides.C_STATE: // is an C
111 partials[k] = 0.0;
112 partials[k + 1] = 1.0;
113 partials[k + 2] = 0.0;
114 partials[k + 3] = 0.0;
115 break;
116 case Nucleotides.G_STATE: // is an G
117 partials[k] = 0.0;
118 partials[k + 1] = 0.0;
119 partials[k + 2] = 1.0;
120 partials[k + 3] = 0.0;
121 break;
122 case Nucleotides.UT_STATE: // is an T
123 partials[k] = 0.0;
124 partials[k + 1] = 0.0;
125 partials[k + 2] = 0.0;
126 partials[k + 3] = 1.0;
127 break;
128 case Nucleotides.R_STATE: // is an A in a APOBEC context
129 if (isHypermutated) {
130 partials[k] = 1.0 - rate;
131 partials[k + 1] = 0.0;
132 partials[k + 2] = rate;
133 partials[k + 3] = 0.0;
134 } else {
135 partials[k] = 1.0;
136 partials[k + 1] = 0.0;
137 partials[k + 2] = 0.0;
138 partials[k + 3] = 0.0;
139 }
140
141 break;
142 default: // is an ambiguity
143 partials[k] = 1.0;
144 partials[k + 1] = 1.0;
145 partials[k + 2] = 1.0;
146 partials[k + 3] = 1.0;
147 }
148
149 k += stateCount;
150 }
151
152 }
153
154 @Override
155 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
156 if (variable == hypermutationIndicatorParameter) {
157 fireModelChanged(tree.getTaxon(index));
158 } else if (variable == hypermutationRateParameter) {
159 if (!unlinkedRates) {
160 fireModelChanged();
161 } else if (hypermutationIndicatorParameter.getValue(index) > 0.5) {
162 // only fire an update if the indicator is on....
163 fireModelChanged(tree.getTaxon(index));
164 }
165 } else {
166 throw new RuntimeException("Unknown parameter has changed in HypermutantErrorModel.handleVariableChangedEvent");
167 }
168
169 }
170
171 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
172
173 public String getParserName() { return HYPERMUTANT_ERROR_MODEL; }
174
175 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
176
177 boolean unlinkedRates = false;
178 if (xo.hasAttribute(UNLINKED_RATES)) {
179 unlinkedRates = xo.getBooleanAttribute(UNLINKED_RATES);
180 }
181
182 HypermutantAlignment hypermutantAlignment = (HypermutantAlignment)xo.getChild(HypermutantAlignment.class);
183
184 Parameter hypermutationRateParameter = null;
185 if (xo.hasChildNamed(HYPERMUTATION_RATE)) {
186 hypermutationRateParameter = (Parameter)xo.getElementFirstChild(HYPERMUTATION_RATE);
187 }
188
189 Parameter hypermuationIndicatorParameter = null;
190 if (xo.hasChildNamed(HYPERMUTATION_INDICATORS)) {
191 hypermuationIndicatorParameter = (Parameter)xo.getElementFirstChild(HYPERMUTATION_INDICATORS);
192 }
193
194 HypermutantErrorModel errorModel = new HypermutantErrorModel(hypermutantAlignment, hypermutationRateParameter, hypermuationIndicatorParameter, unlinkedRates);
195
196 Logger.getLogger("dr.evomodel").info("Using APOBEC error model");
197
198 return errorModel;
199 }
200
201 //************************************************************************
202 // AbstractXMLObjectParser implementation
203 //************************************************************************
204
205 public String getParserDescription() {
206 return
207 "This element returns a model that allows for APOBEC-type RNA editing.";
208 }
209
210 public Class getReturnType() { return HypermutantErrorModel.class; }
211
212 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
213
214 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
215 AttributeRule.newBooleanRule(UNLINKED_RATES, true),
216 new ElementRule(HypermutantAlignment.class),
217 new ElementRule(HYPERMUTATION_RATE, Parameter.class, "The hypermutation rate per target site per sequence"),
218 new ElementRule(HYPERMUTATION_INDICATORS, Parameter.class, "A binary indicator of whether the sequence is hypermutated"),
219 };
220 };
221
222 public class TaxonHypermutatedStatistic extends Statistic.Abstract {
223
224 public TaxonHypermutatedStatistic() {
225 super("isHypermutated");
226 }
227
228 public int getDimension() {
229 return hypermutationIndicatorParameter.getDimension();
230 }
231
232 public String getDimensionName(int dim) {
233 return taxonMap.get(dim);
234 }
235
236 public double getStatisticValue(int dim) {
237 return hypermutationIndicatorParameter.getParameterValue(dim);
238 }
239
240 }
241
242 public class TaxonHypermutationRateStatistic extends Statistic.Abstract {
243
244 public TaxonHypermutationRateStatistic() {
245 super("hypermutationRate");
246 }
247
248 public int getDimension() {
249 return hypermutationRateParameter.getDimension();
250 }
251
252 public String getDimensionName(int dim) {
253 return taxonMap.get(dim) + ".rate";
254 }
255
256 public double getStatisticValue(int dim) {
257 return hypermutationRateParameter.getParameterValue(dim) * hypermutationIndicatorParameter.getParameterValue(dim);
258 }
259
260 }
261
262 public class HypermutatedProportionStatistic extends Statistic.Abstract {
263
264 public HypermutatedProportionStatistic() {
265 super("proportionHypermutated");
266 }
267
268 public int getDimension() {
269 return 1;
270 }
271
272 public String getDimensionName(int dim) {
273 return "P(hypermutated)";
274 }
275
276 public double getStatisticValue(int dim) {
277 if (mutatedContextCounts == null) {
278 mutatedContextCounts = new HashMap<Integer, Integer>();
279 unmutatedContextCounts = new HashMap<Integer, Integer>();
280
281 for (int index : taxonMap.keySet()) {
282 String name = taxonMap.get(index);
283 int i = hypermutantAlignment.getTaxonIndex(name);
284 mutatedContextCounts.put(index, hypermutantAlignment.getMutatedContextCounts()[i]);
285 unmutatedContextCounts.put(index, hypermutantAlignment.getUnmutatedContextCounts()[i]);
286 }
287 }
288
289 double mutatedCount = 0;
290 double totalCount = 0;
291 for (int i = 0; i < hypermutationIndicatorParameter.getDimension(); i++) {
292 if (hypermutationIndicatorParameter.getParameterValue(i) > 0.5) {
293 mutatedCount += mutatedContextCounts.get(i);
294 totalCount += mutatedCount + unmutatedContextCounts.get(i);
295
296 }
297 }
298
299 double r = hypermutationRateParameter.getParameterValue(0);
300 return (r * mutatedCount) / totalCount;
301 }
302
303 }
304
305 Map<Integer, Integer> mutatedContextCounts = null;
306 Map<Integer, Integer> unmutatedContextCounts = null;
307
308 private final HypermutantAlignment hypermutantAlignment;
309 private final Parameter hypermutationRateParameter;
310 private final Parameter hypermutationIndicatorParameter;
311 private final boolean unlinkedRates;
312
313 @Override
314 public Citation.Category getCategory() {
315 return Citation.Category.SUBSTITUTION_MODELS;
316 }
317
318 @Override
319 public String getDescription() {
320 return "Sequence error model";
321 }
322
323 @Override
324 public List<Citation> getCitations() {
325 return Arrays.asList(new Citation(
326 new Author[]{
327 new Author("A", "Rambaut"),
328 new Author("SYW", "Ho"),
329 new Author("AJ", "Drummond"),
330 new Author("B", "Shapiro"),
331 },
332 "Accommodating the effect of ancient DNA damage on inferences of demographic histories",
333 2008,
334 "Mol Biol Evol",
335 26,
336 245, 248,
337 "10.1093/molbev/msn256"
338 ),
339 new Citation(
340 new Author[]{
341 new Author("J", "Felsenstein"),
342 },
343 "Inferring Phylogenies",
344 2004,
345 "Sinauer Associates",
346 ""
347 ));
348 }
349 }