/* * LinkageGroupSwap.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.operators; import dr.evolution.alignment.Alignment; import dr.evolution.util.Taxon; import dr.evomodel.tree.HiddenLinkageModel; import dr.inference.operators.SimpleMCMCOperator; import dr.math.MathUtils; import dr.xml.*; import java.util.ArrayList; import java.util.HashSet; /** * @author Aaron Darling */ public class LinkageGroupSwap extends SimpleMCMCOperator { HiddenLinkageModel hlm; int groupCount; int columnCount; public LinkageGroupSwap(HiddenLinkageModel hlm, double weight){ this.hlm = hlm; groupCount = hlm.getLinkageGroupCount(); columnCount = hlm.getData().getAlignment().getSiteCount(); setWeight(weight); } @Override public double doOperation() { if(MathUtils.nextBoolean()){ // swap all taxa in one group to a new group int A = MathUtils.nextInt(groupCount); int B = MathUtils.nextInt(groupCount); HashSet aTaxa = new HashSet(hlm.getGroup(A)); HashSet bTaxa = new HashSet(hlm.getGroup(B)); for(Taxon taxon : aTaxa){ hlm.moveReadGroup(taxon, A, B); } for(Taxon taxon : bTaxa){ hlm.moveReadGroup(taxon, B, A); } }else{ // pick two linkage groups uniformly A, B // pick an alignment column uniformly X // move all reads in group A that are defined in column X to group B, and // vice versa from B to A. // pick a read uniformly at random, add it to a linkage group uniformly at random ArrayList aTaxa = new ArrayList(); ArrayList bTaxa = new ArrayList(); int A=0, B=0; // iterate until we actually find something to move // this could be done more efficiently, e.g. by limiting // choice of X and Y to groups that have something in col X. while(aTaxa.size()==0&&bTaxa.size()==0) { int X = MathUtils.nextInt(columnCount); A = MathUtils.nextInt(groupCount); B = MathUtils.nextInt(groupCount); if(A==B) continue; // nothing to do. // find all reads intersecting column X Alignment aln = hlm.getData().getAlignment(); for(int i=0; i