| 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.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 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 85 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 189 |
|
| 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 |
} |