| 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.oldevomodel.sitemodel; |
| 27 |
|
| 28 |
import dr.inference.model.BooleanStatistic; |
| 29 |
import dr.inference.model.Statistic; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class CategoryParameterStatistic extends Statistic.Abstract implements BooleanStatistic { |
| 39 |
|
| 40 |
private static String MINIMUM_NUMBER = "minimumNumber"; |
| 41 |
|
| 42 |
public CategoryParameterStatistic(String name, |
| 43 |
SampleStateAndCategoryModel siteModel, |
| 44 |
int minimumNumber) { |
| 45 |
|
| 46 |
super(name); |
| 47 |
this.minimumNumber = minimumNumber; |
| 48 |
this.siteModel = siteModel; |
| 49 |
this.categoryCount = siteModel.getCategoryCount(); |
| 50 |
} |
| 51 |
|
| 52 |
public int getDimension() { |
| 53 |
return 1; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public double getStatisticValue(int dim) { |
| 60 |
return getBoolean(dim) ? 1.0 : 0.0; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public boolean getBoolean(int dim) { |
| 67 |
for (int i = 0; i < categoryCount; i++) { |
| 68 |
if (siteModel.getSitesInCategoryCount(i) < minimumNumber) |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 76 |
|
| 77 |
public String getParserName() { |
| 78 |
return "categoryParameterStatistic"; |
| 79 |
} |
| 80 |
|
| 81 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 82 |
|
| 83 |
SampleStateAndCategoryModel siteModel = null; |
| 84 |
String name = xo.getAttribute("name", null); |
| 85 |
|
| 86 |
int minimum = xo.getAttribute(MINIMUM_NUMBER, 0); |
| 87 |
|
| 88 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 89 |
if (xo.getChild(i) instanceof SampleStateAndCategoryModel) { |
| 90 |
siteModel = (SampleStateAndCategoryModel) xo.getChild(i); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (siteModel == null) |
| 95 |
throw new XMLParseException(getParserName() + " must contain a SampleStateAndCategoryModel."); |
| 96 |
if (minimum < 1) throw new XMLParseException(getParserName() + " minimum number must be greater than 0."); |
| 97 |
return new CategoryParameterStatistic(name, siteModel, minimum); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public String getParserDescription() { |
| 105 |
return "A statistic that returns true if the minimum number of sites in a category are present"; |
| 106 |
} |
| 107 |
|
| 108 |
public Class getReturnType() { |
| 109 |
return CategoryParameterStatistic.class; |
| 110 |
} |
| 111 |
|
| 112 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 113 |
return rules; |
| 114 |
} |
| 115 |
|
| 116 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 117 |
new StringAttributeRule(NAME, "A name for this statistic for the purposes of logging"), |
| 118 |
AttributeRule.newIntegerRule(MINIMUM_NUMBER), |
| 119 |
new ElementRule(SampleStateAndCategoryModel.class) |
| 120 |
}; |
| 121 |
|
| 122 |
}; |
| 123 |
|
| 124 |
private int minimumNumber; |
| 125 |
private int categoryCount; |
| 126 |
private SampleStateAndCategoryModel siteModel; |
| 127 |
|
| 128 |
} |