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