Debian Codegraph @

src/dr/app/beauti/options/TraitData.java

raw symbols SCIP
1 /*
2 * TraitData.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.app.beauti.options;
27
28 import dr.evolution.datatype.ContinuousDataType;
29 import dr.evolution.datatype.DataType;
30 import dr.evolution.datatype.GeneralDataType;
31 import dr.evolution.util.Taxa;
32 import dr.evolution.util.Taxon;
33
34 import java.io.Serializable;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Set;
38 import java.util.TreeSet;
39
40 /**
41 * @author Andrew Rambaut
42 * @author Alexei Drummond
43 * @author Walter Xie
44 */
45 public class TraitData implements Serializable {
46 private static final long serialVersionUID = -9152518508699327745L;
47
48 public enum TraitType {
49 DISCRETE,
50 INTEGER,
51 CONTINUOUS;
52
53 public String toString() {
54 return name().toLowerCase();
55 }
56 }
57
58 private TraitType traitType = TraitType.DISCRETE;
59
60 private final String fileName;
61 private String name;
62
63 protected final BeautiOptions options;
64
65 private List<Predictor> predictorList = new ArrayList<Predictor>();
66
67 public TraitData(BeautiOptions options, String name, String fileName, TraitType traitType) {
68 this.options = options;
69 this.name = name;
70 this.fileName = fileName;
71 this.traitType = traitType;
72 }
73
74 /////////////////////////////////////////////////////////////////////////
75
76 public TraitType getTraitType() {
77 return traitType;
78 }
79
80 public void setTraitType(TraitType traitType) {
81 this.traitType = traitType;
82 }
83
84 // public TraitOptions getTraitOptions() {
85 // return traitOptions;
86 // }
87
88 public int getSiteCount() {
89 return 0;
90 }
91
92 public int getTaxaCount() {
93 return options.taxonList.getTaxonCount();
94 }
95
96 public Taxon getTaxon(int i) {
97 return options.taxonList.getTaxon(i);
98 }
99
100 public boolean hasValue(int i) {
101 if (options.taxonList == null || options.taxonList.getTaxon(i) == null
102 || options.taxonList.getTaxon(i).getAttribute(getName()) == null) return false;
103 return options.taxonList.getTaxon(i).getAttribute(getName()).toString().trim().length() > 0;
104 }
105
106 public DataType
107 getDataType() {
108 switch (traitType) {
109 case DISCRETE:
110 return GeneralDataType.INSTANCE;
111 case INTEGER:
112 return GeneralDataType.INSTANCE;
113 case CONTINUOUS:
114 return ContinuousDataType.INSTANCE;
115 }
116 throw new IllegalArgumentException("Unknown trait type");
117 }
118
119 public String getFileName() {
120 return fileName;
121 }
122
123 public String getName() {
124 return name;
125 }
126
127 public void setName(final String name) {
128 this.name = name;
129 }
130
131 public Set<String> getStatesOfTrait() {
132 return getStatesListOfTrait(options.taxonList, getName());
133 }
134
135 public Set<String> getStatesOfTrait(Taxa taxonList) {
136 return getStatesListOfTrait(taxonList, getName());
137 }
138
139
140 public static Set<String> getStatesListOfTrait(Taxa taxonList, String traitName) {
141 Set<String> states = new TreeSet<String>();
142
143 if (taxonList == null) {
144 throw new IllegalArgumentException("taxon list is null");
145 }
146
147 for (int i = 0; i < taxonList.getTaxonCount(); i++) {
148 Taxon taxon = taxonList.getTaxon(i);
149 String attr = (String) taxon.getAttribute(traitName);
150
151 // ? is used to denote missing data so is not a state...
152 if (attr != null && !attr.equals("?")) {
153 states.add(attr);
154 }
155 }
156
157
158 return states;
159 }
160
161 public static int getEmptyStateIndex(Taxa taxonList, String traitName) {
162 if (taxonList == null) {
163 throw new IllegalArgumentException("taxon list is null");
164 }
165
166 for (int i = 0; i < taxonList.getTaxonCount(); i++) {
167 Taxon taxon = taxonList.getTaxon(i);
168 String attr = (String) taxon.getAttribute(traitName);
169
170 // ? is used to denote missing data so is not a state...
171 if (attr == null || attr.equals("?")) {
172 return i;
173 }
174 }
175
176 return -1;
177 }
178
179 public void addPredictor(Predictor predictor) {
180 predictorList.add(predictor);
181 }
182
183 public void removePredictor(Predictor predictor) {
184 predictorList.remove(predictor);
185 }
186
187 /**
188 * Returns the number of included predictors (counting origin and destination ones separately)
189 * @return
190 */
191 public int getIncludedPredictorCount() {
192 int count = 0;
193 for (Predictor predictor : getPredictors()) {
194 if (predictor.isIncluded()) {
195 count += 1 + (predictor.getType() == Predictor.Type.BOTH_VECTOR ? 1 : 0);
196 }
197 }
198 return count;
199 }
200
201 public List<Predictor> getIncludedPredictors() {
202 List<Predictor> includedPredictors = new ArrayList<Predictor>();
203 for (Predictor predictor : getPredictors()) {
204 if (predictor.isIncluded()) {
205 includedPredictors.add(predictor);
206 }
207 }
208 return includedPredictors;
209 }
210
211 public List<Predictor> getPredictors() {
212 return predictorList;
213 }
214
215 public String toString() {
216 return name;
217 }
218 }