Debian Codegraph @

src/dr/oldevomodel/indel/IstvanOperator.java

raw symbols SCIP
1 /*
2 * IstvanOperator.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.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 * Implements branch exchange operations.
43 * There is a NARROW and WIDE variety.
44 * The narrow exchange is very similar to a rooted-tree
45 * nearest-neighbour interchange but with the restriction
46 * that node height must remain consistent.
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 //System.out.println("Incoming alignment");
77 //System.out.println(alignment);
78 //System.out.println();
79
80 SubstitutionModel substModel = likelihood.getSiteModel().getSubstitutionModel();
81
82 // initialize the iParent and iTau arrays based on the given tree.
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 // initialize the iAlignment array from the given alignment.
91 initAlignment(alignment, treeIndex);
92
93 // initialize the iProbs array from the substitution model -- must be called after populating tree!
94 initSubstitutionModel(substModel);
95
96 DataType dataType = substModel.getDataType();
97 proposal.setGapSymbol(dataType.getGapState());
98
99 int[][] returnedAlignment = new int[iAlignment.length][];
100
101 //System.out.println("Initialization done, starting proposal proper...");
102
103
104 double logq = proposal.propose(iAlignment, iProbs, iBaseFreqs, iParent, iTau, returnedAlignment, tuning, exponent, gapPenalty);
105
106 //System.out.println("Proposal finished, logq=" + logq);
107
108 //create new alignment object
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 // add sequences in order of tree
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 //System.out.println("Outgoing alignment");
132 //System.out.println(newAlignment);
133 //System.out.println();
134
135
136 likelihood.setAlignment(newAlignment);
137
138 return logq;
139 }
140
141 // MUST RESET ALIGNMENT IF REJECTED!!
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 * initialize the iProbs array from the substitution model -- must be called after populating tree!
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 // initialize equlibrium distribution
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 * Initializes the iAlignment array from the given alignment.
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 // populate alignment in order of tree
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 * Populates the iParent and iTau arrays.
207 *
208 * @return the node number of the given node.
209 */
210 private int populate(Tree tree, NodeRef node, int[] current, double mutationRate) {
211
212 int nodeNumber = node.getNumber();
213
214 // if its an external node just return the number
215 if (tree.isExternal(node)) {
216 iTau[nodeNumber] =
217 (tree.getNodeHeight(tree.getParent(node)) - tree.getNodeHeight(node)) * mutationRate;
218 return nodeNumber;
219 }
220
221 // if internal node, first let your children be assigned numbers
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 // now, pick the next available number
228 nodeNumber = current[0];
229 // if you are not the root, then record the branch length above you.
230 if (!tree.isRoot(node)) {
231 //iTau[nodeNumber] = tree.getBranchLength(node) * mutationRate;
232 iTau[nodeNumber] =
233 (tree.getNodeHeight(tree.getParent(node)) - tree.getNodeHeight(node)) * mutationRate;
234 }
235 // increment the next available number
236 current[0] += 1;
237
238 // now that you have your number, populate the iParent entries of your children.
239 for (int i = 0; i < tree.getChildCount(node); i++) {
240 iParent[childNumbers[i]] = nodeNumber;
241 }
242
243 // finally return your number so your parent can do the same.
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 }