Debian Codegraph @

src/dr/evomodelxml/TreeTraceAnalysisParser.java

raw symbols SCIP
1 /*
2 * TreeTraceAnalysisParser.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.evomodelxml;
27
28 import dr.evolution.io.Importer;
29 import dr.evolution.io.NewickImporter;
30 import dr.evolution.tree.Tree;
31 import dr.evomodel.tree.TreeTraceAnalysis;
32 import dr.xml.*;
33
34 import java.io.File;
35 import java.io.FileNotFoundException;
36 import java.io.FileReader;
37 import java.io.Reader;
38
39 /**
40 * @author Alexei Drummond
41 * @version $Id: TreeTraceAnalysisParser.java,v 1.6 2005/05/24 20:25:58 rambaut Exp $
42 */
43 public class TreeTraceAnalysisParser extends AbstractXMLObjectParser {
44
45 public final static String TREE_TRACE_ANALYSIS = "treeTraceAnalysis";
46 public final static String BURN_IN = "burnIn";
47 public final static String MIN_CLADE_PROBABILITY = "minCladeProbability";
48 public final static String CRED_SET_PROBABILITY = "credSetProbability";
49 public static final String FILE_NAME = "fileName";
50
51 public final static String REFERENCE_TREE = "referenceTree";
52 public final static String SHORT_REPORT = "shortReport";
53
54 public String getParserName() {
55 return TREE_TRACE_ANALYSIS;
56 }
57
58 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
59
60 try {
61 Reader reader;
62
63 String fileName = xo.getStringAttribute(FILE_NAME);
64 String name;
65 try {
66 File file = new File(fileName);
67 name = file.getName();
68 String parent = file.getParent();
69
70 if (!file.isAbsolute()) {
71 parent = System.getProperty("user.dir");
72 }
73
74 // System.out.println("Writing log file to "+parent+System.getProperty("path.separator")+name);
75 reader = new FileReader(new File(parent, name));
76 } catch (FileNotFoundException fnfe) {
77 throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
78 }
79
80 // leaving the burnin attribute off will result in 10% being used
81 int burnin = xo.getAttribute(BURN_IN, -1);
82
83 double minCladeProbability = xo.getAttribute(MIN_CLADE_PROBABILITY, 0.5);
84
85 double credSetProbability = xo.getAttribute(CRED_SET_PROBABILITY, 0.95);
86
87 Tree referenceTree = null;
88 Reader refReader;
89 if (xo.hasAttribute(REFERENCE_TREE)) {
90 String referenceName = xo.getStringAttribute(REFERENCE_TREE);
91
92 try {
93 File refFile = new File(referenceName);
94 String refName = refFile.getName();
95 String parent = refFile.getParent();
96
97 if (!refFile.isAbsolute()) {
98 parent = System.getProperty("user.dir");
99 }
100 refReader = new FileReader(new File(parent, refName));
101 } catch (FileNotFoundException fnfe) {
102 throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
103 }
104
105 try {
106 NewickImporter importTree = new NewickImporter(refReader);
107 if (importTree.hasTree()) {
108 referenceTree = importTree.importNextTree();
109 }
110 } catch (Importer.ImportException iee) {
111 throw new XMLParseException("Reference file '" + referenceName + "' is empty.");
112 }
113 }
114
115 boolean shortReport = xo.getAttribute(SHORT_REPORT, false);
116
117 TreeTraceAnalysis analysis = TreeTraceAnalysis.analyzeLogFile(new Reader[]{reader}, burnin, true);
118
119 if (shortReport) {
120 analysis.shortReport(name, referenceTree, true, credSetProbability);
121 } else {
122 analysis.report(minCladeProbability, credSetProbability, 0);
123 }
124
125 System.out.println();
126 System.out.flush();
127
128 return analysis;
129 } catch (java.io.IOException ioe) {
130 throw new XMLParseException(ioe.getMessage());
131 }
132 }
133
134 //************************************************************************
135 // AbstractXMLObjectParser implementation
136 //************************************************************************
137
138 public String getParserDescription() {
139 return "Analyses and reports on a trace consisting of trees.";
140 }
141
142 public Class getReturnType() {
143 return TreeTraceAnalysis.class;
144 }
145
146 public XMLSyntaxRule[] getSyntaxRules() {
147 return rules;
148 }
149
150 private final XMLSyntaxRule[] rules = {
151 new StringAttributeRule(FILE_NAME, "name of a tree log file", "trees.log"),
152 AttributeRule.newIntegerRule(BURN_IN, true),
153 AttributeRule.newDoubleRule(MIN_CLADE_PROBABILITY, true),
154 AttributeRule.newDoubleRule(CRED_SET_PROBABILITY, true),
155 AttributeRule.newBooleanRule(SHORT_REPORT, true),
156 AttributeRule.newStringRule(REFERENCE_TREE, true)
157 };
158 }