| 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.coalescent; |
| 27 |
|
| 28 |
import dr.evolution.io.Importer; |
| 29 |
import dr.evomodel.coalescent.EBSPAnalysis; |
| 30 |
import dr.evomodel.coalescent.VariableDemographicModel; |
| 31 |
import dr.inference.trace.TraceException; |
| 32 |
import dr.util.FileHelpers; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
import java.io.File; |
| 36 |
import java.io.FileWriter; |
| 37 |
import java.io.PrintWriter; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class EBSPAnalysisParser extends AbstractXMLObjectParser { |
| 42 |
|
| 43 |
public static final String VD_ANALYSIS = "VDAnalysis"; |
| 44 |
public static final String FILE_NAME = "fileName"; |
| 45 |
public static final String BURN_IN = "burnIn"; |
| 46 |
public static final String HPD_LEVELS = "Confidencelevels"; |
| 47 |
public static final String QUANTILES = "useQuantiles"; |
| 48 |
public static final String LOG_SPACE = VariableDemographicModelParser.LOG_SPACE; |
| 49 |
public static final String USE_MIDDLE = VariableDemographicModelParser.USE_MIDPOINTS; |
| 50 |
public static final String N_CHANGES = "nChanges"; |
| 51 |
|
| 52 |
public static final String TREE_LOG = "treeOfLoci"; |
| 53 |
|
| 54 |
public static final String LOG_FILE_NAME = "logFileName"; |
| 55 |
public static final String TREE_FILE_NAMES = "treeFileNames"; |
| 56 |
public static final String MODEL_TYPE = "populationModelType"; |
| 57 |
public static final String POPULATION_FIRST_COLUMN = "populationFirstColumn"; |
| 58 |
public static final String INDICATORS_FIRST_COLUMN = "indicatorsFirstColumn"; |
| 59 |
public static final String ROOTHEIGHT_COLUMN = "rootheightColumn"; |
| 60 |
public static final String ALLDEMO_COLUMN = "allDemographicsFileName"; |
| 61 |
public static final String NBINS = "nBins"; |
| 62 |
|
| 63 |
private String getElementText(XMLObject xo, String childName) throws XMLParseException { |
| 64 |
return xo.getChild(childName).getStringChild(0); |
| 65 |
} |
| 66 |
|
| 67 |
public String getParserName() { |
| 68 |
return VD_ANALYSIS; |
| 69 |
} |
| 70 |
|
| 71 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 72 |
|
| 73 |
try { |
| 74 |
|
| 75 |
|
| 76 |
final double burnin = xo.getAttribute(BURN_IN, 0.1); |
| 77 |
if (burnin < 0) { |
| 78 |
throw new XMLParseException("burnIn should be either between 0 and 1 or a positive number"); |
| 79 |
} |
| 80 |
|
| 81 |
final double[] hpdLevels = xo.hasAttribute(HPD_LEVELS) ? xo.getDoubleArrayAttribute(HPD_LEVELS) : null; |
| 82 |
|
| 83 |
final File log = FileHelpers.getFile(getElementText(xo, LOG_FILE_NAME)); |
| 84 |
final XMLObject treeFileNames = xo.getChild(TREE_FILE_NAMES); |
| 85 |
final int nTrees = treeFileNames.getChildCount(); |
| 86 |
File[] treeFiles = new File[nTrees]; |
| 87 |
for (int k = 0; k < nTrees; ++k) { |
| 88 |
treeFiles[k] = FileHelpers.getFile(((XMLObject) treeFileNames.getChild(k)).getStringChild(0)); |
| 89 |
} |
| 90 |
|
| 91 |
String modelTypeName = getElementText(xo, MODEL_TYPE).trim().toUpperCase(); |
| 92 |
String populationFirstColumn = getElementText(xo, POPULATION_FIRST_COLUMN); |
| 93 |
String indicatorsFirstColumn = getElementText(xo, INDICATORS_FIRST_COLUMN); |
| 94 |
VariableDemographicModel.Type modelType = VariableDemographicModel.Type.valueOf(modelTypeName); |
| 95 |
|
| 96 |
String rootHeightColumn = null; |
| 97 |
int nBins = -1; |
| 98 |
if (xo.hasAttribute(NBINS)) { |
| 99 |
if (xo.getChild(ROOTHEIGHT_COLUMN) != null) { |
| 100 |
rootHeightColumn = getElementText(xo, ROOTHEIGHT_COLUMN); |
| 101 |
nBins = xo.getIntegerAttribute(NBINS); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
PrintWriter allDemoWriter = null; |
| 106 |
if (xo.getChild(ALLDEMO_COLUMN) != null) { |
| 107 |
String fName = getElementText(xo, ALLDEMO_COLUMN); |
| 108 |
allDemoWriter = new PrintWriter(new FileWriter(fName)); |
| 109 |
} |
| 110 |
|
| 111 |
final boolean quantiles = xo.getAttribute(QUANTILES, false); |
| 112 |
final boolean logSpace = xo.getAttribute(LOG_SPACE, false); |
| 113 |
final boolean useMid = xo.getAttribute(USE_MIDDLE, false); |
| 114 |
final int onlyNchanges = xo.getAttribute(N_CHANGES, -1); |
| 115 |
|
| 116 |
return new EBSPAnalysis(log, treeFiles, modelType, |
| 117 |
populationFirstColumn, indicatorsFirstColumn, |
| 118 |
rootHeightColumn, nBins, |
| 119 |
burnin, hpdLevels, quantiles, logSpace, useMid, onlyNchanges, |
| 120 |
allDemoWriter); |
| 121 |
|
| 122 |
} catch (java.io.IOException ioe) { |
| 123 |
throw new XMLParseException(ioe.getMessage()); |
| 124 |
} catch (Importer.ImportException e) { |
| 125 |
throw new XMLParseException(e.toString()); |
| 126 |
} catch (TraceException e) { |
| 127 |
throw new XMLParseException(e.toString()); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public String getParserDescription() { |
| 136 |
return "reconstruct population graph from EBSP run."; |
| 137 |
} |
| 138 |
|
| 139 |
public Class getReturnType() { |
| 140 |
return EBSPAnalysis.class; |
| 141 |
} |
| 142 |
|
| 143 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 144 |
return rules; |
| 145 |
} |
| 146 |
|
| 147 |
private final XMLSyntaxRule[] rules = { |
| 148 |
AttributeRule.newDoubleRule(BURN_IN, true, "The number of states (not sampled states, but" + |
| 149 |
" actual states) that are discarded from the beginning of the trace and are excluded from " + |
| 150 |
"the analysis"), |
| 151 |
AttributeRule.newDoubleArrayRule(HPD_LEVELS, true), |
| 152 |
AttributeRule.newBooleanRule(QUANTILES, true), |
| 153 |
AttributeRule.newBooleanRule(LOG_SPACE, true), |
| 154 |
AttributeRule.newBooleanRule(USE_MIDDLE, true), |
| 155 |
AttributeRule.newIntegerRule(NBINS, true), |
| 156 |
AttributeRule.newIntegerRule(N_CHANGES, true), |
| 157 |
|
| 158 |
new ElementRule(LOG_FILE_NAME, String.class, "The name of a BEAST log file"), |
| 159 |
new ElementRule(TREE_FILE_NAMES, |
| 160 |
new XMLSyntaxRule[]{ |
| 161 |
new ElementRule(TREE_LOG, String.class, "The name of a BEAST trees log file", 1, Integer.MAX_VALUE) |
| 162 |
}), |
| 163 |
new ElementRule(MODEL_TYPE, String.class, "population model type (stepwise, linear ..."), |
| 164 |
new ElementRule(POPULATION_FIRST_COLUMN, String.class, "Name of first column of population size"), |
| 165 |
new ElementRule(INDICATORS_FIRST_COLUMN, String.class, "Name of first column of population indicators"), |
| 166 |
new ElementRule(ROOTHEIGHT_COLUMN, String.class, "Name of trace column of root height", true), |
| 167 |
new ElementRule(ALLDEMO_COLUMN, String.class, "Name of file to output all demographic functions", true), |
| 168 |
}; |
| 169 |
} |