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