diff --git a/src/dr/app/beast/release_parsers.properties b/src/dr/app/beast/release_parsers.properties index 2b57dd1c1b..e68fdf2578 100644 --- a/src/dr/app/beast/release_parsers.properties +++ b/src/dr/app/beast/release_parsers.properties @@ -35,7 +35,7 @@ dr.evoxml.PatternSubSetParser dr.evoxml.AscertainedSitePatternsParser dr.evoxml.ConvertAlignmentParser dr.evoxml.MergePatternsParser -dr.evoxml.MaskedPatternsParser +dr.evoxml.MaskedAlignmentParser dr.evoxml.AttributePatternsParser dr.evoxml.RandomTaxaSampleParser dr.evoxml.ConstantPatternsParser diff --git a/src/dr/evolution/alignment/MaskedAlignment.java b/src/dr/evolution/alignment/MaskedAlignment.java new file mode 100644 index 0000000000..9addfde452 --- /dev/null +++ b/src/dr/evolution/alignment/MaskedAlignment.java @@ -0,0 +1,47 @@ +/* + * MaskedAlignment.java + * + * Copyright © 2002-2024 the BEAST Development Team + * http://beast.community/about + * + * 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.evolution.alignment; + +public class MaskedAlignment extends WrappedAlignment { + public MaskedAlignment(Alignment alignment, boolean[] mask) { + super(alignment); + + this.mask = mask; + } + + @Override + public int getState(int taxonIndex, int siteIndex) { + if (mask[siteIndex]) { + return alignment.getState(taxonIndex, siteIndex); + } else { + return alignment.getDataType().getUnknownState(); + } + } + + private final boolean[] mask; +} diff --git a/src/dr/evomodel/treedatalikelihood/BeagleDataLikelihoodInterface.java b/src/dr/evomodel/treedatalikelihood/BeagleDataLikelihoodInterface.java index be914a85b1..b6289babb9 100644 --- a/src/dr/evomodel/treedatalikelihood/BeagleDataLikelihoodInterface.java +++ b/src/dr/evomodel/treedatalikelihood/BeagleDataLikelihoodInterface.java @@ -39,6 +39,7 @@ * @author Andrew Rambaut * @author Marc Suchard */ +@Deprecated // seems not to be used public interface BeagleDataLikelihoodInterface extends DataLikelihoodDelegate { } diff --git a/src/dr/evoxml/MaskedPatternsParser.java b/src/dr/evoxml/MaskedAlignmentParser.java similarity index 62% rename from src/dr/evoxml/MaskedPatternsParser.java rename to src/dr/evoxml/MaskedAlignmentParser.java index 76324d184d..8aea44e51f 100644 --- a/src/dr/evoxml/MaskedPatternsParser.java +++ b/src/dr/evoxml/MaskedAlignmentParser.java @@ -27,45 +27,47 @@ package dr.evoxml; +import dr.evolution.alignment.Alignment; +import dr.evolution.alignment.MaskedAlignment; import dr.evolution.alignment.PatternList; -import dr.evolution.alignment.Patterns; import dr.evolution.alignment.SiteList; -import dr.evolution.alignment.SitePatterns; import dr.xml.*; -import java.util.logging.Logger; - /** * @author Andrew Rambaut * */ -public class MaskedPatternsParser extends AbstractXMLObjectParser { +public class MaskedAlignmentParser extends AbstractXMLObjectParser { - public static final String MASKED_PATTERNS = "maskedPatterns"; + public static final String MASKED_ALIGNMENT = "maskedAlignment"; public static final String MASK = "mask"; public static final String NEGATIVE = "negative"; public static final String INVERSE = "inverse"; - public String getParserName() { return MASKED_PATTERNS; } + public String getParserName() { return MASKED_ALIGNMENT; } /** - * Parses a patterns element and returns a patterns object. + * Parses an alignment element and returns a masked alignment object. */ public Object parseXMLObject(XMLObject xo) throws XMLParseException { - SiteList siteList = (SiteList)xo.getChild(SiteList.class); + Alignment alignment = (Alignment)xo.getChild(Alignment.class); boolean inverseMask = xo.getBooleanAttribute(INVERSE, false) || xo.getBooleanAttribute(NEGATIVE, false); String maskString = (String)xo.getElementFirstChild(MASK); - boolean[] mask = new boolean[siteList.getSiteCount()]; + boolean[] mask = new boolean[alignment.getSiteCount()]; int k = 0; + int onCount = 0; for (char c : maskString.toCharArray()) { if (Character.isDigit(c)) { if (k >= mask.length) { break; } mask[k] = (c == '0' ? inverseMask : !inverseMask); + if (mask[k]) { + onCount += 1; + } k++; } } @@ -74,23 +76,11 @@ public Object parseXMLObject(XMLObject xo) throws XMLParseException { throw new XMLParseException("The mask needs to be the same length as the alignment (spaces are ignored)"); } - throw new UnsupportedOperationException("This has not been implemented"); - - // @todo - work out how to do this. Create a MaskedSitePatterns which can then be put into a SitePatterns - // for compression etc. Or MaskedAlignment may make more sense here. -// SitePatterns patterns = new MaskedSitePatterns(siteList, mask); -// -// if (patterns == null) { -// throw new XMLParseException("The mask needs include at least one pattern"); -// } -// -// if (xo.hasAttribute(XMLParser.ID)) { -// final Logger logger = Logger.getLogger("dr.evoxml"); -// logger.info("Site patterns '" + xo.getId() + "' created by masking alignment with id '" + siteList.getId() + "'"); -// logger.info(" pattern count = " + patterns.getPatternCount()); -// } -// -// return patterns; + if (onCount == 0) { + throw new XMLParseException("The mask needs to have at least one site unmasked"); + } + + return new MaskedAlignment(alignment, mask); } public XMLSyntaxRule[] getSyntaxRules() { return rules; } @@ -103,9 +93,9 @@ public Object parseXMLObject(XMLObject xo) throws XMLParseException { }; public String getParserDescription() { - return "A weighted list of the unique site patterns (unique columns) in an alignment."; + return "Applies a mask to an alignment so that masked sites are ambiguous."; } - public Class getReturnType() { return PatternList.class; } + public Class getReturnType() { return MaskedAlignment.class; } } diff --git a/src/dr/inferencexml/operators/shrinkage/BayesianBridgeShrinkageOperatorParser.java b/src/dr/inferencexml/operators/shrinkage/BayesianBridgeShrinkageOperatorParser.java index 52795a141f..4430855b8d 100644 --- a/src/dr/inferencexml/operators/shrinkage/BayesianBridgeShrinkageOperatorParser.java +++ b/src/dr/inferencexml/operators/shrinkage/BayesianBridgeShrinkageOperatorParser.java @@ -27,7 +27,6 @@ package dr.inferencexml.operators.shrinkage; -import dr.evomodel.branchratemodel.AutoCorrelatedBranchRatesDistribution; import dr.inference.distribution.DistributionLikelihood; import dr.inference.distribution.shrinkage.BayesianBridgeStatisticsProvider; import dr.inference.model.Parameter; @@ -35,12 +34,11 @@ import dr.math.distributions.GammaDistribution; import dr.xml.*; -import static dr.evoxml.MaskedPatternsParser.MASK; -import static dr.inference.operators.MCMCOperator.WEIGHT; - public class BayesianBridgeShrinkageOperatorParser extends AbstractXMLObjectParser { public final static String BAYESIAN_BRIDGE_PARSER = "bayesianBridgeGibbsOperator"; + public final static String MASK = "mask"; + public final static String WEIGHT = "weight"; @Override public Object parseXMLObject(XMLObject xo) throws XMLParseException { diff --git a/src/dr/inferencexml/operators/shrinkage/DimensionMismatchedBayesianBridgeShrinkageOperatorParser.java b/src/dr/inferencexml/operators/shrinkage/DimensionMismatchedBayesianBridgeShrinkageOperatorParser.java index 640a0d7821..18c671e0ee 100644 --- a/src/dr/inferencexml/operators/shrinkage/DimensionMismatchedBayesianBridgeShrinkageOperatorParser.java +++ b/src/dr/inferencexml/operators/shrinkage/DimensionMismatchedBayesianBridgeShrinkageOperatorParser.java @@ -35,12 +35,11 @@ import dr.math.distributions.GammaDistribution; import dr.xml.*; -import static dr.evoxml.MaskedPatternsParser.MASK; -import static dr.inference.operators.MCMCOperator.WEIGHT; - public class DimensionMismatchedBayesianBridgeShrinkageOperatorParser extends AbstractXMLObjectParser { public final static String BAYESIAN_BRIDGE_PARSER = "dimensionMismatchedBayesianBridgeGibbsOperator"; + public final static String MASK = "mask"; + public final static String WEIGHT = "weight"; @Override public Object parseXMLObject(XMLObject xo) throws XMLParseException {