| 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; |
| 27 |
|
| 28 |
import dr.evolution.tree.Tree; |
| 29 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 30 |
import dr.evomodel.coalescent.structure.ColourSamplerModel; |
| 31 |
import dr.inference.loggers.LogFormatter; |
| 32 |
import dr.inference.loggers.MLLogger; |
| 33 |
import dr.inference.loggers.TabDelimitedFormatter; |
| 34 |
import dr.inference.model.Likelihood; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
import java.io.File; |
| 38 |
import java.io.FileNotFoundException; |
| 39 |
import java.io.FileOutputStream; |
| 40 |
import java.io.PrintWriter; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class ARGTreeLogger extends OldTreeLogger { |
| 49 |
|
| 50 |
public static final String LOG_ARG = "logArgTree"; |
| 51 |
public static final String PARTITION = "partition"; |
| 52 |
|
| 53 |
|
| 54 |
private int partition; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public ARGTreeLogger(Tree tree, int partition, BranchRateModel branchRateModel, String rateLabel, |
| 60 |
ColourSamplerModel colourSamplerModel, String colouringLabel, |
| 61 |
Likelihood likelihood, String likelihoodLabel, |
| 62 |
LogFormatter formatter, int logEvery, boolean nexusFormat, boolean substitutions) { |
| 63 |
|
| 64 |
super(tree, branchRateModel, rateLabel, colourSamplerModel, colouringLabel, likelihood, likelihoodLabel, formatter, logEvery, nexusFormat, substitutions); |
| 65 |
this.partition = partition; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
@Override |
| 71 |
protected String additionalInfo() { |
| 72 |
return " [&PARTITION=" + partition + "]" |
| 73 |
+ " [&YULE=" + getLogYuleProbabilityString() + "]" |
| 74 |
+ " [&NUM_REC=" + getNumberOfReassortments() + "]" |
| 75 |
; |
| 76 |
} |
| 77 |
|
| 78 |
private int getNumberOfReassortments() { |
| 79 |
ARGModel arg = (ARGModel) getTree(); |
| 80 |
return arg.getReassortmentNodeCount(); |
| 81 |
} |
| 82 |
|
| 83 |
private String getLogYuleProbabilityString() { |
| 84 |
ARGTree tree = new ARGTree((ARGModel) getTree(), partition); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
double logProbability = 0; |
| 90 |
for (int i = 0, n = tree.getNodeCount(); i < n; i++) { |
| 91 |
|
| 92 |
ARGModel.Node node = (ARGModel.Node) tree.getNode(i); |
| 93 |
int count = node.getDescendentTipCount(); |
| 94 |
|
| 95 |
if (count > 2) |
| 96 |
logProbability -= 2 * Math.log(count - 1); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
return String.format("%5.4f", logProbability); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
protected Tree getPrintTree() { |
| 106 |
return new ARGTree((ARGModel) getTree(), partition); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
protected boolean useTaxonLabels() { |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 115 |
|
| 116 |
public String getParserName() { |
| 117 |
return LOG_ARG; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 124 |
|
| 125 |
ARGModel tree = (ARGModel) xo.getChild(ARGModel.class); |
| 126 |
|
| 127 |
String fileName = null; |
| 128 |
String title = null; |
| 129 |
boolean nexusFormat = false; |
| 130 |
|
| 131 |
String colouringLabel = "demes"; |
| 132 |
String rateLabel = "rate"; |
| 133 |
String likelihoodLabel = "lnP"; |
| 134 |
|
| 135 |
if (xo.hasAttribute(TITLE)) { |
| 136 |
title = xo.getStringAttribute(TITLE); |
| 137 |
} |
| 138 |
|
| 139 |
if (xo.hasAttribute(FILE_NAME)) { |
| 140 |
fileName = xo.getStringAttribute(FILE_NAME); |
| 141 |
} |
| 142 |
|
| 143 |
if (xo.hasAttribute(NEXUS_FORMAT)) { |
| 144 |
nexusFormat = xo.getBooleanAttribute(NEXUS_FORMAT); |
| 145 |
} |
| 146 |
|
| 147 |
boolean substitutions = false; |
| 148 |
if (xo.hasAttribute(BRANCH_LENGTHS)) { |
| 149 |
substitutions = xo.getStringAttribute(BRANCH_LENGTHS).equals(SUBSTITUTIONS); |
| 150 |
} |
| 151 |
|
| 152 |
BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class); |
| 153 |
|
| 154 |
ColourSamplerModel colourSamplerModel = (ColourSamplerModel) xo.getChild(ColourSamplerModel.class); |
| 155 |
|
| 156 |
Likelihood likelihood = (Likelihood) xo.getChild(Likelihood.class); |
| 157 |
|
| 158 |
|
| 159 |
int logEvery = 1; |
| 160 |
|
| 161 |
if (xo.hasAttribute(LOG_EVERY)) { |
| 162 |
logEvery = xo.getIntegerAttribute(LOG_EVERY); |
| 163 |
} |
| 164 |
|
| 165 |
int partition = 0; |
| 166 |
|
| 167 |
if (xo.hasAttribute(PARTITION)) { |
| 168 |
partition = xo.getIntegerAttribute(PARTITION); |
| 169 |
} |
| 170 |
|
| 171 |
if (partition > tree.getMaxPartitionNumber()) |
| 172 |
throw new XMLParseException("ARGModel does not contain a partition #" + partition); |
| 173 |
|
| 174 |
PrintWriter pw = null; |
| 175 |
|
| 176 |
if (fileName != null) { |
| 177 |
|
| 178 |
try { |
| 179 |
File file = new File(fileName); |
| 180 |
String name = file.getName(); |
| 181 |
String parent = file.getParent(); |
| 182 |
|
| 183 |
if (!file.isAbsolute()) { |
| 184 |
parent = System.getProperty("user.dir"); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
pw = new PrintWriter(new FileOutputStream(new File(parent, name))); |
| 189 |
} catch (FileNotFoundException fnfe) { |
| 190 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 191 |
} |
| 192 |
} else { |
| 193 |
pw = new PrintWriter(System.out); |
| 194 |
} |
| 195 |
|
| 196 |
LogFormatter formatter = new TabDelimitedFormatter(pw); |
| 197 |
|
| 198 |
ARGTreeLogger logger = new ARGTreeLogger(tree, partition, branchRateModel, rateLabel, |
| 199 |
colourSamplerModel, colouringLabel, likelihood, likelihoodLabel, |
| 200 |
formatter, logEvery, nexusFormat, substitutions); |
| 201 |
|
| 202 |
if (title != null) { |
| 203 |
logger.setTitle(title); |
| 204 |
} |
| 205 |
|
| 206 |
return logger; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 213 |
return rules; |
| 214 |
} |
| 215 |
|
| 216 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 217 |
AttributeRule.newIntegerRule(LOG_EVERY), |
| 218 |
AttributeRule.newIntegerRule(PARTITION), |
| 219 |
new StringAttributeRule(FILE_NAME, |
| 220 |
"The name of the file to send log output to. " + |
| 221 |
"If no file name is specified then log is sent to standard output", true), |
| 222 |
new StringAttributeRule(TITLE, "The title of the log", true), |
| 223 |
AttributeRule.newBooleanRule(NEXUS_FORMAT, true, |
| 224 |
"Whether to use the NEXUS format for the tree log"), |
| 225 |
new StringAttributeRule(BRANCH_LENGTHS, "What units should the branch lengths be in", new String[]{TIME, SUBSTITUTIONS}, true), |
| 226 |
new ElementRule(ARGModel.class, "The ARG which is to be logged"), |
| 227 |
new ElementRule(BranchRateModel.class, true), |
| 228 |
new ElementRule(ColourSamplerModel.class, true), |
| 229 |
new ElementRule(Likelihood.class, true) |
| 230 |
}; |
| 231 |
|
| 232 |
public String getParserDescription() { |
| 233 |
return "Logs a tree to a file"; |
| 234 |
} |
| 235 |
|
| 236 |
public String getExample() { |
| 237 |
return |
| 238 |
"<!-- The " + getParserName() + " element takes an argTreeModel to be logged -->\n" + |
| 239 |
"<" + getParserName() + " " + LOG_EVERY + "=\"100\" " + FILE_NAME + "=\"log.trees\" " + NEXUS_FORMAT + "=\"true\">\n" + |
| 240 |
" <treeModel idref=\"treeModel1\"/>\n" + |
| 241 |
"</" + getParserName() + ">\n"; |
| 242 |
} |
| 243 |
|
| 244 |
public Class getReturnType() { |
| 245 |
return MLLogger.class; |
| 246 |
} |
| 247 |
}; |
| 248 |
|
| 249 |
} |