in progress
[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 PropertiesMap      _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( ( PropertiesMap ) 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         return _distributions.get( index );
182     }
183
184     public List<Distribution> getDistributions() {
185         return _distributions;
186     }
187
188     public Event getEvent() {
189         return _event;
190     }
191
192     public PropertiesMap getProperties() {
193         return _properties;
194     }
195
196     /**
197      * Convenience method -- always returns the first Reference.
198      *
199      *  @return Reference
200      *
201      */
202     public Reference getReference() {
203         return getReference( 0 );
204     }
205
206     public Reference getReference( final int index ) {
207         return _references.get( index );
208     }
209
210     public List<Reference> getReferences() {
211         return _references;
212     }
213
214     /**
215      * Convenience method -- always returns the first Sequence.
216      *
217      * @return Sequence
218      */
219     public Sequence getSequence() {
220         return getSequence( 0 );
221     }
222
223     public Sequence getSequence( final int index ) {
224         return _sequences.get( index );
225     }
226
227     public List<Sequence> getSequences() {
228         return _sequences;
229     }
230
231     public List<Taxonomy> getTaxonomies() {
232         return _taxonomies;
233     }
234
235     /**
236      * Convenience method -- always returns the first Taxonomy.
237      *
238      * @return  Taxonomy
239      *
240      */
241     public Taxonomy getTaxonomy() {
242         return getTaxonomy( 0 );
243     }
244
245     public Taxonomy getTaxonomy( final int index ) {
246         return _taxonomies.get( index );
247     }
248
249     @Override
250     public boolean isEqual( final PhylogenyData data ) {
251         throw new NoSuchMethodError();
252     }
253
254     public boolean isHasBinaryCharacters() {
255         return getBinaryCharacters() != null;
256     }
257
258     public boolean isEmpty() {
259         return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
260                 && !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
261                         .isEmpty() ) );
262     }
263
264     public boolean isHasDate() {
265         return ( getDate() != null )
266                 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
267                         || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
268                         .isEmpty( getDate().getUnit() ) );
269     }
270
271     public boolean isHasDistribution() {
272         return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
273                 .isEmpty( getDistribution().getDesc() ) )
274                 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
275                         .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
276     }
277
278     public boolean isHasEvent() {
279         return getEvent() != null;
280     }
281
282     public boolean isHasProperties() {
283         return ( getProperties() != null ) && ( getProperties().size() > 0 );
284     }
285
286     public boolean isHasReference() {
287         return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
288                 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
289                                                                                                 .getDescription() ) );
290     }
291
292     public boolean isHasSequence() {
293         return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
294     }
295
296     public boolean isHasTaxonomy() {
297         return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
298     }
299
300     public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
301         _binary_characters = binary_characters;
302     }
303
304     public void setDate( final Date date ) {
305         _date = date;
306     }
307
308     /**
309      * Convenience method -- always sets the first Distribution.
310      *
311      */
312     public void setDistribution( final Distribution distribution ) {
313         if ( _distributions == null ) {
314             _distributions = new ArrayList<Distribution>();
315         }
316         if ( _distributions.size() == 0 ) {
317             _distributions.add( distribution );
318         }
319         else {
320             _distributions.set( 0, distribution );
321         }
322     }
323
324     public void setDistribution( final int index, final Distribution distribution ) {
325         if ( _distributions == null ) {
326             _distributions = new ArrayList<Distribution>();
327         }
328         _distributions.set( index, distribution );
329     }
330
331     private void setDistributions( final List<Distribution> distributions ) {
332         _distributions = distributions;
333     }
334
335     public void setEvent( final Event event ) {
336         _event = event;
337     }
338
339     public void setProperties( final PropertiesMap custom_data ) {
340         _properties = custom_data;
341     }
342
343     public void setReference( final int index, final Reference reference ) {
344         if ( _references == null ) {
345             _references = new ArrayList<Reference>();
346         }
347         _references.set( index, reference );
348     }
349
350     /**
351      * Convenience method -- always sets the first Reference.
352      *
353      */
354     public void setReference( final Reference reference ) {
355         if ( _references == null ) {
356             _references = new ArrayList<Reference>();
357         }
358         if ( _references.size() == 0 ) {
359             _references.add( reference );
360         }
361         else {
362             _references.set( 0, reference );
363         }
364     }
365
366     private void setReferences( final List<Reference> references ) {
367         _references = references;
368     }
369
370     public void setSequence( final int index, final Sequence sequence ) {
371         if ( _sequences == null ) {
372             _sequences = new ArrayList<Sequence>();
373         }
374         _sequences.set( index, sequence );
375     }
376
377     /**
378      * Convenience method -- always sets the first Sequence.
379      *
380      */
381     public void setSequence( final Sequence sequence ) {
382         if ( _sequences == null ) {
383             _sequences = new ArrayList<Sequence>();
384         }
385         if ( _sequences.size() == 0 ) {
386             _sequences.add( sequence );
387         }
388         else {
389             _sequences.set( 0, sequence );
390         }
391     }
392
393     private void setSequences( final List<Sequence> sequences ) {
394         _sequences = sequences;
395     }
396
397     private void setTaxonomies( final List<Taxonomy> taxonomies ) {
398         _taxonomies = taxonomies;
399     }
400
401     public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
402         if ( _taxonomies == null ) {
403             _taxonomies = new ArrayList<Taxonomy>();
404         }
405         _taxonomies.set( index, taxonomy );
406     }
407
408     /**
409      * Convenience method -- always sets the first Taxonomy.
410      *
411      */
412     public void setTaxonomy( final Taxonomy taxonomy ) {
413         if ( _taxonomies == null ) {
414             _taxonomies = new ArrayList<Taxonomy>();
415         }
416         if ( _taxonomies.size() == 0 ) {
417             _taxonomies.add( taxonomy );
418         }
419         else {
420             _taxonomies.set( 0, taxonomy );
421         }
422     }
423
424     @Override
425     public StringBuffer toNHX() {
426         final StringBuffer sb = new StringBuffer();
427         if ( isHasTaxonomy() ) {
428             sb.append( getTaxonomy().toNHX() );
429         }
430         if ( isHasSequence() ) {
431             sb.append( getSequence().toNHX() );
432         }
433         if ( isHasEvent() ) {
434             sb.append( getEvent().toNHX() );
435         }
436         return sb;
437     }
438
439     @Override
440     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
441         if ( isHasTaxonomy() ) {
442             for( final Taxonomy t : getTaxonomies() ) {
443                 if ( !t.isEmpty() ) {
444                     t.toPhyloXML( writer, level, indentation );
445                 }
446             }
447         }
448         if ( isHasSequence() ) {
449             for( final Sequence s : getSequences() ) {
450                 if ( !s.isEmpty() ) {
451                     s.toPhyloXML( writer, level, indentation );
452                 }
453             }
454         }
455         if ( isHasEvent() ) {
456             getEvent().toPhyloXML( writer, level, indentation );
457         }
458         if ( isHasBinaryCharacters() ) {
459             getBinaryCharacters().toPhyloXML( writer, level, indentation );
460         }
461         if ( isHasDistribution() ) {
462             for( final Distribution d : getDistributions() ) {
463                 d.toPhyloXML( writer, level, indentation );
464             }
465         }
466         if ( isHasDate() ) {
467             getDate().toPhyloXML( writer, level, indentation );
468         }
469         if ( isHasReference() ) {
470             for( final Reference r : getReferences() ) {
471                 r.toPhyloXML( writer, level, indentation );
472             }
473         }
474         if ( isHasProperties() ) {
475             getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
476         }
477         if ( ( level == 0 ) && ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
478             getNodeVisualData().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
479         }
480         if ( ( getVector() != null )
481                 && !getVector().isEmpty()
482                 && ( ( getProperties() == null ) || getProperties()
483                         .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
484             final List<Property> ps = vectorToProperties( getVector() );
485             final String my_indent = indentation.substring( 0, indentation.length() - 2 );
486             for( final Property p : ps ) {
487                 p.toPhyloXML( writer, level, my_indent );
488             }
489         }
490     }
491
492     private List<Property> vectorToProperties( final List<Double> vector ) {
493         final List<Property> properties = new ArrayList<Property>();
494         for( int i = 0; i < vector.size(); ++i ) {
495             properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
496                                           String.valueOf( vector.get( i ) ),
497                                           "",
498                                           PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
499                                           AppliesTo.NODE ) );
500         }
501         return properties;
502     }
503
504     public void setVector( final List<Double> vector ) {
505         _vector = vector;
506     }
507
508     public List<Double> getVector() {
509         return _vector;
510     }
511
512     public String getNodeName() {
513         return _node_name;
514     }
515
516     public void setNodeName( final String node_name ) {
517         _node_name = node_name;
518     }
519
520     public void setNodeVisualData( final NodeVisualData node_visual_data ) {
521         _node_visual_data = node_visual_data;
522     }
523
524     public NodeVisualData getNodeVisualData() {
525         return _node_visual_data;
526     }
527 }