/* * NPAntigenicLikelihood.java * * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ package dr.evomodel.antigenic; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.logging.Logger; import dr.evolution.tree.NodeRef; import dr.evolution.tree.TreeUtils; import dr.evomodel.tree.TreeModel; import dr.evomodelxml.treelikelihood.TreeTraitParserUtilities; import dr.inference.model.AbstractModelLikelihood; import dr.inference.model.CompoundParameter; import dr.inference.model.Model; import dr.inference.model.Parameter; import dr.inference.model.Variable; import dr.math.GammaFunction; import dr.xml.AbstractXMLObjectParser; import dr.xml.ElementRule; import dr.xml.StringAttributeRule; import dr.xml.XMLObject; import dr.xml.XMLObjectParser; import dr.xml.XMLParseException; import dr.xml.XMLSyntaxRule; /** * @author Gabriela Cybis * @author Marc Suchard */ public class NPAntigenicLikelihood extends AbstractModelLikelihood { public static final String NP_ANTIGENIC_LIKELIHOOD = "NPAntigenicLikelihood"; public NPAntigenicLikelihood (TreeModel treeModel,CompoundParameter traitParameter , Parameter assignments, Parameter links, Parameter chi, Parameter clusterPrec, Parameter priorMean, Parameter priorPrec, Parameter transformFactor,Parameter means1, Parameter means2, Parameter locationDrift, Parameter offsets, Boolean hasDrift){ super(NP_ANTIGENIC_LIKELIHOOD); this.assignments = assignments; this.links = links; this.clusterPrec = clusterPrec; this.priorPrec = priorPrec; this.priorMean = priorMean; this.treeModel= treeModel; this.traitParameter= traitParameter; this.transformFactor=transformFactor; this.means1=means1; this.means2=means2; this.alpha= chi; this.locationDrift=locationDrift; this.offsets=offsets; // this.hasDrift=hasDrift; this.hasDrift=false; addVariable(traitParameter); addVariable(assignments); addVariable(links); addModel(treeModel); addVariable(chi); addVariable(transformFactor); addVariable(alpha); // addVariable(locationDrift); addVariable(offsets); numdata = traitParameter.getParameterCount(); this.allTips= TreeUtils.getExternalNodes(treeModel,treeModel.getRoot()); setDepMatrix(); for (int i=0; i childList = new ArrayList(); recursion(treeModel.getRoot(),childList); logCorrectMatrix(transformFactor.getParameterValue(0)); logDepMatrix = new double[numdata][numdata]; for(int i=0;i leftChildTipList = new ArrayList(); List rightChildTipList = new ArrayList(); if(!treeModel.isExternal(node)){ recursion(treeModel.getChild(node, 0),leftChildTipList); recursion(treeModel.getChild(node, 1),rightChildTipList); double lBranch = treeModel.getBranchLength(treeModel.getChild(node, 0)); double rBranch = treeModel.getBranchLength(treeModel.getChild(node, 1)); Set notLeftChildList = new HashSet(); notLeftChildList.addAll(allTips); for (NodeRef i :leftChildTipList){ notLeftChildList.remove(i); } Set notRightChildList = new HashSet(); notRightChildList.addAll(allTips); for (NodeRef i :rightChildTipList){ notRightChildList.remove(i); } for (NodeRef lChild : leftChildTipList){ for (NodeRef Child : notLeftChildList){ depMatrix[Child.getNumber()][lChild.getNumber()] += lBranch; depMatrix[lChild.getNumber()][Child.getNumber()] += lBranch; } } for (NodeRef rChild : rightChildTipList){ for (NodeRef Child : notRightChildList){ depMatrix[Child.getNumber()][rChild.getNumber()] += rBranch; depMatrix[rChild.getNumber()][Child.getNumber()] += rBranch; } } childList.addAll(leftChildTipList); childList.addAll(rightChildTipList); } else{ childList.add(node); } } void logCorrectMatrix(double p){ for (int i=0; i leafNames = new HashSet(); leafNames.add(treeModel.getTaxonId(iTip)); leafNames.add(treeModel.getTaxonId(jTip)); return TreeUtils.getCommonAncestorNode(treeModel, leafNames); } public void printInformtion(double[][] Mat) { StringBuffer sb = new StringBuffer("matrix \n"); for(int i=0;i allTips; CompoundParameter traitParameter; Parameter alpha; Parameter clusterPrec ; Parameter priorPrec ; Parameter priorMean ; Parameter assignments; Parameter links; Parameter means2; Parameter means1; Parameter locationDrift; Parameter offsets; boolean hasDrift; private boolean depMatrixKnown= false; private boolean[] dataMatrixKnown; private boolean logLikelihoodKnown=false; private double logLikelihood =0.0; private boolean[] logLikelihoodsVectorKnown; boolean proposedChangeDepMatrix=false; boolean proposedChangeDataMatrix=false; TreeModel treeModel; String traitName; //double[][] data; double[][] depMatrix; double[][] logDepMatrix; double[] logLikelihoodsVector; double[] storedLogLikelihoodsVector; int numdata; Parameter transformFactor; double k0; double v0; double[][] T0Inv; double[] m; double logDetT0; public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { public final static String CLUSTER_PREC = "clusterPrec"; public final static String PRIOR_PREC = "priorPrec"; public final static String PRIOR_MEAN = "priorMean"; public final static String ASSIGNMENTS = "assignments"; public final static String LINKS = "links"; public final static String MEANS_1 = "clusterMeans1"; public final static String MEANS_2 = "clusterMeans2"; public final static String TRANSFORM_FACTOR = "transformFactor"; public final static String CHI = "chi"; public final static String OFFSETS = "offsets"; public final static String LOCATION_DRIFT = "locationDrift"; boolean integrate = false; public String getParserName() { return NP_ANTIGENIC_LIKELIHOOD; } public Object parseXMLObject(XMLObject xo) throws XMLParseException { TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); //String traitName = (String) xo.getAttribute(TRAIT_NAME); XMLObject cxo = xo.getChild(CLUSTER_PREC); Parameter clusterPrec = (Parameter) cxo.getChild(Parameter.class); cxo = xo.getChild(PRIOR_PREC); Parameter priorPrec = (Parameter) cxo.getChild(Parameter.class); cxo = xo.getChild(PRIOR_MEAN); Parameter priorMean = (Parameter) cxo.getChild(Parameter.class); cxo = xo.getChild(ASSIGNMENTS); Parameter assignments = (Parameter) cxo.getChild(Parameter.class); cxo = xo.getChild(LINKS); Parameter links = (Parameter) cxo.getChild(Parameter.class); cxo = xo.getChild(MEANS_2); Parameter means2 = (Parameter) cxo.getChild(Parameter.class); cxo = xo.getChild(MEANS_1); Parameter means1 = (Parameter) cxo.getChild(Parameter.class); cxo = xo.getChild(CHI); Parameter chi = (Parameter) cxo.getChild(Parameter.class); cxo = xo.getChild(TRANSFORM_FACTOR); Parameter transformFactor = (Parameter) cxo.getChild(Parameter.class); cxo=xo.getChild(LOCATION_DRIFT) ; Parameter locationDrift= (Parameter) cxo.getChild(Parameter.class); cxo=xo.getChild(OFFSETS); Parameter offsets =(Parameter) cxo.getChild(Parameter.class); boolean hasDrift = false; if (offsets.getDimension()>1){ hasDrift=true; } TreeTraitParserUtilities utilities = new TreeTraitParserUtilities(); String traitName = TreeTraitParserUtilities.DEFAULT_TRAIT_NAME; TreeTraitParserUtilities.TraitsAndMissingIndices returnValue = utilities.parseTraitsFromTaxonAttributes(xo, traitName, treeModel, integrate); // traitName = returnValue.traitName; CompoundParameter traitParameter = returnValue.traitParameter; return new NPAntigenicLikelihood(treeModel,traitParameter, assignments, links, chi,clusterPrec, priorMean,priorPrec, transformFactor, means1,means2,locationDrift,offsets,hasDrift); } //************************************************************************ // AbstractXMLObjectParser implementation //************************************************************************ public String getParserDescription() { return "conditional likelihood ddCRP"; } public Class getReturnType() { return NPAntigenicLikelihood.class; } public XMLSyntaxRule[] getSyntaxRules() { return rules; } private final XMLSyntaxRule[] rules = { new StringAttributeRule(TreeTraitParserUtilities.TRAIT_NAME, "The name of the trait for which a likelihood should be calculated"), new ElementRule(TreeTraitParserUtilities.TRAIT_PARAMETER, new XMLSyntaxRule[]{ new ElementRule(Parameter.class) }), new ElementRule(PRIOR_PREC, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), new ElementRule(CLUSTER_PREC, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), new ElementRule(PRIOR_MEAN, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), new ElementRule(ASSIGNMENTS, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), new ElementRule(LINKS, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), new ElementRule(TRANSFORM_FACTOR, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), new ElementRule(MEANS_1, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), new ElementRule(MEANS_2, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), new ElementRule(CHI, new XMLSyntaxRule[] { new ElementRule(Parameter.class), }), new ElementRule(OFFSETS, Parameter.class), new ElementRule(LOCATION_DRIFT, Parameter.class), new ElementRule(TreeModel.class), }; }; }