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,
55 private String _node_name;
57 private List<Sequence> _sequences;
58 private List<Taxonomy> _taxonomies;
59 private List<Distribution> _distributions;
61 private BinaryCharacters _binary_characters;
62 private PropertiesMap _properties;
63 private List<Reference> _references;
64 private List<Double> _vector;
65 private List<NodeVisualization> _node_visualizations;
75 public void addDistribution( final Distribution distribution ) {
76 if ( _distributions == null ) {
77 _distributions = new ArrayList<Distribution>();
79 _distributions.add( distribution );
82 public void addReference( final Reference reference ) {
83 if ( _references == null ) {
84 _references = new ArrayList<Reference>();
86 _references.add( reference );
89 public void addSequence( final Sequence sequence ) {
90 if ( _sequences == null ) {
91 _sequences = new ArrayList<Sequence>();
93 _sequences.add( sequence );
96 public void addTaxonomy( final Taxonomy taxonomy ) {
97 if ( _taxonomies == null ) {
98 _taxonomies = new ArrayList<Taxonomy>();
100 _taxonomies.add( taxonomy );
104 public StringBuffer asSimpleText() {
105 throw new UnsupportedOperationException();
109 public StringBuffer asText() {
110 throw new UnsupportedOperationException();
114 public PhylogenyData copy() {
115 final NodeData new_data = new NodeData();
116 new_data.setNodeName( getNodeName() );
117 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
118 new_data.setSequences( new ArrayList<Sequence>() );
119 for( final Sequence s : getSequences() ) {
121 new_data.addSequence( ( Sequence ) s.copy() );
125 if ( isHasEvent() ) {
126 new_data.setEvent( ( Event ) getEvent().copy() );
128 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
129 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
130 for( final Taxonomy t : getTaxonomies() ) {
132 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
136 if ( isHasBinaryCharacters() ) {
137 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
139 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
140 new_data.setReferences( new ArrayList<Reference>() );
141 for( final Reference r : getReferences() ) {
143 new_data.addReference( ( Reference ) r.copy() );
147 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
148 new_data.setDistributions( new ArrayList<Distribution>() );
149 for( final Distribution d : getDistributions() ) {
151 new_data.addDistribution( ( Distribution ) d.copy() );
155 if ( ( getNodeVisualizations() != null ) && ( getNodeVisualizations().size() > 0 ) ) {
156 new_data.setNodeVisualizations( new ArrayList<NodeVisualization>() );
157 for( final NodeVisualization v : getNodeVisualizations() ) {
159 new_data.getNodeVisualizations().add( ( NodeVisualization ) v.copy() );
164 new_data.setDate( ( Date ) getDate().copy() );
166 if ( isHasProperties() ) {
167 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
172 public BinaryCharacters getBinaryCharacters() {
173 return _binary_characters;
176 public Date getDate() {
181 * Convenience method -- always returns the first Distribution.
183 * @return Distribution
185 public Distribution getDistribution() {
186 return getDistribution( 0 );
189 public Distribution getDistribution( final int index ) {
190 return _distributions.get( index );
193 public List<Distribution> getDistributions() {
194 return _distributions;
197 public Event getEvent() {
201 public PropertiesMap getProperties() {
206 * Convenience method -- always returns the first Reference.
211 public Reference getReference() {
212 return getReference( 0 );
215 public Reference getReference( final int index ) {
216 return _references.get( index );
219 public List<Reference> getReferences() {
224 * Convenience method -- always returns the first Sequence.
228 public Sequence getSequence() {
229 return getSequence( 0 );
232 public Sequence getSequence( final int index ) {
233 return _sequences.get( index );
236 public List<Sequence> getSequences() {
240 public List<Taxonomy> getTaxonomies() {
245 * Convenience method -- always returns the first Taxonomy.
250 public Taxonomy getTaxonomy() {
251 return getTaxonomy( 0 );
254 public Taxonomy getTaxonomy( final int index ) {
255 return _taxonomies.get( index );
259 public boolean isEqual( final PhylogenyData data ) {
260 throw new NoSuchMethodError();
263 public boolean isHasBinaryCharacters() {
264 return getBinaryCharacters() != null;
267 public boolean isEmpty() {
268 return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
269 && !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
273 public boolean isHasDate() {
274 return ( getDate() != null )
275 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
276 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
277 .isEmpty( getDate().getUnit() ) );
280 public boolean isHasDistribution() {
281 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
282 .isEmpty( getDistribution().getDesc() ) )
283 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
284 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
287 public boolean isHasEvent() {
288 return getEvent() != null;
291 public boolean isHasProperties() {
292 return ( getProperties() != null ) && ( getProperties().size() > 0 );
295 public boolean isHasReference() {
296 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
297 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
298 .getDescription() ) );
301 public boolean isHasSequence() {
302 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
305 public boolean isHasTaxonomy() {
306 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
309 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
310 _binary_characters = binary_characters;
313 public void setDate( final Date date ) {
318 * Convenience method -- always sets the first Distribution.
321 public void setDistribution( final Distribution distribution ) {
322 if ( _distributions == null ) {
323 _distributions = new ArrayList<Distribution>();
325 if ( _distributions.size() == 0 ) {
326 _distributions.add( distribution );
329 _distributions.set( 0, distribution );
333 public void setDistribution( final int index, final Distribution distribution ) {
334 if ( _distributions == null ) {
335 _distributions = new ArrayList<Distribution>();
337 _distributions.set( index, distribution );
340 private void setDistributions( final List<Distribution> distributions ) {
341 _distributions = distributions;
344 public void setEvent( final Event event ) {
348 public void setProperties( final PropertiesMap custom_data ) {
349 _properties = custom_data;
352 public void setReference( final int index, final Reference reference ) {
353 if ( _references == null ) {
354 _references = new ArrayList<Reference>();
356 _references.set( index, reference );
360 * Convenience method -- always sets the first Reference.
363 public void setReference( final Reference reference ) {
364 if ( _references == null ) {
365 _references = new ArrayList<Reference>();
367 if ( _references.size() == 0 ) {
368 _references.add( reference );
371 _references.set( 0, reference );
375 private void setReferences( final List<Reference> references ) {
376 _references = references;
379 public void setSequence( final int index, final Sequence sequence ) {
380 if ( _sequences == null ) {
381 _sequences = new ArrayList<Sequence>();
383 _sequences.set( index, sequence );
387 * Convenience method -- always sets the first Sequence.
390 public void setSequence( final Sequence sequence ) {
391 if ( _sequences == null ) {
392 _sequences = new ArrayList<Sequence>();
394 if ( _sequences.size() == 0 ) {
395 _sequences.add( sequence );
398 _sequences.set( 0, sequence );
402 private void setSequences( final List<Sequence> sequences ) {
403 _sequences = sequences;
406 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
407 _taxonomies = taxonomies;
410 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
411 if ( _taxonomies == null ) {
412 _taxonomies = new ArrayList<Taxonomy>();
414 _taxonomies.set( index, taxonomy );
418 * Convenience method -- always sets the first Taxonomy.
421 public void setTaxonomy( final Taxonomy taxonomy ) {
422 if ( _taxonomies == null ) {
423 _taxonomies = new ArrayList<Taxonomy>();
425 if ( _taxonomies.size() == 0 ) {
426 _taxonomies.add( taxonomy );
429 _taxonomies.set( 0, taxonomy );
434 public StringBuffer toNHX() {
435 final StringBuffer sb = new StringBuffer();
436 if ( isHasTaxonomy() ) {
437 sb.append( getTaxonomy().toNHX() );
439 if ( isHasSequence() ) {
440 sb.append( getSequence().toNHX() );
442 if ( isHasEvent() ) {
443 sb.append( getEvent().toNHX() );
449 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
450 if ( isHasTaxonomy() ) {
451 for( final Taxonomy t : getTaxonomies() ) {
452 if ( !t.isEmpty() ) {
453 t.toPhyloXML( writer, level, indentation );
457 if ( isHasSequence() ) {
458 for( final Sequence s : getSequences() ) {
459 if ( !s.isEmpty() ) {
460 s.toPhyloXML( writer, level, indentation );
464 if ( isHasEvent() ) {
465 getEvent().toPhyloXML( writer, level, indentation );
467 if ( isHasBinaryCharacters() ) {
468 getBinaryCharacters().toPhyloXML( writer, level, indentation );
470 if ( isHasDistribution() ) {
471 for( final Distribution d : getDistributions() ) {
472 d.toPhyloXML( writer, level, indentation );
476 getDate().toPhyloXML( writer, level, indentation );
478 if ( isHasReference() ) {
479 for( final Reference r : getReferences() ) {
480 r.toPhyloXML( writer, level, indentation );
483 if ( isHasProperties() ) {
484 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
486 if ( ( getVector() != null )
487 && !getVector().isEmpty()
488 && ( ( getProperties() == null ) || getProperties()
489 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
490 final List<Property> ps = vectorToProperties( getVector() );
491 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
492 for( final Property p : ps ) {
493 p.toPhyloXML( writer, level, my_indent );
498 private List<Property> vectorToProperties( final List<Double> vector ) {
499 final List<Property> properties = new ArrayList<Property>();
500 for( int i = 0; i < vector.size(); ++i ) {
501 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
502 String.valueOf( vector.get( i ) ),
504 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
510 public void setVector( final List<Double> vector ) {
514 public List<Double> getVector() {
518 public String getNodeName() {
522 public void setNodeName( final String node_name ) {
523 _node_name = node_name;
526 public void setNodeVisualizations( final List<NodeVisualization> _node_visualizations ) {
527 this._node_visualizations = _node_visualizations;
530 public List<NodeVisualization> getNodeVisualizations() {
531 return _node_visualizations;