Debian Codegraph @

src/dr/oldevomodelxml/substmodel/EmpiricalCodonModelParser.java

raw symbols SCIP
1 /*
2 * EmpiricalCodonModelParser.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.oldevomodelxml.substmodel;
27
28 import java.util.logging.Logger;
29
30 import dr.evolution.datatype.Codons;
31 import dr.evolution.datatype.DataType;
32 import dr.evolution.datatype.GeneticCode;
33 import dr.evomodel.substmodel.*;
34 import dr.inference.model.Parameter;
35 import dr.oldevomodel.substmodel.EmpiricalCodonModel;
36 import dr.oldevomodel.substmodel.EmpiricalCodonRateMatrix;
37 import dr.oldevomodel.substmodel.FrequencyModel;
38 import dr.xml.*;
39
40 /**
41 * XML parser for ECM
42 *
43 * @author Stefan Zoller
44 */
45 public class EmpiricalCodonModelParser extends AbstractXMLObjectParser {
46 public static final String EMPIRICAL_CODON_MODEL = "empiricalCodonModel";
47 public static final String EMPIRICAL_RATE_MATRIX = "empiricalRateMatrix";
48 public static final String ECM_DATA_DIR = "ecmDataDir";
49 public static final String ECM_DATA_MATRIX = "ecmRateFile";
50 public static final String ECM_FREQ_MATRIX = "ecmFreqFile";
51 public static final String OMEGA = "omega";
52 public static final String KAPPATSTV = "kappaTsTv";
53 public static final String MULTI_NT_CHANGE = "multiNtChange";
54 public String getParserName() { return EMPIRICAL_CODON_MODEL; }
55
56 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
57
58 Codons codons = Codons.UNIVERSAL;
59 if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) {
60 String codeStr = xo.getStringAttribute(GeneticCode.GENETIC_CODE);
61 if (codeStr.equals(GeneticCode.UNIVERSAL.getName())) {
62 codons = Codons.UNIVERSAL;
63 } else if (codeStr.equals(GeneticCode.VERTEBRATE_MT.getName())) {
64 codons = Codons.VERTEBRATE_MT;
65 } else if (codeStr.equals(GeneticCode.YEAST.getName())) {
66 codons = Codons.YEAST;
67 } else if (codeStr.equals(GeneticCode.MOLD_PROTOZOAN_MT.getName())) {
68 codons = Codons.MOLD_PROTOZOAN_MT;
69 } else if (codeStr.equals(GeneticCode.INVERTEBRATE_MT.getName())) {
70 codons = Codons.INVERTEBRATE_MT;
71 } else if (codeStr.equals(GeneticCode.CILIATE.getName())) {
72 codons = Codons.CILIATE;
73 } else if (codeStr.equals(GeneticCode.ECHINODERM_MT.getName())) {
74 codons = Codons.ECHINODERM_MT;
75 } else if (codeStr.equals(GeneticCode.EUPLOTID_NUC.getName())) {
76 codons = Codons.EUPLOTID_NUC;
77 } else if (codeStr.equals(GeneticCode.BACTERIAL.getName())) {
78 codons = Codons.BACTERIAL;
79 } else if (codeStr.equals(GeneticCode.ALT_YEAST.getName())) {
80 codons = Codons.ALT_YEAST;
81 } else if (codeStr.equals(GeneticCode.ASCIDIAN_MT.getName())) {
82 codons = Codons.ASCIDIAN_MT;
83 } else if (codeStr.equals(GeneticCode.FLATWORM_MT.getName())) {
84 codons = Codons.FLATWORM_MT;
85 } else if (codeStr.equals(GeneticCode.BLEPHARISMA_NUC.getName())) {
86 codons = Codons.BLEPHARISMA_NUC;
87 } else if (codeStr.equals(GeneticCode.NO_STOPS.getName())) {
88 codons = Codons.NO_STOPS;
89 }
90 }
91
92 Parameter omegaParam = (Parameter)xo.getElementFirstChild(OMEGA);
93 Parameter kappaParam = null;
94 Parameter mntParam = null;
95 if(xo.hasChildNamed(KAPPATSTV)) {
96 kappaParam = (Parameter)xo.getElementFirstChild(KAPPATSTV);
97 if(kappaParam.getDimension() != 2 && kappaParam.getDimension() != 9) {
98 throw new XMLParseException("If you use the kappa parameter, you need to enter exactly\n" +
99 "two values for ts and tv or nine values\n" +
100 "according to the Kosiol ECM+F+omega+9k model");
101 }
102 } else {
103 mntParam = (Parameter)xo.getElementFirstChild(MULTI_NT_CHANGE);
104 }
105
106 String dirString = xo.getStringAttribute(ECM_DATA_DIR);
107 String freqString = xo.getStringAttribute(ECM_FREQ_MATRIX);
108 String matString = xo.getStringAttribute(ECM_DATA_MATRIX);
109
110 EmpiricalCodonRateMatrix rateMat = new EmpiricalCodonRateMatrix(EMPIRICAL_RATE_MATRIX, codons,
111 dirString, freqString, matString);
112
113 // get frequencies from XML, from frequency csv file or estimate from data
114 FrequencyModel freqModel = null;
115 if (xo.getChild(FrequencyModel.class) != null) {
116 freqModel = (FrequencyModel)xo.getChild(FrequencyModel.class);
117 } else {
118 freqModel = createNewFreqModel(codons, rateMat);
119 }
120
121 return new EmpiricalCodonModel(codons, omegaParam, kappaParam, mntParam, rateMat, freqModel);
122 }
123
124 // creates new FrequencyModel from XML frequencies
125 private FrequencyModel createNewFreqModel(DataType codons, EmpiricalCodonRateMatrix type) throws XMLParseException {
126 double[] freqs = type.getFrequencies();
127 double sum = 0;
128 for (int j = 0; j < freqs.length; j++) {
129 sum += freqs[j];
130 }
131
132 if (Math.abs(sum - 1.0) > 1e-8) {
133 throw new XMLParseException("Frequencies do not sum to 1 (they sum to " + sum + ")");
134 }
135
136 FrequencyModel fm = new FrequencyModel(codons, freqs);
137
138 Logger.getLogger("dr.evomodel").info("Using frequencies from data file");
139
140 return fm;
141 }
142
143 //************************************************************************
144 // AbstractXMLObjectParser implementation
145 //************************************************************************
146
147 public String getParserDescription() {
148 return "This element represents the empirical model of codon evolution.";
149 }
150
151 public Class getReturnType() { return EmpiricalCodonModel.class; }
152
153 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
154
155 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
156 new StringAttributeRule(GeneticCode.GENETIC_CODE,
157 "The genetic code to use",
158 new String[] {
159 GeneticCode.UNIVERSAL.getName(),
160 GeneticCode.VERTEBRATE_MT.getName(),
161 GeneticCode.YEAST.getName(),
162 GeneticCode.MOLD_PROTOZOAN_MT.getName(),
163 GeneticCode.INVERTEBRATE_MT.getName(),
164 GeneticCode.CILIATE.getName(),
165 GeneticCode.ECHINODERM_MT.getName(),
166 GeneticCode.EUPLOTID_NUC.getName(),
167 GeneticCode.BACTERIAL.getName(),
168 GeneticCode.ALT_YEAST.getName(),
169 GeneticCode.ASCIDIAN_MT.getName(),
170 GeneticCode.FLATWORM_MT.getName(),
171 GeneticCode.BLEPHARISMA_NUC.getName(),
172 GeneticCode.NO_STOPS.getName()}, true),
173 new StringAttributeRule(ECM_DATA_DIR,
174 "The directory with the ECM data file",
175 "ecmdata", true),
176 new StringAttributeRule(ECM_DATA_MATRIX,
177 "The csv file with the ECM data matrix",
178 "matrix.csv", true),
179 new StringAttributeRule(ECM_FREQ_MATRIX,
180 "The csv file with the ECM frequency matrix",
181 "freqs.csv", true),
182 new ElementRule(OMEGA,
183 new XMLSyntaxRule[] { new ElementRule(Parameter.class) }),
184 new XORRule(
185 new ElementRule(KAPPATSTV,
186 new XMLSyntaxRule[] { new ElementRule(Parameter.class) }),
187 new ElementRule(MULTI_NT_CHANGE,
188 new XMLSyntaxRule[] { new ElementRule(Parameter.class) })),
189 new ElementRule(FrequencyModel.class, true)
190 };
191 }
192