Debian Codegraph @

src/dr/oldevomodel/substmodel/EmpiricalCodonRateMatrix.java

raw symbols SCIP
1 /*
2 * EmpiricalCodonRateMatrix.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.oldevomodel.substmodel;
27
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.FileNotFoundException;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.util.StringTokenizer;
34 import java.util.logging.Logger;
35
36 import dr.evolution.datatype.DataType;
37
38 /**
39 * Empirical rate matrix
40 *
41 * @author Stefan Zoller
42 *
43 */
44 public class EmpiricalCodonRateMatrix {
45
46 /**
47 * constructor
48 *
49 * @param name Name of matrix
50 * @param dataType Data type as Codons.UNIVERSAL
51 * @param dir Directory that contains csv files with matrices
52 * @param fName Name of frequency csv file
53 * @param mName Name of initial matrix csv file
54 */
55 public EmpiricalCodonRateMatrix(String name, DataType dataType, String dir, String fName, String mName) {
56 this.name = name;
57 this.dataType = dataType;
58 this.dataDir = dir;
59 this.matName = mName;
60 this.freqName = fName;
61
62 setupRates();
63 setupFreqs();
64 }
65
66 public final String getName() { return name; }
67 public final DataType getDataType() { return dataType; }
68
69 public double[] getRates() { return rates; }
70 public double[] getFrequencies() { return frequencies; }
71
72 public String getFreqName() { return freqName; }
73 public String getMatName() { return matName; }
74 public String getDirName() { return dataDir; }
75
76 public void setFrequencies(double[] f) {
77 this.frequencies = f;
78 }
79
80 public void setRates(double[] p) {
81 this.rates = p;
82 }
83
84 private void setupRates() {
85 this.rates = new double[1830];
86 readFile(this.rates, matName, 1830);
87 }
88
89 private void setupFreqs() {
90 this.frequencies = new double[61];
91 readFile(this.frequencies, freqName, 61);
92 }
93
94 // read csv data
95 private void readFile(double[] r, String f, int nr) {
96 File file = new File(dataDir, f);
97
98 try {
99 BufferedReader bufRdr = new BufferedReader(new FileReader(file));
100 String line = null;
101 int col = 0;
102
103 while((line = bufRdr.readLine()) != null) {
104 StringTokenizer st = new StringTokenizer(line,",");
105 while (st.hasMoreTokens()) {
106 r[col] = Double.valueOf(st.nextToken()).doubleValue();
107 col++;
108 }
109 }
110 bufRdr.close();
111 if(col != nr) {
112 Logger.getLogger("dr.evomodel").severe("Matrix does not contain " + nr + " values but " + col);
113 }
114 } catch (FileNotFoundException e) {
115 Logger.getLogger("dr.evomodel").severe("Caught FileNotFoundException: " + e.getMessage());
116 } catch (IOException e) {
117 Logger.getLogger("dr.evomodel").severe("Caught IOException: " + e.getMessage());
118 } finally {
119 }
120 }
121
122 protected double[] rates;
123 protected double[] frequencies;
124
125 private String name;
126 protected String dataDir;
127 protected String matName;
128 protected String freqName;
129 protected DataType dataType;
130 }