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