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