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