in progress (special coloring is still true)
[jalview.git] / forester / java / src / org / forester / phylogeny / data / NodeData.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 // Copyright (C) 2000-2001 Washington University School of Medicine
8 // and Howard Hughes Medical Institute
9 // All rights reserved
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 //
25 // Contact: phylosoft @ gmail . com
26 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27
28 package org.forester.phylogeny.data;
29
30 import java.io.IOException;
31 import java.io.Writer;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
36 import org.forester.phylogeny.data.Property.AppliesTo;
37 import org.forester.util.ForesterUtil;
38
39 public class NodeData implements PhylogenyData {
40
41     public enum NODE_DATA {
42         NODE_NAME,
43         EVENT,
44         SEQUENCE_NAME,
45         GENE_NAME,
46         SEQUENCE_SYMBOL,
47         SEQUENCE_MOL_SEQ,
48         SEQUENCE_MOL_SEQ_FASTA,
49         SEQUENCE_ACC,
50         TAXONOMY_SCIENTIFIC_NAME,
51         TAXONOMY_COMM0N_NAME,
52         TAXONOMY_CODE,
53         UNKNOWN;
54     }
55     private String             _node_name;
56     private Event              _event;
57     private List<Sequence>     _sequences;
58     private List<Taxonomy>     _taxonomies;
59     private List<Distribution> _distributions;
60     private Date               _date;
61     private BinaryCharacters   _binary_characters;
62     private PropertiesMap      _properties;
63     private List<Reference>    _references;
64     private List<Double>       _vector;
65     private NodeVisualData     _node_visual_data;
66
67     public NodeData() {
68         init();
69     }
70
71     private void init() {
72         _node_name = "";
73         _event = null;
74         _sequences = null;
75         _taxonomies = null;
76         _distributions = null;
77         _date = null;
78         _binary_characters = null;
79         _properties = null;
80         _references = null;
81         _vector = null;
82         _node_visual_data = null;
83     }
84
85     public void addDistribution( final Distribution distribution ) {
86         if ( _distributions == null ) {
87             _distributions = new ArrayList<Distribution>();
88         }
89         _distributions.add( distribution );
90     }
91
92     public void addReference( final Reference reference ) {
93         if ( _references == null ) {
94             _references = new ArrayList<Reference>();
95         }
96         _references.add( reference );
97     }
98
99     public void addSequence( final Sequence sequence ) {
100         if ( _sequences == null ) {
101             _sequences = new ArrayList<Sequence>();
102         }
103         _sequences.add( sequence );
104     }
105
106     public void addTaxonomy( final Taxonomy taxonomy ) {
107         if ( _taxonomies == null ) {
108             _taxonomies = new ArrayList<Taxonomy>();
109         }
110         _taxonomies.add( taxonomy );
111     }
112
113     @Override
114     public StringBuffer asSimpleText() {
115         throw new UnsupportedOperationException();
116     }
117
118     @Override
119     public StringBuffer asText() {
120         throw new UnsupportedOperationException();
121     }
122
123     @Override
124     public PhylogenyData copy() {
125         final NodeData new_data = new NodeData();
126         new_data.setNodeName( getNodeName() );
127         if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
128             new_data.setSequences( new ArrayList<Sequence>() );
129             for( final Sequence s : getSequences() ) {
130                 if ( s != null ) {
131                     new_data.addSequence( ( Sequence ) s.copy() );
132                 }
133             }
134         }
135         if ( isHasEvent() ) {
136             new_data.setEvent( ( Event ) getEvent().copy() );
137         }
138         if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
139             new_data.setTaxonomies( new ArrayList<Taxonomy>() );
140             for( final Taxonomy t : getTaxonomies() ) {
141                 if ( t != null ) {
142                     new_data.addTaxonomy( ( Taxonomy ) t.copy() );
143                 }
144             }
145         }
146         if ( isHasBinaryCharacters() ) {
147             new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
148         }
149         if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
150             new_data.setReferences( new ArrayList<Reference>() );
151             for( final Reference r : getReferences() ) {
152                 if ( r != null ) {
153                     new_data.addReference( ( Reference ) r.copy() );
154                 }
155             }
156         }
157         if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
158             new_data.setDistributions( new ArrayList<Distribution>() );
159             for( final Distribution d : getDistributions() ) {
160                 if ( d != null ) {
161                     new_data.addDistribution( ( Distribution ) d.copy() );
162                 }
163             }
164         }
165         if ( ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
166             new_data.setNodeVisualData( ( NodeVisualData ) getNodeVisualData().copy() );
167         }
168         if ( isHasDate() ) {
169             new_data.setDate( ( Date ) getDate().copy() );
170         }
171         if ( isHasProperties() ) {
172             new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
173         }
174         return new_data;
175     }
176
177     public BinaryCharacters getBinaryCharacters() {
178         return _binary_characters;
179     }
180
181     public Date getDate() {
182         return _date;
183     }
184
185     /**
186      * Convenience method -- always returns the first Distribution.
187      *  
188      * @return Distribution
189      */
190     public Distribution getDistribution() {
191         return getDistribution( 0 );
192     }
193
194     public Distribution getDistribution( final int index ) {
195         return _distributions.get( index );
196     }
197
198     public List<Distribution> getDistributions() {
199         return _distributions;
200     }
201
202     public Event getEvent() {
203         return _event;
204     }
205
206     public PropertiesMap getProperties() {
207         return _properties;
208     }
209
210     /**
211      * Convenience method -- always returns the first Reference.
212      * 
213      *  @return Reference
214      *  
215      */
216     public Reference getReference() {
217         return getReference( 0 );
218     }
219
220     public Reference getReference( final int index ) {
221         return _references.get( index );
222     }
223
224     public List<Reference> getReferences() {
225         return _references;
226     }
227
228     /**
229      * Convenience method -- always returns the first Sequence.
230      * 
231      * @return Sequence
232      */
233     public Sequence getSequence() {
234         return getSequence( 0 );
235     }
236
237     public Sequence getSequence( final int index ) {
238         return _sequences.get( index );
239     }
240
241     public List<Sequence> getSequences() {
242         return _sequences;
243     }
244
245     public List<Taxonomy> getTaxonomies() {
246         return _taxonomies;
247     }
248
249     /**
250      * Convenience method -- always returns the first Taxonomy.
251      * 
252      * @return  Taxonomy
253      * 
254      */
255     public Taxonomy getTaxonomy() {
256         return getTaxonomy( 0 );
257     }
258
259     public Taxonomy getTaxonomy( final int index ) {
260         return _taxonomies.get( index );
261     }
262
263     @Override
264     public boolean isEqual( final PhylogenyData data ) {
265         throw new NoSuchMethodError();
266     }
267
268     public boolean isHasBinaryCharacters() {
269         return getBinaryCharacters() != null;
270     }
271
272     public boolean isEmpty() {
273         return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
274                 && !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
275                 .isEmpty() ) );
276     }
277
278     public boolean isHasDate() {
279         return ( getDate() != null )
280                 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
281                         || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
282                             .isEmpty( getDate().getUnit() ) );
283     }
284
285     public boolean isHasDistribution() {
286         return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
287                 .isEmpty( getDistribution().getDesc() ) )
288                 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
289                 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
290     }
291
292     public boolean isHasEvent() {
293         return getEvent() != null;
294     }
295
296     public boolean isHasProperties() {
297         return ( getProperties() != null ) && ( getProperties().size() > 0 );
298     }
299
300     public boolean isHasReference() {
301         return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
302                 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
303                         .getDescription() ) );
304     }
305
306     public boolean isHasSequence() {
307         return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
308     }
309
310     public boolean isHasTaxonomy() {
311         return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
312     }
313
314     public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
315         _binary_characters = binary_characters;
316     }
317
318     public void setDate( final Date date ) {
319         _date = date;
320     }
321
322     /**
323      * Convenience method -- always sets the first Distribution.
324      * 
325      */
326     public void setDistribution( final Distribution distribution ) {
327         if ( _distributions == null ) {
328             _distributions = new ArrayList<Distribution>();
329         }
330         if ( _distributions.size() == 0 ) {
331             _distributions.add( distribution );
332         }
333         else {
334             _distributions.set( 0, distribution );
335         }
336     }
337
338     public void setDistribution( final int index, final Distribution distribution ) {
339         if ( _distributions == null ) {
340             _distributions = new ArrayList<Distribution>();
341         }
342         _distributions.set( index, distribution );
343     }
344
345     private void setDistributions( final List<Distribution> distributions ) {
346         _distributions = distributions;
347     }
348
349     public void setEvent( final Event event ) {
350         _event = event;
351     }
352
353     public void setProperties( final PropertiesMap custom_data ) {
354         _properties = custom_data;
355     }
356
357     public void setReference( final int index, final Reference reference ) {
358         if ( _references == null ) {
359             _references = new ArrayList<Reference>();
360         }
361         _references.set( index, reference );
362     }
363
364     /**
365      * Convenience method -- always sets the first Reference.
366      * 
367      */
368     public void setReference( final Reference reference ) {
369         if ( _references == null ) {
370             _references = new ArrayList<Reference>();
371         }
372         if ( _references.size() == 0 ) {
373             _references.add( reference );
374         }
375         else {
376             _references.set( 0, reference );
377         }
378     }
379
380     private void setReferences( final List<Reference> references ) {
381         _references = references;
382     }
383
384     public void setSequence( final int index, final Sequence sequence ) {
385         if ( _sequences == null ) {
386             _sequences = new ArrayList<Sequence>();
387         }
388         _sequences.set( index, sequence );
389     }
390
391     /**
392      * Convenience method -- always sets the first Sequence.
393      * 
394      */
395     public void setSequence( final Sequence sequence ) {
396         if ( _sequences == null ) {
397             _sequences = new ArrayList<Sequence>();
398         }
399         if ( _sequences.size() == 0 ) {
400             _sequences.add( sequence );
401         }
402         else {
403             _sequences.set( 0, sequence );
404         }
405     }
406
407     private void setSequences( final List<Sequence> sequences ) {
408         _sequences = sequences;
409     }
410
411     private void setTaxonomies( final List<Taxonomy> taxonomies ) {
412         _taxonomies = taxonomies;
413     }
414
415     public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
416         if ( _taxonomies == null ) {
417             _taxonomies = new ArrayList<Taxonomy>();
418         }
419         _taxonomies.set( index, taxonomy );
420     }
421
422     /**
423      * Convenience method -- always sets the first Taxonomy.
424      * 
425      */
426     public void setTaxonomy( final Taxonomy taxonomy ) {
427         if ( _taxonomies == null ) {
428             _taxonomies = new ArrayList<Taxonomy>();
429         }
430         if ( _taxonomies.size() == 0 ) {
431             _taxonomies.add( taxonomy );
432         }
433         else {
434             _taxonomies.set( 0, taxonomy );
435         }
436     }
437
438     @Override
439     public StringBuffer toNHX() {
440         final StringBuffer sb = new StringBuffer();
441         if ( isHasTaxonomy() ) {
442             sb.append( getTaxonomy().toNHX() );
443         }
444         if ( isHasSequence() ) {
445             sb.append( getSequence().toNHX() );
446         }
447         if ( isHasEvent() ) {
448             sb.append( getEvent().toNHX() );
449         }
450         return sb;
451     }
452
453     @Override
454     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
455         if ( isHasTaxonomy() ) {
456             for( final Taxonomy t : getTaxonomies() ) {
457                 if ( !t.isEmpty() ) {
458                     t.toPhyloXML( writer, level, indentation );
459                 }
460             }
461         }
462         if ( isHasSequence() ) {
463             for( final Sequence s : getSequences() ) {
464                 if ( !s.isEmpty() ) {
465                     s.toPhyloXML( writer, level, indentation );
466                 }
467             }
468         }
469         if ( isHasEvent() ) {
470             getEvent().toPhyloXML( writer, level, indentation );
471         }
472         if ( isHasBinaryCharacters() ) {
473             getBinaryCharacters().toPhyloXML( writer, level, indentation );
474         }
475         if ( isHasDistribution() ) {
476             for( final Distribution d : getDistributions() ) {
477                 d.toPhyloXML( writer, level, indentation );
478             }
479         }
480         if ( isHasDate() ) {
481             getDate().toPhyloXML( writer, level, indentation );
482         }
483         if ( isHasReference() ) {
484             for( final Reference r : getReferences() ) {
485                 r.toPhyloXML( writer, level, indentation );
486             }
487         }
488         if ( isHasProperties() ) {
489             getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
490         }
491         if ( ( level == 0 ) && ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
492             getNodeVisualData().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
493         }
494         if ( ( getVector() != null )
495                 && !getVector().isEmpty()
496                 && ( ( getProperties() == null ) || getProperties()
497                         .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
498             final List<Property> ps = vectorToProperties( getVector() );
499             final String my_indent = indentation.substring( 0, indentation.length() - 2 );
500             for( final Property p : ps ) {
501                 p.toPhyloXML( writer, level, my_indent );
502             }
503         }
504     }
505
506     private List<Property> vectorToProperties( final List<Double> vector ) {
507         final List<Property> properties = new ArrayList<Property>();
508         for( int i = 0; i < vector.size(); ++i ) {
509             properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
510                                           String.valueOf( vector.get( i ) ),
511                                           "",
512                                           PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
513                                           AppliesTo.NODE ) );
514         }
515         return properties;
516     }
517
518     public void setVector( final List<Double> vector ) {
519         _vector = vector;
520     }
521
522     public List<Double> getVector() {
523         return _vector;
524     }
525
526     public String getNodeName() {
527         return _node_name;
528     }
529
530     public void setNodeName( final String node_name ) {
531         _node_name = node_name;
532     }
533
534     public void setNodeVisualData( final NodeVisualData node_visual_data ) {
535         _node_visual_data = node_visual_data;
536     }
537
538     public NodeVisualData getNodeVisualData() {
539         return _node_visual_data;
540     }
541 }