Debian Codegraph @

src/dr/evomodelxml/substmodel/FrequencyModelParser.java

raw symbols SCIP
1 /*
2 * FrequencyModelParser.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 import dr.evomodel.substmodel.FrequencyModel;
29 import dr.app.bss.Utils;
30 import dr.evolution.alignment.PatternList;
31 import dr.evolution.datatype.Codons;
32 import dr.evolution.datatype.DataType;
33 import dr.evolution.datatype.HiddenDataType;
34 import dr.evolution.datatype.Nucleotides;
35 import dr.evoxml.util.DataTypeUtils;
36 import dr.inference.model.Parameter;
37 import dr.xml.*;
38
39 import java.text.NumberFormat;
40 import java.util.Arrays;
41 import java.util.List;
42 import java.util.logging.Logger;
43
44 /**
45 * @author Alexei Drummond
46 * @author Andrew Rambaut
47 */
48 public class FrequencyModelParser extends AbstractXMLObjectParser {
49
50 public static final String FREQUENCIES = "frequencies";
51 public static final String FREQUENCY_MODEL = "frequencyModel";
52 public static final String NORMALIZE = "normalize";
53 public static final String COMPRESS = "compress";
54
55 public static final String COMPOSITION = "composition";
56 public static final String FREQ_3x4 = "3x4";
57 public static final String[] COMPOSITION_TYPES = new String[] { FREQ_3x4 };
58
59 public String getParserName() {
60 return FREQUENCY_MODEL;
61 }
62
63 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
64
65 DataType dataType = DataTypeUtils.getDataType(xo);
66
67 Parameter freqsParam = (Parameter) xo.getElementFirstChild(FREQUENCIES);
68 double[] frequencies = null;
69
70 for (int i = 0; i < xo.getChildCount(); i++) {
71 Object obj = xo.getChild(i);
72 if (obj instanceof PatternList) {
73
74 PatternList patternList = (PatternList) obj;
75 if (xo.getAttribute(COMPRESS, false)
76 && (patternList.getDataType() instanceof HiddenDataType)) {
77
78 double[] hiddenFrequencies = patternList
79 .getStateFrequencies();
80 int hiddenCount = ((HiddenDataType) patternList
81 .getDataType()).getHiddenClassCount();
82 int baseStateCount = hiddenFrequencies.length / hiddenCount;
83 frequencies = new double[baseStateCount];
84 for (int j = 0; j < baseStateCount; ++j) {
85 for (int k = 0; k < hiddenCount; ++k) {
86 frequencies[j] += hiddenFrequencies[j + k
87 * baseStateCount];
88 }
89 }
90
91 } else {
92
93 // TODO
94 if (xo.hasAttribute(COMPOSITION)) {
95
96 String type = xo.getStringAttribute(COMPOSITION);
97 if (type.equalsIgnoreCase(FREQ_3x4)) {
98
99 frequencies = getEmpirical3x4Freqs(patternList);
100
101 }
102
103 } else {
104
105 frequencies = patternList.getStateFrequencies();
106
107 } // END: composition check
108
109 }
110 break;
111 }// END: patternList check
112 }
113
114 StringBuilder sb = new StringBuilder(
115 "\nCreating state frequencies model '"
116 + freqsParam.getParameterName() + "': ");
117 if (frequencies != null) {
118
119 if (freqsParam.getDimension() != frequencies.length) {
120 throw new XMLParseException(
121 "dimension of frequency parameter and number of sequence states don't match.");
122 }
123
124 for (int j = 0; j < frequencies.length; j++) {
125 freqsParam.setParameterValue(j, frequencies[j]);
126 }
127
128 sb.append("Using empirical frequencies from data ");
129
130 } else {
131 sb.append("Initial frequencies ");
132 }
133 sb.append("= {");
134
135 if (xo.getAttribute(NORMALIZE, false)) {
136 double sum = 0;
137 for (int j = 0; j < freqsParam.getDimension(); j++)
138 sum += freqsParam.getParameterValue(j);
139 for (int j = 0; j < freqsParam.getDimension(); j++) {
140 if (sum != 0)
141 freqsParam.setParameterValue(j,
142 freqsParam.getParameterValue(j) / sum);
143 else
144 freqsParam.setParameterValue(j,
145 1.0 / freqsParam.getDimension());
146 }
147 }
148
149 NumberFormat format = NumberFormat.getNumberInstance();
150 format.setMaximumFractionDigits(5);
151
152 sb.append(format.format(freqsParam.getParameterValue(0)));
153 for (int j = 1; j < freqsParam.getDimension(); j++) {
154 sb.append(", ");
155 sb.append(format.format(freqsParam.getParameterValue(j)));
156 }
157 sb.append("}");
158 Logger.getLogger("dr.evomodel").info(sb.toString());
159
160 return new FrequencyModel(dataType, freqsParam);
161 }// END: parseXMLObject
162
163 private double[] getEmpirical3x4Freqs(PatternList patternList) {
164
165 DataType nucleotideDataType = Nucleotides.INSTANCE;
166 Codons codonDataType = Codons.UNIVERSAL;
167 List<String> stopCodonsList = Arrays.asList(Utils.STOP_CODONS);
168
169 int cStateCount = codonDataType.getStateCount();
170 int nStateCount = nucleotideDataType.getStateCount();
171 int npos = 3;
172
173 double[] stopCodonFreqs = new double[Utils.STOP_CODONS.length];
174 double counts[][] = new double[nStateCount][npos];
175 int countsPos[] = new int[npos];
176
177 int patternCount = patternList.getPatternCount();
178 for (int i = 0; i < patternCount; i++) {
179
180 int[] sitePatterns = patternList.getPattern(i);
181 for (int k = 0; k < sitePatterns.length; k++) {
182
183 int codonState = sitePatterns[k];
184 int[] nucleotideStates = codonDataType.getTripletStates(codonState);
185
186 String triplet = codonDataType.getTriplet(codonState);
187
188 // if(triplet.equals("TAA")) {
189 // System.err.println(triplet);
190 // }
191
192 if(stopCodonsList.contains(triplet)) {
193
194 int stopCodonIndex = stopCodonsList.indexOf(triplet);
195 stopCodonFreqs[stopCodonIndex]++;
196
197 }//END: stopCodon check
198
199 for(int pos = 0; pos < npos; pos++) {
200
201 int nucleotideState = nucleotideStates[pos];
202 counts[nucleotideState][pos]++;
203 countsPos[pos]++;
204
205 }//END: nucleotide positions loop
206
207 }//END: sitePatterns loop
208 }// sites loop
209
210 int total = 0;
211 for (int pos = 0; pos < npos; pos++) {
212
213 int totalPos = countsPos[pos];
214 for (int s = 0; s < nStateCount; s++) {
215 counts[s][pos] = counts[s][pos] / totalPos;
216 }//END: nucleotide states loop
217
218 total += totalPos;
219 }//END: nucleotide positions loop
220
221 // Utils.printArray(stopCodonFreqs);
222 // System.out.println(stopCodonFreqs.length);
223
224 // add stop codon frequencies
225 double pi = 0.0;
226 for(int i=0;i<stopCodonFreqs.length;i++) {
227 double freq = stopCodonFreqs[i] / total;
228 pi += freq;
229 }
230
231 // System.out.println(pi);
232
233 double[] freqs = new double[cStateCount];
234 Arrays.fill(freqs, 1.0);
235 for (int codonState = 0; codonState < cStateCount; codonState++) {
236
237 int[] nucleotideStates = codonDataType.getTripletStates(codonState);
238 for (int pos = 0; pos < npos; pos++) {
239
240 int nucleotide = nucleotideStates[pos];
241 freqs[codonState] *= counts[nucleotide][pos];
242
243 }// END: nucleotide positions loop
244
245 // TODO: stop codons freqs
246 freqs[codonState] = freqs[codonState] / (1-pi);
247
248 }//END: codon states loop
249
250
251
252
253
254
255
256
257 return freqs;
258 }// END: getEmpirical3x4Freqs
259
260
261 public String getParserDescription() {
262 return "A model of equilibrium base frequencies.";
263 }
264
265 public Class getReturnType() {
266 return FrequencyModel.class;
267 }
268
269 public XMLSyntaxRule[] getSyntaxRules() {
270 return rules;
271 }
272
273 private final XMLSyntaxRule[] rules = {
274
275 new StringAttributeRule(COMPOSITION, "Composition type",
276 COMPOSITION_TYPES, true),
277
278 AttributeRule.newBooleanRule(NORMALIZE, true),
279 AttributeRule.newBooleanRule(COMPRESS, true),
280
281 new ElementRule(PatternList.class, "Initial value", 0, 1),
282
283 new XORRule(new StringAttributeRule(DataType.DATA_TYPE,
284 "The type of sequence data",
285 DataType.getRegisteredDataTypeNames(), false),
286 new ElementRule(DataType.class)),
287
288 new ElementRule(FREQUENCIES, new XMLSyntaxRule[] { new ElementRule(
289 Parameter.class) }),
290
291 };
292
293 }