Search in sources :

Example 1 with GMRFMultilocusSkyrideLikelihood

use of dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood in project beast-mcmc by beast-dev.

the class GMRFSkyrideBlockUpdateOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    boolean logRecord = xo.getAttribute(KEEP_LOG_RECORD, false);
    Handler gmrfHandler;
    Logger gmrfLogger = Logger.getLogger("dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator");
    gmrfLogger.setUseParentHandlers(false);
    if (logRecord) {
        gmrfLogger.setLevel(Level.FINE);
        try {
            gmrfHandler = new FileHandler("GMRFBlockUpdate.log." + MathUtils.getSeed());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
        gmrfHandler.setLevel(Level.FINE);
        gmrfHandler.setFormatter(new XMLFormatter() {

            public String format(LogRecord record) {
                return "<record>\n \t<message>\n\t" + record.getMessage() + "\n\t</message>\n<record>\n";
            }
        });
        gmrfLogger.addHandler(gmrfHandler);
    }
    CoercionMode mode = CoercionMode.parseMode(xo);
    if (mode == CoercionMode.DEFAULT)
        mode = CoercionMode.COERCION_ON;
    double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
    if (scaleFactor == 1.0) {
        mode = CoercionMode.COERCION_OFF;
    }
    //                throw new XMLParseException("scaleFactor must be greater than 0.0");
    if (scaleFactor < 1.0) {
        throw new XMLParseException("scaleFactor must be greater than or equal to 1.0");
    }
    int maxIterations = xo.getAttribute(MAX_ITERATIONS, 200);
    double stopValue = xo.getAttribute(STOP_VALUE, 0.01);
    if (xo.getAttribute(OLD_SKYRIDE, true) && !(xo.getName().compareTo(GRID_BLOCK_UPDATE_OPERATOR) == 0)) {
        GMRFSkyrideLikelihood gmrfLikelihood = (GMRFSkyrideLikelihood) xo.getChild(GMRFSkyrideLikelihood.class);
        return new GMRFSkyrideBlockUpdateOperator(gmrfLikelihood, weight, mode, scaleFactor, maxIterations, stopValue);
    } else {
        GMRFMultilocusSkyrideLikelihood gmrfMultilocusLikelihood = (GMRFMultilocusSkyrideLikelihood) xo.getChild(GMRFMultilocusSkyrideLikelihood.class);
        return new GMRFMultilocusSkyrideBlockUpdateOperator(gmrfMultilocusLikelihood, weight, mode, scaleFactor, maxIterations, stopValue);
    }
}
Also used : GMRFSkyrideLikelihood(dr.evomodel.coalescent.GMRFSkyrideLikelihood) IOException(java.io.IOException) GMRFSkyrideBlockUpdateOperator(dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator) CoercionMode(dr.inference.operators.CoercionMode) GMRFMultilocusSkyrideLikelihood(dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood) GMRFMultilocusSkyrideBlockUpdateOperator(dr.evomodel.coalescent.operators.GMRFMultilocusSkyrideBlockUpdateOperator)

Example 2 with GMRFMultilocusSkyrideLikelihood

use of dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood in project beast-mcmc by beast-dev.

the class GMRFSkyrideLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    XMLObject cxo = xo.getChild(POPULATION_PARAMETER);
    Parameter popParameter = (Parameter) cxo.getChild(Parameter.class);
    cxo = xo.getChild(PRECISION_PARAMETER);
    Parameter precParameter = (Parameter) cxo.getChild(Parameter.class);
    cxo = xo.getChild(POPULATION_TREE);
    List<Tree> treeList = new ArrayList<Tree>();
    for (int i = 0; i < cxo.getChildCount(); i++) {
        Object testObject = cxo.getChild(i);
        if (testObject instanceof Tree) {
            treeList.add((TreeModel) testObject);
        }
    }
    //        TreeModel treeModel = (TreeModel) cxo.getChild(TreeModel.class);
    cxo = xo.getChild(GROUP_SIZES);
    Parameter groupParameter = null;
    if (cxo != null) {
        groupParameter = (Parameter) cxo.getChild(Parameter.class);
        if (popParameter.getDimension() != groupParameter.getDimension())
            throw new XMLParseException("Population and group size parameters must have the same length");
    }
    Parameter lambda;
    if (xo.getChild(LAMBDA_PARAMETER) != null) {
        cxo = xo.getChild(LAMBDA_PARAMETER);
        lambda = (Parameter) cxo.getChild(Parameter.class);
    } else {
        lambda = new Parameter.Default(LAMBDA_PARAMETER, 1.0);
    }
    Parameter gridPoints = null;
    if (xo.getChild(GRID_POINTS) != null) {
        cxo = xo.getChild(GRID_POINTS);
        gridPoints = (Parameter) cxo.getChild(Parameter.class);
    }
    Parameter numGridPoints = null;
    if (xo.getChild(NUM_GRID_POINTS) != null) {
        cxo = xo.getChild(NUM_GRID_POINTS);
        numGridPoints = (Parameter) cxo.getChild(Parameter.class);
    }
    Parameter cutOff = null;
    if (xo.getChild(CUT_OFF) != null) {
        cxo = xo.getChild(CUT_OFF);
        cutOff = (Parameter) cxo.getChild(Parameter.class);
    }
    Parameter phi = null;
    if (xo.getChild(PHI_PARAMETER) != null) {
        cxo = xo.getChild(PHI_PARAMETER);
        phi = (Parameter) cxo.getChild(Parameter.class);
    }
    List<Parameter> lastObservedIndex = null;
    if (xo.hasChildNamed(LAST_OBSERVED_INDEX)) {
        lastObservedIndex = new ArrayList<Parameter>();
        cxo = xo.getChild(LAST_OBSERVED_INDEX);
        final int numObsInd = cxo.getChildCount();
        for (int i = 0; i < numObsInd; ++i) {
            lastObservedIndex.add((Parameter) cxo.getChild(i));
        }
    }
    Parameter ploidyFactors = null;
    if (xo.getChild(PLOIDY) != null) {
        cxo = xo.getChild(PLOIDY);
        ploidyFactors = (Parameter) cxo.getChild(Parameter.class);
    } else {
        ploidyFactors = new Parameter.Default(PLOIDY, treeList.size());
        for (int i = 0; i < treeList.size(); i++) {
            ploidyFactors.setParameterValue(i, 1.0);
        }
    }
    Parameter betaParameter = null;
    if (xo.hasChildNamed(SINGLE_BETA)) {
        betaParameter = (Parameter) xo.getElementFirstChild(SINGLE_BETA);
    }
    List<Parameter> betaList = null;
    if (xo.getChild(BETA_PARAMETER) != null) {
        betaList = new ArrayList<Parameter>();
        cxo = xo.getChild(BETA_PARAMETER);
        final int numBeta = cxo.getChildCount();
        for (int i = 0; i < numBeta; ++i) {
            betaList.add((Parameter) cxo.getChild(i));
        }
    }
    MatrixParameter dMatrix = null;
    if (xo.getChild(COVARIATE_MATRIX) != null) {
        cxo = xo.getChild(COVARIATE_MATRIX);
        dMatrix = (MatrixParameter) cxo.getChild(MatrixParameter.class);
    }
    boolean timeAwareSmoothing = GMRFSkyrideLikelihood.TIME_AWARE_IS_ON_BY_DEFAULT;
    if (xo.hasAttribute(TIME_AWARE_SMOOTHING)) {
        timeAwareSmoothing = xo.getBooleanAttribute(TIME_AWARE_SMOOTHING);
    }
    if (dMatrix != null) {
        if (dMatrix.getRowDimension() != popParameter.getDimension())
            throw new XMLParseException("Design matrix row dimension must equal the population parameter length.");
        if (dMatrix.getColumnDimension() != betaParameter.getDimension())
            throw new XMLParseException("Design matrix column dimension must equal the regression coefficient length.");
    }
    List<Parameter> covPrecParam = null;
    if (xo.hasChildNamed(COV_PREC_PARAM)) {
        covPrecParam = new ArrayList<Parameter>();
        cxo = xo.getChild(COV_PREC_PARAM);
        final int numCovPrec = cxo.getChildCount();
        for (int i = 0; i < numCovPrec; ++i) {
            covPrecParam.add((Parameter) cxo.getChild(i));
        }
    }
    List<MatrixParameter> covariates = null;
    if (xo.hasChildNamed(COVARIATES)) {
        covariates = new ArrayList<MatrixParameter>();
        cxo = xo.getChild(COVARIATES);
        final int numCov = cxo.getChildCount();
        for (int i = 0; i < numCov; ++i) {
            covariates.add((MatrixParameter) cxo.getChild(i));
        }
    }
    if ((covariates != null && betaList == null) || (covariates == null && betaList != null))
        throw new XMLParseException("Must specify both a set of regression coefficients and a design matrix.");
    if (xo.getAttribute(RANDOMIZE_TREE, false)) {
        for (Tree tree : treeList) {
            if (tree instanceof TreeModel) {
                GMRFSkyrideLikelihood.checkTree((TreeModel) tree);
            } else {
                throw new XMLParseException("Can not randomize a fixed tree");
            }
        }
    }
    boolean rescaleByRootHeight = xo.getAttribute(RESCALE_BY_ROOT_ISSUE, true);
    Logger.getLogger("dr.evomodel").info("The " + SKYLINE_LIKELIHOOD + " has " + (timeAwareSmoothing ? "time aware smoothing" : "uniform smoothing"));
    if (xo.getAttribute(OLD_SKYRIDE, true) && xo.getName().compareTo(SKYGRID_LIKELIHOOD) != 0) {
        return new GMRFSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, lambda, betaParameter, dMatrix, timeAwareSmoothing, rescaleByRootHeight);
    } else {
        if (xo.getChild(GRID_POINTS) != null) {
            return new GMRFMultilocusSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, lambda, betaParameter, dMatrix, timeAwareSmoothing, gridPoints, covariates, ploidyFactors, lastObservedIndex, covPrecParam, betaList);
        } else {
            return new GMRFMultilocusSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, lambda, betaParameter, dMatrix, timeAwareSmoothing, cutOff.getParameterValue(0), (int) numGridPoints.getParameterValue(0), phi, ploidyFactors);
        }
    }
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) ArrayList(java.util.ArrayList) GMRFSkyrideLikelihood(dr.evomodel.coalescent.GMRFSkyrideLikelihood) TreeModel(dr.evomodel.tree.TreeModel) Parameter(dr.inference.model.Parameter) MatrixParameter(dr.inference.model.MatrixParameter) Tree(dr.evolution.tree.Tree) GMRFMultilocusSkyrideLikelihood(dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood)

Aggregations

GMRFMultilocusSkyrideLikelihood (dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood)2 GMRFSkyrideLikelihood (dr.evomodel.coalescent.GMRFSkyrideLikelihood)2 Tree (dr.evolution.tree.Tree)1 GMRFMultilocusSkyrideBlockUpdateOperator (dr.evomodel.coalescent.operators.GMRFMultilocusSkyrideBlockUpdateOperator)1 GMRFSkyrideBlockUpdateOperator (dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator)1 TreeModel (dr.evomodel.tree.TreeModel)1 MatrixParameter (dr.inference.model.MatrixParameter)1 Parameter (dr.inference.model.Parameter)1 CoercionMode (dr.inference.operators.CoercionMode)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1