Debian Codegraph @

src/dr/evomodel/substmodel/PCARateMatrixMammalia.java

raw symbols SCIP
1 /*
2 * PCARateMatrixMammalia.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.evomodel.substmodel;
27
28 import dr.evolution.datatype.Codons;
29 import java.util.*;
30 import java.io.*;
31
32 /**
33 * Mammalia rate matrix for PCACodonModel
34 *
35 * @author Stefan Zoller
36 *
37 */
38 public class PCARateMatrixMammalia extends AbstractPCARateMatrix{
39
40 // filenames for csv files including actual rate matrices
41 public static final String FREQS_FILE = "freqs.csv";
42 public static final String SCALES_FILE = "scales.csv";
43 public static final String MEANS_FILE = "means.csv";
44 public static final String FACTORS_FILE = "startfacs.csv";
45 public static final String PCS_FILE = "pcs.csv";
46
47 public PCARateMatrixMammalia(int nrOfPCs, String dir) {
48 super("mammalia", Codons.UNIVERSAL, dir);
49
50 // reading data files
51 setupMeans();
52 setupFreqs();
53 setupScales();
54 setupStartFacs();
55 setupPCs(nrOfPCs);
56 }
57
58 private void setupFreqs() {
59 // 61 frequencies for the codons
60 double[] f = readSingleArray(FREQS_FILE, 61);
61 setFrequencies(f);
62 }
63
64 private void setupScales() {
65 // 1830 scale factors for the PCs
66 double[] s = readSingleArray(SCALES_FILE, 1830);
67 setScales(s);
68 }
69
70 private void setupMeans() {
71 // 1830 mean values for the PCs
72 double[] m = readSingleArray(MEANS_FILE, 1830);
73 setMeans(m);
74 }
75
76 private void setupStartFacs() {
77 // 1830 start factors for the PCs
78 double[] sf = readSingleArray(FACTORS_FILE, 1830);
79 setStartFacs(sf);
80 }
81
82 private void setupPCs(int nr) {
83 double[][] p = new double[nr][1830];
84
85 File file = new File(dataDir, PCS_FILE);
86
87 try {
88 BufferedReader bufRdr = new BufferedReader(new FileReader(file));
89
90 String line = null;
91 int row = 0;
92 int col = 0;
93
94 //read each line of text file
95 while(((line = bufRdr.readLine()) != null) && (row < nr))
96 {
97 col = 0;
98 StringTokenizer st = new StringTokenizer(line,",");
99 while (st.hasMoreTokens())
100 {
101 //get next token and store it in the array
102 p[row][col] = Double.valueOf(st.nextToken()).doubleValue();
103 col++;
104 }
105 row++;
106 }
107 bufRdr.close();
108 } catch (FileNotFoundException e) {
109 System.err.println("Caught FileNotFoundException: " + e.getMessage());
110 } catch (IOException e) {
111 System.err.println("Caught IOException: " + e.getMessage());
112 } finally {
113 }
114 setPCs(p);
115 }
116
117 private double[] readSingleArray(String filename, int dim) {
118 File file = new File(dataDir, filename);
119 double[] arr = new double[dim];
120
121 try {
122 BufferedReader bufRdr = new BufferedReader(new FileReader(file));
123 String line = null;
124 int col = 0;
125
126 while((line = bufRdr.readLine()) != null) {
127 StringTokenizer st = new StringTokenizer(line,",");
128 while (st.hasMoreTokens()) {
129 arr[col] = Double.valueOf(st.nextToken()).doubleValue();
130 col++;
131 }
132 }
133 bufRdr.close();
134 } catch (FileNotFoundException e) {
135 System.err.println("Caught FileNotFoundException: " + e.getMessage());
136 } catch (IOException e) {
137 System.err.println("Caught IOException: " + e.getMessage());
138 } finally {
139 }
140 return arr;
141 }
142 }