-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRateMatrix.java
88 lines (69 loc) · 2.84 KB
/
RateMatrix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package lphy.base.evolution.substitutionmodel;
import lphy.core.model.DeterministicFunction;
import lphy.core.model.Value;
import lphy.core.model.annotation.ParameterInfo;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
/**
* Created by Alexei Drummond on 2/02/20.
*/
public abstract class RateMatrix extends DeterministicFunction<Double[][]> {
public static final String meanRateParamName = "meanRate";
public RateMatrix(@ParameterInfo(name = meanRateParamName, description = "the mean rate of the process. Default value is 1.0.", optional = true) Value<Number> rate) {
if (rate != null) setParam(meanRateParamName, rate);
}
void normalize(Double[] freqs, Double[][] Q) {
normalize(freqs, Q, totalRateDefault1());
}
protected void normalize(Double[] freqs, Double[][] Q, double rate) {
// normalise rate matrix to one expected substitution per unit time
double subst = 0.0;
for (int i = 0; i < Q.length; i++) {
subst += -Q[i][i] * freqs[i];
}
for (int i = 0; i < Q.length; i++) {
for (int j = 0; j < Q.length; j++) {
Q[i][j] = rate * (Q[i][j] / subst);
}
}
}
// Java Double matter
Double[][] normalize(double[] freqs, double[][] Q, double rate) {
Double[][] Qn = new Double[Q.length][Q.length];
// normalise rate matrix to one expected substitution per unit time
double subst = 0.0;
for (int i = 0; i < Q.length; i++) {
subst += -Q[i][i] * freqs[i];
}
for (int i = 0; i < Q.length; i++) {
for (int j = 0; j < Q.length; j++) {
Qn[i][j] = rate * (Q[i][j] / subst);
}
}
return Qn;
}
public double totalRateDefault1() {
Value<Double> meanRate = getMeanRate();
if (meanRate != null) return meanRate.value();
return 1.0;
}
public Value<Double> getMeanRate() {
return getParams().get(meanRateParamName);
}
public static void main(String... args) throws ClassNotFoundException {
Constructor constructor = RateMatrix.class.getConstructors()[0];
System.out.println("Generic parameter types");
Type[] generics = constructor.getGenericParameterTypes();
for (int i = 0; i < generics.length; i++) {
String name = generics[i].getTypeName();
System.out.println("name=" + name);
System.out.println("class=" + Class.forName(name.substring(name.indexOf('<')+1, name.indexOf('>'))));
}
System.out.println("Type variables");
TypeVariable[] typeVariables = constructor.getTypeParameters();
for (int i = 0; i < typeVariables.length; i++) {
System.out.println("name=" + typeVariables[i].getName());
}
}
}