| 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.math.MathUtils; |
| 30 |
import dr.oldevomodel.sitemodel.CategorySampleModel; |
| 31 |
import dr.inference.model.Parameter; |
| 32 |
import dr.inference.operators.SimpleMCMCOperator; |
| 33 |
import dr.xml.*; |
| 34 |
import org.w3c.dom.Document; |
| 35 |
import org.w3c.dom.Element; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class CategoryOperator extends SimpleMCMCOperator { |
| 44 |
|
| 45 |
public static final String CATEGORY_OPERATOR = "categoryOperator"; |
| 46 |
|
| 47 |
|
| 48 |
public CategoryOperator(CategorySampleModel siteModel, int siteCount, |
| 49 |
Parameter categoryParameter, double weight) { |
| 50 |
this.categoryParameter = categoryParameter; |
| 51 |
setWeight(weight); |
| 52 |
this.siteModel = siteModel; |
| 53 |
this.categoryCount = siteModel.getCategoryCount(); |
| 54 |
this.siteCount = siteCount; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public final double doOperation() { |
| 62 |
|
| 63 |
int randomSite = (int) (MathUtils.nextDouble() * siteCount); |
| 64 |
|
| 65 |
int currentCategory = (int) categoryParameter.getParameterValue(randomSite); |
| 66 |
|
| 67 |
siteModel.subtractSitesInCategoryCount(currentCategory); |
| 68 |
|
| 69 |
int[] temp = new int[categoryCount - 1]; |
| 70 |
|
| 71 |
int count = 0; |
| 72 |
|
| 73 |
for (int i = 0; i < categoryCount; i++) { |
| 74 |
if (i != currentCategory) { |
| 75 |
temp[count] = i; |
| 76 |
count++; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
int newCategory = temp[(int) (MathUtils.nextDouble() * temp.length)]; |
| 81 |
|
| 82 |
categoryParameter.setParameterValue(randomSite, newCategory); |
| 83 |
siteModel.addSitesInCategoryCount(newCategory); |
| 84 |
|
| 85 |
return 0.0; |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public final String getOperatorName() { |
| 92 |
return CATEGORY_OPERATOR; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public Element createOperatorElement(Document d) { |
| 100 |
throw new RuntimeException("Not implemented!"); |
| 101 |
} |
| 102 |
|
| 103 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 104 |
|
| 105 |
public String getParserName() { |
| 106 |
return CATEGORY_OPERATOR; |
| 107 |
} |
| 108 |
|
| 109 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 110 |
|
| 111 |
Parameter catParam = (Parameter) xo.getChild(Parameter.class); |
| 112 |
CategorySampleModel siteModel = (CategorySampleModel) xo.getChild(CategorySampleModel.class); |
| 113 |
Alignment alignment = (Alignment) xo.getChild(Alignment.class); |
| 114 |
|
| 115 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 116 |
|
| 117 |
return new CategoryOperator(siteModel, alignment.getSiteCount(), |
| 118 |
catParam, weight); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public String getParserDescription() { |
| 126 |
return "An operator on categories of sites."; |
| 127 |
} |
| 128 |
|
| 129 |
public Class getReturnType() { |
| 130 |
return CategoryOperator.class; |
| 131 |
} |
| 132 |
|
| 133 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 134 |
return rules; |
| 135 |
} |
| 136 |
|
| 137 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 138 |
AttributeRule.newDoubleRule("weight"), |
| 139 |
new ElementRule(Parameter.class), |
| 140 |
new ElementRule(CategorySampleModel.class), |
| 141 |
new ElementRule(Alignment.class) |
| 142 |
}; |
| 143 |
|
| 144 |
}; |
| 145 |
|
| 146 |
public String toString() { |
| 147 |
return getOperatorName(); |
| 148 |
} |
| 149 |
|
| 150 |
public String getPerformanceSuggestion() { |
| 151 |
return ""; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
private Parameter categoryParameter; |
| 156 |
|
| 157 |
private CategorySampleModel siteModel; |
| 158 |
|
| 159 |
private int categoryCount; |
| 160 |
|
| 161 |
private int siteCount; |
| 162 |
|
| 163 |
} |
| 164 |
|