| 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.substmodel; |
| 27 |
|
| 28 |
import dr.evomodel.substmodel.CodonLabeling; |
| 29 |
import dr.evomodel.substmodel.CodonPartitionedRobustCounting; |
| 30 |
import dr.evomodel.substmodel.StratifiedTraitOutputFormat; |
| 31 |
import dr.evomodel.treelikelihood.AncestralStateBeagleTreeLikelihood; |
| 32 |
import dr.evolution.datatype.Codons; |
| 33 |
import dr.evolution.datatype.GeneticCode; |
| 34 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 35 |
import dr.evomodel.tree.TreeModel; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class CodonPartitionedRobustCountingParser extends AbstractXMLObjectParser { |
| 42 |
|
| 43 |
public static final String PARSER_NAME = "codonPartitionedRobustCounting"; |
| 44 |
public static final String FIRST = "firstPosition"; |
| 45 |
public static final String SECOND = "secondPosition"; |
| 46 |
public static final String THIRD = "thirdPosition"; |
| 47 |
public static final String LABELING = "labeling"; |
| 48 |
public static final String BRANCH_FORMAT = "branchFormat"; |
| 49 |
public static final String LOG_FORMAT = "logFormat"; |
| 50 |
public static final String USE_UNIFORMIZATION = "useUniformization"; |
| 51 |
public static final String INCLUDE_EXTERNAL = "includeExternalBranches"; |
| 52 |
public static final String INCLUDE_INTERNAL = "includeInternalBranches"; |
| 53 |
public static final String DO_UNCONDITIONED_PER_BRANCH = "unconditionedPerBranch"; |
| 54 |
public static final String SAVE_HISTORY = "saveCompleteHistory"; |
| 55 |
public static final String AVERAGE_RATES = "averageRates"; |
| 56 |
public static final String USE_NEW_NEUTRAL_MODEL = "useNewNeutralModel"; |
| 57 |
public static final String PREFIX = "prefix"; |
| 58 |
|
| 59 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 60 |
|
| 61 |
AncestralStateBeagleTreeLikelihood[] partition = new AncestralStateBeagleTreeLikelihood[3]; |
| 62 |
String[] labels = new String[]{FIRST, SECOND, THIRD}; |
| 63 |
|
| 64 |
int patternCount = -1; |
| 65 |
|
| 66 |
BranchRateModel testBranchRateModel = null; |
| 67 |
for (int i = 0; i < 3; i++) { |
| 68 |
partition[i] = (AncestralStateBeagleTreeLikelihood) |
| 69 |
xo.getChild(labels[i]).getChild(AncestralStateBeagleTreeLikelihood.class); |
| 70 |
|
| 71 |
if (i == 0) { |
| 72 |
patternCount = partition[i].getPatternCount(); |
| 73 |
} else { |
| 74 |
if (partition[i].getPatternCount() != patternCount) { |
| 75 |
throw new XMLParseException("Codon-partitioned robust counting requires all partitions to have the same length." + |
| 76 |
" Make sure that partitions include all unique sites and do not strip gaps."); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if (partition[i].getSiteRateModel().getCategoryCount() > 1) { |
| 81 |
throw new XMLParseException("Robust counting currently only implemented for single category models"); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
if (testBranchRateModel == null) { |
| 86 |
testBranchRateModel = partition[i].getBranchRateModel(); |
| 87 |
} else if (testBranchRateModel != partition[i].getBranchRateModel()) { |
| 88 |
throw new XMLParseException( |
| 89 |
"Robust counting currently requires the same branch rate model for all partitions"); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class); |
| 94 |
|
| 95 |
Codons codons = Codons.UNIVERSAL; |
| 96 |
if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) { |
| 97 |
String codeStr = xo.getStringAttribute(GeneticCode.GENETIC_CODE); |
| 98 |
codons = Codons.findByName(codeStr); |
| 99 |
} |
| 100 |
|
| 101 |
String labelingString = (String) xo.getAttribute(LABELING); |
| 102 |
CodonLabeling codonLabeling = CodonLabeling.parseFromString(labelingString); |
| 103 |
if (codonLabeling == null) { |
| 104 |
throw new XMLParseException("Unrecognized codon labeling '" + labelingString + "'"); |
| 105 |
} |
| 106 |
|
| 107 |
String branchFormatString = xo.getAttribute(BRANCH_FORMAT, |
| 108 |
StratifiedTraitOutputFormat.SUM_OVER_SITES.getText()); |
| 109 |
StratifiedTraitOutputFormat branchFormat = StratifiedTraitOutputFormat.parseFromString(branchFormatString); |
| 110 |
if (branchFormat == null) { |
| 111 |
throw new XMLParseException("Unrecognized branch output format '" + branchFormat + "'"); |
| 112 |
} |
| 113 |
|
| 114 |
String logFormatString = xo.getAttribute(LOG_FORMAT, |
| 115 |
StratifiedTraitOutputFormat.SUM_OVER_SITES.getText()); |
| 116 |
StratifiedTraitOutputFormat logFormat = StratifiedTraitOutputFormat.parseFromString(logFormatString); |
| 117 |
if (logFormat == null) { |
| 118 |
throw new XMLParseException("Unrecognized log output format '" + branchFormat + "'"); |
| 119 |
} |
| 120 |
|
| 121 |
boolean useUniformization = xo.getAttribute(USE_UNIFORMIZATION, false); |
| 122 |
boolean includeExternalBranches = xo.getAttribute(INCLUDE_EXTERNAL, true); |
| 123 |
boolean includeInternalBranches = xo.getAttribute(INCLUDE_INTERNAL, true); |
| 124 |
boolean doUnconditionedPerBranch = xo.getAttribute(DO_UNCONDITIONED_PER_BRANCH, false); |
| 125 |
boolean averageRates = xo.getAttribute(AVERAGE_RATES, true); |
| 126 |
boolean saveCompleteHistory = xo.getAttribute(SAVE_HISTORY, false); |
| 127 |
boolean useNewNeutralModel = xo.getAttribute(USE_NEW_NEUTRAL_MODEL, false); |
| 128 |
|
| 129 |
String prefix = xo.hasAttribute(PREFIX) ? xo.getStringAttribute(PREFIX) : null; |
| 130 |
|
| 131 |
return new CodonPartitionedRobustCounting( |
| 132 |
xo.getId(), |
| 133 |
tree, |
| 134 |
partition, |
| 135 |
codons, |
| 136 |
codonLabeling, |
| 137 |
useUniformization, |
| 138 |
includeExternalBranches, |
| 139 |
includeInternalBranches, |
| 140 |
doUnconditionedPerBranch, |
| 141 |
saveCompleteHistory, |
| 142 |
averageRates, |
| 143 |
useNewNeutralModel, |
| 144 |
branchFormat, |
| 145 |
logFormat, |
| 146 |
prefix); |
| 147 |
} |
| 148 |
|
| 149 |
private static final XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 150 |
new ElementRule(FIRST, |
| 151 |
new XMLSyntaxRule[]{ |
| 152 |
new ElementRule(AncestralStateBeagleTreeLikelihood.class), |
| 153 |
}), |
| 154 |
new ElementRule(SECOND, |
| 155 |
new XMLSyntaxRule[]{ |
| 156 |
new ElementRule(AncestralStateBeagleTreeLikelihood.class), |
| 157 |
}), |
| 158 |
new ElementRule(THIRD, |
| 159 |
new XMLSyntaxRule[]{ |
| 160 |
new ElementRule(AncestralStateBeagleTreeLikelihood.class), |
| 161 |
}), |
| 162 |
new ElementRule(TreeModel.class), |
| 163 |
new StringAttributeRule(GeneticCode.GENETIC_CODE, |
| 164 |
"The genetic code to use", |
| 165 |
GeneticCode.GENETIC_CODE_NAMES, true), |
| 166 |
AttributeRule.newBooleanRule(USE_UNIFORMIZATION, true), |
| 167 |
AttributeRule.newBooleanRule(INCLUDE_EXTERNAL, true), |
| 168 |
AttributeRule.newBooleanRule(INCLUDE_INTERNAL, true), |
| 169 |
AttributeRule.newBooleanRule(DO_UNCONDITIONED_PER_BRANCH, true), |
| 170 |
AttributeRule.newBooleanRule(AVERAGE_RATES, true), |
| 171 |
AttributeRule.newStringRule(LABELING), |
| 172 |
AttributeRule.newBooleanRule(SAVE_HISTORY, true), |
| 173 |
AttributeRule.newBooleanRule(USE_NEW_NEUTRAL_MODEL, true), |
| 174 |
}; |
| 175 |
|
| 176 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 177 |
return rules; |
| 178 |
} |
| 179 |
|
| 180 |
public String getParserDescription() { |
| 181 |
return "A parser to specify robust counting procedures on codon partitioned models"; |
| 182 |
} |
| 183 |
|
| 184 |
public Class getReturnType() { |
| 185 |
return CodonPartitionedRobustCounting.class; |
| 186 |
} |
| 187 |
|
| 188 |
public String getParserName() { |
| 189 |
return PARSER_NAME; |
| 190 |
} |
| 191 |
} |