| 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.evoxml.util.GraphMLUtils; |
| 29 |
import dr.inference.loggers.LogFormatter; |
| 30 |
import dr.inference.loggers.MCLogger; |
| 31 |
import dr.inference.loggers.MLLogger; |
| 32 |
import dr.inference.loggers.TabDelimitedFormatter; |
| 33 |
import dr.xml.*; |
| 34 |
import org.jdom.Element; |
| 35 |
import org.jdom.output.XMLOutputter; |
| 36 |
|
| 37 |
import java.io.File; |
| 38 |
import java.io.FileNotFoundException; |
| 39 |
import java.io.FileOutputStream; |
| 40 |
import java.io.PrintWriter; |
| 41 |
import java.util.logging.Logger; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class ARGLogger extends MCLogger { |
| 51 |
|
| 52 |
public static final String LOG_ARG = "logArg"; |
| 53 |
public static final String DOT_FORMAT = "dotFormat"; |
| 54 |
public static final String COMPRESSED_STRING = "compressedString"; |
| 55 |
public static final String EXTENDED_NEWICK = "extendedNewick"; |
| 56 |
public static final String STRIPPED_NEWICK = "strippedNewick"; |
| 57 |
public static final String FULL_STRING = "fullString"; |
| 58 |
public static final String DISTINCT_TREES = "distinctTrees"; |
| 59 |
public static final String FORMAT = "format"; |
| 60 |
|
| 61 |
|
| 62 |
public static final String LOG = "log"; |
| 63 |
public static final String ECHO = "echo"; |
| 64 |
public static final String ECHO_EVERY = "echoEvery"; |
| 65 |
public static final String TITLE = "title"; |
| 66 |
public static final String FILE_NAME = "fileName"; |
| 67 |
|
| 68 |
public static final String TAB = "tab"; |
| 69 |
public static final String HTML = "html"; |
| 70 |
public static final String PRETTY = "pretty"; |
| 71 |
public static final String LOG_EVERY = "logEvery"; |
| 72 |
|
| 73 |
public static final String COLUMNS = "columns"; |
| 74 |
public static final String COLUMN = "column"; |
| 75 |
public static final String LABEL = "label"; |
| 76 |
public static final String SIGNIFICANT_FIGURES = "sf"; |
| 77 |
public static final String DECIMAL_PLACES = "dp"; |
| 78 |
public static final String WIDTH = "width"; |
| 79 |
|
| 80 |
|
| 81 |
private ARGModel argModel; |
| 82 |
private String formatType; |
| 83 |
|
| 84 |
private ARGDistinctTreeCountStatistic treeStat; |
| 85 |
|
| 86 |
public ARGLogger(ARGModel argModel, LogFormatter formatter, int logEvery, String formatType) { |
| 87 |
super(formatter, logEvery, false); |
| 88 |
this.argModel = argModel; |
| 89 |
this.formatType = formatType; |
| 90 |
|
| 91 |
if(formatType.equals(DISTINCT_TREES)){ |
| 92 |
treeStat = new ARGDistinctTreeCountStatistic(argModel); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
public static final String GRAPHML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">"; |
| 98 |
public static final String GRAPHML_FOOTER = "</graphml>"; |
| 99 |
|
| 100 |
public void startLogging() { |
| 101 |
if (formatType.equals(EXTENDED_NEWICK)) { |
| 102 |
logLine("stateARG.string"); |
| 103 |
} else if (formatType.equals(STRIPPED_NEWICK)) { |
| 104 |
logLine("StrippedARGString"); |
| 105 |
} else { |
| 106 |
logLine(GRAPHML_HEADER); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
public void stopLogging() { |
| 115 |
if (!formatType.equals(EXTENDED_NEWICK) && !formatType.equals(STRIPPED_NEWICK)) { |
| 116 |
logLine(GRAPHML_FOOTER); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
static XMLOutputter outputter = new XMLOutputter(org.jdom.output.Format.getPrettyFormat()); |
| 123 |
|
| 124 |
public void log(long state) { |
| 125 |
if (logEvery <= 0 || ((state % logEvery) == 0)) { |
| 126 |
Element graphElement = argModel.toXML(); |
| 127 |
graphElement.setAttribute(ARGModel.ID_ATTRIBUTE, "STATE_" + state); |
| 128 |
if (formatType.equals(DOT_FORMAT)) |
| 129 |
logLine(GraphMLUtils.dotFormat(graphElement)); |
| 130 |
else if (formatType.equals(COMPRESSED_STRING)) |
| 131 |
logLine("ARG STATE_" + state + " = " + argModel.toGraphStringCompressed(false)); |
| 132 |
else if (formatType.equals(FULL_STRING)) |
| 133 |
logLine(outputter.outputString(graphElement)); |
| 134 |
else if (formatType.equals(EXTENDED_NEWICK)) |
| 135 |
logLine(argModel.toExtendedNewick()); |
| 136 |
else if (formatType.equals(DISTINCT_TREES)) |
| 137 |
logLine(treeStat.getStatisticValue(0) + "\t" + treeStat.getFullOutput()); |
| 138 |
else |
| 139 |
logLine(argModel.toStrippedNewick()); |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 146 |
|
| 147 |
public String getParserName() { |
| 148 |
return LOG_ARG; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 155 |
|
| 156 |
ARGModel argModel = (ARGModel) xo.getChild(ARGModel.class); |
| 157 |
|
| 158 |
String fileName = null; |
| 159 |
String title = null; |
| 160 |
String argModelLoggerFormat = null; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
if (xo.hasAttribute(TITLE)) { |
| 170 |
title = xo.getStringAttribute(TITLE); |
| 171 |
} |
| 172 |
|
| 173 |
if (xo.hasAttribute(FILE_NAME)) { |
| 174 |
fileName = xo.getStringAttribute(FILE_NAME); |
| 175 |
} |
| 176 |
|
| 177 |
if (xo.hasAttribute(FORMAT)) { |
| 178 |
argModelLoggerFormat = xo.getStringAttribute(FORMAT); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
int logEvery = 1; |
| 194 |
|
| 195 |
if (xo.hasAttribute(LOG_EVERY)) { |
| 196 |
logEvery = xo.getIntegerAttribute(LOG_EVERY); |
| 197 |
} |
| 198 |
|
| 199 |
PrintWriter pw; |
| 200 |
|
| 201 |
if (fileName != null) { |
| 202 |
|
| 203 |
try { |
| 204 |
File file = new File(fileName); |
| 205 |
String name = file.getName(); |
| 206 |
String parent = file.getParent(); |
| 207 |
|
| 208 |
if (!file.isAbsolute()) { |
| 209 |
parent = System.getProperty("user.dir"); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
pw = new PrintWriter(new FileOutputStream(new File(parent, name))); |
| 214 |
} catch (FileNotFoundException fnfe) { |
| 215 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 216 |
} |
| 217 |
} else { |
| 218 |
pw = new PrintWriter(System.out); |
| 219 |
} |
| 220 |
|
| 221 |
LogFormatter formatter = new TabDelimitedFormatter(pw); |
| 222 |
|
| 223 |
Logger.getLogger("dr.evomodel").info("Creating " + LOG_ARG + " of type " + argModelLoggerFormat); |
| 224 |
|
| 225 |
ARGLogger logger = new ARGLogger(argModel, formatter, logEvery, argModelLoggerFormat); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if (title != null) { |
| 232 |
logger.setTitle(title); |
| 233 |
} |
| 234 |
|
| 235 |
return logger; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 242 |
return rules; |
| 243 |
} |
| 244 |
|
| 245 |
String[] validFormats = {DOT_FORMAT, EXTENDED_NEWICK, |
| 246 |
COMPRESSED_STRING, FULL_STRING, STRIPPED_NEWICK, DISTINCT_TREES}; |
| 247 |
|
| 248 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 249 |
|
| 250 |
AttributeRule.newIntegerRule(LOG_EVERY), |
| 251 |
new StringAttributeRule(FORMAT, "The type logger's output type", |
| 252 |
validFormats, false), |
| 253 |
new StringAttributeRule(FILE_NAME, |
| 254 |
"The name of the file to send log output to. " + |
| 255 |
"If no file name is specified then log is sent to standard output", true), |
| 256 |
new StringAttributeRule(TITLE, "The title of the log", true), |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
new ElementRule(ARGModel.class, "The ARG which is to be logged"), |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
}; |
| 265 |
|
| 266 |
public String getParserDescription() { |
| 267 |
return "Logs an ARG to a file"; |
| 268 |
} |
| 269 |
|
| 270 |
public String getExample() { |
| 271 |
return |
| 272 |
"<!-- The " + getParserName() + " element takes an argModel to be logged -->\n" + |
| 273 |
"<" + getParserName() + " " + LOG_EVERY + "=\"100\" " + FILE_NAME + "=\"log.args\">\n" + |
| 274 |
" <argModel idref=\"treeModel1\"/>\n" + |
| 275 |
"</" + getParserName() + ">\n"; |
| 276 |
} |
| 277 |
|
| 278 |
public Class getReturnType() { |
| 279 |
return MLLogger.class; |
| 280 |
} |
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
} |