Debian Codegraph @

src/dr/oldevomodel/sitemodel/ScoreMatrix.java

raw symbols SCIP
1 /*
2 * ScoreMatrix.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.sitemodel;
27
28 import dr.evolution.alignment.SitePatterns;
29
30 /**
31 * @author alexei
32 * @version $Id: ScoreMatrix.java,v 1.2 2005/04/08 15:35:36 rambaut Exp $
33 */
34 public class ScoreMatrix {
35
36 SiteModel siteModel;
37 double time;
38 double[] matrix;
39
40 double[] logp;
41
42 // base frequencies
43
44 double[][] logOddScores;
45 double[][] pscores;
46 int stateCount;
47
48 public ScoreMatrix(SiteModel siteModel, double time) {
49 this.siteModel = siteModel;
50
51 logp = siteModel.getSubstitutionModel().getFrequencyModel().getFrequencies();
52
53 for (int i = 0; i < logp.length; i++) {
54 logp[i] = Math.log(logp[i]);
55 }
56
57 stateCount = siteModel.getFrequencyModel().getFrequencyCount();
58 matrix = new double[stateCount*stateCount];
59 pscores = new double[stateCount][stateCount];
60 logOddScores = new double[stateCount][stateCount];
61
62 setTime(time);
63 }
64
65 public void setTime(double time) {
66 this.time = time;
67 siteModel.getSubstitutionModel().getTransitionProbabilities(time, matrix);
68
69 //base frequencies
70
71 for (int i = 0; i < stateCount; i++) {
72 for (int j = 0; j < stateCount; j++) {
73 pscores[i][j] = Math.log(matrix[i*stateCount + j]);
74 logOddScores[i][j] = pscores[i][j] - logp[i] - logp[j];
75 }
76 }
77 }
78
79 /**
80 * @param i state in first sequences
81 * @param j state in second sequences
82 * @return the score of aligning state i with state j
83 */
84 public final double getScore(int i, int j) {
85 return pscores[i][j] - logp[j];
86 }
87
88 public final double getScore(SitePatterns patterns) {
89
90 if (patterns.getPatternLength() != 2) throw new IllegalArgumentException();
91
92 double logL = 0.0;
93 for (int i = 0; i < patterns.getPatternCount(); i++) {
94 double weight = patterns.getPatternWeight(i);
95 int[] pattern = patterns.getPattern(i);
96
97 int x = pattern[0];
98 int y = pattern[1];
99
100 if (x < stateCount && y < stateCount) {
101 logL += getScore(x,y) * weight;
102 }
103
104 }
105 return logL;
106 }
107 }