Debian Codegraph @

src/dr/evomodel/arg/ARGTreeLogger.java

raw symbols SCIP
1 /*
2 * ARGTreeLogger.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.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 * A logger that logs tree and clade frequencies from a given partition in an ARG
44 *
45 * @author Marc Suchard
46 * @version $Id: ARGTreeLogger.java,v 1.25 2006/09/05 13:29:34 rambaut Exp $
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 * Constructor
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 // BetaSplittingModel betaModel = new BetaSplittingModel(
86 // new Parameter.Default(1.0), tree);
87 // betaModel.setBeta(0.0);
88 // double otherLP = 0;
89 double logProbability = 0;
90 for (int i = 0, n = tree.getNodeCount(); i < n; i++) {
91 // System.err.println(n);
92 ARGModel.Node node = (ARGModel.Node) tree.getNode(i);
93 int count = node.getDescendentTipCount();
94 // System.err.println(count);
95 if (count > 2)
96 logProbability -= 2 * Math.log(count - 1);
97 // otherLP += betaModel.logNodeProbability(tree,node);
98 }
99 // System.err.println("me : "+logProbability);
100 // System.err.println("old: "+otherLP);
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 * @return an object based on the XML element it was passed.
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 // logEvery of zero only displays at the end
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 // System.out.println("Writing log file to "+parent+System.getProperty("path.separator")+name);
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 // AbstractXMLObjectParser implementation
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 }