Debian Codegraph @

src/dr/evomodelxml/ConditionalCladeFrequencyParser.java

raw symbols SCIP
1 /*
2 * ConditionalCladeFrequencyParser.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 /**
27 *
28 */
29 package dr.evomodelxml;
30
31 import dr.evolution.io.Importer;
32 import dr.evolution.io.NewickImporter;
33 import dr.evolution.tree.Tree;
34 import dr.evomodel.tree.ConditionalCladeFrequency;
35 import dr.xml.*;
36
37 import java.io.File;
38 import java.io.FileNotFoundException;
39 import java.io.FileReader;
40 import java.io.Reader;
41
42 /**
43 * @author Sebastian Hoehna
44 * <p/>
45 * This is the parser for the xml block in the BEAST input file for the conditional clade frequency statistic.
46 * It is used to estimate the posterior of a tree given a trace of trees.
47 */
48 public class ConditionalCladeFrequencyParser extends AbstractXMLObjectParser {
49
50 public final static String CONDITIONAL_CLADE_PROBABILITY_ANALYSIS = "ConditionalCladeProbabilityAnalysis";
51 public final static String BURN_IN = "burnIn";
52 public final static String EPSILON = "epsilon";
53 public final static String MIN_CLADE_PROBABILITY = "minCladeProbability";
54 public static final String FILE_NAME = "fileName";
55
56 public final static String REFERENCE_TREE = "referenceTree";
57
58 public String getParserName() {
59 return CONDITIONAL_CLADE_PROBABILITY_ANALYSIS;
60 }
61
62 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
63
64 try {
65 Reader reader;
66
67 // the tree file with the trace of trees. Usually an output of a previous BEAST run like *.trees
68 String fileName = xo.getStringAttribute(FILE_NAME);
69 String name;
70 try {
71 File file = new File(fileName);
72 name = file.getName();
73 String parent = file.getParent();
74
75 if (!file.isAbsolute()) {
76 parent = System.getProperty("user.dir");
77 }
78
79 reader = new FileReader(new File(parent, name));
80 } catch (FileNotFoundException fnfe) {
81 throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
82 }
83
84 // the burn-in is used as the number of trees discarded
85 int burnin = -1;
86 if (xo.hasAttribute(BURN_IN)) {
87 // leaving the burnin attribute off will result in 10% being used
88 burnin = xo.getIntegerAttribute(BURN_IN);
89 }
90
91 // the epsilon value which represents the number of occurrences for every not observed clade
92 double e = 1.0;
93 if (xo.hasAttribute(EPSILON)) {
94 // leaving the epsilon attribute off will result in 1.0 being used
95 e = xo.getDoubleAttribute(EPSILON);
96 }
97
98
99 // read the reference tree from a newick file
100 Reader refReader = null;
101 if (xo.hasAttribute(REFERENCE_TREE)) {
102 String referenceName = xo.getStringAttribute(REFERENCE_TREE);
103
104 try {
105 File refFile = new File(referenceName);
106 String refName = refFile.getName();
107 String parent = refFile.getParent();
108
109 if (!refFile.isAbsolute()) {
110 parent = System.getProperty("user.dir");
111 }
112 refReader = new FileReader(new File(parent, refName));
113 } catch (FileNotFoundException fnfe) {
114 throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
115 }
116 }
117
118 // creates a new analyzer object
119 ConditionalCladeFrequency analysis = ConditionalCladeFrequency.analyzeLogFile(new Reader[]{reader}, e, burnin, true);
120
121 // analyze the reference tree and print its estimated posterior
122 analysis.report(refReader);
123
124 System.out.println();
125 System.out.flush();
126
127 return analysis;
128 } catch (Exception ioe) {
129 throw new XMLParseException(ioe.getMessage());
130 }
131 }
132
133 //************************************************************************
134 // AbstractXMLObjectParser implementation
135 //************************************************************************
136
137 public String getParserDescription() {
138 return "Calculates posterior of a tree on a trace consisting of trees.";
139 }
140
141 public Class getReturnType() {
142 return ConditionalCladeFrequency.class;
143 }
144
145 public XMLSyntaxRule[] getSyntaxRules() {
146 return rules;
147 }
148
149 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
150 new StringAttributeRule(FILE_NAME, "name of a tree log file", "trees.log"),
151 AttributeRule.newIntegerRule(BURN_IN, true, "The number of trees discarded because of the burn-in period."),
152 AttributeRule.newDoubleRule(MIN_CLADE_PROBABILITY, true, "The frequency in % that a clade must achieve to be considered for the evaulation. Otherwise it's set to epsilon."),
153 AttributeRule.newDoubleRule(EPSILON, true, "Epsilon is the default number of occurences for a clade if a clade wasn't observed. The default is 1.0"),
154 AttributeRule.newStringRule(REFERENCE_TREE, false, "File in newick format containing a tree which is evaluated with the statistic.")
155 };
156
157 }