| 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.indel; |
| 27 |
|
| 28 |
import dr.evolution.alignment.Alignment; |
| 29 |
import dr.evolution.alignment.SimpleAlignment; |
| 30 |
import dr.evolution.datatype.DataType; |
| 31 |
import dr.evolution.sequence.Sequence; |
| 32 |
import dr.evolution.tree.NodeRef; |
| 33 |
import dr.evolution.tree.Tree; |
| 34 |
import dr.oldevomodel.substmodel.SubstitutionModel; |
| 35 |
import dr.inference.operators.MCMCOperator; |
| 36 |
import dr.inference.operators.SimpleMCMCOperator; |
| 37 |
import org.w3c.dom.Document; |
| 38 |
import org.w3c.dom.Element; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class IstvanOperator extends SimpleMCMCOperator { |
| 49 |
|
| 50 |
private double tuning = 0.1; |
| 51 |
private double exponent = 1.5; |
| 52 |
private double gapPenalty = -10; |
| 53 |
private TKF91Likelihood likelihood; |
| 54 |
private IstvansProposal proposal = new IstvansProposal(); |
| 55 |
private Alignment alignment; |
| 56 |
|
| 57 |
int[][] iAlignment; |
| 58 |
double[][][] iProbs; |
| 59 |
double[] iBaseFreqs; |
| 60 |
int[] iParent; |
| 61 |
double[] iTau; |
| 62 |
|
| 63 |
public IstvanOperator(double iP, double exponent, double gapPenalty, double weight, TKF91Likelihood likelihood) { |
| 64 |
this.tuning = iP; |
| 65 |
this.exponent = exponent; |
| 66 |
this.gapPenalty = gapPenalty; |
| 67 |
this.likelihood = likelihood; |
| 68 |
setWeight(weight); |
| 69 |
} |
| 70 |
|
| 71 |
public double doOperation() { |
| 72 |
|
| 73 |
Tree tree = likelihood.getTreeModel(); |
| 74 |
alignment = likelihood.getAlignment(); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
SubstitutionModel substModel = likelihood.getSiteModel().getSubstitutionModel(); |
| 81 |
|
| 82 |
|
| 83 |
initTree(tree, likelihood.getSiteModel().getMu()); |
| 84 |
|
| 85 |
int[] treeIndex = new int[tree.getTaxonCount()]; |
| 86 |
for (int i = 0; i < treeIndex.length; i++) { |
| 87 |
treeIndex[i] = tree.getTaxonIndex(alignment.getTaxonId(i)); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
initAlignment(alignment, treeIndex); |
| 92 |
|
| 93 |
|
| 94 |
initSubstitutionModel(substModel); |
| 95 |
|
| 96 |
DataType dataType = substModel.getDataType(); |
| 97 |
proposal.setGapSymbol(dataType.getGapState()); |
| 98 |
|
| 99 |
int[][] returnedAlignment = new int[iAlignment.length][]; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
double logq = proposal.propose(iAlignment, iProbs, iBaseFreqs, iParent, iTau, returnedAlignment, tuning, exponent, gapPenalty); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
SimpleAlignment newAlignment = new SimpleAlignment(); |
| 110 |
for (int i = 0; i < alignment.getTaxonCount(); i++) { |
| 111 |
|
| 112 |
StringBuffer seqBuffer = new StringBuffer(); |
| 113 |
for (int j = 0; j < returnedAlignment[i].length; j++) { |
| 114 |
seqBuffer.append(dataType.getChar(returnedAlignment[treeIndex[i]][j])); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
String seqString = seqBuffer.toString(); |
| 119 |
Sequence sequence = new Sequence(alignment.getTaxon(i), seqString); |
| 120 |
newAlignment.addSequence(sequence); |
| 121 |
|
| 122 |
String oldunaligned = alignment.getUnalignedSequenceString(i); |
| 123 |
String unaligned = newAlignment.getUnalignedSequenceString(i); |
| 124 |
if (!unaligned.equals(oldunaligned)) { |
| 125 |
System.err.println("Sequence changed from:"); |
| 126 |
System.err.println("old:'" + oldunaligned + "'"); |
| 127 |
System.err.println("new:'" + unaligned + "'"); |
| 128 |
throw new RuntimeException(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
likelihood.setAlignment(newAlignment); |
| 137 |
|
| 138 |
return logq; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
public void reject() { |
| 143 |
super.reject(); |
| 144 |
likelihood.setAlignment(alignment); |
| 145 |
} |
| 146 |
|
| 147 |
private void initTree(Tree tree, double mutationRate) { |
| 148 |
iParent = new int[tree.getNodeCount()]; |
| 149 |
iTau = new double[tree.getNodeCount() - 1]; |
| 150 |
populate(tree, tree.getRoot(), new int[]{tree.getExternalNodeCount()}, mutationRate); |
| 151 |
iParent[tree.getNodeCount() - 1] = -1; |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
private void initSubstitutionModel(SubstitutionModel model) { |
| 159 |
|
| 160 |
DataType dataType = model.getDataType(); |
| 161 |
int stateCount = dataType.getStateCount(); |
| 162 |
|
| 163 |
iProbs = new double[iTau.length][stateCount][stateCount]; |
| 164 |
|
| 165 |
double[] transProb = new double[stateCount * stateCount]; |
| 166 |
int count; |
| 167 |
for (int i = 0; i < iTau.length; i++) { |
| 168 |
model.getTransitionProbabilities(iTau[i], transProb); |
| 169 |
count = 0; |
| 170 |
for (int j = 0; j < stateCount; j++) { |
| 171 |
for (int k = 0; k < stateCount; k++) { |
| 172 |
iProbs[i][j][k] = transProb[count]; |
| 173 |
count += 1; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
iBaseFreqs = new double[stateCount]; |
| 180 |
for (int k = 0; k < stateCount; k++) { |
| 181 |
iBaseFreqs[k] = model.getFrequencyModel().getFrequency(k); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private void initAlignment(Alignment alignment, int[] treeIndex) { |
| 189 |
|
| 190 |
int numSeqs = alignment.getSequenceCount(); |
| 191 |
int numSites = alignment.getSiteCount(); |
| 192 |
DataType dataType = alignment.getDataType(); |
| 193 |
int numStates = dataType.getStateCount(); |
| 194 |
|
| 195 |
iAlignment = new int[numSeqs][numSites]; |
| 196 |
|
| 197 |
|
| 198 |
for (int i = 0; i < numSeqs; i++) { |
| 199 |
for (int j = 0; j < numSites; j++) { |
| 200 |
iAlignment[treeIndex[i]][j] = alignment.getState(i, j); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private int populate(Tree tree, NodeRef node, int[] current, double mutationRate) { |
| 211 |
|
| 212 |
int nodeNumber = node.getNumber(); |
| 213 |
|
| 214 |
|
| 215 |
if (tree.isExternal(node)) { |
| 216 |
iTau[nodeNumber] = |
| 217 |
(tree.getNodeHeight(tree.getParent(node)) - tree.getNodeHeight(node)) * mutationRate; |
| 218 |
return nodeNumber; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
int[] childNumbers = new int[tree.getChildCount(node)]; |
| 223 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 224 |
childNumbers[i] = populate(tree, tree.getChild(node, i), current, mutationRate); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
nodeNumber = current[0]; |
| 229 |
|
| 230 |
if (!tree.isRoot(node)) { |
| 231 |
|
| 232 |
iTau[nodeNumber] = |
| 233 |
(tree.getNodeHeight(tree.getParent(node)) - tree.getNodeHeight(node)) * mutationRate; |
| 234 |
} |
| 235 |
|
| 236 |
current[0] += 1; |
| 237 |
|
| 238 |
|
| 239 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 240 |
iParent[childNumbers[i]] = nodeNumber; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
return nodeNumber; |
| 245 |
} |
| 246 |
|
| 247 |
public String getOperatorName() { |
| 248 |
return "IstvansOperator"; |
| 249 |
} |
| 250 |
|
| 251 |
public double getMinimumAcceptanceLevel() { |
| 252 |
return 0.1; |
| 253 |
} |
| 254 |
|
| 255 |
public double getMinimumGoodAcceptanceLevel() { |
| 256 |
return 0.4; |
| 257 |
} |
| 258 |
|
| 259 |
public Element createOperatorElement(Document doc) { |
| 260 |
throw new RuntimeException(); |
| 261 |
} |
| 262 |
|
| 263 |
} |