| 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.siteratemodel; |
| 27 |
|
| 28 |
import java.util.logging.Logger; |
| 29 |
|
| 30 |
import dr.evomodel.siteratemodel.GammaSiteRateModel; |
| 31 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 32 |
import dr.oldevomodel.sitemodel.SiteModel; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.xml.AbstractXMLObjectParser; |
| 35 |
import dr.xml.AttributeRule; |
| 36 |
import dr.xml.ElementRule; |
| 37 |
import dr.xml.XMLObject; |
| 38 |
import dr.xml.XMLParseException; |
| 39 |
import dr.xml.XMLSyntaxRule; |
| 40 |
import dr.xml.XORRule; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class GammaSiteModelParser extends AbstractXMLObjectParser { |
| 48 |
|
| 49 |
public static final String SITE_MODEL = SiteModel.SITE_MODEL; |
| 50 |
public static final String SUBSTITUTION_MODEL = "substitutionModel"; |
| 51 |
public static final String BRANCH_SUBSTITUTION_MODEL = "branchSubstitutionModel"; |
| 52 |
public static final String MUTATION_RATE = "mutationRate"; |
| 53 |
public static final String SUBSTITUTION_RATE = "substitutionRate"; |
| 54 |
public static final String RELATIVE_RATE = "relativeRate"; |
| 55 |
public static final String WEIGHT = "weight"; |
| 56 |
public static final String GAMMA_SHAPE = "gammaShape"; |
| 57 |
public static final String GAMMA_CATEGORIES = "gammaCategories"; |
| 58 |
public static final String PROPORTION_INVARIANT = "proportionInvariant"; |
| 59 |
|
| 60 |
public String getParserName() { |
| 61 |
return SITE_MODEL; |
| 62 |
} |
| 63 |
|
| 64 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 65 |
|
| 66 |
String msg = ""; |
| 67 |
SubstitutionModel substitutionModel = null; |
| 68 |
|
| 69 |
double muWeight = 1.0; |
| 70 |
|
| 71 |
Parameter muParam = null; |
| 72 |
if (xo.hasChildNamed(SUBSTITUTION_RATE)) { |
| 73 |
muParam = (Parameter) xo.getElementFirstChild(SUBSTITUTION_RATE); |
| 74 |
|
| 75 |
msg += "\n with initial substitution rate = " + muParam.getParameterValue(0); |
| 76 |
} else if (xo.hasChildNamed(MUTATION_RATE)) { |
| 77 |
muParam = (Parameter) xo.getElementFirstChild(MUTATION_RATE); |
| 78 |
|
| 79 |
msg += "\n with initial substitution rate = " + muParam.getParameterValue(0); |
| 80 |
} else if (xo.hasChildNamed(RELATIVE_RATE)) { |
| 81 |
XMLObject cxo = xo.getChild(RELATIVE_RATE); |
| 82 |
muParam = (Parameter) cxo.getChild(Parameter.class); |
| 83 |
msg += "\n with initial relative rate = " + muParam.getParameterValue(0); |
| 84 |
if (cxo.hasAttribute(WEIGHT)) { |
| 85 |
muWeight = cxo.getDoubleAttribute(WEIGHT); |
| 86 |
msg += " with weight: " + muWeight; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
Parameter shapeParam = null; |
| 91 |
int catCount = 4; |
| 92 |
if (xo.hasChildNamed(GAMMA_SHAPE)) { |
| 93 |
XMLObject cxo = xo.getChild(GAMMA_SHAPE); |
| 94 |
catCount = cxo.getIntegerAttribute(GAMMA_CATEGORIES); |
| 95 |
shapeParam = (Parameter) cxo.getChild(Parameter.class); |
| 96 |
|
| 97 |
msg += "\n " + catCount + " category discrete gamma with initial shape = " + shapeParam.getParameterValue(0); |
| 98 |
} |
| 99 |
|
| 100 |
Parameter invarParam = null; |
| 101 |
if (xo.hasChildNamed(PROPORTION_INVARIANT)) { |
| 102 |
invarParam = (Parameter) xo.getElementFirstChild(PROPORTION_INVARIANT); |
| 103 |
msg += "\n initial proportion of invariant sites = " + invarParam.getParameterValue(0); |
| 104 |
} |
| 105 |
|
| 106 |
if (msg.length() > 0) { |
| 107 |
Logger.getLogger("dr.evomodel").info("\nCreating site rate model: " + msg); |
| 108 |
} else { |
| 109 |
Logger.getLogger("dr.evomodel").info("\nCreating site rate model."); |
| 110 |
} |
| 111 |
|
| 112 |
GammaSiteRateModel siteRateModel = new GammaSiteRateModel(SITE_MODEL, muParam, muWeight, shapeParam, catCount, invarParam); |
| 113 |
|
| 114 |
if (xo.hasChildNamed(SUBSTITUTION_MODEL)) { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
substitutionModel = (SubstitutionModel) xo.getElementFirstChild(SUBSTITUTION_MODEL); |
| 120 |
siteRateModel.setSubstitutionModel(substitutionModel); |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
return siteRateModel; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public String getParserDescription() { |
| 132 |
return "A SiteModel that has a gamma distributed rates across sites"; |
| 133 |
} |
| 134 |
|
| 135 |
public Class<GammaSiteRateModel> getReturnType() { |
| 136 |
return GammaSiteRateModel.class; |
| 137 |
} |
| 138 |
|
| 139 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 140 |
return rules; |
| 141 |
} |
| 142 |
|
| 143 |
private final XMLSyntaxRule[] rules = { |
| 144 |
|
| 145 |
new ElementRule(SUBSTITUTION_MODEL, new XMLSyntaxRule[]{ |
| 146 |
new ElementRule(SubstitutionModel.class) |
| 147 |
}, true), |
| 148 |
|
| 149 |
new XORRule( |
| 150 |
new XORRule( |
| 151 |
new ElementRule(SUBSTITUTION_RATE, new XMLSyntaxRule[]{ |
| 152 |
new ElementRule(Parameter.class) |
| 153 |
}), |
| 154 |
new ElementRule(MUTATION_RATE, new XMLSyntaxRule[]{ |
| 155 |
new ElementRule(Parameter.class) |
| 156 |
}) |
| 157 |
), |
| 158 |
new ElementRule(RELATIVE_RATE, new XMLSyntaxRule[]{ |
| 159 |
AttributeRule.newDoubleRule(WEIGHT, true), |
| 160 |
new ElementRule(Parameter.class) |
| 161 |
}), true |
| 162 |
), |
| 163 |
|
| 164 |
new ElementRule(GAMMA_SHAPE, new XMLSyntaxRule[]{ |
| 165 |
AttributeRule.newIntegerRule(GAMMA_CATEGORIES, true), |
| 166 |
new ElementRule(Parameter.class) |
| 167 |
}, true), |
| 168 |
|
| 169 |
new ElementRule(PROPORTION_INVARIANT, new XMLSyntaxRule[]{ |
| 170 |
new ElementRule(Parameter.class) |
| 171 |
}, true) |
| 172 |
|
| 173 |
}; |
| 174 |
|
| 175 |
} |