Debian Codegraph @

src/dr/oldevomodel/sitemodel/CategoryParameterStatistic.java

raw symbols SCIP
1 /*
2 * CategoryParameterStatistic.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.oldevomodel.sitemodel;
27
28 import dr.inference.model.BooleanStatistic;
29 import dr.inference.model.Statistic;
30 import dr.xml.*;
31
32
33 /**
34 * Performs test of whether a category parameter has a given minimum number of sites in each category
35 *
36 * @author Roald Forsberg
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 * @return boolean result of test.
58 */
59 public double getStatisticValue(int dim) {
60 return getBoolean(dim) ? 1.0 : 0.0;
61 }
62
63 /**
64 * @return boolean result of test.
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 // AbstractXMLObjectParser implementation
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 }