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