Debian Codegraph @

src/dr/oldevomodelxml/substmodel/PCACodonModelParser.java

raw symbols SCIP
1 /*
2 * PCACodonModelParser.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.AbstractPCARateMatrix;
36 import dr.oldevomodel.substmodel.FrequencyModel;
37 import dr.oldevomodel.substmodel.PCACodonModel;
38 import dr.oldevomodel.substmodel.PCARateMatrixMammalia;
39 import dr.xml.*;
40
41 /**
42 * XML parser for PCACodonModel
43 *
44 * @author Stefan Zoller
45 */
46 public class PCACodonModelParser extends AbstractXMLObjectParser {
47 public static final String PCA_CODON_MODEL = "pcaCodonModel";
48 public static final String PCATYPE = "pcaType";
49 public static final String PCANUMBER = "pcaNumber";
50 public static final String PCA_DIMENSION = "pcaDimension";
51 public static final String PCA_DATA_DIR = "pcaDataDir";
52
53 public String getParserName() { return PCA_CODON_MODEL; }
54
55 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
56
57 Codons codons = Codons.UNIVERSAL;
58 if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) {
59 String codeStr = xo.getStringAttribute(GeneticCode.GENETIC_CODE);
60 if (codeStr.equals(GeneticCode.UNIVERSAL.getName())) {
61 codons = Codons.UNIVERSAL;
62 } else if (codeStr.equals(GeneticCode.VERTEBRATE_MT.getName())) {
63 codons = Codons.VERTEBRATE_MT;
64 } else if (codeStr.equals(GeneticCode.YEAST.getName())) {
65 codons = Codons.YEAST;
66 } else if (codeStr.equals(GeneticCode.MOLD_PROTOZOAN_MT.getName())) {
67 codons = Codons.MOLD_PROTOZOAN_MT;
68 } else if (codeStr.equals(GeneticCode.INVERTEBRATE_MT.getName())) {
69 codons = Codons.INVERTEBRATE_MT;
70 } else if (codeStr.equals(GeneticCode.CILIATE.getName())) {
71 codons = Codons.CILIATE;
72 } else if (codeStr.equals(GeneticCode.ECHINODERM_MT.getName())) {
73 codons = Codons.ECHINODERM_MT;
74 } else if (codeStr.equals(GeneticCode.EUPLOTID_NUC.getName())) {
75 codons = Codons.EUPLOTID_NUC;
76 } else if (codeStr.equals(GeneticCode.BACTERIAL.getName())) {
77 codons = Codons.BACTERIAL;
78 } else if (codeStr.equals(GeneticCode.ALT_YEAST.getName())) {
79 codons = Codons.ALT_YEAST;
80 } else if (codeStr.equals(GeneticCode.ASCIDIAN_MT.getName())) {
81 codons = Codons.ASCIDIAN_MT;
82 } else if (codeStr.equals(GeneticCode.FLATWORM_MT.getName())) {
83 codons = Codons.FLATWORM_MT;
84 } else if (codeStr.equals(GeneticCode.BLEPHARISMA_NUC.getName())) {
85 codons = Codons.BLEPHARISMA_NUC;
86 } else if (codeStr.equals(GeneticCode.NO_STOPS.getName())) {
87 codons = Codons.NO_STOPS;
88 }
89 }
90
91 // get number of PCs
92 Parameter pcaDimensionParameter = (Parameter)xo.getElementFirstChild(PCA_DIMENSION);
93
94 // get directory with pca rate matrix files; fallback to default "pcadata"
95 String dirString = "pcadata";
96 if(xo.hasAttribute(PCA_DATA_DIR)) {
97 dirString = xo.getStringAttribute(PCA_DATA_DIR);
98 }
99
100 // get type of rate matrix; fallback to mammalia pca
101 AbstractPCARateMatrix pcaType = new PCARateMatrixMammalia(pcaDimensionParameter.getDimension(), dirString);
102 // check for other type of pca
103 if(xo.hasAttribute(PCATYPE)) {
104 String pcaTypeString = xo.getStringAttribute(PCATYPE);
105 if(pcaTypeString.equals(PCARateMatrixMammalia.getName())) {
106 pcaType = new PCARateMatrixMammalia(pcaDimensionParameter.getDimension(), dirString);
107 }
108 }
109
110 // decide if getting frequencies from csv or estimating from MSA
111 FrequencyModel freqModel = null;
112 if (xo.getChild(FrequencyModel.class) != null) {
113 freqModel = (FrequencyModel)xo.getChild(FrequencyModel.class);
114 } else {
115 freqModel = createNewFreqModel(codons, pcaType);
116 }
117
118 return new PCACodonModel(codons, pcaType, pcaDimensionParameter, freqModel);
119 }
120
121 // read frequencies from XML and return FrequencyModel object
122 private FrequencyModel createNewFreqModel(DataType codons, AbstractPCARateMatrix type) throws XMLParseException {
123 double[] freqs = type.getFrequencies();
124 double sum = 0;
125 for (int j = 0; j < freqs.length; j++) {
126 sum += freqs[j];
127 }
128
129 if (Math.abs(sum - 1.0) > 1e-8) {
130 throw new XMLParseException("Frequencies do not sum to 1 (they sum to " + sum + ")");
131 }
132
133 FrequencyModel fm = new FrequencyModel(codons, freqs);
134
135 Logger.getLogger("dr.evomodel").info("Using frequencies from data file");
136
137 return fm;
138 }
139
140 //************************************************************************
141 // AbstractXMLObjectParser implementation
142 //************************************************************************
143
144 public String getParserDescription() {
145 return "This element represents the PCA model of codon evolution.";
146 }
147
148 public Class getReturnType() { return PCACodonModel.class; }
149
150 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
151
152 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
153 new StringAttributeRule(GeneticCode.GENETIC_CODE,
154 "The genetic code to use",
155 new String[] {
156 GeneticCode.UNIVERSAL.getName(),
157 GeneticCode.VERTEBRATE_MT.getName(),
158 GeneticCode.YEAST.getName(),
159 GeneticCode.MOLD_PROTOZOAN_MT.getName(),
160 GeneticCode.INVERTEBRATE_MT.getName(),
161 GeneticCode.CILIATE.getName(),
162 GeneticCode.ECHINODERM_MT.getName(),
163 GeneticCode.EUPLOTID_NUC.getName(),
164 GeneticCode.BACTERIAL.getName(),
165 GeneticCode.ALT_YEAST.getName(),
166 GeneticCode.ASCIDIAN_MT.getName(),
167 GeneticCode.FLATWORM_MT.getName(),
168 GeneticCode.BLEPHARISMA_NUC.getName(),
169 GeneticCode.NO_STOPS.getName()}, true),
170 new StringAttributeRule(PCATYPE,
171 "The PCA type to use",
172 new String[] {
173 PCARateMatrixMammalia.getName(),
174 }, true),
175 new StringAttributeRule(PCA_DATA_DIR,
176 "The directory with the PCA data files",
177 "pcadata", true),
178 new ElementRule(PCA_DIMENSION,
179 new XMLSyntaxRule[] { new ElementRule(Parameter.class) }),
180 new ElementRule(FrequencyModel.class, true)
181 };
182 }
183