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;
49 private List<String> _lineage;
56 public StringBuffer asSimpleText() {
61 public Uri getUri( final int index ) {
62 return getUris().get( index );
66 public void addUri( final Uri uri ) {
67 if ( getUris() == null ) {
68 setUris( new ArrayList<Uri>() );
74 public StringBuffer asText() {
75 final StringBuffer sb = new StringBuffer();
76 if ( getIdentifier() != null ) {
78 sb.append( getIdentifier().asSimpleText() );
81 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
82 if ( sb.length() > 0 ) {
86 sb.append( getTaxonomyCode() );
89 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
90 if ( sb.length() > 0 ) {
93 sb.append( getScientificName() );
94 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
96 sb.append( getAuthority() );
100 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
101 if ( sb.length() > 0 ) {
104 sb.append( getCommonName() );
110 public PhylogenyData copy() {
111 final Taxonomy t = new Taxonomy();
112 t.setTaxonomyCode( getTaxonomyCode() );
113 t.setScientificName( getScientificName() );
114 t.setCommonName( getCommonName() );
115 t.setAuthority( getAuthority() );
116 for( final String syn : getSynonyms() ) {
117 t.getSynonyms().add( syn );
119 if ( getIdentifier() != null ) {
120 t.setIdentifier( ( Identifier ) getIdentifier().copy() );
123 t.setIdentifier( null );
125 t.setRank( new String( getRank() ) );
126 if ( getUris() != null ) {
127 t.setUris( new ArrayList<Uri>() );
128 for( final Uri uri : getUris() ) {
130 t.getUris().add( uri );
134 if ( getLineage() != null ) {
135 t.setLineage( new ArrayList<String>() );
136 for( final String l : getLineage() ) {
138 t.getLineage().add( l );
146 public boolean equals( final Object o ) {
150 else if ( o == null ) {
153 else if ( o.getClass() != this.getClass() ) {
154 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
155 + o.getClass() + "]" );
158 return isEqual( ( Taxonomy ) o );
162 public String getAuthority() {
166 public String getCommonName() {
170 public Identifier getIdentifier() {
174 public String getRank() {
178 public String getScientificName() {
179 return _scientific_name;
182 public List<String> getSynonyms() {
183 if ( _synonyms == null ) {
184 _synonyms = new ArrayList<String>();
189 public String getTaxonomyCode() {
190 return _taxonomy_code;
194 public List<Uri> getUris() {
199 public int hashCode() {
200 if ( getIdentifier() != null ) {
201 return getIdentifier().hashCode();
203 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
204 return getTaxonomyCode().hashCode();
206 else if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
207 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
208 return ( getScientificName().toLowerCase() + getAuthority().toLowerCase() ).hashCode();
210 return getScientificName().toLowerCase().hashCode();
213 return getCommonName().toLowerCase().hashCode();
218 setScientificName( "" );
220 setIdentifier( null );
222 setTaxonomyCode( "" );
229 public boolean isEmpty() {
230 return ( ( getIdentifier() == null ) && ForesterUtil.isEmpty( getTaxonomyCode() )
231 && ForesterUtil.isEmpty( getCommonName() ) && ForesterUtil.isEmpty( getScientificName() )
232 && ForesterUtil.isEmpty( getRank() ) && ForesterUtil.isEmpty( _uris )
233 && ForesterUtil.isEmpty( getAuthority() ) && ForesterUtil.isEmpty( _synonyms ) && ForesterUtil
234 .isEmpty( _lineage ) );
239 * If this and taxonomy 'data' has an identifier, comparison will be based on that.
240 * Otherwise, if this and taxonomy 'data' has a code, comparison will be based on that.
241 * Otherwise, if Taxonomy 'data' has a scientific name, comparison will be
242 * based on that (case insensitive!).
243 * Otherwise, if Taxonomy 'data' has a common name, comparison will be
244 * based on that (case insensitive!).
245 * (Note. This is important and should not be change without a very good reason.)
249 public boolean isEqual( final PhylogenyData data ) {
250 if ( this == data ) {
253 final Taxonomy tax = ( Taxonomy ) data;
254 if ( ( getIdentifier() != null ) && ( tax.getIdentifier() != null ) ) {
255 return getIdentifier().isEqual( tax.getIdentifier() );
257 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
258 return getTaxonomyCode().equals( tax.getTaxonomyCode() );
260 else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
261 if ( !ForesterUtil.isEmpty( getAuthority() ) && !ForesterUtil.isEmpty( tax.getAuthority() ) ) {
262 return ( getScientificName().equalsIgnoreCase( tax.getScientificName() ) )
263 && ( getAuthority().equalsIgnoreCase( tax.getAuthority() ) );
265 return getScientificName().equalsIgnoreCase( tax.getScientificName() );
267 else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
268 return getCommonName().equalsIgnoreCase( tax.getCommonName() );
270 else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
271 return getScientificName().equalsIgnoreCase( tax.getCommonName() );
273 else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
274 return getCommonName().equalsIgnoreCase( tax.getScientificName() );
276 throw new RuntimeException( "comparison not possible with empty fields" );
279 public void setAuthority( final String authority ) {
280 _authority = authority;
283 public void setCommonName( final String common_name ) {
284 _common_name = common_name;
287 public void setIdentifier( final Identifier identifier ) {
288 _identifier = identifier;
291 public void setRank( final String rank ) {
292 if ( !ForesterUtil.isEmpty( rank ) && !PhyloXmlUtil.TAXONOMY_RANKS_SET.contains( rank ) ) {
293 throw new PhyloXmlDataFormatException( "illegal rank: [" + rank + "]" );
298 public void setScientificName( final String scientific_name ) {
299 _scientific_name = scientific_name;
302 private void setSynonyms( final List<String> synonyms ) {
303 _synonyms = synonyms;
306 public void setTaxonomyCode( final String taxonomy_code ) {
307 if ( !ForesterUtil.isEmpty( taxonomy_code )
308 && !PhyloXmlUtil.TAXOMONY_CODE_PATTERN.matcher( taxonomy_code ).matches() ) {
309 throw new PhyloXmlDataFormatException( "illegal taxonomy code: [" + taxonomy_code + "]" );
311 _taxonomy_code = taxonomy_code;
315 public void setUris( final List<Uri> uris ) {
320 public StringBuffer toNHX() {
321 final StringBuffer sb = new StringBuffer();
322 if ( getIdentifier() != null ) {
323 sb.append( ':' + NHXtags.TAXONOMY_ID );
324 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getIdentifier().getValue() ) );
326 final StringBuffer species = new StringBuffer();
327 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
328 species.append( ForesterUtil.replaceIllegalNhxCharacters( getTaxonomyCode() ) );
330 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
331 ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
332 species.append( ForesterUtil.replaceIllegalNhxCharacters( getScientificName() ) );
334 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
335 ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
336 species.append( ForesterUtil.replaceIllegalNhxCharacters( getCommonName() ) );
338 if ( species.length() > 0 ) {
339 sb.append( ':' + NHXtags.SPECIES_NAME );
340 sb.append( species );
346 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
350 writer.write( ForesterUtil.LINE_SEPARATOR );
351 writer.write( indentation );
352 PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.TAXONOMY );
353 if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
354 getIdentifier().toPhyloXML( writer, level, indentation );
356 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
357 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_CODE, getTaxonomyCode(), indentation );
359 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
360 PhylogenyDataUtil.appendElement( writer,
361 PhyloXmlMapping.TAXONOMY_SCIENTIFIC_NAME,
365 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
366 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_AUTHORITY, getAuthority(), indentation );
368 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
370 .appendElement( writer, PhyloXmlMapping.TAXONOMY_COMMON_NAME, getCommonName(), indentation );
372 if ( _synonyms != null ) {
373 for( final String syn : getSynonyms() ) {
374 if ( !ForesterUtil.isEmpty( syn ) ) {
375 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_SYNONYM, syn, indentation );
379 if ( !ForesterUtil.isEmpty( getRank() ) ) {
380 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_RANK, getRank(), indentation );
382 if ( getUris() != null ) {
383 for( final Uri uri : getUris() ) {
385 uri.toPhyloXML( writer, level, indentation );
389 writer.write( ForesterUtil.LINE_SEPARATOR );
390 writer.write( indentation );
391 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.TAXONOMY );
395 public String toString() {
396 return asText().toString();
400 public int compareTo( final Taxonomy o ) {
404 else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( o.getScientificName() ) ) {
405 return getScientificName().compareToIgnoreCase( o.getScientificName() );
407 else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( o.getCommonName() ) ) {
408 return getCommonName().compareToIgnoreCase( o.getCommonName() );
410 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( o.getTaxonomyCode() ) ) {
411 return getTaxonomyCode().compareToIgnoreCase( o.getTaxonomyCode() );
416 public void setLineage( final List<String> lineage ) {
420 public List<String> getLineage() {