Debian Codegraph @

src/dr/app/beauti/components/sequenceerror/SequenceErrorModelComponentGenerator.java

raw symbols SCIP
1 /*
2 * SequenceErrorModelComponentGenerator.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.app.beauti.components.sequenceerror;
27
28 import dr.app.beauti.generator.BaseComponentGenerator;
29 import dr.app.beauti.options.AbstractPartitionData;
30 import dr.app.beauti.options.BeautiOptions;
31 import dr.app.beauti.types.SequenceErrorType;
32 import dr.app.beauti.util.XMLWriter;
33 import dr.evolution.alignment.HypermutantAlignment;
34 import dr.evomodel.tipstatesmodel.HypermutantErrorModel;
35 import dr.evomodelxml.tipstatesmodel.SequenceErrorModelParser;
36 import dr.evoxml.HypermutantAlignmentParser;
37 import dr.inference.model.ParameterParser;
38 import dr.inference.model.StatisticParser;
39 import dr.inferencexml.model.SumStatisticParser;
40 import dr.util.Attribute;
41 import dr.xml.XMLParser;
42
43 /**
44 * @author Andrew Rambaut
45 * @version $Id$
46 */
47 public class SequenceErrorModelComponentGenerator extends BaseComponentGenerator {
48
49 SequenceErrorModelComponentGenerator(final BeautiOptions options) {
50 super(options);
51 }
52
53 public boolean usesInsertionPoint(final InsertionPoint point) {
54 SequenceErrorModelComponentOptions comp = (SequenceErrorModelComponentOptions) options.getComponentOptions(SequenceErrorModelComponentOptions.class);
55
56
57 if (!comp.usingSequenceErrorModel()) {
58 return false;
59 }
60
61 switch (point) {
62 case AFTER_PATTERNS:
63 case AFTER_SITE_MODEL:
64 case IN_TREE_LIKELIHOOD:
65 case IN_FILE_LOG_PARAMETERS:
66 return true;
67 }
68 return false;
69 }
70
71 protected void generate(final InsertionPoint point, final Object item, final String prefix, final XMLWriter writer) {
72 SequenceErrorModelComponentOptions component = (SequenceErrorModelComponentOptions) options.getComponentOptions(SequenceErrorModelComponentOptions.class);
73
74 switch (point) {
75 case AFTER_PATTERNS:
76 writeHypermutationAlignments(writer, component);
77 break;
78 case AFTER_SITE_MODEL:
79 writeErrorModels(writer, component);
80 break;
81 case IN_TREE_LIKELIHOOD:
82 AbstractPartitionData partition = (AbstractPartitionData) item;
83 SequenceErrorType errorType = component.getSequenceErrorType(partition);
84 if (errorType != SequenceErrorType.NO_ERROR) {
85 writer.writeIDref(SequenceErrorModelParser.SEQUENCE_ERROR_MODEL, partition.getPrefix() + "errorModel");
86 }
87 break;
88 case IN_FILE_LOG_PARAMETERS:
89 writeLogParameters(writer, component);
90 break;
91 default:
92 throw new IllegalArgumentException("This insertion point is not implemented for " + this.getClass().getName());
93 }
94
95 }
96
97 protected String getCommentLabel() {
98 return "Sequence Error Model";
99 }
100
101 private void writeHypermutationAlignments(XMLWriter writer, SequenceErrorModelComponentOptions component) {
102 for (AbstractPartitionData partition : options.getDataPartitions()) {
103 String prefix = partition.getPrefix();//partition.getName() + ".";
104
105 if (component.isHypermutation(partition)) {
106 SequenceErrorType errorType = component.getSequenceErrorType(partition);
107
108 final String errorTypeName;
109 switch (errorType) {
110 case HYPERMUTATION_ALL:
111 errorTypeName = HypermutantAlignment.APOBECType.ALL.toString();
112 break;
113 case HYPERMUTATION_BOTH:
114 errorTypeName = HypermutantAlignment.APOBECType.BOTH.toString();
115 break;
116 case HYPERMUTATION_HA3F:
117 errorTypeName = HypermutantAlignment.APOBECType.HA3F.toString();
118 break;
119 case HYPERMUTATION_HA3G:
120 errorTypeName = HypermutantAlignment.APOBECType.HA3G.toString();
121 break;
122 default:
123 throw new RuntimeException("Unknown ErrorModelType: " + errorType.toString());
124 }
125 writer.writeOpenTag(
126 HypermutantAlignmentParser.HYPERMUTANT_ALIGNMENT,
127 new Attribute[]{
128 new Attribute.Default<String>(XMLParser.ID, prefix + "hypermutants"),
129 new Attribute.Default<String>("type", errorTypeName)
130 }
131 );
132
133 writer.writeIDref("alignment", partition.getTaxonList().getId());
134
135 writer.writeCloseTag(HypermutantAlignmentParser.HYPERMUTANT_ALIGNMENT);
136 }
137 }
138 }
139
140 private void writeErrorModels(XMLWriter writer, SequenceErrorModelComponentOptions component) {
141 for (AbstractPartitionData partition : options.getDataPartitions()) {
142 String prefix = partition.getPrefix();//partition.getName() + ".";
143
144 SequenceErrorType errorType = component.getSequenceErrorType(partition);
145 if (component.isHypermutation(partition)) {
146 writer.writeOpenTag(
147 HypermutantErrorModel.HYPERMUTANT_ERROR_MODEL,
148 new Attribute[]{
149 new Attribute.Default<String>(XMLParser.ID, prefix + SequenceErrorModelComponentOptions.ERROR_MODEL)
150 }
151 );
152
153 writer.writeIDref(HypermutantAlignmentParser.HYPERMUTANT_ALIGNMENT, prefix + "hypermutants");
154
155 writeParameter(HypermutantErrorModel.HYPERMUTATION_RATE, prefix + SequenceErrorModelComponentOptions.HYPERMUTION_RATE_PARAMETER, 1, writer);
156 writeParameter(HypermutantErrorModel.HYPERMUTATION_INDICATORS, prefix + SequenceErrorModelComponentOptions.HYPERMUTANT_INDICATOR_PARAMETER, 1, writer);
157
158 writer.writeCloseTag(HypermutantErrorModel.HYPERMUTANT_ERROR_MODEL);
159
160 writer.writeOpenTag(SumStatisticParser.SUM_STATISTIC, new Attribute[]{
161 new Attribute.Default<String>(XMLParser.ID, prefix + SequenceErrorModelComponentOptions.HYPERMUTANT_COUNT_STATISTIC),
162 new Attribute.Default<Boolean>(SumStatisticParser.ELEMENTWISE, true)});
163 writer.writeIDref(ParameterParser.PARAMETER, prefix + SequenceErrorModelComponentOptions.HYPERMUTANT_INDICATOR_PARAMETER);
164 writer.writeCloseTag(SumStatisticParser.SUM_STATISTIC);
165 } else if (errorType != SequenceErrorType.NO_ERROR) {
166 final String errorTypeName = (errorType == SequenceErrorType.AGE_TRANSITIONS ||
167 errorType == SequenceErrorType.BASE_TRANSITIONS ?
168 "transitions" : "all");
169
170 writer.writeOpenTag(
171 SequenceErrorModelParser.SEQUENCE_ERROR_MODEL,
172 new Attribute[]{
173 new Attribute.Default<String>(XMLParser.ID, prefix + SequenceErrorModelComponentOptions.ERROR_MODEL),
174 new Attribute.Default<String>("type", errorTypeName)
175 }
176 );
177
178 if (component.hasAgeDependentRate(partition)) {
179 writeParameter(SequenceErrorModelComponentOptions.AGE_RATE, prefix + SequenceErrorModelComponentOptions.AGE_RATE_PARAMETER, 1, writer);
180 }
181 if (component.hasBaseRate(partition)) {
182 writeParameter(SequenceErrorModelComponentOptions.BASE_RATE, prefix + SequenceErrorModelComponentOptions.BASE_RATE_PARAMETER, 1, writer);
183 }
184
185 writer.writeCloseTag(SequenceErrorModelParser.SEQUENCE_ERROR_MODEL);
186 }
187 }
188 }
189
190 private void writeLogParameters(final XMLWriter writer, final SequenceErrorModelComponentOptions component) {
191 for (AbstractPartitionData partition : options.getDataPartitions()) {
192 String prefix = partition.getPrefix();//partition.getName() + ".";
193
194 SequenceErrorType errorType = component.getSequenceErrorType(partition);
195 if (errorType != SequenceErrorType.NO_ERROR) {
196 if (component.isHypermutation(partition)) {
197 writer.writeIDref(ParameterParser.PARAMETER, prefix + SequenceErrorModelComponentOptions.HYPERMUTION_RATE_PARAMETER);
198 writer.writeIDref(StatisticParser.STATISTIC, prefix + SequenceErrorModelComponentOptions.HYPERMUTANT_COUNT_STATISTIC);
199 writer.writeOpenTag(StatisticParser.STATISTIC,
200 new Attribute.Default<String>("name", "isHypermutated"));
201 writer.writeIDref(HypermutantErrorModel.HYPERMUTANT_ERROR_MODEL,
202 prefix + SequenceErrorModelComponentOptions.ERROR_MODEL);
203 writer.writeCloseTag(StatisticParser.STATISTIC);
204 }
205
206 if (component.hasAgeDependentRate(partition)) {
207 writer.writeIDref(ParameterParser.PARAMETER, prefix + SequenceErrorModelComponentOptions.AGE_RATE_PARAMETER);
208 }
209 if (component.hasBaseRate(partition)) {
210 writer.writeIDref(ParameterParser.PARAMETER, prefix + SequenceErrorModelComponentOptions.BASE_RATE_PARAMETER);
211 }
212 }
213 }
214 }
215 }