2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.phylogeny.data;
28 import java.io.IOException;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.List;
33 import org.forester.io.parsers.nhx.NHXtags;
34 import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
35 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
36 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
37 import org.forester.util.ForesterUtil;
39 public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonomy> {
41 private String _scientific_name;
42 private String _common_name;
43 private List<String> _synonyms;
44 private String _authority;
45 private Identifier _identifier;
46 private String _taxonomy_code;
48 private List<Uri> _uris;
55 public StringBuffer asSimpleText() {
60 public Uri getUri( final int index ) {
61 return getUris().get( index );
65 public void addUri( final Uri uri ) {
66 if ( getUris() == null ) {
67 setUris( new ArrayList<Uri>() );
73 public StringBuffer asText() {
74 final StringBuffer sb = new StringBuffer();
75 if ( getIdentifier() != null ) {
77 sb.append( getIdentifier().asSimpleText() );
80 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
81 if ( sb.length() > 0 ) {
85 sb.append( getTaxonomyCode() );
88 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
89 if ( sb.length() > 0 ) {
92 sb.append( getScientificName() );
93 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
95 sb.append( getAuthority() );
99 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
100 if ( sb.length() > 0 ) {
103 sb.append( getCommonName() );
109 public PhylogenyData copy() {
110 final Taxonomy t = new Taxonomy();
111 t.setTaxonomyCode( getTaxonomyCode() );
112 t.setScientificName( getScientificName() );
113 t.setCommonName( getCommonName() );
114 t.setAuthority( getAuthority() );
115 for( final String syn : getSynonyms() ) {
116 t.getSynonyms().add( syn );
118 if ( getIdentifier() != null ) {
119 t.setIdentifier( ( Identifier ) getIdentifier().copy() );
122 t.setIdentifier( null );
124 t.setRank( new String( getRank() ) );
125 if ( getUris() != null ) {
126 t.setUris( new ArrayList<Uri>() );
127 for( final Uri uri : getUris() ) {
129 t.getUris().add( uri );
137 public boolean equals( final Object o ) {
141 else if ( o == null ) {
144 else if ( o.getClass() != this.getClass() ) {
145 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
146 + o.getClass() + "]" );
149 return isEqual( ( Taxonomy ) o );
153 public String getAuthority() {
157 public String getCommonName() {
161 public Identifier getIdentifier() {
165 public String getRank() {
169 public String getScientificName() {
170 return _scientific_name;
173 public List<String> getSynonyms() {
174 if ( _synonyms == null ) {
175 _synonyms = new ArrayList<String>();
180 public String getTaxonomyCode() {
181 return _taxonomy_code;
185 public List<Uri> getUris() {
190 public int hashCode() {
191 if ( getIdentifier() != null ) {
192 return getIdentifier().hashCode();
194 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
195 return getTaxonomyCode().hashCode();
197 else if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
198 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
199 return ( getScientificName().toLowerCase() + getAuthority().toLowerCase() ).hashCode();
201 return getScientificName().toLowerCase().hashCode();
204 return getCommonName().toLowerCase().hashCode();
209 setScientificName( "" );
211 setIdentifier( null );
213 setTaxonomyCode( "" );
219 public boolean isEmpty() {
220 return ( ( getIdentifier() == null ) && ForesterUtil.isEmpty( getTaxonomyCode() )
221 && ForesterUtil.isEmpty( getCommonName() ) && ForesterUtil.isEmpty( getScientificName() )
222 && ForesterUtil.isEmpty( getRank() ) && ForesterUtil.isEmpty( _uris )
223 && ForesterUtil.isEmpty( getAuthority() ) && ForesterUtil.isEmpty( _synonyms ) );
228 * If this and taxonomy 'data' has an identifier, comparison will be based on that.
229 * Otherwise, if this and taxonomy 'data' has a code, comparison will be based on that.
230 * Otherwise, if Taxonomy 'data' has a scientific name, comparison will be
231 * based on that (case insensitive!).
232 * Otherwise, if Taxonomy 'data' has a common name, comparison will be
233 * based on that (case insensitive!).
234 * (Note. This is important and should not be change without a very good reason.)
238 public boolean isEqual( final PhylogenyData data ) {
239 if ( this == data ) {
242 final Taxonomy tax = ( Taxonomy ) data;
243 if ( ( getIdentifier() != null ) && ( tax.getIdentifier() != null ) ) {
244 return getIdentifier().isEqual( tax.getIdentifier() );
246 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
247 return getTaxonomyCode().equals( tax.getTaxonomyCode() );
249 else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
250 if ( !ForesterUtil.isEmpty( getAuthority() ) && !ForesterUtil.isEmpty( tax.getAuthority() ) ) {
251 return ( getScientificName().equalsIgnoreCase( tax.getScientificName() ) )
252 && ( getAuthority().equalsIgnoreCase( tax.getAuthority() ) );
254 return getScientificName().equalsIgnoreCase( tax.getScientificName() );
256 else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
257 return getCommonName().equalsIgnoreCase( tax.getCommonName() );
259 else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
260 return getScientificName().equalsIgnoreCase( tax.getCommonName() );
262 else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
263 return getCommonName().equalsIgnoreCase( tax.getScientificName() );
265 throw new RuntimeException( "comparison not possible with empty fields" );
268 public void setAuthority( final String authority ) {
269 _authority = authority;
272 public void setCommonName( final String common_name ) {
273 _common_name = common_name;
276 public void setIdentifier( final Identifier identifier ) {
277 _identifier = identifier;
280 public void setRank( final String rank ) {
281 if ( !ForesterUtil.isEmpty( rank ) && !PhyloXmlUtil.TAXONOMY_RANKS.contains( rank ) ) {
282 throw new PhyloXmlDataFormatException( "illegal rank: [" + rank + "]" );
287 public void setScientificName( final String scientific_name ) {
288 _scientific_name = scientific_name;
291 private void setSynonyms( final List<String> synonyms ) {
292 _synonyms = synonyms;
295 public void setTaxonomyCode( final String taxonomy_code ) {
296 if ( !ForesterUtil.isEmpty( taxonomy_code )
297 && !PhyloXmlUtil.TAXOMONY_CODE_PATTERN.matcher( taxonomy_code ).matches() ) {
298 throw new PhyloXmlDataFormatException( "illegal taxonomy code: [" + taxonomy_code + "]" );
300 _taxonomy_code = taxonomy_code;
304 public void setUris( final List<Uri> uris ) {
309 public StringBuffer toNHX() {
310 final StringBuffer sb = new StringBuffer();
311 if ( getIdentifier() != null ) {
312 sb.append( ':' + NHXtags.TAXONOMY_ID );
313 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getIdentifier().getValue() ) );
315 final StringBuffer species = new StringBuffer();
316 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
317 species.append( ForesterUtil.replaceIllegalNhxCharacters( getTaxonomyCode() ) );
319 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
320 ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
321 species.append( ForesterUtil.replaceIllegalNhxCharacters( getScientificName() ) );
323 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
324 ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
325 species.append( ForesterUtil.replaceIllegalNhxCharacters( getCommonName() ) );
327 if ( species.length() > 0 ) {
328 sb.append( ':' + NHXtags.SPECIES_NAME );
329 sb.append( species );
335 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
339 writer.write( ForesterUtil.LINE_SEPARATOR );
340 writer.write( indentation );
341 PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.TAXONOMY );
342 if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
343 getIdentifier().toPhyloXML( writer, level, indentation );
345 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
346 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_CODE, getTaxonomyCode(), indentation );
348 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
349 PhylogenyDataUtil.appendElement( writer,
350 PhyloXmlMapping.TAXONOMY_SCIENTIFIC_NAME,
354 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
355 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_AUTHORITY, getAuthority(), indentation );
357 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
359 .appendElement( writer, PhyloXmlMapping.TAXONOMY_COMMON_NAME, getCommonName(), indentation );
361 if ( _synonyms != null ) {
362 for( final String syn : getSynonyms() ) {
363 if ( !ForesterUtil.isEmpty( syn ) ) {
364 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_SYNONYM, syn, indentation );
368 if ( !ForesterUtil.isEmpty( getRank() ) ) {
369 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_RANK, getRank(), indentation );
371 if ( getUris() != null ) {
372 for( final Uri uri : getUris() ) {
374 uri.toPhyloXML( writer, level, indentation );
378 writer.write( ForesterUtil.LINE_SEPARATOR );
379 writer.write( indentation );
380 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.TAXONOMY );
384 public String toString() {
385 return asText().toString();
389 public int compareTo( final Taxonomy o ) {
393 else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( o.getScientificName() ) ) {
394 return getScientificName().compareToIgnoreCase( o.getScientificName() );
396 else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( o.getCommonName() ) ) {
397 return getCommonName().compareToIgnoreCase( o.getCommonName() );
399 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( o.getTaxonomyCode() ) ) {
400 return getTaxonomyCode().compareToIgnoreCase( o.getTaxonomyCode() );