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