| 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 |
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 |
|
| 41 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |