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.ForesterConstants;
38 import org.forester.util.ForesterUtil;
40 public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonomy> {
44 private String _scientific_name;
45 private String _common_name;
46 private List<String> _synonyms;
47 private String _authority;
48 private Identifier _identifier;
49 private String _taxonomy_code;
51 private List<Uri> _uris;
52 private List<String> _lineage;
59 public StringBuffer asSimpleText() {
64 public Uri getUri( final int index ) {
65 return getUris().get( index );
69 public void addUri( final Uri uri ) {
70 if ( getUris() == null ) {
71 setUris( new ArrayList<Uri>() );
77 public StringBuffer asText() {
78 final StringBuffer sb = new StringBuffer();
79 if ( getIdentifier() != null ) {
81 sb.append( getIdentifier().asSimpleText() );
84 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
85 if ( sb.length() > 0 ) {
89 sb.append( getTaxonomyCode() );
92 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
93 if ( sb.length() > 0 ) {
96 sb.append( getScientificName() );
97 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
99 sb.append( getAuthority() );
103 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
104 if ( sb.length() > 0 ) {
107 sb.append( getCommonName() );
113 public PhylogenyData copy() {
114 final Taxonomy t = new Taxonomy();
116 t.setTaxonomyCode( getTaxonomyCode() );
118 catch ( final PhyloXmlDataFormatException e ) {
121 t.setScientificName( getScientificName() );
122 t.setCommonName( getCommonName() );
123 t.setAuthority( getAuthority() );
124 for( final String syn : getSynonyms() ) {
125 t.getSynonyms().add( syn );
127 if ( getIdentifier() != null ) {
128 t.setIdentifier( ( Identifier ) getIdentifier().copy() );
131 t.setIdentifier( null );
134 t.setRank( new String( getRank() ) );
136 catch ( final PhyloXmlDataFormatException e ) {
139 if ( getUris() != null ) {
140 t.setUris( new ArrayList<Uri>() );
141 for( final Uri uri : getUris() ) {
143 t.getUris().add( uri );
147 if ( getLineage() != null ) {
148 t.setLineage( new ArrayList<String>() );
149 for( final String l : getLineage() ) {
151 t.getLineage().add( l );
159 public boolean equals( final Object o ) {
163 else if ( o == null ) {
166 else if ( o.getClass() != this.getClass() ) {
167 throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
168 + o.getClass() + "]" );
171 return isEqual( ( Taxonomy ) o );
175 public String getAuthority() {
179 public String getCommonName() {
183 public Identifier getIdentifier() {
187 public String getRank() {
191 public String getScientificName() {
192 return _scientific_name;
195 public List<String> getSynonyms() {
196 if ( _synonyms == null ) {
197 _synonyms = new ArrayList<String>();
202 public String getTaxonomyCode() {
203 return _taxonomy_code;
207 public List<Uri> getUris() {
212 public int hashCode() {
213 if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
214 return getIdentifier().hashCode();
216 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
217 return getTaxonomyCode().hashCode();
219 else if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
220 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
221 return ( getScientificName().toLowerCase() + getAuthority().toLowerCase() ).hashCode();
223 return getScientificName().toLowerCase().hashCode();
226 return getCommonName().toLowerCase().hashCode();
231 setScientificName( "" );
233 setIdentifier( null );
237 catch ( final PhyloXmlDataFormatException e ) {
241 setTaxonomyCode( "" );
243 catch ( final PhyloXmlDataFormatException e ) {
252 public boolean isEmpty() {
253 return ( ( getIdentifier() == null ) && ForesterUtil.isEmpty( getTaxonomyCode() )
254 && ForesterUtil.isEmpty( getCommonName() ) && ForesterUtil.isEmpty( getScientificName() )
255 && ForesterUtil.isEmpty( getRank() ) && ForesterUtil.isEmpty( _uris )
256 && ForesterUtil.isEmpty( getAuthority() ) && ForesterUtil.isEmpty( _synonyms ) && ForesterUtil
257 .isEmpty( _lineage ) );
262 * If this and taxonomy 'data' has an identifier, comparison will be based on that.
263 * Otherwise, if this and taxonomy 'data' has a code, comparison will be based on that.
264 * Otherwise, if Taxonomy 'data' has a scientific name, comparison will be
265 * based on that (case insensitive!).
266 * Otherwise, if Taxonomy 'data' has a common name, comparison will be
267 * based on that (case insensitive!).
268 * (Note. This is important and should not be change without a very good reason.)
272 public boolean isEqual( final PhylogenyData data ) {
273 if ( this == data ) {
276 final Taxonomy tax = ( Taxonomy ) data;
277 if ( ( getIdentifier() != null ) && ( tax.getIdentifier() != null )
278 && !ForesterUtil.isEmpty( getIdentifier().getValue() )
279 && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
280 return getIdentifier().isEqual( tax.getIdentifier() );
282 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
283 return getTaxonomyCode().equals( tax.getTaxonomyCode() );
285 else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
286 if ( !ForesterUtil.isEmpty( getAuthority() ) && !ForesterUtil.isEmpty( tax.getAuthority() ) ) {
287 return ( getScientificName().equalsIgnoreCase( tax.getScientificName() ) )
288 && ( getAuthority().equalsIgnoreCase( tax.getAuthority() ) );
290 return getScientificName().equalsIgnoreCase( tax.getScientificName() );
292 else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
293 return getCommonName().equalsIgnoreCase( tax.getCommonName() );
295 else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
296 return getScientificName().equalsIgnoreCase( tax.getCommonName() );
298 else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
299 return getCommonName().equalsIgnoreCase( tax.getScientificName() );
301 throw new RuntimeException( "comparison not possible with empty fields" );
304 public void setAuthority( final String authority ) {
305 _authority = authority;
308 public void setCommonName( final String common_name ) {
309 _common_name = common_name;
312 public void setIdentifier( final Identifier identifier ) {
313 _identifier = identifier;
316 public void setRank( final String rank ) throws PhyloXmlDataFormatException {
317 if ( !ForesterUtil.isEmpty( rank ) && !PhyloXmlUtil.TAXONOMY_RANKS_SET.contains( rank ) ) {
318 throw new PhyloXmlDataFormatException( "illegal rank: [" + rank + "]" );
323 public void setScientificName( final String scientific_name ) {
324 _scientific_name = scientific_name;
327 private void setSynonyms( final List<String> synonyms ) {
328 _synonyms = synonyms;
331 public void setTaxonomyCode( final String taxonomy_code ) throws PhyloXmlDataFormatException {
333 if ( !ForesterUtil.isEmpty( taxonomy_code )
334 && !PhyloXmlUtil.TAXOMONY_CODE_PATTERN.matcher( taxonomy_code ).matches() ) {
335 throw new PhyloXmlDataFormatException( "illegal taxonomy code: [" + taxonomy_code + "]" );
338 _taxonomy_code = taxonomy_code;
342 public void setUris( final List<Uri> uris ) {
347 public StringBuffer toNHX() {
348 final StringBuffer sb = new StringBuffer();
349 if ( getIdentifier() != null ) {
350 sb.append( ':' + NHXtags.TAXONOMY_ID );
351 sb.append( ForesterUtil.replaceIllegalNhxCharacters( getIdentifier().getValue() ) );
353 final StringBuffer species = new StringBuffer();
354 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
355 species.append( ForesterUtil.replaceIllegalNhxCharacters( getTaxonomyCode() ) );
357 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
358 ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
359 species.append( ForesterUtil.replaceIllegalNhxCharacters( getScientificName() ) );
361 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
362 ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
363 species.append( ForesterUtil.replaceIllegalNhxCharacters( getCommonName() ) );
365 if ( species.length() > 0 ) {
366 sb.append( ':' + NHXtags.SPECIES_NAME );
367 sb.append( species );
373 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
377 writer.write( ForesterUtil.LINE_SEPARATOR );
378 writer.write( indentation );
379 PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.TAXONOMY );
380 if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
381 getIdentifier().toPhyloXML( writer, level, indentation );
383 if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
384 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_CODE, getTaxonomyCode(), indentation );
386 if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
387 PhylogenyDataUtil.appendElement( writer,
388 PhyloXmlMapping.TAXONOMY_SCIENTIFIC_NAME,
392 if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
393 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_AUTHORITY, getAuthority(), indentation );
395 if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
397 .appendElement( writer, PhyloXmlMapping.TAXONOMY_COMMON_NAME, getCommonName(), indentation );
399 if ( _synonyms != null ) {
400 for( final String syn : getSynonyms() ) {
401 if ( !ForesterUtil.isEmpty( syn ) ) {
402 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_SYNONYM, syn, indentation );
406 if ( !ForesterUtil.isEmpty( getRank() ) ) {
407 PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_RANK, getRank(), indentation );
409 if ( getUris() != null ) {
410 for( final Uri uri : getUris() ) {
412 uri.toPhyloXML( writer, level, indentation );
416 writer.write( ForesterUtil.LINE_SEPARATOR );
417 writer.write( indentation );
418 PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.TAXONOMY );
422 public String toString() {
423 return asText().toString();
427 public int compareTo( final Taxonomy o ) {
431 else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( o.getScientificName() ) ) {
432 return getScientificName().compareToIgnoreCase( o.getScientificName() );
434 else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( o.getCommonName() ) ) {
435 return getCommonName().compareToIgnoreCase( o.getCommonName() );
437 else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( o.getTaxonomyCode() ) ) {
438 return getTaxonomyCode().compareToIgnoreCase( o.getTaxonomyCode() );
443 public void setLineage( final List<String> lineage ) {
447 public List<String> getLineage() {