Added getValues and write to DistanceMatrix interface for broader use
[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     private String             _node_name;
42     private Event              _event;
43     private List<Sequence>     _sequences;
44     private List<Taxonomy>     _taxonomies;
45     private List<Distribution> _distributions;
46     private Date               _date;
47     private BinaryCharacters   _binary_characters;
48     private PropertiesList      _properties;
49     private List<Reference>    _references;
50     private List<Double>       _vector;
51     private NodeVisualData     _node_visual_data;
52
53     public NodeData() {
54         init();
55     }
56
57     private void init() {
58         _node_name = "";
59         _event = null;
60         _sequences = null;
61         _taxonomies = null;
62         _distributions = null;
63         _date = null;
64         _binary_characters = null;
65         _properties = null;
66         _references = null;
67         _vector = null;
68         _node_visual_data = null;
69     }
70
71     public void addDistribution( final Distribution distribution ) {
72         if ( _distributions == null ) {
73             _distributions = new ArrayList<Distribution>();
74         }
75         _distributions.add( distribution );
76     }
77
78     public void addReference( final Reference reference ) {
79         if ( _references == null ) {
80             _references = new ArrayList<Reference>();
81         }
82         _references.add( reference );
83     }
84
85     public void addSequence( final Sequence sequence ) {
86         if ( _sequences == null ) {
87             _sequences = new ArrayList<Sequence>();
88         }
89         _sequences.add( sequence );
90     }
91
92     public void addTaxonomy( final Taxonomy taxonomy ) {
93         if ( _taxonomies == null ) {
94             _taxonomies = new ArrayList<Taxonomy>();
95         }
96         _taxonomies.add( taxonomy );
97     }
98
99     @Override
100     public StringBuffer asSimpleText() {
101         throw new UnsupportedOperationException();
102     }
103
104     @Override
105     public StringBuffer asText() {
106         throw new UnsupportedOperationException();
107     }
108
109     @Override
110     public PhylogenyData copy() {
111         final NodeData new_data = new NodeData();
112         new_data.setNodeName( getNodeName() );
113         if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
114             new_data.setSequences( new ArrayList<Sequence>() );
115             for( final Sequence s : getSequences() ) {
116                 if ( s != null ) {
117                     new_data.addSequence( ( Sequence ) s.copy() );
118                 }
119             }
120         }
121         if ( isHasEvent() ) {
122             new_data.setEvent( ( Event ) getEvent().copy() );
123         }
124         if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
125             new_data.setTaxonomies( new ArrayList<Taxonomy>() );
126             for( final Taxonomy t : getTaxonomies() ) {
127                 if ( t != null ) {
128                     new_data.addTaxonomy( ( Taxonomy ) t.copy() );
129                 }
130             }
131         }
132         if ( isHasBinaryCharacters() ) {
133             new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
134         }
135         if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
136             new_data.setReferences( new ArrayList<Reference>() );
137             for( final Reference r : getReferences() ) {
138                 if ( r != null ) {
139                     new_data.addReference( ( Reference ) r.copy() );
140                 }
141             }
142         }
143         if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
144             new_data.setDistributions( new ArrayList<Distribution>() );
145             for( final Distribution d : getDistributions() ) {
146                 if ( d != null ) {
147                     new_data.addDistribution( ( Distribution ) d.copy() );
148                 }
149             }
150         }
151         if ( ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
152             new_data.setNodeVisualData( ( NodeVisualData ) getNodeVisualData().copy() );
153         }
154         if ( isHasDate() ) {
155             new_data.setDate( ( Date ) getDate().copy() );
156         }
157         if ( isHasProperties() ) {
158             new_data.setProperties( ( PropertiesList ) getProperties().copy() );
159         }
160         return new_data;
161     }
162
163     public BinaryCharacters getBinaryCharacters() {
164         return _binary_characters;
165     }
166
167     public Date getDate() {
168         return _date;
169     }
170
171     /**
172      * Convenience method -- always returns the first Distribution.
173      *
174      * @return Distribution
175      */
176     public Distribution getDistribution() {
177         return getDistribution( 0 );
178     }
179
180     public Distribution getDistribution( final int index ) {
181         if ( _distributions == null ) {
182             return null;
183         }
184         return _distributions.get( index );
185     }
186
187     public List<Distribution> getDistributions() {
188         return _distributions;
189     }
190
191     public Event getEvent() {
192         return _event;
193     }
194
195     public PropertiesList getProperties() {
196         return _properties;
197     }
198
199     /**
200      * Convenience method -- always returns the first Reference.
201      *
202      *  @return Reference
203      *
204      */
205     public Reference getReference() {
206         return getReference( 0 );
207     }
208
209     public Reference getReference( final int index ) {
210         if ( _references == null ) {
211             return null;
212         }
213         return _references.get( index );
214     }
215
216     public List<Reference> getReferences() {
217         return _references;
218     }
219
220     /**
221      * Convenience method -- always returns the first Sequence.
222      *
223      * @return Sequence
224      */
225     public Sequence getSequence() {
226         return getSequence( 0 );
227     }
228
229     public Sequence getSequence( final int index ) {
230         if ( _sequences == null ) {
231             return null;
232         }
233         return _sequences.get( index );
234     }
235
236     public List<Sequence> getSequences() {
237         return _sequences;
238     }
239
240     public List<Taxonomy> getTaxonomies() {
241         return _taxonomies;
242     }
243
244     /**
245      * Convenience method -- always returns the first Taxonomy.
246      *
247      * @return  Taxonomy
248      *
249      */
250     public Taxonomy getTaxonomy() {
251         return getTaxonomy( 0 );
252     }
253
254     public Taxonomy getTaxonomy( final int index ) {
255         if ( _taxonomies == null ) {
256             return null;
257         }
258         return _taxonomies.get( index );
259     }
260
261     @Override
262     public boolean isEqual( final PhylogenyData data ) {
263         throw new NoSuchMethodError();
264     }
265
266     public boolean isHasBinaryCharacters() {
267         return getBinaryCharacters() != null;
268     }
269
270     public boolean isEmpty() {
271         return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
272                 && !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
273                         .isEmpty() ) );
274     }
275
276     public boolean isHasDate() {
277         return ( getDate() != null )
278                 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
279                         || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
280                         .isEmpty( getDate().getUnit() ) );
281     }
282
283     public boolean isHasDistribution() {
284         return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
285                 .isEmpty( getDistribution().getDesc() ) )
286                 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
287                         .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
288     }
289
290     public boolean isHasEvent() {
291         return getEvent() != null;
292     }
293
294     public boolean isHasProperties() {
295         return ( getProperties() != null ) && ( getProperties().size() > 0 );
296     }
297
298     public boolean isHasReference() {
299         return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
300                 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
301                                                                                                 .getDescription() ) );
302     }
303
304     public boolean isHasSequence() {
305         return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
306     }
307
308     public boolean isHasTaxonomy() {
309         return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
310     }
311
312     public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
313         _binary_characters = binary_characters;
314     }
315
316     public void setDate( final Date date ) {
317         _date = date;
318     }
319
320     /**
321      * Convenience method -- always sets the first Distribution.
322      *
323      */
324     public void setDistribution( final Distribution distribution ) {
325         if ( _distributions == null ) {
326             _distributions = new ArrayList<Distribution>();
327         }
328         if ( _distributions.size() == 0 ) {
329             _distributions.add( distribution );
330         }
331         else {
332             _distributions.set( 0, distribution );
333         }
334     }
335
336     public void setDistribution( final int index, final Distribution distribution ) {
337         if ( _distributions == null ) {
338             _distributions = new ArrayList<Distribution>();
339         }
340         _distributions.set( index, distribution );
341     }
342
343     private void setDistributions( final List<Distribution> distributions ) {
344         _distributions = distributions;
345     }
346
347     public void setEvent( final Event event ) {
348         _event = event;
349     }
350
351     public void setProperties( final PropertiesList custom_data ) {
352         _properties = custom_data;
353     }
354
355     public void setReference( final int index, final Reference reference ) {
356         if ( _references == null ) {
357             _references = new ArrayList<Reference>();
358         }
359         _references.set( index, reference );
360     }
361
362     /**
363      * Convenience method -- always sets the first Reference.
364      *
365      */
366     public void setReference( final Reference reference ) {
367         if ( _references == null ) {
368             _references = new ArrayList<Reference>();
369         }
370         if ( _references.size() == 0 ) {
371             _references.add( reference );
372         }
373         else {
374             _references.set( 0, reference );
375         }
376     }
377
378     private void setReferences( final List<Reference> references ) {
379         _references = references;
380     }
381
382     public void setSequence( final int index, final Sequence sequence ) {
383         if ( _sequences == null ) {
384             _sequences = new ArrayList<Sequence>();
385         }
386         _sequences.set( index, sequence );
387     }
388
389     /**
390      * Convenience method -- always sets the first Sequence.
391      *
392      */
393     public void setSequence( final Sequence sequence ) {
394         if ( _sequences == null ) {
395             _sequences = new ArrayList<Sequence>();
396         }
397         if ( _sequences.size() == 0 ) {
398             _sequences.add( sequence );
399         }
400         else {
401             _sequences.set( 0, sequence );
402         }
403     }
404
405     private void setSequences( final List<Sequence> sequences ) {
406         _sequences = sequences;
407     }
408
409     private void setTaxonomies( final List<Taxonomy> taxonomies ) {
410         _taxonomies = taxonomies;
411     }
412
413     public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
414         if ( _taxonomies == null ) {
415             _taxonomies = new ArrayList<Taxonomy>();
416         }
417         _taxonomies.set( index, taxonomy );
418     }
419
420     /**
421      * Convenience method -- always sets the first Taxonomy.
422      *
423      */
424     public void setTaxonomy( final Taxonomy taxonomy ) {
425         if ( _taxonomies == null ) {
426             _taxonomies = new ArrayList<Taxonomy>();
427         }
428         if ( _taxonomies.size() == 0 ) {
429             _taxonomies.add( taxonomy );
430         }
431         else {
432             _taxonomies.set( 0, taxonomy );
433         }
434     }
435
436     @Override
437     public StringBuffer toNHX() {
438         final StringBuffer sb = new StringBuffer();
439         if ( isHasTaxonomy() ) {
440             sb.append( getTaxonomy().toNHX() );
441         }
442         if ( isHasSequence() ) {
443             sb.append( getSequence().toNHX() );
444         }
445         if ( isHasEvent() ) {
446             sb.append( getEvent().toNHX() );
447         }
448         return sb;
449     }
450
451     @Override
452     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
453         if ( isHasTaxonomy() ) {
454             for( final Taxonomy t : getTaxonomies() ) {
455                 if ( !t.isEmpty() ) {
456                     t.toPhyloXML( writer, level, indentation );
457                 }
458             }
459         }
460         if ( isHasSequence() ) {
461             for( final Sequence s : getSequences() ) {
462                 if ( !s.isEmpty() ) {
463                     s.toPhyloXML( writer, level, indentation );
464                 }
465             }
466         }
467         if ( isHasEvent() ) {
468             getEvent().toPhyloXML( writer, level, indentation );
469         }
470         if ( isHasBinaryCharacters() ) {
471             getBinaryCharacters().toPhyloXML( writer, level, indentation );
472         }
473         if ( isHasDistribution() ) {
474             for( final Distribution d : getDistributions() ) {
475                 d.toPhyloXML( writer, level, indentation );
476             }
477         }
478         if ( isHasDate() ) {
479             getDate().toPhyloXML( writer, level, indentation );
480         }
481         if ( isHasReference() ) {
482             for( final Reference r : getReferences() ) {
483                 r.toPhyloXML( writer, level, indentation );
484             }
485         }
486         if ( isHasProperties() ) {
487             getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
488         }
489         if ( ( level == 0 ) && ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
490             getNodeVisualData().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
491         }
492         if ( ( getVector() != null )
493                 && !getVector().isEmpty()
494                 && ( ( getProperties() == null ) || getProperties()
495                         .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
496             final List<Property> ps = vectorToProperties( getVector() );
497             final String my_indent = indentation.substring( 0, indentation.length() - 2 );
498             for( final Property p : ps ) {
499                 p.toPhyloXML( writer, level, my_indent );
500             }
501         }
502     }
503
504     private List<Property> vectorToProperties( final List<Double> vector ) {
505         final List<Property> properties = new ArrayList<Property>();
506         for( int i = 0; i < vector.size(); ++i ) {
507             properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
508                                           String.valueOf( vector.get( i ) ),
509                                           "",
510                                           PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
511                                           AppliesTo.NODE ) );
512         }
513         return properties;
514     }
515
516     public void setVector( final List<Double> vector ) {
517         _vector = vector;
518     }
519
520     public List<Double> getVector() {
521         return _vector;
522     }
523
524     public String getNodeName() {
525         return _node_name;
526     }
527
528     public void setNodeName( final String node_name ) {
529         _node_name = node_name;
530     }
531
532     public void setNodeVisualData( final NodeVisualData node_visual_data ) {
533         _node_visual_data = node_visual_data;
534     }
535
536     public NodeVisualData getNodeVisualData() {
537         return _node_visual_data;
538     }
539 }