Debian Codegraph @

src/dr/oldevomodel/treelikelihood/NativeNucleotideLikelihoodCore.java

raw symbols SCIP
1 /*
2 * NativeNucleotideLikelihoodCore.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.treelikelihood;
27
28 import dr.app.util.OSType;
29
30 import java.io.File;
31 import java.net.URISyntaxException;
32
33 /**
34 * NativeNucleotideLikelihoodCore - An implementation of LikelihoodCore for nucleotides
35 * that calls native methods for maximum speed. The native methods should be in
36 * a shared library called "NucleotideLikelihoodCore" but the exact name will be system
37 * dependent (i.e. "libNucleotideLikelihoodCore.so" or "NucleotideLikelihoodCore.dll").
38 *
39 * @version $Id: NativeNucleotideLikelihoodCore.java,v 1.14 2006/08/31 14:57:24 rambaut Exp $
40 *
41 * @author Andrew Rambaut
42 */
43
44 @Deprecated // Switching to BEAGLE
45 public class NativeNucleotideLikelihoodCore extends AbstractLikelihoodCore {
46
47 /**
48 * Constructor
49 */
50 public NativeNucleotideLikelihoodCore() {
51
52 super(4);
53
54 }
55
56 protected void calculateStatesStatesPruning(int[] states1, double[] matrices1,
57 int[] states2, double[] matrices2,
58 double[] partials3) {
59 nativeStatesStatesPruning(states1, matrices1, states2, matrices2, patternCount, matrixCount, partials3);
60 }
61
62 protected void calculateStatesPartialsPruning( int[] states1, double[] matrices1,
63 double[] partials2, double[] matrices2,
64 double[] partials3) {
65 nativeStatesPartialsPruning(states1, matrices1, partials2, matrices2, patternCount, matrixCount, partials3);
66 }
67 /*
68 private void out(PrintStream s, double[] a, String name) {
69 s.print("double " + name + "[" + a.length + "] = {");
70 for( double f : a ) s.print(f + ", ");
71 s.println("};");
72 }
73 */
74 protected void calculatePartialsPartialsPruning(double[] partials1, double[] matrices1,
75 double[] partials2, double[] matrices2,
76 double[] partials3) {
77 /* if( false ) {
78 System.out.println("*//*nativePartialsPartialsPruning*//* int patternCount = " +
79 patternCount + "; int matrixCount = " + matrixCount);
80 out(System.out, partials1, "partials1");
81 out(System.out, partials2, "partials2");
82 out(System.out, partials2, "matrices1");
83 out(System.out, partials2, "matrices2");
84 }*/
85 nativePartialsPartialsPruning(partials1, matrices1, partials2, matrices2, patternCount, matrixCount, partials3);
86 }
87
88 /**
89 * Calculates partial likelihoods at a node when both children have states.
90 */
91 protected void calculateStatesStatesPruning(int[] states1, double[] matrices1,
92 int[] states2, double[] matrices2,
93 double[] partials3, int[] matrixMap)
94 {
95 throw new RuntimeException("calculateStatesStatesPruning not implemented using matrixMap");
96 }
97
98 /**
99 * Calculates partial likelihoods at a node when one child has states and one has partials.
100 */
101 protected void calculateStatesPartialsPruning( int[] states1, double[] matrices1,
102 double[] partials2, double[] matrices2,
103 double[] partials3, int[] matrixMap)
104 {
105 throw new RuntimeException("calculateStatesStatesPruning not implemented using matrixMap");
106 }
107
108 /**
109 * Calculates partial likelihoods at a node when both children have partials.
110 */
111 protected void calculatePartialsPartialsPruning(double[] partials1, double[] matrices1,
112 double[] partials2, double[] matrices2,
113 double[] partials3, int[] matrixMap)
114 {
115 throw new RuntimeException("calculateStatesStatesPruning not implemented using matrixMap");
116 }
117
118 protected void calculateIntegratePartials(double[] inPartials, double[] proportions, double[] outPartials)
119 {
120 nativeIntegratePartials(inPartials, proportions, patternCount, matrixCount, outPartials);
121 }
122
123
124 /**
125 * Calculates partial likelihoods at a node when both children have states.
126 */
127 protected native void nativeStatesStatesPruning( int[] states1, double[] matrices1,
128 int[] states2, double[] matrices2,
129 int patternCount, int matrixCount,
130 double[] partials3);
131 protected native void nativeStatesPartialsPruning( int[] states1, double[] matrices1,
132 double[] partials2, double[] matrices2,
133 int patternCount, int matrixCount,
134 double[] partials3);
135 protected native void nativePartialsPartialsPruning(double[] partials1, double[] matrices1,
136 double[] partials2, double[] matrices2,
137 int patternCount, int matrixCount,
138 double[] partials3);
139 public native void nativeIntegratePartials( double[] partials, double[] proportions,
140 int patternCount, int matrixCount,
141 double[] outPartials);
142
143 public void calculateLogLikelihoods(double[] partials, double[] frequencies, double[] outLogLikelihoods)
144 {
145 int v = 0;
146 for (int k = 0; k < patternCount; k++) {
147
148 double sum = frequencies[0] * partials[v]; v++;
149 sum += frequencies[1] * partials[v]; v++;
150 sum += frequencies[2] * partials[v]; v++;
151 sum += frequencies[3] * partials[v]; v++;
152 outLogLikelihoods[k] = Math.log(sum) + getLogScalingFactor(k);
153 }
154 }
155
156 public static boolean isAvailable() { return isNativeAvailable; }
157
158 private static boolean isNativeAvailable = false;
159
160 static {
161 String currentDir = null;
162 try {
163 currentDir = new File(NativeNucleotideLikelihoodCore.class.getProtectionDomain().getCodeSource().
164 getLocation().toURI()).getParent() + System.getProperty("file.separator")
165 + "lib" + System.getProperty("file.separator"); // get path to find lib http://code.google.com/p/beast-mcmc/issues/detail?id=203
166 // System.out.println("currentDir = " + currentDir);
167 } catch (URISyntaxException e) {
168 e.printStackTrace();
169 }
170 try {
171 if (OSType.isWindows()) {
172 System.load(currentDir + "NucleotideLikelihoodCore.dll");
173 } else {
174 currentDir = System.getProperty("user.dir");
175 System.loadLibrary("NucleotideLikelihoodCore");
176 }
177 isNativeAvailable = true;
178 System.out.println("Loading native NucleotideLikelihoodCore successfully from " + currentDir);
179 } catch (UnsatisfiedLinkError e) {
180 System.err.println("Failed to load native NucleotideLikelihoodCore in : " + currentDir);
181 System.err.println("Using Java nucleotide likelihood core because " + e.toString());
182 // System.err.println("Looking for NucleotideLikelihoodCore in java.library.path : " + System.getProperty("java.library.path"));
183 // System.err.println("Looking for NucleotideLikelihoodCore in user.dir : " + System.getProperty("user.dir"));
184
185 }
186
187 }
188 }
189