| 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 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 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 |
|
| 85 |
int burnin = -1; |
| 86 |
if (xo.hasAttribute(BURN_IN)) { |
| 87 |
|
| 88 |
burnin = xo.getIntegerAttribute(BURN_IN); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
double e = 1.0; |
| 93 |
if (xo.hasAttribute(EPSILON)) { |
| 94 |
|
| 95 |
e = xo.getDoubleAttribute(EPSILON); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 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 |
|
| 119 |
ConditionalCladeFrequency analysis = ConditionalCladeFrequency.analyzeLogFile(new Reader[]{reader}, e, burnin, true); |
| 120 |
|
| 121 |
|
| 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 |
|
| 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 |
} |