use of dr.evomodel.substmodel.SubstitutionModel in project beast-mcmc by beast-dev.
the class RandomBranchModel method setup.
// END: Constructor
private void setup() {
DataType dataType = baseSubstitutionModel.getDataType();
FrequencyModel freqModel = baseSubstitutionModel.getFrequencyModel();
Parameter kappaParameter = new Parameter.Default("kappa", 1, baseSubstitutionModel.getKappa());
substitutionModels = new LinkedList<SubstitutionModel>();
branchAssignmentMap = new LinkedHashMap<NodeRef, Integer>();
int branchClass = 0;
for (NodeRef node : treeModel.getNodes()) {
if (!treeModel.isRoot(node)) {
double nodeHeight = treeModel.getNodeHeight(node);
double parentHeight = treeModel.getNodeHeight(treeModel.getParent(node));
double time = 0.5 * (parentHeight + nodeHeight);
double baseOmega = baseSubstitutionModel.getOmega();
double fixed = baseOmega * time;
// Math.exp((random.nextGaussian() * stdev + mean));
double epsilon = (Math.log(1 - random.nextDouble()) / (-rate));
double value = fixed + epsilon;
Parameter omegaParameter = new Parameter.Default("omega", 1, value);
GY94CodonModel gy94 = new GY94CodonModel((Codons) dataType, omegaParameter, kappaParameter, freqModel);
substitutionModels.add(gy94);
branchAssignmentMap.put(node, branchClass);
branchClass++;
}
// END: root check
}
// END: nodes loop
}
use of dr.evomodel.substmodel.SubstitutionModel in project beast-mcmc by beast-dev.
the class ArbitrarySubstitutionParameterBranchModelParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Logger.getLogger("dr.evomodel").info("\nUsing branch-specific substitution parameter branch model.");
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
if (!(substitutionModel instanceof ParameterReplaceableSubstitutionModel)) {
throw new RuntimeException("The substitution model is not parameter replaceable!");
}
XMLObject cxo = xo.getChild(BRANCH_SPECIFIC_PARAMETER);
// XMLObject dxo = xo.getChild(SINGLE_RATE);
BranchSpecificSubstitutionModelProvider substitutionModelProvider;
ArbitrarySubstitutionParameterBranchModel branchParameterModel;
// assert (dxo.getChildCount() == cxo.getChildCount());
List<Parameter> oldParameters = new ArrayList<Parameter>();
List<BranchParameter> parameterList = new ArrayList<BranchParameter>();
for (int i = 0; i < cxo.getChildCount(); i++) {
// Parameter rootParameter = (Parameter) dxo.getChild(i);
// ArbitraryBranchRates branchRateModel = (ArbitraryBranchRates) cxo.getChild(i);
// BranchParameter branchParameter = new BranchParameter("branchSpecific.substitution.parameter",
// tree,
// branchRateModel,
// rootParameter);
// branchParameter.setId("branchSpecific." + rootParameter.getId());
// parameterList.add(branchParameter);
// xo.setNativeObject(branchParameter);
BranchParameter branchParameter = (BranchParameter) cxo.getChild(i);
parameterList.add(branchParameter);
oldParameters.add(branchParameter.getRootParameter());
}
List<SubstitutionModel> substitutionModelList = new ArrayList<SubstitutionModel>();
ParameterReplaceableSubstitutionModel rootSubstitutionModel = (ParameterReplaceableSubstitutionModel) substitutionModel;
// List<Parameter> oldParameters = parseParameters(dxo);
// List<Parameter> branchParameters = parseParameters(cxo);
final int parameterCount = parameterList.size();
int v = 0;
for (int nodeNum = 0; nodeNum < tree.getNodeCount(); ++nodeNum) {
NodeRef node = tree.getNode(nodeNum);
ParameterReplaceableSubstitutionModel branchSubstitutionModel = (ParameterReplaceableSubstitutionModel) substitutionModel;
List<Parameter> newParameters = new ArrayList<Parameter>();
if (tree.isRoot(node)) {
for (int i = 0; i < parameterCount; i++) {
BranchParameter branchParameter = parameterList.get(i);
newParameters.add(branchParameter.getRootParameter());
}
} else {
for (int i = 0; i < parameterCount; i++) {
BranchParameter branchParameter = parameterList.get(i);
newParameters.add(branchParameter.getParameter(v));
}
v++;
}
branchSubstitutionModel = branchSubstitutionModel.factory(oldParameters, newParameters);
substitutionModelList.add(branchSubstitutionModel);
}
substitutionModelProvider = new BranchSpecificSubstitutionModelProvider.Default(substitutionModelList, tree);
branchParameterModel = new ArbitrarySubstitutionParameterBranchModel(ARBITRARY_SUBSTITUTION_PARAMETER_BRANCH_MODEL, substitutionModelProvider, parameterList, tree);
return branchParameterModel;
}
use of dr.evomodel.substmodel.SubstitutionModel in project beast-mcmc by beast-dev.
the class GammaSiteModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
String msg = "";
SubstitutionModel substitutionModel = null;
double muWeight = 1.0;
Parameter muParam = null;
if (xo.hasChildNamed(SUBSTITUTION_RATE)) {
muParam = (Parameter) xo.getElementFirstChild(SUBSTITUTION_RATE);
msg += "\n with initial substitution rate = " + muParam.getParameterValue(0);
} else if (xo.hasChildNamed(MUTATION_RATE)) {
muParam = (Parameter) xo.getElementFirstChild(MUTATION_RATE);
msg += "\n with initial substitution rate = " + muParam.getParameterValue(0);
} else if (xo.hasChildNamed(RELATIVE_RATE)) {
XMLObject cxo = xo.getChild(RELATIVE_RATE);
muParam = (Parameter) cxo.getChild(Parameter.class);
msg += "\n with initial relative rate = " + muParam.getParameterValue(0);
if (cxo.hasAttribute(WEIGHT)) {
muWeight = cxo.getDoubleAttribute(WEIGHT);
msg += " with weight: " + muWeight;
}
}
Parameter shapeParam = null;
int catCount = 4;
if (xo.hasChildNamed(GAMMA_SHAPE)) {
XMLObject cxo = xo.getChild(GAMMA_SHAPE);
catCount = cxo.getIntegerAttribute(GAMMA_CATEGORIES);
shapeParam = (Parameter) cxo.getChild(Parameter.class);
msg += "\n " + catCount + " category discrete gamma with initial shape = " + shapeParam.getParameterValue(0);
}
Parameter invarParam = null;
if (xo.hasChildNamed(PROPORTION_INVARIANT)) {
invarParam = (Parameter) xo.getElementFirstChild(PROPORTION_INVARIANT);
msg += "\n initial proportion of invariant sites = " + invarParam.getParameterValue(0);
}
if (msg.length() > 0) {
Logger.getLogger("dr.evomodel").info("\nCreating site rate model: " + msg);
} else {
Logger.getLogger("dr.evomodel").info("\nCreating site rate model.");
}
GammaSiteRateModel siteRateModel = new GammaSiteRateModel(SITE_MODEL, muParam, muWeight, shapeParam, catCount, invarParam);
if (xo.hasChildNamed(SUBSTITUTION_MODEL)) {
// System.err.println("Doing the substitution model stuff");
// set this to pass it along to the OldTreeLikelihoodParser...
substitutionModel = (SubstitutionModel) xo.getElementFirstChild(SUBSTITUTION_MODEL);
siteRateModel.setSubstitutionModel(substitutionModel);
}
return siteRateModel;
}
use of dr.evomodel.substmodel.SubstitutionModel in project beast-mcmc by beast-dev.
the class SubstitutionModelDelegate method updateSubstitutionModels.
// END: getStateFrequencies
@Override
public void updateSubstitutionModels(Beagle beagle, boolean flipBuffers) {
for (int i = 0; i < eigenCount; i++) {
if (flipBuffers) {
eigenBufferHelper.flipOffset(i);
}
SubstitutionModel substitutionModel = substitutionModelList.get(i);
EigenDecomposition ed = substitutionModel.getEigenDecomposition();
beagle.setEigenDecomposition(eigenBufferHelper.getOffsetIndex(i), ed.getEigenVectors(), ed.getInverseEigenVectors(), ed.getEigenValues());
}
}
use of dr.evomodel.substmodel.SubstitutionModel in project beast-mcmc by beast-dev.
the class EpochBranchModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Logger.getLogger("dr.evomodel").info("\nUsing multi-epoch branch model.");
MutableTreeModel treeModel = (MutableTreeModel) xo.getChild(MutableTreeModel.class);
SubstitutionModel ancestralSubstitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
List<Epoch> epochs = new ArrayList<Epoch>();
for (int i = 0; i < xo.getChildCount(); i++) {
if (xo.getChild(i) instanceof XMLObject) {
XMLObject xoc = (XMLObject) xo.getChild(i);
if (xoc.getName().equals(EPOCH)) {
Parameter tt = null;
if (xoc.hasAttribute(TRANSITION_TIME)) {
double t = xoc.getAttribute(TRANSITION_TIME, 0.0);
tt = new Parameter.Default(1, t);
}
SubstitutionModel s = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
if (xoc.hasChildNamed(TRANSITION_TIME)) {
if (tt != null) {
throw new XMLParseException("An epoch cannot have a transitionTime attribute and a parameter");
}
tt = (Parameter) xoc.getElementFirstChild(TRANSITION_TIME);
}
epochs.add(new Epoch(s, tt));
}
}
}
Collections.sort(epochs);
List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
CompoundParameter transitionTimes = new CompoundParameter("epochTimes");
for (Epoch epoch : epochs) {
substitutionModels.add(epoch.substitutionModel);
transitionTimes.addParameter(epoch.timeParameter);
}
substitutionModels.add(ancestralSubstitutionModel);
return new EpochBranchModel(treeModel, substitutionModels, transitionTimes);
}
Aggregations