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