| 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.evolution.util; |
| 27 |
|
| 28 |
import dr.util.Identifiable; |
| 29 |
|
| 30 |
import java.util.HashSet; |
| 31 |
import java.util.List; |
| 32 |
import java.util.Set; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public interface TaxonList extends Identifiable, Iterable<Taxon> { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public int getTaxonCount(); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public Taxon getTaxon(int taxonIndex); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public String getTaxonId(int taxonIndex); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
int getTaxonIndex(String id); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
int getTaxonIndex(Taxon taxon); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
List<Taxon> asList(); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public Object getTaxonAttribute(int taxonIndex, String name); |
| 81 |
|
| 82 |
class Utils { |
| 83 |
|
| 84 |
public static boolean hasAttribute(TaxonList taxa, int index, String name) { |
| 85 |
return taxa.getTaxonAttribute(index, name) != null; |
| 86 |
} |
| 87 |
|
| 88 |
public static Set<String> getTaxonListIdSet(TaxonList taxa) { |
| 89 |
Set<String> taxaSet = new HashSet<String>(); |
| 90 |
for (int i =0; i < taxa.getTaxonCount(); i++) { |
| 91 |
taxaSet.add(taxa.getTaxonId(i)); |
| 92 |
} |
| 93 |
return taxaSet; |
| 94 |
} |
| 95 |
|
| 96 |
public static int findDuplicateTaxon(TaxonList taxonList) { |
| 97 |
Set<String> taxaSet = new HashSet<String>(); |
| 98 |
for (int i = 0; i < taxonList.getTaxonCount(); i++) { |
| 99 |
Taxon taxon = taxonList.getTaxon(i); |
| 100 |
if (taxaSet.contains(taxon.getId())) { |
| 101 |
return i; |
| 102 |
} |
| 103 |
taxaSet.add(taxon.getId()); |
| 104 |
} |
| 105 |
return -1; |
| 106 |
} |
| 107 |
|
| 108 |
public static boolean areTaxaIdentical(TaxonList taxa1, TaxonList taxa2) { |
| 109 |
if (taxa1.getTaxonCount() != taxa2.getTaxonCount()) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
for (int i =0; i < taxa1.getTaxonCount(); i++) { |
| 113 |
if (taxa2.getTaxonIndex(taxa1.getTaxon(i)) == -1) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
} |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
class MissingTaxonException extends Exception { |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
private static final long serialVersionUID = 1864895946392309485L; |
| 128 |
|
| 129 |
public MissingTaxonException(String message) { super(message); } |
| 130 |
} |
| 131 |
} |