| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
package dr.evomodel.tree; |
| 30 |
|
| 31 |
import dr.evolution.tree.NodeRef; |
| 32 |
import dr.evolution.tree.Tree; |
| 33 |
import dr.evolution.util.Taxon; |
| 34 |
import dr.inference.loggers.LogFormatter; |
| 35 |
import dr.inference.loggers.MCLogger; |
| 36 |
import dr.inference.loggers.MLLogger; |
| 37 |
import dr.inference.loggers.TabDelimitedFormatter; |
| 38 |
import dr.inferencexml.loggers.LoggerParser; |
| 39 |
import dr.xml.*; |
| 40 |
|
| 41 |
import java.io.*; |
| 42 |
import java.util.*; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class ConvergenceLogger extends MCLogger { |
| 48 |
|
| 49 |
public final static String LOG_CONVERGENCE = "LogConvergence"; |
| 50 |
|
| 51 |
public final static String TREE_FILE_NAME = "treeFilename"; |
| 52 |
|
| 53 |
public final static String REFERENCE_FILE_NAME = "referenceFilename"; |
| 54 |
|
| 55 |
public final static String CHECK_EVERY = "checkEvery"; |
| 56 |
|
| 57 |
private Tree tree = null; |
| 58 |
|
| 59 |
private BufferedWriter bwDistances = null; |
| 60 |
|
| 61 |
private long trees = 0; |
| 62 |
|
| 63 |
private HashMap<BitSet, Double> referenceCladeFrequencies = null; |
| 64 |
|
| 65 |
private HashMap<BitSet, Double> cladeOccurences = null; |
| 66 |
|
| 67 |
private String outputFilename; |
| 68 |
|
| 69 |
private HashMap<String, Integer> taxonMap; |
| 70 |
|
| 71 |
private String[] taxonArray; |
| 72 |
|
| 73 |
private String referenceTreeFileName; |
| 74 |
|
| 75 |
public ConvergenceLogger(Tree tree, LogFormatter formatter, int logEvery, |
| 76 |
String outputFilename, String referenceTreeFileName) { |
| 77 |
|
| 78 |
super(formatter, logEvery, false); |
| 79 |
|
| 80 |
this.referenceTreeFileName = referenceTreeFileName; |
| 81 |
this.tree = tree; |
| 82 |
this.outputFilename = outputFilename.substring(0, outputFilename |
| 83 |
.indexOf('.')) |
| 84 |
+ ".dist"; |
| 85 |
|
| 86 |
taxonMap = new HashMap<String, Integer>(); |
| 87 |
cladeOccurences = new HashMap<BitSet, Double>(); |
| 88 |
} |
| 89 |
|
| 90 |
public void startLogging() { |
| 91 |
|
| 92 |
File f = new File(outputFilename.substring(0, outputFilename |
| 93 |
.indexOf('.')) |
| 94 |
+ ".dist"); |
| 95 |
if (f.exists()) { |
| 96 |
f.delete(); |
| 97 |
} |
| 98 |
|
| 99 |
parseReferenceFile(referenceTreeFileName); |
| 100 |
|
| 101 |
taxonArray = new String[taxonMap.size()]; |
| 102 |
Set<String> taxon = taxonMap.keySet(); |
| 103 |
for (String s : taxon) { |
| 104 |
taxonArray[taxonMap.get(s)] = s; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
private void parseReferenceFile(String log) { |
| 109 |
FileReader fr; |
| 110 |
BufferedReader br; |
| 111 |
String line; |
| 112 |
referenceCladeFrequencies = new HashMap<BitSet, Double>(); |
| 113 |
|
| 114 |
try { |
| 115 |
fr = new FileReader(log); |
| 116 |
br = new BufferedReader(fr); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
line = br.readLine(); |
| 147 |
String[] tokens = line.split("\\s++"); |
| 148 |
long total = Long.parseLong(tokens[0]); |
| 149 |
|
| 150 |
while ((line = br.readLine()) != null) { |
| 151 |
String[] taxa = getTaxa(line); |
| 152 |
BitSet split = new BitSet(); |
| 153 |
for (String t : taxa) { |
| 154 |
if (!taxonMap.containsKey(t)) { |
| 155 |
taxonMap.put(t, taxonMap.size()); |
| 156 |
} |
| 157 |
split.set(taxonMap.get(t)); |
| 158 |
} |
| 159 |
tokens = line.split("\\s++"); |
| 160 |
double frequency = (100.0 * Long.parseLong(tokens[0])) / (double) total; |
| 161 |
referenceCladeFrequencies.put(split, |
| 162 |
frequency); |
| 163 |
} |
| 164 |
|
| 165 |
br.close(); |
| 166 |
fr.close(); |
| 167 |
} catch (FileNotFoundException e) { |
| 168 |
|
| 169 |
e.printStackTrace(); |
| 170 |
} catch (IOException e) { |
| 171 |
|
| 172 |
e.printStackTrace(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
private String[] getTaxa(String line) { |
| 177 |
line = line.substring(line.indexOf('(') + 1, line.indexOf(')')); |
| 178 |
String[] taxa = line.split(","); |
| 179 |
return taxa; |
| 180 |
} |
| 181 |
|
| 182 |
public void log(long state) { |
| 183 |
|
| 184 |
if (logEvery <= 0 || ((state % logEvery) == 0)) { |
| 185 |
|
| 186 |
addTree(tree); |
| 187 |
|
| 188 |
if (state % (logEvery * 10) == 0) { |
| 189 |
double distance = getMaxCladeDistance(); |
| 190 |
|
| 191 |
try { |
| 192 |
if (bwDistances == null) { |
| 193 |
bwDistances = new BufferedWriter(new FileWriter( |
| 194 |
outputFilename, true)); |
| 195 |
} |
| 196 |
|
| 197 |
bwDistances.append((state) + "\t" + distance); |
| 198 |
bwDistances.newLine(); |
| 199 |
bwDistances.flush(); |
| 200 |
} catch (IOException e) { |
| 201 |
|
| 202 |
e.printStackTrace(); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
private void addTree(Tree tree) { |
| 209 |
trees++; |
| 210 |
List<BitSet> splits = getSplits(tree); |
| 211 |
|
| 212 |
for (BitSet s : splits) { |
| 213 |
if (!cladeOccurences.containsKey(s)) { |
| 214 |
cladeOccurences.put(s, 1.0); |
| 215 |
} else { |
| 216 |
cladeOccurences.put(s, cladeOccurences.get(s) + 1); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
private List<BitSet> getSplits(Tree tree) { |
| 222 |
List<BitSet> splits = new ArrayList<BitSet>(); |
| 223 |
|
| 224 |
NodeRef root = tree.getRoot(); |
| 225 |
fillSplits(splits, root, tree); |
| 226 |
|
| 227 |
return splits; |
| 228 |
} |
| 229 |
|
| 230 |
private BitSet fillSplits(List<BitSet> splits, NodeRef root, Tree tree) { |
| 231 |
BitSet split = new BitSet(); |
| 232 |
|
| 233 |
if (!tree.isExternal(root)) { |
| 234 |
split.or(fillSplits(splits, tree.getChild(root, 0), tree)); |
| 235 |
split.or(fillSplits(splits, tree.getChild(root, 1), tree)); |
| 236 |
splits.add(split); |
| 237 |
} else { |
| 238 |
Taxon taxon = tree.getNodeTaxon(root); |
| 239 |
String name = taxon.getId(); |
| 240 |
split.set(taxonMap.get(name)); |
| 241 |
} |
| 242 |
|
| 243 |
return split; |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
private double getMaxCladeDistance() { |
| 248 |
|
| 249 |
return getMaxDeviation(); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
private double getMaxDeviation() { |
| 306 |
double[] deviation = getDeviations(); |
| 307 |
|
| 308 |
double max = 0; |
| 309 |
for (double m : deviation) { |
| 310 |
if (m > max) { |
| 311 |
max = m; |
| 312 |
} |
| 313 |
} |
| 314 |
return max; |
| 315 |
} |
| 316 |
|
| 317 |
private double[] getDeviations() { |
| 318 |
double[] deviations = new double[Math.max(referenceCladeFrequencies |
| 319 |
.size(), cladeOccurences.size())]; |
| 320 |
|
| 321 |
Set<BitSet> keys = null; |
| 322 |
if (referenceCladeFrequencies.size() < cladeOccurences.size()) { |
| 323 |
keys = cladeOccurences.keySet(); |
| 324 |
} else { |
| 325 |
keys = referenceCladeFrequencies.keySet(); |
| 326 |
} |
| 327 |
|
| 328 |
Iterator<BitSet> it = keys.iterator(); |
| 329 |
for (int i = 0; i < deviations.length; i++) { |
| 330 |
BitSet k = it.next(); |
| 331 |
double r = 0.0; |
| 332 |
if (referenceCladeFrequencies.containsKey(k)) { |
| 333 |
r = referenceCladeFrequencies.get(k); |
| 334 |
} |
| 335 |
double o = 0.0; |
| 336 |
if (cladeOccurences.containsKey(k)) { |
| 337 |
o = cladeOccurences.get(k); |
| 338 |
} |
| 339 |
deviations[i] = Math.abs(r - ((o * 100.0) / (double) trees)); |
| 340 |
} |
| 341 |
|
| 342 |
return deviations; |
| 343 |
} |
| 344 |
|
| 345 |
public void stopLogging() { |
| 346 |
|
| 347 |
logLine("End;"); |
| 348 |
super.stopLogging(); |
| 349 |
} |
| 350 |
|
| 351 |
public static XMLObjectParser PARSER = new LoggerParser() { |
| 352 |
|
| 353 |
public String getParserName() { |
| 354 |
return LOG_CONVERGENCE; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 361 |
|
| 362 |
String treeFileName = xo.getStringAttribute(TREE_FILE_NAME); |
| 363 |
|
| 364 |
String referenceFileName = xo |
| 365 |
.getStringAttribute(REFERENCE_FILE_NAME); |
| 366 |
|
| 367 |
int checkEvery = 1; |
| 368 |
if (xo.hasAttribute(CHECK_EVERY)) { |
| 369 |
checkEvery = xo.getIntegerAttribute(CHECK_EVERY); |
| 370 |
} |
| 371 |
|
| 372 |
Tree tree = (Tree) xo.getChild(Tree.class); |
| 373 |
PrintWriter pw = getLogFile(xo, getParserName()); |
| 374 |
LogFormatter formatter = new TabDelimitedFormatter(pw); |
| 375 |
|
| 376 |
ConvergenceLogger logger = new ConvergenceLogger(tree, formatter, |
| 377 |
checkEvery, treeFileName, referenceFileName); |
| 378 |
|
| 379 |
return logger; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 386 |
return rules; |
| 387 |
} |
| 388 |
|
| 389 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 390 |
new StringAttributeRule(TREE_FILE_NAME, |
| 391 |
"name of a tree log file", "trees.log"), |
| 392 |
new StringAttributeRule(REFERENCE_FILE_NAME, |
| 393 |
"name of a reference tree file", "trees.log"), |
| 394 |
AttributeRule.newIntegerRule(CHECK_EVERY, true), |
| 395 |
new ElementRule(TreeModel.class)}; |
| 396 |
|
| 397 |
public String getParserDescription() { |
| 398 |
return "Checks the convergence in terms of distance to the reference run."; |
| 399 |
} |
| 400 |
|
| 401 |
public String getExample() { |
| 402 |
return "<!-- The " + getParserName() |
| 403 |
+ " element takes a treeModel to be logged -->\n" + "<" |
| 404 |
+ getParserName() + " " + LOG_EVERY + "=\"100\" " |
| 405 |
+ TREE_FILE_NAME + "=\"log.trees\" " + REFERENCE_FILE_NAME |
| 406 |
+ "=\"log.trees\" " |
| 407 |
+ " <treeModel idref=\"treeModel1\"/>\n" + "</" |
| 408 |
+ getParserName() + ">\n"; |
| 409 |
} |
| 410 |
|
| 411 |
public Class getReturnType() { |
| 412 |
return MLLogger.class; |
| 413 |
} |
| 414 |
}; |
| 415 |
|
| 416 |
} |