| 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.app.realtime; |
| 27 |
|
| 28 |
import dr.app.checkpoint.BeastCheckpointer; |
| 29 |
import dr.evolution.tree.BranchRates; |
| 30 |
import dr.evolution.tree.NodeRef; |
| 31 |
import dr.evomodel.tree.TreeModel; |
| 32 |
import dr.evomodel.tree.TreeParameterModel; |
| 33 |
import dr.inference.markovchain.MarkovChain; |
| 34 |
import dr.inference.model.Model; |
| 35 |
import dr.inference.model.Parameter; |
| 36 |
import dr.inference.operators.AdaptableMCMCOperator; |
| 37 |
import dr.inference.operators.MCMCOperator; |
| 38 |
import dr.inference.operators.OperatorSchedule; |
| 39 |
|
| 40 |
import java.io.BufferedReader; |
| 41 |
import java.io.File; |
| 42 |
import java.io.FileReader; |
| 43 |
import java.io.IOException; |
| 44 |
import java.util.ArrayList; |
| 45 |
import java.util.HashSet; |
| 46 |
import java.util.Set; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class CheckPointModifier extends BeastCheckpointer { |
| 52 |
|
| 53 |
private static final boolean DEBUG = false; |
| 54 |
|
| 55 |
private CheckPointTreeModifier modifyTree; |
| 56 |
private BranchRates rateModel; |
| 57 |
private ArrayList<TreeParameterModel> traitModels; |
| 58 |
|
| 59 |
public final static String LOAD_STATE_FILE = "load.state.file"; |
| 60 |
public final static String SAVE_STATE_FILE = "save.state.file"; |
| 61 |
|
| 62 |
private final String loadStateFileName; |
| 63 |
private final String saveStateFileName; |
| 64 |
|
| 65 |
public CheckPointModifier() { |
| 66 |
loadStateFileName = System.getProperty(LOAD_STATE_FILE, null); |
| 67 |
saveStateFileName = System.getProperty(SAVE_STATE_FILE, null); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public long loadState(MarkovChain markovChain, double[] savedLnL) { |
| 72 |
return readStateFromFile(new File(loadStateFileName), markovChain, savedLnL); |
| 73 |
} |
| 74 |
|
| 75 |
protected long readStateFromFile(File file, MarkovChain markovChain, double[] lnL) { |
| 76 |
|
| 77 |
OperatorSchedule operatorSchedule = markovChain.getSchedule(); |
| 78 |
long state = -1; |
| 79 |
|
| 80 |
this.traitModels = new ArrayList<TreeParameterModel>(); |
| 81 |
|
| 82 |
try { |
| 83 |
FileReader fileIn = new FileReader(file); |
| 84 |
BufferedReader in = new BufferedReader(fileIn); |
| 85 |
|
| 86 |
int[] rngState = null; |
| 87 |
|
| 88 |
String line = in.readLine(); |
| 89 |
String[] fields = line.split("\t"); |
| 90 |
if (fields[0].equals("rng")) { |
| 91 |
|
| 92 |
try { |
| 93 |
rngState = new int[fields.length - 1]; |
| 94 |
for (int i = 0; i < rngState.length; i++) { |
| 95 |
rngState[i] = Integer.parseInt(fields[i + 1]); |
| 96 |
} |
| 97 |
|
| 98 |
} catch (NumberFormatException nfe) { |
| 99 |
throw new RuntimeException("Unable to read state number from state file"); |
| 100 |
} |
| 101 |
|
| 102 |
line = in.readLine(); |
| 103 |
fields = line.split("\t"); |
| 104 |
} |
| 105 |
|
| 106 |
try { |
| 107 |
if (!fields[0].equals("state")) { |
| 108 |
throw new RuntimeException("Unable to read state number from state file"); |
| 109 |
} |
| 110 |
state = Long.parseLong(fields[1]); |
| 111 |
} catch (NumberFormatException nfe) { |
| 112 |
throw new RuntimeException("Unable to read state number from state file"); |
| 113 |
} |
| 114 |
|
| 115 |
line = in.readLine(); |
| 116 |
fields = line.split("\t"); |
| 117 |
|
| 118 |
try { |
| 119 |
if (!fields[0].equals("lnL")) { |
| 120 |
throw new RuntimeException("Unable to read lnL from state file"); |
| 121 |
} |
| 122 |
if (lnL != null) { |
| 123 |
lnL[0] = Double.parseDouble(fields[1]); |
| 124 |
} |
| 125 |
} catch (NumberFormatException nfe) { |
| 126 |
throw new RuntimeException("Unable to read lnL from state file"); |
| 127 |
} |
| 128 |
|
| 129 |
line = in.readLine(); |
| 130 |
|
| 131 |
fields = line.split("\t"); |
| 132 |
|
| 133 |
|
| 134 |
for (Parameter parameter : Parameter.CONNECTED_PARAMETER_SET) { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if (isTreeNode(parameter.getId()) && isTreeNode(fields[1]) || parameter.getId().equals(fields[1])) { |
| 139 |
|
| 140 |
int dimension = Integer.parseInt(fields[2]); |
| 141 |
|
| 142 |
if (dimension != parameter.getDimension() && !fields[1].equals("branchRates.categories")) { |
| 143 |
System.err.println("Unable to match state parameter dimension: " + dimension + ", expecting " + parameter.getDimension() + " for parameter: " + parameter.getParameterName()); |
| 144 |
System.err.print("Read from file: "); |
| 145 |
for (int i = 0; i < fields.length; i++) { |
| 146 |
System.err.print(fields[i] + "\t"); |
| 147 |
} |
| 148 |
System.err.println(); |
| 149 |
} |
| 150 |
|
| 151 |
if (fields[1].equals("branchRates.categories.rootNodeNumber")) { |
| 152 |
|
| 153 |
double value = Double.parseDouble(fields[3]); |
| 154 |
parameter.setParameterValue(0, value); |
| 155 |
if (DEBUG) { |
| 156 |
System.out.println("restoring " + fields[1] + " with value " + value); |
| 157 |
} |
| 158 |
} else { |
| 159 |
if (DEBUG) { |
| 160 |
System.out.print("restoring " + fields[1] + " with values "); |
| 161 |
} |
| 162 |
if (fields[1].equals("branchRates.categories")) { |
| 163 |
for (int dim = 0; dim < (fields.length-3); dim++) { |
| 164 |
|
| 165 |
parameter.setParameterValue(dim, Double.parseDouble(fields[dim + 3])); |
| 166 |
if (DEBUG) { |
| 167 |
System.out.print(Double.parseDouble(fields[dim + 3]) + " "); |
| 168 |
} |
| 169 |
} |
| 170 |
} else { |
| 171 |
for (int dim = 0; dim < parameter.getDimension(); dim++) { |
| 172 |
parameter.setParameterValue(dim, Double.parseDouble(fields[dim + 3])); |
| 173 |
if (DEBUG) { |
| 174 |
System.out.print(Double.parseDouble(fields[dim + 3]) + " "); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
if (DEBUG) { |
| 179 |
System.out.println(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
line = in.readLine(); |
| 184 |
|
| 185 |
fields = line.split("\t"); |
| 186 |
|
| 187 |
} else { |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
for (int i = 0; i < operatorSchedule.getOperatorCount(); i++) { |
| 198 |
MCMCOperator operator = operatorSchedule.getOperator(i); |
| 199 |
if (!fields[1].equals(operator.getOperatorName())) { |
| 200 |
throw new RuntimeException("Unable to match operator: " + fields[1]); |
| 201 |
} |
| 202 |
if (fields.length < 4) { |
| 203 |
throw new RuntimeException("Operator missing values: " + fields[1]); |
| 204 |
} |
| 205 |
operator.setAcceptCount(Integer.parseInt(fields[2])); |
| 206 |
operator.setRejectCount(Integer.parseInt(fields[3])); |
| 207 |
if (operator instanceof AdaptableMCMCOperator) { |
| 208 |
if (fields.length != 5) { |
| 209 |
throw new RuntimeException("Coercable operator missing parameter: " + fields[1]); |
| 210 |
} |
| 211 |
((AdaptableMCMCOperator)operator).setAdaptableParameter(Double.parseDouble(fields[4])); |
| 212 |
} |
| 213 |
line = in.readLine(); |
| 214 |
fields = line.split("\t"); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
Set<String> expectedTreeModelNames = new HashSet<String>(); |
| 220 |
for (Model model : Model.CONNECTED_MODEL_SET) { |
| 221 |
|
| 222 |
if (model instanceof TreeModel) { |
| 223 |
expectedTreeModelNames.add(model.getModelName()); |
| 224 |
} |
| 225 |
|
| 226 |
if (model instanceof TreeParameterModel) { |
| 227 |
this.traitModels.add((TreeParameterModel)model); |
| 228 |
} |
| 229 |
|
| 230 |
if (model instanceof BranchRates) { |
| 231 |
this.rateModel = (BranchRates)model; |
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
while (fields[0].equals("tree")) { |
| 237 |
|
| 238 |
for (Model model : Model.CONNECTED_MODEL_SET) { |
| 239 |
if (model instanceof TreeModel && fields[1].equals(model.getModelName())) { |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
line = in.readLine(); |
| 255 |
line = in.readLine(); |
| 256 |
fields = line.split("\t"); |
| 257 |
|
| 258 |
int nodeCount = Integer.parseInt(fields[0]); |
| 259 |
double[] nodeHeights = new double[nodeCount]; |
| 260 |
String[] taxaNames = new String[(nodeCount+1)/2]; |
| 261 |
|
| 262 |
for (int i = 0; i < nodeCount; i++) { |
| 263 |
line = in.readLine(); |
| 264 |
fields = line.split("\t"); |
| 265 |
nodeHeights[i] = Double.parseDouble(fields[1]); |
| 266 |
if (i < taxaNames.length) { |
| 267 |
taxaNames[i] = fields[2]; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
line = in.readLine(); |
| 273 |
line = in.readLine(); |
| 274 |
line = in.readLine(); |
| 275 |
fields = line.split("\t"); |
| 276 |
|
| 277 |
int edgeCount = Integer.parseInt(fields[0]); |
| 278 |
|
| 279 |
|
| 280 |
double[][] traitValues = new double[traitModels.size()][edgeCount]; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
int[] childOrder = new int[edgeCount]; |
| 285 |
for (int i = 0; i < childOrder.length; i++) { |
| 286 |
childOrder[i] = -1; |
| 287 |
} |
| 288 |
|
| 289 |
int[] parents = new int[edgeCount]; |
| 290 |
for (int i = 0; i < edgeCount; i++){ |
| 291 |
parents[i] = -1; |
| 292 |
} |
| 293 |
for (int i = 0; i < edgeCount; i++) { |
| 294 |
line = in.readLine(); |
| 295 |
if (line != null) { |
| 296 |
fields = line.split("\t"); |
| 297 |
parents[Integer.parseInt(fields[0])] = Integer.parseInt(fields[1]); |
| 298 |
childOrder[i] = Integer.parseInt(fields[2]); |
| 299 |
for (int j = 0; j < traitModels.size(); j++) { |
| 300 |
traitValues[j][i] = Double.parseDouble(fields[3+j]); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
this.modifyTree = new CheckPointTreeModifier((TreeModel) model); |
| 308 |
modifyTree.adoptTreeStructure(parents, nodeHeights, childOrder, taxaNames); |
| 309 |
if (traitModels.size() > 0) { |
| 310 |
modifyTree.adoptTraitData(parents, this.traitModels, traitValues); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
expectedTreeModelNames.remove(model.getModelName()); |
| 319 |
|
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
line = in.readLine(); |
| 324 |
if (line != null) { |
| 325 |
fields = line.split("\t"); |
| 326 |
} |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
if (expectedTreeModelNames.size() > 0) { |
| 331 |
StringBuilder sb = new StringBuilder(); |
| 332 |
for (String notFoundName : expectedTreeModelNames) { |
| 333 |
sb.append("Expecting, but unable to match state parameter:" + notFoundName + "\n"); |
| 334 |
} |
| 335 |
throw new RuntimeException(sb.toString()); |
| 336 |
} |
| 337 |
|
| 338 |
in.close(); |
| 339 |
fileIn.close(); |
| 340 |
|
| 341 |
} catch (IOException ioe) { |
| 342 |
throw new RuntimeException("Unable to read file: " + ioe.getMessage()); |
| 343 |
} |
| 344 |
|
| 345 |
return state; |
| 346 |
} |
| 347 |
|
| 348 |
public void extendLoadState(CheckPointUpdaterApp.UpdateChoice choice) { |
| 349 |
|
| 350 |
if (this.rateModel == null) { |
| 351 |
throw new RuntimeException("BranchRates model has not been set correctly."); |
| 352 |
} else { |
| 353 |
ArrayList<NodeRef> newTaxa = modifyTree.incorporateAdditionalTaxa(choice, this.rateModel); |
| 354 |
modifyTree.interpolateTraitValues(this.traitModels); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
@Override |
| 359 |
public void checkLoadState(double savedLnL, double lnL) { |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
private boolean isTreeNode(String name) { |
| 364 |
if (name == null) { |
| 365 |
return false; |
| 366 |
} |
| 367 |
int length = name.length(); |
| 368 |
if (length == 0) { |
| 369 |
return false; |
| 370 |
} |
| 371 |
int i = 0; |
| 372 |
if (name.charAt(0) == '-') { |
| 373 |
return false; |
| 374 |
} |
| 375 |
for (; i < length; i++) { |
| 376 |
char c = name.charAt(i); |
| 377 |
if (c < '0' || c > '9') { |
| 378 |
return false; |
| 379 |
} |
| 380 |
} |
| 381 |
return true; |
| 382 |
} |
| 383 |
|
| 384 |
} |