Debian Codegraph @

src/dr/evomodel/arg/operators/SlidingPatternsOperator.java

raw symbols SCIP
1 /*
2 * SlidingPatternsOperator.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.evomodel.arg.operators;
27
28 import dr.evolution.alignment.SitePatterns;
29 import dr.inference.model.Parameter;
30 import dr.inference.model.ParameterParser;
31 import dr.inference.operators.*;
32 import dr.math.MathUtils;
33 import dr.xml.*;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 /**
39 * Created by IntelliJ IDEA.
40 * User: msuchard
41 * Date: Jan 18, 2007
42 * Time: 8:01:56 PM
43 * To change this template use File | Settings | File Templates.
44 */
45 public class SlidingPatternsOperator extends AbstractAdaptableOperator {
46 //
47 // SimpleMCMCOperator implements AdaptableMCMCOperator {
48
49 public static final String WINDOW_SIZE = "windowSize";
50 public static final String OPERATOR_NAME = "slidingPatternsOperator";
51 public static final String BREAK_POINTS = "breakPoints";
52
53 public SlidingPatternsOperator(List<SitePatterns> list, Parameter breakPoints, int windowSize, int weight, AdaptationMode mode) {
54 super(mode);
55 this.partitions = list;
56 this.windowSize = windowSize;
57 // this.weight = weight;
58 // this.mode = mode;
59 this.breakPoints = breakPoints;
60 // System.err.println("Starting values: "+currentBreakPointsString() );
61 // System.exit(-1) ;
62 }
63
64
65 public String currentBreakPointsString() {
66 StringBuilder sb = new StringBuilder();
67 sb.append("[");
68 boolean first = true;
69 for (double value : breakPoints.getParameterValues()) {
70 int pt = (int) value;
71 if (!first) {
72 sb.append(",");
73 first = false;
74 }
75 sb.append(pt);
76 }
77 sb.append("]");
78 return sb.toString();
79
80 }
81
82 // http://www.google.com/search?client=safari&rls=en&q=The+week+renewal+add+a+friend&ie=UTF-8&oe=UTF-8
83 public void addNewSitePatterns(SitePatterns addPatterns) {
84 // todo need to implement for a variable number of partitions
85 }
86
87 public String getOperatorName() {
88 return OPERATOR_NAME;
89 }
90
91 public double doOperation() {
92
93 // Select boundary to update, 0 => btw partition 0 and 1, 1 => btw partition 1 and 2, etc.
94 int whichBoundary = MathUtils.nextInt(breakPoints.getDimension());
95
96 int cBreakPt = (int) breakPoints.getParameterValue(whichBoundary);
97 SitePatterns left = partitions.get(whichBoundary);
98 SitePatterns right = partitions.get(whichBoundary + 1);
99 int min = left.getFrom();
100 int max = right.getTo();
101
102 int pBreakPt = min;
103 while (pBreakPt <= min || pBreakPt >= max) { // cBreakPt + [windowSize-1, windowSize+1] (and not 0)
104 if (MathUtils.nextBoolean())
105 pBreakPt = cBreakPt + MathUtils.nextInt(windowSize) + 1;
106 else
107 pBreakPt = cBreakPt - MathUtils.nextInt(windowSize) - 1;
108 }
109
110
111 return 0;
112 }
113
114 public double getAdaptableParameter() {
115 return Math.log(windowSize);
116 }
117
118 public void setAdaptableParameter(double value) {
119 windowSize = (int) Math.exp(value);
120 }
121
122 public double getRawParameter() {
123 return windowSize;
124 }
125
126 public String getAdaptableParameterName() {
127 return "windowSize";
128 }
129
130 // public int getMode() {
131 // return mode;
132 // }
133
134 public static boolean arePartitionsContiguous(List<SitePatterns> list) {
135 int current = -1;
136 int index = 0;
137 for (SitePatterns patterns : list) {
138 int start = patterns.getFrom();
139 int end = patterns.getTo();
140 /* System.err.println(start+" -> "+end+" : "+patterns.getSiteCount()+" "+patterns.getPatternCount());
141 int[] data = patterns.getSitePattern(0);
142 System.err.print("Data 0:");
143 for (int i : data)
144 System.err.print(" "+i);
145 System.err.println("");*/
146 // if (current == -1)
147 // current = end;
148 if (current != -1 && start != (current + 1))
149 // throw new NonContiguousPartitionsException("Partition #"+0+" does not start contiguously");
150 return false;
151 current = end;
152 }
153
154 return true;
155 }
156
157 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
158
159 public String getParserName() {
160 return OPERATOR_NAME;
161 }
162
163 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
164
165 // int mode = AdaptableMCMCOperator.DEFAULT;
166 // if (xo.hasAttribute(AUTO_OPTIMIZE)) {
167 // if (xo.getBooleanAttribute(AUTO_OPTIMIZE)) {
168 // mode = AdaptableMCMCOperator.ADAPTATION_ON;
169 // } else {
170 // mode = AdaptableMCMCOperator.ADAPTATION_OFF;
171 // }
172 // }
173
174 AdaptationMode mode = AdaptationMode.parseMode(xo);
175
176 int weight = xo.getIntegerAttribute(WEIGHT);
177 int windowSize = xo.getIntegerAttribute(WINDOW_SIZE);
178
179 List<SitePatterns> list = new ArrayList<SitePatterns>();
180
181 final int numChild = xo.getChildCount();
182 for (int i = 0; i < numChild; i++) {
183 Object obj = xo.getChild(i);
184 if (obj instanceof SitePatterns) {
185 // System.err.println("Found: "+((SitePatterns)obj).getId()) ;
186 list.add((SitePatterns) obj);
187 }
188 }
189 if (!arePartitionsContiguous(list))
190 throw new XMLParseException("Only contiguous partitions are allowed");
191
192 // Set current breakpoints
193 int dim = list.size() - 1;
194 XMLObject cxo = xo.getChild(BREAK_POINTS);
195 Parameter breakPoints = new Parameter.Default(dim);
196
197 ParameterParser.replaceParameter(cxo, breakPoints);
198
199 breakPoints.setDimension(dim);
200 for (int index = 0; index < dim; index++)
201 breakPoints.setParameterValueQuietly(index,
202 list.get(index + 1).getFrom());
203
204 // Parameter parameter = (Parameter)xo.getChild(Parameter.class);
205
206 return new SlidingPatternsOperator(list, breakPoints, windowSize, weight, mode);
207 }
208
209 //************************************************************************
210 // AbstractXMLObjectParser implementation
211 //************************************************************************
212
213 public String getParserDescription() {
214 return "This element returns a sliding window operator on alignment sites.";
215 }
216
217 public Class getReturnType() {
218 return MCMCOperator.class;
219 }
220
221 public XMLSyntaxRule[] getSyntaxRules() {
222 return rules;
223 }
224
225 private final XMLSyntaxRule[] rules = {
226 AttributeRule.newIntegerRule(WINDOW_SIZE),
227 AttributeRule.newIntegerRule(WEIGHT),
228 AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true),
229 new ElementRule(BREAK_POINTS, Parameter.class),
230 new ElementRule(SitePatterns.class, 2, 100) // todo fix hard-coded 100
231 };
232
233 };
234
235
236 private int totalAlignmentLength;
237 private final List<SitePatterns> partitions;
238 private int windowSize = 10;
239 // private int mode = AdaptableMCMCOperator.DEFAULT;
240 // private int weight = 1;
241 private final Parameter breakPoints;
242 }