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: https://sites.google.com/site/cmzmasek/home/software/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();
113 t.setTaxonomyCode( getTaxonomyCode() );
115 catch ( final PhyloXmlDataFormatException e ) {
118 t.setScientificName( getScientificName() );
119 t.setCommonName( getCommonName() );
120 t.setAuthority( getAuthority() );
121 for( final String syn : getSynonyms() ) {
122 t.getSynonyms().add( syn );
124 if ( getIdentifier() != null ) {
125 t.setIdentifier( ( Identifier ) getIdentifier().copy() );
128 t.setIdentifier( null );
131 t.setRank( new String( getRank() ) );
133 catch ( final PhyloXmlDataFormatException e ) {
136 if ( getUris() != null ) {
137 t.setUris( new ArrayList<Uri>() );
138 for( final Uri uri : getUris() ) {
140 t.getUris().add( uri );
144 if ( getLineage() != null ) {
145 t.setLineage( new ArrayList<String>() );
146 for( final String l : getLineage() ) {
148 t.getLineage().add( l );
156 public boolean equals( final Object o ) {
160 else if ( o == null ) {
163 else if ( o.getClass() != this.getClass() ) {
164 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
165 + o.getClass() + "]" );
168 return isEqual( ( Taxonomy ) o );
172 public String getAuthority() {
176 public String getCommonName() {
180 public Identifier getIdentifier() {
184 public String getRank() {
188 public String getScientificName() {
189 return _scientific_name;
192 public List<String> getSynonyms() {
193 if ( _synonyms == null ) {
194 _synonyms = new ArrayList<String>();
199 public String getTaxonomyCode() {
200 return _taxonomy_code;
204 public List<Uri> getUris() {
209 public int hashCode() {
210 if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
211 return getIdentifier().hashCode();
213 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
214 return getTaxonomyCode().hashCode();
216 else if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
217 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
218 return ( getScientificName().toLowerCase() + getAuthority().toLowerCase() ).hashCode();
220 return getScientificName().toLowerCase().hashCode();
223 return getCommonName().toLowerCase().hashCode();
228 setScientificName( "" );
230 setIdentifier( null );
234 catch ( final PhyloXmlDataFormatException e ) {
238 setTaxonomyCode( "" );
240 catch ( final PhyloXmlDataFormatException e ) {
249 public boolean isEmpty() {
250 return ( ( getIdentifier() == null ) && ForesterUtil.isEmpty( getTaxonomyCode() )
251 && ForesterUtil.isEmpty( getCommonName() ) && ForesterUtil.isEmpty( getScientificName() ) && ForesterUtil
252 .isEmpty( _lineage ) );
257 * If this and taxonomy 'data' has an identifier, comparison will be based on that.
258 * Otherwise, if this and taxonomy 'data' has a code, comparison will be based on that.
259 * Otherwise, if Taxonomy 'data' has a scientific name, comparison will be
260 * based on that (case insensitive!).
261 * Otherwise, if Taxonomy 'data' has a common name, comparison will be
262 * based on that (case insensitive!).
263 * (Note. This is important and should not be change without a very good reason.)
267 public boolean isEqual( final PhylogenyData data ) {
268 if ( this == data ) {
271 final Taxonomy tax = ( Taxonomy ) data;
272 if ( ( getIdentifier() != null ) && ( tax.getIdentifier() != null )
273 && !ForesterUtil.isEmpty( getIdentifier().getValue() )
274 && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
275 return getIdentifier().isEqual( tax.getIdentifier() );
277 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
278 return getTaxonomyCode().equals( tax.getTaxonomyCode() );
280 else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
281 if ( !ForesterUtil.isEmpty( getAuthority() ) && !ForesterUtil.isEmpty( tax.getAuthority() ) ) {
282 return ( getScientificName().equalsIgnoreCase( tax.getScientificName() ) )
283 && ( getAuthority().equalsIgnoreCase( tax.getAuthority() ) );
285 return getScientificName().equalsIgnoreCase( tax.getScientificName() );
287 else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
288 return getCommonName().equalsIgnoreCase( tax.getCommonName() );
290 //throw new RuntimeException( "comparison not possible with empty fields" );
294 public void setAuthority( final String authority ) {
295 _authority = authority;
298 public void setCommonName( final String common_name ) {
299 _common_name = common_name;
302 public void setIdentifier( final Identifier identifier ) {
303 _identifier = identifier;
306 public void setRank( final String rank ) throws PhyloXmlDataFormatException {
307 if ( !ForesterUtil.isEmpty( rank ) && !PhyloXmlUtil.TAXONOMY_RANKS_SET.contains( rank ) ) {
308 throw new PhyloXmlDataFormatException( "illegal rank: [" + rank + "]" );
313 public void setScientificName( final String scientific_name ) {
314 _scientific_name = scientific_name;
317 private void setSynonyms( final List<String> synonyms ) {
318 _synonyms = synonyms;
321 public void setTaxonomyCode( String taxonomy_code ) throws PhyloXmlDataFormatException {
322 if ( !ForesterUtil.isEmpty( taxonomy_code )
323 && !PhyloXmlUtil.TAXOMONY_CODE_PATTERN.matcher( taxonomy_code ).matches() ) {
324 throw new PhyloXmlDataFormatException( "illegal taxonomy code: [" + taxonomy_code + "]" );
326 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
327 //TODO FIXME (added on 13-11-18) remove me eventually
328 if ( taxonomy_code.equals( "ACIBL" ) ) {
329 taxonomy_code = "KORVE";
331 else if ( taxonomy_code.equals( "PYRKO" ) ) {
332 taxonomy_code = "THEKO";
334 //TODO FIXME (added on 13-11-18) remove me eventually
335 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
336 _taxonomy_code = taxonomy_code;
340 public void setUris( final List<Uri> uris ) {
345 public StringBuffer toNHX() {
346 final StringBuffer sb = new StringBuffer();
347 if ( getIdentifier() != null ) {
348 sb.append( ':' + NHXtags.TAXONOMY_ID );
349 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getIdentifier().getValue() ) );
351 final StringBuffer species = new StringBuffer();
352 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
353 species.append( ForesterUtil.replaceIllegalNhxCharacters( getTaxonomyCode() ) );
355 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
356 ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
357 species.append( ForesterUtil.replaceIllegalNhxCharacters( getScientificName() ) );
359 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
360 ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
361 species.append( ForesterUtil.replaceIllegalNhxCharacters( getCommonName() ) );
363 if ( species.length() > 0 ) {
364 sb.append( ':' + NHXtags.SPECIES_NAME );
365 sb.append( species );
371 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
375 writer.write( ForesterUtil.LINE_SEPARATOR );
376 writer.write( indentation );
377 PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.TAXONOMY );
378 if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
379 getIdentifier().toPhyloXML( writer, level, indentation );
381 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
382 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_CODE, getTaxonomyCode(), indentation );
384 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
385 PhylogenyDataUtil.appendElement( writer,
386 PhyloXmlMapping.TAXONOMY_SCIENTIFIC_NAME,
390 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
391 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_AUTHORITY, getAuthority(), indentation );
393 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
395 .appendElement( writer, PhyloXmlMapping.TAXONOMY_COMMON_NAME, getCommonName(), indentation );
397 if ( _synonyms != null ) {
398 for( final String syn : getSynonyms() ) {
399 if ( !ForesterUtil.isEmpty( syn ) ) {
400 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_SYNONYM, syn, indentation );
404 if ( !ForesterUtil.isEmpty( getRank() ) ) {
405 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_RANK, getRank(), indentation );
407 if ( getUris() != null ) {
408 for( final Uri uri : getUris() ) {
410 uri.toPhyloXML( writer, level, indentation );
414 writer.write( ForesterUtil.LINE_SEPARATOR );
415 writer.write( indentation );
416 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.TAXONOMY );
420 public String toString() {
421 return asText().toString();
425 public int compareTo( final Taxonomy o ) {
429 if ( ( getIdentifier() != null ) && ( o.getIdentifier() != null )
430 && !ForesterUtil.isEmpty( getIdentifier().getValue() )
431 && !ForesterUtil.isEmpty( o.getIdentifier().getValue() ) ) {
432 final int x = getIdentifier().getValuePlusProvider().compareTo( o.getIdentifier().getValuePlusProvider() );
437 if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( o.getScientificName() ) ) {
438 return getScientificName().compareToIgnoreCase( o.getScientificName() );
440 if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( o.getCommonName() ) ) {
441 return getCommonName().compareToIgnoreCase( o.getCommonName() );
443 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( o.getTaxonomyCode() ) ) {
444 return getTaxonomyCode().compareToIgnoreCase( o.getTaxonomyCode() );
446 if ( ( getIdentifier() != null ) && ( o.getIdentifier() != null )
447 && !ForesterUtil.isEmpty( getIdentifier().getValue() )
448 && !ForesterUtil.isEmpty( o.getIdentifier().getValue() ) ) {
449 return getIdentifier().getValuePlusProvider().compareTo( o.getIdentifier().getValuePlusProvider() );
454 public void setLineage( final List<String> lineage ) {
458 public List<String> getLineage() {