Debian Codegraph @

src/dr/evomodel/arg/ARGLogger.java

raw symbols SCIP
1 /*
2 * ARGLogger.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * Created by IntelliJ IDEA.
45 * User: msuchard
46 * Date: Jan 13, 2007
47 * Time: 2:16:08 PM
48 * To change this template use File | Settings | File Templates.
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 // The following were in MCLogger; where did they go?
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 // public static final String FORMAT = "format";
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 // if (!dotFormat && !newickFormat)
109 // logLine(GRAPHML_HEADER);
110 // if (newickFormat)
111 // logLine("state ARG.string");
112 }
113
114 public void stopLogging() {
115 if (!formatType.equals(EXTENDED_NEWICK) && !formatType.equals(STRIPPED_NEWICK)) {
116 logLine(GRAPHML_FOOTER);
117 }
118 // if (!dotFormat && !newickFormat)
119 // logLine(GRAPHML_FOOTER);
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 // logLine("ARG STATE_" + state + " = " + argModel.toExtendedNewick());
142 }
143
144
145 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
146
147 public String getParserName() {
148 return LOG_ARG;
149 }
150
151 /**
152 * @return an object based on the XML element it was passed.
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 // boolean dotFormat = false;
162 // boolean newickFormat = false;
163 // boolean nexusFormat = false;
164 //
165 // String colouringLabel = "demes";
166 // String rateLabel = "rate";
167 // String likelihoodLabel = "lnP";
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 // boolean substitutions = false;
182 // if (xo.hasAttribute(BRANCH_LENGTHS)) {
183 // substitutions = xo.getStringAttribute(BRANCH_LENGTHS).equals(SUBSTITUTIONS);
184 // }
185
186 // BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
187
188 // ColourSamplerModel colourSamplerModel = (ColourSamplerModel) xo.getChild(ColourSamplerModel.class);
189
190 // Likelihood likelihood = (Likelihood) xo.getChild(Likelihood.class);
191
192 // logEvery of zero only displays at the end
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 // System.out.println("Writing log file to "+parent+System.getProperty("path.separator")+name);
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 // TreeLogger logger = new TreeLogger(tree, branchRateModel, rateLabel,
228 // colourSamplerModel, colouringLabel, likelihood, likelihoodLabel,
229 // formatter, logEvery, nexusFormat, substitutions);
230
231 if (title != null) {
232 logger.setTitle(title);
233 }
234
235 return logger;
236 }
237
238 //************************************************************************
239 // AbstractXMLObjectParser implementation
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 // AttributeRule.newBooleanRule(NEXUS_FORMAT, true,
258 // "Whether to use the NEXUS format for the tree log"),
259 // new StringAttributeRule(BRANCH_LENGTHS, "What units should the branch lengths be in", new String[]{TIME, SUBSTITUTIONS}, true),
260 new ElementRule(ARGModel.class, "The ARG which is to be logged"),
261 // new ElementRule(BranchRateModel.class, true),
262 // new ElementRule(ColourSamplerModel.class, true),
263 // new ElementRule(Likelihood.class, true)
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 }