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