Debian Codegraph @

src/dr/oldevomodel/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.oldevomodel.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 /**
48 * constructor
49 *
50 * @param nrOfPCs Number of PCs to use
51 * @param dir Directory which includes the rate matrix csv files
52 */
53 public PCARateMatrixMammalia(int nrOfPCs, String dir) {
54 super("mammalia", Codons.UNIVERSAL, dir);
55
56 // reading data files:
57 setupMeans();
58 setupFreqs();
59 setupScales();
60 setupStartFacs();
61 setupPCs(nrOfPCs);
62 }
63
64 private void setupFreqs() {
65 // 61 frequencies for the codons
66 double[] f = readSingleArray(FREQS_FILE, 61);
67 setFrequencies(f);
68 }
69
70 private void setupScales() {
71 // 1830 scale factors for the PCs
72 double[] s = readSingleArray(SCALES_FILE, 1830);
73 setScales(s);
74 }
75
76 private void setupMeans() {
77 // 1830 mean values for the PCs
78 double[] m = readSingleArray(MEANS_FILE, 1830);
79 setMeans(m);
80 }
81
82 private void setupStartFacs() {
83 // 1830 start factors for the PCs
84 double[] sf = readSingleArray(FACTORS_FILE, 1830);
85 setStartFacs(sf);
86 }
87
88 private void setupPCs(int nr) {
89 double[][] p = new double[nr][1830];
90
91 File file = new File(dataDir, PCS_FILE);
92
93 try {
94 BufferedReader bufRdr = new BufferedReader(new FileReader(file));
95
96 String line = null;
97 int row = 0;
98 int col = 0;
99
100 //read each line of text file
101 while(((line = bufRdr.readLine()) != null) && (row < nr))
102 {
103 col = 0;
104 StringTokenizer st = new StringTokenizer(line,",");
105 while (st.hasMoreTokens())
106 {
107 //get next token and store it in the array
108 p[row][col] = Double.valueOf(st.nextToken()).doubleValue();
109 col++;
110 }
111 row++;
112 }
113 bufRdr.close();
114 } catch (FileNotFoundException e) {
115 System.err.println("Caught FileNotFoundException: " + e.getMessage());
116 } catch (IOException e) {
117 System.err.println("Caught IOException: " + e.getMessage());
118 } finally {
119 }
120 setPCs(p);
121 }
122
123 private double[] readSingleArray(String filename, int dim) {
124 File file = new File(dataDir, filename);
125 double[] arr = new double[dim];
126
127 try {
128 BufferedReader bufRdr = new BufferedReader(new FileReader(file));
129 String line = null;
130 int col = 0;
131
132 while((line = bufRdr.readLine()) != null) {
133 StringTokenizer st = new StringTokenizer(line,",");
134 while (st.hasMoreTokens()) {
135 arr[col] = Double.valueOf(st.nextToken()).doubleValue();
136 col++;
137 }
138 }
139 bufRdr.close();
140 } catch (FileNotFoundException e) {
141 System.err.println("Caught FileNotFoundException: " + e.getMessage());
142 } catch (IOException e) {
143 System.err.println("Caught IOException: " + e.getMessage());
144 } finally {
145 }
146 return arr;
147 }
148 }