| 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.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 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class SlidingPatternsOperator extends AbstractAdaptableOperator { |
| 46 |
|
| 47 |
|
| 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 |
|
| 58 |
|
| 59 |
this.breakPoints = breakPoints; |
| 60 |
|
| 61 |
|
| 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 |
|
| 83 |
public void addNewSitePatterns(SitePatterns addPatterns) { |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
public String getOperatorName() { |
| 88 |
return OPERATOR_NAME; |
| 89 |
} |
| 90 |
|
| 91 |
public double doOperation() { |
| 92 |
|
| 93 |
|
| 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) { |
| 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 |
|
| 131 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if (current != -1 && start != (current + 1)) |
| 149 |
|
| 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 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 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 |
|
| 186 |
list.add((SitePatterns) obj); |
| 187 |
} |
| 188 |
} |
| 189 |
if (!arePartitionsContiguous(list)) |
| 190 |
throw new XMLParseException("Only contiguous partitions are allowed"); |
| 191 |
|
| 192 |
|
| 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 |
|
| 205 |
|
| 206 |
return new SlidingPatternsOperator(list, breakPoints, windowSize, weight, mode); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 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) |
| 231 |
}; |
| 232 |
|
| 233 |
}; |
| 234 |
|
| 235 |
|
| 236 |
private int totalAlignmentLength; |
| 237 |
private final List<SitePatterns> partitions; |
| 238 |
private int windowSize = 10; |
| 239 |
|
| 240 |
|
| 241 |
private final Parameter breakPoints; |
| 242 |
} |