Debian Codegraph @

src/dr/evomodel/arg/ARGDistinctTreeCountStatistic.java

raw symbols SCIP
1 /*
2 * ARGDistinctTreeCountStatistic.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.arg;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30
31 import dr.inference.model.Statistic;
32 import dr.xml.AbstractXMLObjectParser;
33 import dr.xml.ElementRule;
34 import dr.xml.XMLObject;
35 import dr.xml.XMLObjectParser;
36 import dr.xml.XMLParseException;
37 import dr.xml.XMLSyntaxRule;
38
39
40 public class ARGDistinctTreeCountStatistic extends Statistic.Abstract{
41
42 public static final String ARG_TREE_COUNT = "argTreeCount";
43 public static final String PRINT_TREES = "printTrees";
44
45 private ARGModel arg;
46 private int numberOfPartitions;
47
48 public ARGDistinctTreeCountStatistic(ARGModel arg){
49 this.arg = arg;
50 numberOfPartitions = arg.getNumberOfPartitions();
51 }
52
53 public int getDimension() {
54
55 return 1;
56 }
57
58
59 public double getStatisticValue(int dim){
60 ArrayList<String> listOfTrees = new ArrayList<String>(numberOfPartitions/10);
61
62 for(int i = 0; i < numberOfPartitions; i++){
63 ARGTree tree = new ARGTree(arg,i);
64
65 String newick = tree.getUniqueNewick();
66
67
68
69 if(!listOfTrees.contains(newick)){
70 listOfTrees.add(newick);
71
72 }
73 }
74
75
76
77
78 return listOfTrees.size();
79 }
80
81 private class DistinctTreeCount implements Comparable<DistinctTreeCount>{
82 private final String tree;
83 private int count;
84
85 public DistinctTreeCount(String tree, int count){
86 this.tree = tree;
87 this.count = count;
88 }
89
90 public void increaseCount(){
91 count++;
92 }
93
94 public int compareTo(DistinctTreeCount o) {
95 if(this.tree.equals(o.tree)){
96 return 0;
97 }else if(this.count < o.count){
98 return 1;
99 }
100 return -1;
101 }
102
103 public boolean equals(Object o){
104 try{
105 DistinctTreeCount dtc = (DistinctTreeCount)o;
106
107 if(dtc.compareTo(this) == 0){
108 return true;
109 }
110 return false;
111 }catch(ClassCastException e){
112 return false;
113 }
114
115 }
116
117 public String toString(){
118 return tree + " " + count;
119 }
120
121
122
123
124 };
125
126
127
128 public String getFullOutput(){
129 ArrayList<String> listOfTrees = new ArrayList<String>(numberOfPartitions);
130 ArrayList<Integer> numbers = new ArrayList<Integer>(numberOfPartitions);
131
132 for(int i = 0; i < numberOfPartitions; i++){
133 ARGTree tree = new ARGTree(arg,i);
134
135 String newick = tree.getUniqueNewick();
136 if(!listOfTrees.contains(newick)){
137 listOfTrees.add(newick);
138 numbers.add(1);
139 }else{
140 int index = listOfTrees.indexOf(newick);
141 numbers.set(index, numbers.get(index) + 1);
142 }
143 }
144
145 ArrayList<DistinctTreeCount> trees = new ArrayList<DistinctTreeCount>(numberOfPartitions);
146
147 for(int i = 0; i < listOfTrees.size(); i++){
148 trees.add(new DistinctTreeCount(listOfTrees.get(i),numbers.get(i)));
149 }
150
151 Collections.sort(trees);
152
153 return trees.toString();
154 }
155
156 public static XMLObjectParser PARSER = new AbstractXMLObjectParser(){
157
158 public String getParserDescription() {
159
160 return "Provides number of distinct loci trees";
161 }
162
163 public Class getReturnType() {
164 return ARGDistinctTreeCountStatistic.class;
165 }
166
167 public XMLSyntaxRule[] getSyntaxRules() {
168 return new XMLSyntaxRule[]{
169 new ElementRule(ARGModel.class),
170 };
171 }
172
173 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
174 ARGModel arg = (ARGModel) xo.getChild(ARGModel.class);
175
176 return new ARGDistinctTreeCountStatistic(arg);
177 }
178
179 public String getParserName() {
180 return ARG_TREE_COUNT;
181 }
182
183 };
184
185
186
187 }