2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
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
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.
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.
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
25 // Contact: phylosoft @ gmail . com
26 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
28 package org.forester.phylogeny.data;
30 import java.io.IOException;
31 import java.io.Writer;
32 import java.util.ArrayList;
33 import java.util.List;
35 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
36 import org.forester.phylogeny.data.Property.AppliesTo;
37 import org.forester.util.ForesterUtil;
39 public class NodeData implements PhylogenyData {
41 public enum NODE_DATA {
48 SEQUENCE_MOL_SEQ_FASTA,
50 TAXONOMY_SCIENTIFIC_NAME,
57 DOMAINS_COLLAPSED_PER_PROTEIN;
58 public String toString() {
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();
80 private String _node_name;
82 private List<Sequence> _sequences;
83 private List<Taxonomy> _taxonomies;
84 private List<Distribution> _distributions;
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;
101 _distributions = null;
103 _binary_characters = null;
107 _node_visual_data = null;
110 public void addDistribution( final Distribution distribution ) {
111 if ( _distributions == null ) {
112 _distributions = new ArrayList<Distribution>();
114 _distributions.add( distribution );
117 public void addReference( final Reference reference ) {
118 if ( _references == null ) {
119 _references = new ArrayList<Reference>();
121 _references.add( reference );
124 public void addSequence( final Sequence sequence ) {
125 if ( _sequences == null ) {
126 _sequences = new ArrayList<Sequence>();
128 _sequences.add( sequence );
131 public void addTaxonomy( final Taxonomy taxonomy ) {
132 if ( _taxonomies == null ) {
133 _taxonomies = new ArrayList<Taxonomy>();
135 _taxonomies.add( taxonomy );
139 public StringBuffer asSimpleText() {
140 throw new UnsupportedOperationException();
144 public StringBuffer asText() {
145 throw new UnsupportedOperationException();
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() ) {
156 new_data.addSequence( ( Sequence ) s.copy() );
160 if ( isHasEvent() ) {
161 new_data.setEvent( ( Event ) getEvent().copy() );
163 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
164 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
165 for( final Taxonomy t : getTaxonomies() ) {
167 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
171 if ( isHasBinaryCharacters() ) {
172 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
174 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
175 new_data.setReferences( new ArrayList<Reference>() );
176 for( final Reference r : getReferences() ) {
178 new_data.addReference( ( Reference ) r.copy() );
182 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
183 new_data.setDistributions( new ArrayList<Distribution>() );
184 for( final Distribution d : getDistributions() ) {
186 new_data.addDistribution( ( Distribution ) d.copy() );
190 if ( ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
191 new_data.setNodeVisualData( ( NodeVisualData ) getNodeVisualData().copy() );
194 new_data.setDate( ( Date ) getDate().copy() );
196 if ( isHasProperties() ) {
197 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
202 public BinaryCharacters getBinaryCharacters() {
203 return _binary_characters;
206 public Date getDate() {
211 * Convenience method -- always returns the first Distribution.
213 * @return Distribution
215 public Distribution getDistribution() {
216 return getDistribution( 0 );
219 public Distribution getDistribution( final int index ) {
220 return _distributions.get( index );
223 public List<Distribution> getDistributions() {
224 return _distributions;
227 public Event getEvent() {
231 public PropertiesMap getProperties() {
236 * Convenience method -- always returns the first Reference.
241 public Reference getReference() {
242 return getReference( 0 );
245 public Reference getReference( final int index ) {
246 return _references.get( index );
249 public List<Reference> getReferences() {
254 * Convenience method -- always returns the first Sequence.
258 public Sequence getSequence() {
259 return getSequence( 0 );
262 public Sequence getSequence( final int index ) {
263 return _sequences.get( index );
266 public List<Sequence> getSequences() {
270 public List<Taxonomy> getTaxonomies() {
275 * Convenience method -- always returns the first Taxonomy.
280 public Taxonomy getTaxonomy() {
281 return getTaxonomy( 0 );
284 public Taxonomy getTaxonomy( final int index ) {
285 return _taxonomies.get( index );
289 public boolean isEqual( final PhylogenyData data ) {
290 throw new NoSuchMethodError();
293 public boolean isHasBinaryCharacters() {
294 return getBinaryCharacters() != null;
297 public boolean isEmpty() {
298 return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
299 && !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
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() ) );
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 ) ) ) );
317 public boolean isHasEvent() {
318 return getEvent() != null;
321 public boolean isHasProperties() {
322 return ( getProperties() != null ) && ( getProperties().size() > 0 );
325 public boolean isHasReference() {
326 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
327 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
328 .getDescription() ) );
331 public boolean isHasSequence() {
332 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
335 public boolean isHasTaxonomy() {
336 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
339 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
340 _binary_characters = binary_characters;
343 public void setDate( final Date date ) {
348 * Convenience method -- always sets the first Distribution.
351 public void setDistribution( final Distribution distribution ) {
352 if ( _distributions == null ) {
353 _distributions = new ArrayList<Distribution>();
355 if ( _distributions.size() == 0 ) {
356 _distributions.add( distribution );
359 _distributions.set( 0, distribution );
363 public void setDistribution( final int index, final Distribution distribution ) {
364 if ( _distributions == null ) {
365 _distributions = new ArrayList<Distribution>();
367 _distributions.set( index, distribution );
370 private void setDistributions( final List<Distribution> distributions ) {
371 _distributions = distributions;
374 public void setEvent( final Event event ) {
378 public void setProperties( final PropertiesMap custom_data ) {
379 _properties = custom_data;
382 public void setReference( final int index, final Reference reference ) {
383 if ( _references == null ) {
384 _references = new ArrayList<Reference>();
386 _references.set( index, reference );
390 * Convenience method -- always sets the first Reference.
393 public void setReference( final Reference reference ) {
394 if ( _references == null ) {
395 _references = new ArrayList<Reference>();
397 if ( _references.size() == 0 ) {
398 _references.add( reference );
401 _references.set( 0, reference );
405 private void setReferences( final List<Reference> references ) {
406 _references = references;
409 public void setSequence( final int index, final Sequence sequence ) {
410 if ( _sequences == null ) {
411 _sequences = new ArrayList<Sequence>();
413 _sequences.set( index, sequence );
417 * Convenience method -- always sets the first Sequence.
420 public void setSequence( final Sequence sequence ) {
421 if ( _sequences == null ) {
422 _sequences = new ArrayList<Sequence>();
424 if ( _sequences.size() == 0 ) {
425 _sequences.add( sequence );
428 _sequences.set( 0, sequence );
432 private void setSequences( final List<Sequence> sequences ) {
433 _sequences = sequences;
436 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
437 _taxonomies = taxonomies;
440 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
441 if ( _taxonomies == null ) {
442 _taxonomies = new ArrayList<Taxonomy>();
444 _taxonomies.set( index, taxonomy );
448 * Convenience method -- always sets the first Taxonomy.
451 public void setTaxonomy( final Taxonomy taxonomy ) {
452 if ( _taxonomies == null ) {
453 _taxonomies = new ArrayList<Taxonomy>();
455 if ( _taxonomies.size() == 0 ) {
456 _taxonomies.add( taxonomy );
459 _taxonomies.set( 0, taxonomy );
464 public StringBuffer toNHX() {
465 final StringBuffer sb = new StringBuffer();
466 if ( isHasTaxonomy() ) {
467 sb.append( getTaxonomy().toNHX() );
469 if ( isHasSequence() ) {
470 sb.append( getSequence().toNHX() );
472 if ( isHasEvent() ) {
473 sb.append( getEvent().toNHX() );
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 );
487 if ( isHasSequence() ) {
488 for( final Sequence s : getSequences() ) {
489 if ( !s.isEmpty() ) {
490 s.toPhyloXML( writer, level, indentation );
494 if ( isHasEvent() ) {
495 getEvent().toPhyloXML( writer, level, indentation );
497 if ( isHasBinaryCharacters() ) {
498 getBinaryCharacters().toPhyloXML( writer, level, indentation );
500 if ( isHasDistribution() ) {
501 for( final Distribution d : getDistributions() ) {
502 d.toPhyloXML( writer, level, indentation );
506 getDate().toPhyloXML( writer, level, indentation );
508 if ( isHasReference() ) {
509 for( final Reference r : getReferences() ) {
510 r.toPhyloXML( writer, level, indentation );
513 if ( isHasProperties() ) {
514 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
516 if ( ( level == 0 ) && ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
517 getNodeVisualData().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
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 );
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 ) ),
537 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
543 public void setVector( final List<Double> vector ) {
547 public List<Double> getVector() {
551 public String getNodeName() {
555 public void setNodeName( final String node_name ) {
556 _node_name = node_name;
559 public void setNodeVisualData( final NodeVisualData node_visual_data ) {
560 _node_visual_data = node_visual_data;
563 public NodeVisualData getNodeVisualData() {
564 return _node_visual_data;