Debian Codegraph @

src/dr/oldevomodel/substmodel/EmpiricalRateMatrix.java

raw symbols SCIP
1 /*
2 * EmpiricalRateMatrix.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.AminoAcids;
29 import dr.evolution.datatype.DataType;
30 import dr.util.Citable;
31
32 /**
33 * An interface for empirical rate matrices.
34 *
35 * @version $Id: EmpiricalRateMatrix.java,v 1.3 2005/05/24 20:25:58 rambaut Exp $
36 *
37 * @author Andrew Rambaut
38 * @author Alexei Drummond
39 */
40 public interface EmpiricalRateMatrix extends Citable {
41
42 String getName();
43 DataType getDataType();
44
45 double[] getEmpiricalRates();
46 double[] getEmpiricalFrequencies();
47
48 public abstract class Abstract implements EmpiricalRateMatrix {
49
50 public Abstract(String name, DataType dataType) {
51 this.name = name;
52 this.dataType = dataType;
53 }
54
55 public final String getName() { return name; }
56
57 public final DataType getDataType() { return dataType; }
58
59 public final double[] getEmpiricalRates() { return rates; }
60 public final double[] getEmpiricalFrequencies() { return frequencies; }
61
62 protected double[] rates = null;
63 protected double[] frequencies = null;
64
65 private String name;
66 protected DataType dataType;
67 }
68
69 public abstract class AbstractAminoAcid extends Abstract {
70
71 public AbstractAminoAcid(String name) {
72 super(name, AminoAcids.INSTANCE);
73
74 int n = dataType.getStateCount();
75 rates = new double[(n * (n - 1)) / 2];
76 frequencies = new double[n];
77 }
78
79 public final void setEmpiricalRates(double[][]matrix, String aminoAcidOrder) {
80 int k = 0;
81
82 for (int i = 0; i < dataType.getStateCount(); i++) {
83
84 int u = aminoAcidOrder.indexOf(dataType.getChar(i));
85
86 for (int j = i + 1; j < dataType.getStateCount(); j++) {
87
88 int v = aminoAcidOrder.indexOf(dataType.getChar(j));
89
90 if (u < v) {
91 rates[k] = matrix[u][v];
92 } else {
93 rates[k] = matrix[v][u];
94 }
95
96 k++;
97 }
98 }
99 }
100
101 public final void setEmpiricalFrequencies(double[]freqs, String aminoAcidOrder) {
102
103 double sum = 0.0;
104 for (int i = 0; i < dataType.getStateCount(); i++) {
105 int u = aminoAcidOrder.indexOf(dataType.getChar(i));
106 frequencies[i] = freqs[u];
107 sum += frequencies[i];
108 }
109
110 // normalize - we should probably detect large discrepancies but the empirical
111 // matrices have numerical rounding that cause small discrepancies.
112 for (int i = 0; i < dataType.getStateCount(); i++) {
113 frequencies[i] /= sum;
114 }
115 }
116 }
117 }