| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
ArrayList<Taxon> aTaxa = new ArrayList<Taxon>(); |
| 75 |
ArrayList<Taxon> bTaxa = new ArrayList<Taxon>(); |
| 76 |
int A=0, B=0; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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; |
| 89 |
|
| 90 |
|
| 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; |
| 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 |
|
| 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 |
|
| 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 |
} |