Debian Codegraph @

src/dr/evomodel/operators/LinkageGroupSwap.java

raw symbols SCIP
1 /*
2 * LinkageGroupSwap.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.operators;
27
28 import dr.evolution.alignment.Alignment;
29 import dr.evolution.util.Taxon;
30 import dr.evomodel.tree.HiddenLinkageModel;
31 import dr.inference.operators.SimpleMCMCOperator;
32 import dr.math.MathUtils;
33 import dr.xml.*;
34
35 import java.util.ArrayList;
36 import java.util.HashSet;
37
38 /**
39 * @author Aaron Darling
40 */
41 public class LinkageGroupSwap extends SimpleMCMCOperator {
42
43 HiddenLinkageModel hlm;
44 int groupCount;
45 int columnCount;
46
47 public LinkageGroupSwap(HiddenLinkageModel hlm, double weight){
48 this.hlm = hlm;
49 groupCount = hlm.getLinkageGroupCount();
50 columnCount = hlm.getData().getAlignment().getSiteCount();
51 setWeight(weight);
52 }
53
54 @Override
55 public double doOperation() {
56 if(MathUtils.nextBoolean()){
57 // swap all taxa in one group to a new group
58 int A = MathUtils.nextInt(groupCount);
59 int B = MathUtils.nextInt(groupCount);
60 HashSet<Taxon> aTaxa = new HashSet<Taxon>(hlm.getGroup(A));
61 HashSet<Taxon> bTaxa = new HashSet<Taxon>(hlm.getGroup(B));
62 for(Taxon taxon : aTaxa){
63 hlm.moveReadGroup(taxon, A, B);
64 }
65 for(Taxon taxon : bTaxa){
66 hlm.moveReadGroup(taxon, B, A);
67 }
68 }else{
69 // pick two linkage groups uniformly A, B
70 // pick an alignment column uniformly X
71 // move all reads in group A that are defined in column X to group B, and
72 // vice versa from B to A.
73 // pick a read uniformly at random, add it to a linkage group uniformly at random
74 ArrayList<Taxon> aTaxa = new ArrayList<Taxon>();
75 ArrayList<Taxon> bTaxa = new ArrayList<Taxon>();
76 int A=0, B=0;
77
78 // iterate until we actually find something to move
79 // this could be done more efficiently, e.g. by limiting
80 // choice of X and Y to groups that have something in col X.
81 while(aTaxa.size()==0&&bTaxa.size()==0)
82 {
83 int X = MathUtils.nextInt(columnCount);
84
85 A = MathUtils.nextInt(groupCount);
86 B = MathUtils.nextInt(groupCount);
87 if(A==B)
88 continue; // nothing to do.
89
90 // find all reads intersecting column X
91 Alignment aln = hlm.getData().getAlignment();
92 for(int i=0; i<aln.getTaxonCount(); i++){
93 int state = aln.getPatternState(i, X);
94 if(state==hlm.getDataType().getGapState() || state==hlm.getDataType().getUnknownState())
95 continue; // seq undefined in this column
96 if(hlm.getGroup(A).contains(aln.getTaxon(i))){
97 aTaxa.add(aln.getTaxon(i));
98 }
99 if(hlm.getGroup(B).contains(aln.getTaxon(i))){
100 bTaxa.add(aln.getTaxon(i));
101 }
102 }
103 }
104
105 // move taxa from A to B and from B to A
106 for(Taxon taxon : aTaxa){
107 hlm.moveReadGroup(taxon, A, B);
108 }
109 for(Taxon taxon : bTaxa){
110 hlm.moveReadGroup(taxon, B, A);
111 }
112 }
113 return 0;
114 }
115
116 public String getOperatorName() {
117 return "linkageGroupSwap";
118 }
119
120 public String getPerformanceSuggestion() {
121 return "Ask Aaron Darling to write a better operator";
122 }
123
124 public static XMLObjectParser LINKAGE_GROUP_SWAP_PARSER = new AbstractXMLObjectParser() {
125
126 public String getParserName() {
127 return "linkageGroupSwap";
128 }
129
130 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
131
132 HiddenLinkageModel hlm = (HiddenLinkageModel)xo.getChild(HiddenLinkageModel.class);
133 double weight = xo.getDoubleAttribute("weight");
134
135 return new LinkageGroupSwap(hlm, weight);
136 }
137
138 //************************************************************************
139 // AbstractXMLObjectParser implementation
140 //************************************************************************
141
142 public String getParserDescription() {
143 return "This element represents an operator that swaps taxa among two linkage groups. ";
144 }
145
146 public Class getReturnType() {
147 return LinkageGroupSwap.class;
148 }
149
150 public XMLSyntaxRule[] getSyntaxRules() {
151 return rules;
152 }
153
154 private final XMLSyntaxRule[] rules = {
155 AttributeRule.newDoubleRule("weight"),
156 new ElementRule(HiddenLinkageModel.class)
157 };
158
159 };
160 }