Debian Codegraph @

src/dr/evomodelxml/substmodel/EmpiricalCodonModelParser.java

raw symbols SCIP
1 /*
2 * EmpiricalCodonModelParser.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.evomodelxml.substmodel;
27
28
29 import java.util.logging.Logger;
30
31 import dr.evomodel.substmodel.codon.EmpiricalCodonModel;
32 import dr.evolution.datatype.Codons;
33 import dr.evolution.datatype.DataType;
34 import dr.evolution.datatype.GeneticCode;
35 import dr.evomodel.substmodel.EmpiricalRateMatrixReader;
36 import dr.evomodel.substmodel.FrequencyModel;
37 import dr.inference.model.Parameter;
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
55 public String getParserName() { return EMPIRICAL_CODON_MODEL; }
56
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58
59 Codons codons = Codons.UNIVERSAL;
60 if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) {
61 String codeStr = xo.getStringAttribute(GeneticCode.GENETIC_CODE);
62 if (codeStr.equals(GeneticCode.UNIVERSAL.getName())) {
63 codons = Codons.UNIVERSAL;
64 } else if (codeStr.equals(GeneticCode.VERTEBRATE_MT.getName())) {
65 codons = Codons.VERTEBRATE_MT;
66 } else if (codeStr.equals(GeneticCode.YEAST.getName())) {
67 codons = Codons.YEAST;
68 } else if (codeStr.equals(GeneticCode.MOLD_PROTOZOAN_MT.getName())) {
69 codons = Codons.MOLD_PROTOZOAN_MT;
70 } else if (codeStr.equals(GeneticCode.INVERTEBRATE_MT.getName())) {
71 codons = Codons.INVERTEBRATE_MT;
72 } else if (codeStr.equals(GeneticCode.CILIATE.getName())) {
73 codons = Codons.CILIATE;
74 } else if (codeStr.equals(GeneticCode.ECHINODERM_MT.getName())) {
75 codons = Codons.ECHINODERM_MT;
76 } else if (codeStr.equals(GeneticCode.EUPLOTID_NUC.getName())) {
77 codons = Codons.EUPLOTID_NUC;
78 } else if (codeStr.equals(GeneticCode.BACTERIAL.getName())) {
79 codons = Codons.BACTERIAL;
80 } else if (codeStr.equals(GeneticCode.ALT_YEAST.getName())) {
81 codons = Codons.ALT_YEAST;
82 } else if (codeStr.equals(GeneticCode.ASCIDIAN_MT.getName())) {
83 codons = Codons.ASCIDIAN_MT;
84 } else if (codeStr.equals(GeneticCode.FLATWORM_MT.getName())) {
85 codons = Codons.FLATWORM_MT;
86 } else if (codeStr.equals(GeneticCode.BLEPHARISMA_NUC.getName())) {
87 codons = Codons.BLEPHARISMA_NUC;
88 } else if (codeStr.equals(GeneticCode.NO_STOPS.getName())) {
89 codons = Codons.NO_STOPS;
90 }
91 }
92
93 Parameter omegaParam = (Parameter)xo.getElementFirstChild(OMEGA);
94 Parameter kappaParam = null;
95 Parameter mntParam = null;
96 if(xo.hasChildNamed(KAPPATSTV)) {
97 kappaParam = (Parameter)xo.getElementFirstChild(KAPPATSTV);
98 if(kappaParam.getDimension() != 2 && kappaParam.getDimension() != 9) {
99 throw new XMLParseException("If you use the kappa parameter, you need to enter exactly\n" +
100 "two values for ts and tv or nine values\n" +
101 "according to the Kosiol ECM+F+omega+9k model");
102 }
103 } else {
104 mntParam = (Parameter)xo.getElementFirstChild(MULTI_NT_CHANGE);
105 }
106
107 String dirString = xo.getStringAttribute(ECM_DATA_DIR);
108 String freqString = xo.getStringAttribute(ECM_FREQ_MATRIX);
109 String matString = xo.getStringAttribute(ECM_DATA_MATRIX);
110
111 EmpiricalRateMatrixReader rateMat = new EmpiricalRateMatrixReader(EMPIRICAL_RATE_MATRIX, codons,
112 dirString, freqString, matString);
113
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, EmpiricalRateMatrixReader 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 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