| 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 |
package dr.oldevomodel.substmodel; |
| 27 |
|
| 28 |
import dr.evolution.datatype.Codons; |
| 29 |
import dr.oldevomodelxml.substmodel.PCACodonModelParser; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class PCACodonModel extends AbstractCodonModel { |
| 38 |
|
| 39 |
protected AbstractPCARateMatrix rateMatrix; |
| 40 |
|
| 41 |
protected byte[] rateMap; |
| 42 |
|
| 43 |
private Parameter pcFactors; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public PCACodonModel(Codons codonDataType, |
| 54 |
AbstractPCARateMatrix pcaType, |
| 55 |
Parameter pcaDimensionParameter, |
| 56 |
FrequencyModel freqModel) |
| 57 |
{ |
| 58 |
super(PCACodonModelParser.PCA_CODON_MODEL, codonDataType, freqModel); |
| 59 |
|
| 60 |
this.rateMatrix = pcaType; |
| 61 |
|
| 62 |
|
| 63 |
this.pcFactors = pcaDimensionParameter; |
| 64 |
double[] startFacs = pcaType.getStartFacs(); |
| 65 |
double facSum = 0.0; |
| 66 |
for(int i=0; i<pcFactors.getDimension(); i++) { |
| 67 |
facSum += startFacs[i]; |
| 68 |
} |
| 69 |
for(int i=0; i<pcFactors.getDimension(); i++) { |
| 70 |
pcFactors.setParameterValueQuietly(i, startFacs[i]/facSum); |
| 71 |
} |
| 72 |
addVariable(pcFactors); |
| 73 |
pcFactors.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, |
| 74 |
pcFactors.getDimension())); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public void setupRelativeRates() { |
| 80 |
double[] m = rateMatrix.getMeans(); |
| 81 |
double[] sc = rateMatrix.getScales(); |
| 82 |
for (int i = 0; i < rateCount; i++) { |
| 83 |
relativeRates[i] = m[i]; |
| 84 |
} |
| 85 |
for(int j = 0; j < pcFactors.getDimension(); j++) { |
| 86 |
double[] pc = rateMatrix.getPCAt(j); |
| 87 |
double f = pcFactors.getParameterValue(j); |
| 88 |
for (int i = 0; i < rateCount; i++) { |
| 89 |
relativeRates[i] += f*pc[i]*sc[i]; |
| 90 |
} |
| 91 |
} |
| 92 |
for (int i = 0; i < rateCount; i++) { |
| 93 |
if(relativeRates[i]<0) { |
| 94 |
relativeRates[i] = 0.0; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
protected void ratesChanged() { |
| 100 |
} |
| 101 |
|
| 102 |
protected void frequenciesChanged() { |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public double getPcFactor(int dim) { |
| 110 |
return pcFactors.getParameterValue(dim); |
| 111 |
} |
| 112 |
|
| 113 |
public double[] getPcFactor() { |
| 114 |
return pcFactors.getParameterValues(); |
| 115 |
} |
| 116 |
|
| 117 |
public void setPcFactor(int dim, double fac) { |
| 118 |
pcFactors.setParameterValue(dim, fac); |
| 119 |
updateMatrix = true; |
| 120 |
} |
| 121 |
|
| 122 |
public void setPcFactor(double[] fac) { |
| 123 |
for(int i=0; i<pcFactors.getDimension(); i++) { |
| 124 |
pcFactors.setParameterValue(i, fac[i]); |
| 125 |
} |
| 126 |
updateMatrix = true; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public String toXHTML() { |
| 134 |
StringBuffer buffer = new StringBuffer(); |
| 135 |
|
| 136 |
buffer.append("<em>PCA Codon Model</em>"); |
| 137 |
|
| 138 |
return buffer.toString(); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
static String format1 = "%2.1e"; |
| 143 |
static String format2 = "%2.4e"; |
| 144 |
|
| 145 |
public String printQ() { |
| 146 |
double[][] myQ = getQ(); |
| 147 |
if (myQ != null) { |
| 148 |
StringBuffer sb = new StringBuffer(); |
| 149 |
for (int i = 0; i < myQ.length; i++) { |
| 150 |
for(int j = 0; j < myQ[0].length; j++) { |
| 151 |
sb.append(String.format(format1, myQ[i][j])); |
| 152 |
sb.append("\t"); |
| 153 |
} |
| 154 |
sb.append("\n"); |
| 155 |
} |
| 156 |
return sb.toString(); |
| 157 |
} else { |
| 158 |
return "No Q ready."; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public String printRelRates() { |
| 163 |
StringBuffer sb = new StringBuffer(); |
| 164 |
for (int i = 0; i < relativeRates.length; i++) { |
| 165 |
sb.append(String.format(format2, relativeRates[i])); |
| 166 |
sb.append("\t"); |
| 167 |
} |
| 168 |
sb.append("\n"); |
| 169 |
return sb.toString(); |
| 170 |
} |
| 171 |
} |