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