Debian Codegraph @

src/dr/evolution/util/TaxonList.java

raw symbols SCIP
1 /*
2 * TaxonList.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.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 * Interface for a list of taxa.
36 *
37 * @version $Id: TaxonList.java,v 1.16 2006/09/05 13:29:34 rambaut Exp $
38 *
39 * @author Andrew Rambaut
40 * @author Alexei Drummond
41 */
42 public interface TaxonList extends Identifiable, Iterable<Taxon> {
43
44 /**
45 * @return a count of the number of taxa in the list.
46 */
47 public int getTaxonCount();
48
49 /**
50 * @return the ith taxon.
51 */
52 public Taxon getTaxon(int taxonIndex);
53
54 /**
55 * @return the ID of the ith taxon.
56 */
57 public String getTaxonId(int taxonIndex);
58
59 /**
60 * returns the index of the taxon with the given id.
61 */
62 int getTaxonIndex(String id);
63
64 /**
65 * returns the index of the given taxon.
66 */
67 int getTaxonIndex(Taxon taxon);
68
69 /**
70 * returns the taxa as a Java list
71 * @return
72 */
73 List<Taxon> asList();
74
75 /**
76 * @return an object representing the named attributed for the given taxon.
77 * @param taxonIndex the index of the taxon whose attribute is being fetched.
78 * @param name the name of the attribute of interest.
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 }