48984f01ea82a80d95bd786cec02b3fc5594248f
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Taxonomy.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.phylogeny.data;
27
28 import java.io.IOException;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.List;
32
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;
38
39 public class Taxonomy implements PhylogenyData, MultipleUris, Comparable<Taxonomy> {
40
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;
47     private String       _rank;
48     private List<Uri>    _uris;
49     private List<String> _lineage;
50
51     public Taxonomy() {
52         init();
53     }
54
55     @Override
56     public StringBuffer asSimpleText() {
57         return asText();
58     }
59
60     @Override
61     public Uri getUri( final int index ) {
62         return getUris().get( index );
63     }
64
65     @Override
66     public void addUri( final Uri uri ) {
67         if ( getUris() == null ) {
68             setUris( new ArrayList<Uri>() );
69         }
70         getUris().add( uri );
71     }
72
73     @Override
74     public StringBuffer asText() {
75         final StringBuffer sb = new StringBuffer();
76         if ( getIdentifier() != null ) {
77             sb.append( "[" );
78             sb.append( getIdentifier().asSimpleText() );
79             sb.append( "]" );
80         }
81         if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
82             if ( sb.length() > 0 ) {
83                 sb.append( " " );
84             }
85             sb.append( "[" );
86             sb.append( getTaxonomyCode() );
87             sb.append( "]" );
88         }
89         if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
90             if ( sb.length() > 0 ) {
91                 sb.append( " " );
92             }
93             sb.append( getScientificName() );
94             if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
95                 sb.append( " (" );
96                 sb.append( getAuthority() );
97                 sb.append( ")" );
98             }
99         }
100         if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
101             if ( sb.length() > 0 ) {
102                 sb.append( " " );
103             }
104             sb.append( getCommonName() );
105         }
106         return sb;
107     }
108
109     @Override
110     public PhylogenyData copy() {
111         final Taxonomy t = new Taxonomy();
112         try {
113             t.setTaxonomyCode( getTaxonomyCode() );
114         }
115         catch ( final PhyloXmlDataFormatException e ) {
116             e.printStackTrace();
117         }
118         t.setScientificName( getScientificName() );
119         t.setCommonName( getCommonName() );
120         t.setAuthority( getAuthority() );
121         for( final String syn : getSynonyms() ) {
122             t.getSynonyms().add( syn );
123         }
124         if ( getIdentifier() != null ) {
125             t.setIdentifier( ( Identifier ) getIdentifier().copy() );
126         }
127         else {
128             t.setIdentifier( null );
129         }
130         try {
131             t.setRank( new String( getRank() ) );
132         }
133         catch ( final PhyloXmlDataFormatException e ) {
134             e.printStackTrace();
135         }
136         if ( getUris() != null ) {
137             t.setUris( new ArrayList<Uri>() );
138             for( final Uri uri : getUris() ) {
139                 if ( uri != null ) {
140                     t.getUris().add( uri );
141                 }
142             }
143         }
144         if ( getLineage() != null ) {
145             t.setLineage( new ArrayList<String>() );
146             for( final String l : getLineage() ) {
147                 if ( l != null ) {
148                     t.getLineage().add( l );
149                 }
150             }
151         }
152         return t;
153     }
154
155     @Override
156     public boolean equals( final Object o ) {
157         if ( this == o ) {
158             return true;
159         }
160         else if ( o == null ) {
161             return false;
162         }
163         else if ( o.getClass() != this.getClass() ) {
164             throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
165                     + o.getClass() + "]" );
166         }
167         else {
168             return isEqual( ( Taxonomy ) o );
169         }
170     }
171
172     public String getAuthority() {
173         return _authority;
174     }
175
176     public String getCommonName() {
177         return _common_name;
178     }
179
180     public Identifier getIdentifier() {
181         return _identifier;
182     }
183
184     public String getRank() {
185         return _rank;
186     }
187
188     public String getScientificName() {
189         return _scientific_name;
190     }
191
192     public List<String> getSynonyms() {
193         if ( _synonyms == null ) {
194             _synonyms = new ArrayList<String>();
195         }
196         return _synonyms;
197     }
198
199     public String getTaxonomyCode() {
200         return _taxonomy_code;
201     }
202
203     @Override
204     public List<Uri> getUris() {
205         return _uris;
206     }
207
208     @Override
209     public int hashCode() {
210         if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
211             return getIdentifier().hashCode();
212         }
213         else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
214             return getTaxonomyCode().hashCode();
215         }
216         else if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
217             if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
218                 return ( getScientificName().toLowerCase() + getAuthority().toLowerCase() ).hashCode();
219             }
220             return getScientificName().toLowerCase().hashCode();
221         }
222         else {
223             return getCommonName().toLowerCase().hashCode();
224         }
225     }
226
227     public void init() {
228         setScientificName( "" );
229         setCommonName( "" );
230         setIdentifier( null );
231         try {
232             setRank( "" );
233         }
234         catch ( final PhyloXmlDataFormatException e ) {
235             e.printStackTrace();
236         }
237         try {
238             setTaxonomyCode( "" );
239         }
240         catch ( final PhyloXmlDataFormatException e ) {
241             e.printStackTrace();
242         }
243         setAuthority( "" );
244         setSynonyms( null );
245         setUris( null );
246         setLineage( null );
247     }
248
249     public boolean isEmpty() {
250         return ( ( getIdentifier() == null ) && ForesterUtil.isEmpty( getTaxonomyCode() )
251                 && ForesterUtil.isEmpty( getCommonName() ) && ForesterUtil.isEmpty( getScientificName() )
252                 && ForesterUtil.isEmpty( getRank() ) && ForesterUtil.isEmpty( _uris )
253                 && ForesterUtil.isEmpty( getAuthority() ) && ForesterUtil.isEmpty( _synonyms ) && ForesterUtil
254                 .isEmpty( _lineage ) );
255     }
256
257     /**
258      * 
259      * If this and taxonomy 'data' has an identifier, comparison will be based on that.
260      * Otherwise,  if this and taxonomy 'data' has a code, comparison will be based on that.
261      * Otherwise,  if Taxonomy 'data' has a scientific name, comparison will be
262      * based on that (case insensitive!).
263      * Otherwise,  if Taxonomy 'data' has a common  name, comparison will be
264      * based on that (case insensitive!).
265      * (Note. This is important and should not be change without a very good reason.)
266      * 
267      */
268     @Override
269     public boolean isEqual( final PhylogenyData data ) {
270         if ( this == data ) {
271             return true;
272         }
273         final Taxonomy tax = ( Taxonomy ) data;
274         if ( ( getIdentifier() != null ) && ( tax.getIdentifier() != null )
275                 && !ForesterUtil.isEmpty( getIdentifier().getValue() )
276                 && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
277             return getIdentifier().isEqual( tax.getIdentifier() );
278         }
279         else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
280             return getTaxonomyCode().equals( tax.getTaxonomyCode() );
281         }
282         else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
283             if ( !ForesterUtil.isEmpty( getAuthority() ) && !ForesterUtil.isEmpty( tax.getAuthority() ) ) {
284                 return ( getScientificName().equalsIgnoreCase( tax.getScientificName() ) )
285                         && ( getAuthority().equalsIgnoreCase( tax.getAuthority() ) );
286             }
287             return getScientificName().equalsIgnoreCase( tax.getScientificName() );
288         }
289         else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
290             return getCommonName().equalsIgnoreCase( tax.getCommonName() );
291         }
292         else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
293             return getScientificName().equalsIgnoreCase( tax.getCommonName() );
294         }
295         else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
296             return getCommonName().equalsIgnoreCase( tax.getScientificName() );
297         }
298         throw new RuntimeException( "comparison not possible with empty fields" );
299     }
300
301     public void setAuthority( final String authority ) {
302         _authority = authority;
303     }
304
305     public void setCommonName( final String common_name ) {
306         _common_name = common_name;
307     }
308
309     public void setIdentifier( final Identifier identifier ) {
310         _identifier = identifier;
311     }
312
313     public void setRank( final String rank ) throws PhyloXmlDataFormatException {
314         if ( !ForesterUtil.isEmpty( rank ) && !PhyloXmlUtil.TAXONOMY_RANKS_SET.contains( rank ) ) {
315             throw new PhyloXmlDataFormatException( "illegal rank: [" + rank + "]" );
316         }
317         _rank = rank;
318     }
319
320     public void setScientificName( final String scientific_name ) {
321         _scientific_name = scientific_name;
322     }
323
324     private void setSynonyms( final List<String> synonyms ) {
325         _synonyms = synonyms;
326     }
327
328     public void setTaxonomyCode( final String taxonomy_code ) throws PhyloXmlDataFormatException {
329       //  if ( !ForesterUtil.isEmpty( taxonomy_code )
330       //          && !PhyloXmlUtil.TAXOMONY_CODE_PATTERN.matcher( taxonomy_code ).matches() ) {
331       //      throw new PhyloXmlDataFormatException( "illegal taxonomy code: [" + taxonomy_code + "]" );
332       //  } 
333         //TODO
334         //FIXME
335         _taxonomy_code = taxonomy_code;
336     }
337
338     @Override
339     public void setUris( final List<Uri> uris ) {
340         _uris = uris;
341     }
342
343     @Override
344     public StringBuffer toNHX() {
345         final StringBuffer sb = new StringBuffer();
346         if ( getIdentifier() != null ) {
347             sb.append( ':' + NHXtags.TAXONOMY_ID );
348             sb.append( ForesterUtil.replaceIllegalNhxCharacters( getIdentifier().getValue() ) );
349         }
350         final StringBuffer species = new StringBuffer();
351         if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
352             species.append( ForesterUtil.replaceIllegalNhxCharacters( getTaxonomyCode() ) );
353         }
354         if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
355             ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
356             species.append( ForesterUtil.replaceIllegalNhxCharacters( getScientificName() ) );
357         }
358         if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
359             ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
360             species.append( ForesterUtil.replaceIllegalNhxCharacters( getCommonName() ) );
361         }
362         if ( species.length() > 0 ) {
363             sb.append( ':' + NHXtags.SPECIES_NAME );
364             sb.append( species );
365         }
366         return sb;
367     }
368
369     @Override
370     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
371         if ( isEmpty() ) {
372             return;
373         }
374         writer.write( ForesterUtil.LINE_SEPARATOR );
375         writer.write( indentation );
376         PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.TAXONOMY );
377         if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
378             getIdentifier().toPhyloXML( writer, level, indentation );
379         }
380         if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
381             PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_CODE, getTaxonomyCode(), indentation );
382         }
383         if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
384             PhylogenyDataUtil.appendElement( writer,
385                                              PhyloXmlMapping.TAXONOMY_SCIENTIFIC_NAME,
386                                              getScientificName(),
387                                              indentation );
388         }
389         if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
390             PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_AUTHORITY, getAuthority(), indentation );
391         }
392         if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
393             PhylogenyDataUtil
394                     .appendElement( writer, PhyloXmlMapping.TAXONOMY_COMMON_NAME, getCommonName(), indentation );
395         }
396         if ( _synonyms != null ) {
397             for( final String syn : getSynonyms() ) {
398                 if ( !ForesterUtil.isEmpty( syn ) ) {
399                     PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_SYNONYM, syn, indentation );
400                 }
401             }
402         }
403         if ( !ForesterUtil.isEmpty( getRank() ) ) {
404             PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_RANK, getRank(), indentation );
405         }
406         if ( getUris() != null ) {
407             for( final Uri uri : getUris() ) {
408                 if ( uri != null ) {
409                     uri.toPhyloXML( writer, level, indentation );
410                 }
411             }
412         }
413         writer.write( ForesterUtil.LINE_SEPARATOR );
414         writer.write( indentation );
415         PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.TAXONOMY );
416     }
417
418     @Override
419     public String toString() {
420         return asText().toString();
421     }
422
423     @Override
424     public int compareTo( final Taxonomy o ) {
425         if ( equals( o ) ) {
426             return 0;
427         }
428         else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( o.getScientificName() ) ) {
429             return getScientificName().compareToIgnoreCase( o.getScientificName() );
430         }
431         else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( o.getCommonName() ) ) {
432             return getCommonName().compareToIgnoreCase( o.getCommonName() );
433         }
434         else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( o.getTaxonomyCode() ) ) {
435             return getTaxonomyCode().compareToIgnoreCase( o.getTaxonomyCode() );
436         }
437         return 0;
438     }
439
440     public void setLineage( final List<String> lineage ) {
441         _lineage = lineage;
442     }
443
444     public List<String> getLineage() {
445         return _lineage;
446     }
447 }