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