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