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 private String _node_name;
43 private List<Sequence> _sequences;
44 private List<Taxonomy> _taxonomies;
45 private List<Distribution> _distributions;
47 private BinaryCharacters _binary_characters;
48 private PropertiesMap _properties;
49 private List<Reference> _references;
50 private List<Double> _vector;
51 private NodeVisualData _node_visual_data;
62 _distributions = null;
64 _binary_characters = null;
68 _node_visual_data = null;
71 public void addDistribution( final Distribution distribution ) {
72 if ( _distributions == null ) {
73 _distributions = new ArrayList<Distribution>();
75 _distributions.add( distribution );
78 public void addReference( final Reference reference ) {
79 if ( _references == null ) {
80 _references = new ArrayList<Reference>();
82 _references.add( reference );
85 public void addSequence( final Sequence sequence ) {
86 if ( _sequences == null ) {
87 _sequences = new ArrayList<Sequence>();
89 _sequences.add( sequence );
92 public void addTaxonomy( final Taxonomy taxonomy ) {
93 if ( _taxonomies == null ) {
94 _taxonomies = new ArrayList<Taxonomy>();
96 _taxonomies.add( taxonomy );
100 public StringBuffer asSimpleText() {
101 throw new UnsupportedOperationException();
105 public StringBuffer asText() {
106 throw new UnsupportedOperationException();
110 public PhylogenyData copy() {
111 final NodeData new_data = new NodeData();
112 new_data.setNodeName( getNodeName() );
113 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
114 new_data.setSequences( new ArrayList<Sequence>() );
115 for( final Sequence s : getSequences() ) {
117 new_data.addSequence( ( Sequence ) s.copy() );
121 if ( isHasEvent() ) {
122 new_data.setEvent( ( Event ) getEvent().copy() );
124 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
125 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
126 for( final Taxonomy t : getTaxonomies() ) {
128 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
132 if ( isHasBinaryCharacters() ) {
133 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
135 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
136 new_data.setReferences( new ArrayList<Reference>() );
137 for( final Reference r : getReferences() ) {
139 new_data.addReference( ( Reference ) r.copy() );
143 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
144 new_data.setDistributions( new ArrayList<Distribution>() );
145 for( final Distribution d : getDistributions() ) {
147 new_data.addDistribution( ( Distribution ) d.copy() );
151 if ( ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
152 new_data.setNodeVisualData( ( NodeVisualData ) getNodeVisualData().copy() );
155 new_data.setDate( ( Date ) getDate().copy() );
157 if ( isHasProperties() ) {
158 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
163 public BinaryCharacters getBinaryCharacters() {
164 return _binary_characters;
167 public Date getDate() {
172 * Convenience method -- always returns the first Distribution.
174 * @return Distribution
176 public Distribution getDistribution() {
177 return getDistribution( 0 );
180 public Distribution getDistribution( final int index ) {
181 return _distributions.get( index );
184 public List<Distribution> getDistributions() {
185 return _distributions;
188 public Event getEvent() {
192 public PropertiesMap getProperties() {
197 * Convenience method -- always returns the first Reference.
202 public Reference getReference() {
203 return getReference( 0 );
206 public Reference getReference( final int index ) {
207 return _references.get( index );
210 public List<Reference> getReferences() {
215 * Convenience method -- always returns the first Sequence.
219 public Sequence getSequence() {
220 return getSequence( 0 );
223 public Sequence getSequence( final int index ) {
224 return _sequences.get( index );
227 public List<Sequence> getSequences() {
231 public List<Taxonomy> getTaxonomies() {
236 * Convenience method -- always returns the first Taxonomy.
241 public Taxonomy getTaxonomy() {
242 return getTaxonomy( 0 );
245 public Taxonomy getTaxonomy( final int index ) {
246 return _taxonomies.get( index );
250 public boolean isEqual( final PhylogenyData data ) {
251 throw new NoSuchMethodError();
254 public boolean isHasBinaryCharacters() {
255 return getBinaryCharacters() != null;
258 public boolean isEmpty() {
259 return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
260 && !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
264 public boolean isHasDate() {
265 return ( getDate() != null )
266 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
267 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
268 .isEmpty( getDate().getUnit() ) );
271 public boolean isHasDistribution() {
272 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
273 .isEmpty( getDistribution().getDesc() ) )
274 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
275 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
278 public boolean isHasEvent() {
279 return getEvent() != null;
282 public boolean isHasProperties() {
283 return ( getProperties() != null ) && ( getProperties().size() > 0 );
286 public boolean isHasReference() {
287 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
288 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
289 .getDescription() ) );
292 public boolean isHasSequence() {
293 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
296 public boolean isHasTaxonomy() {
297 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
300 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
301 _binary_characters = binary_characters;
304 public void setDate( final Date date ) {
309 * Convenience method -- always sets the first Distribution.
312 public void setDistribution( final Distribution distribution ) {
313 if ( _distributions == null ) {
314 _distributions = new ArrayList<Distribution>();
316 if ( _distributions.size() == 0 ) {
317 _distributions.add( distribution );
320 _distributions.set( 0, distribution );
324 public void setDistribution( final int index, final Distribution distribution ) {
325 if ( _distributions == null ) {
326 _distributions = new ArrayList<Distribution>();
328 _distributions.set( index, distribution );
331 private void setDistributions( final List<Distribution> distributions ) {
332 _distributions = distributions;
335 public void setEvent( final Event event ) {
339 public void setProperties( final PropertiesMap custom_data ) {
340 _properties = custom_data;
343 public void setReference( final int index, final Reference reference ) {
344 if ( _references == null ) {
345 _references = new ArrayList<Reference>();
347 _references.set( index, reference );
351 * Convenience method -- always sets the first Reference.
354 public void setReference( final Reference reference ) {
355 if ( _references == null ) {
356 _references = new ArrayList<Reference>();
358 if ( _references.size() == 0 ) {
359 _references.add( reference );
362 _references.set( 0, reference );
366 private void setReferences( final List<Reference> references ) {
367 _references = references;
370 public void setSequence( final int index, final Sequence sequence ) {
371 if ( _sequences == null ) {
372 _sequences = new ArrayList<Sequence>();
374 _sequences.set( index, sequence );
378 * Convenience method -- always sets the first Sequence.
381 public void setSequence( final Sequence sequence ) {
382 if ( _sequences == null ) {
383 _sequences = new ArrayList<Sequence>();
385 if ( _sequences.size() == 0 ) {
386 _sequences.add( sequence );
389 _sequences.set( 0, sequence );
393 private void setSequences( final List<Sequence> sequences ) {
394 _sequences = sequences;
397 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
398 _taxonomies = taxonomies;
401 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
402 if ( _taxonomies == null ) {
403 _taxonomies = new ArrayList<Taxonomy>();
405 _taxonomies.set( index, taxonomy );
409 * Convenience method -- always sets the first Taxonomy.
412 public void setTaxonomy( final Taxonomy taxonomy ) {
413 if ( _taxonomies == null ) {
414 _taxonomies = new ArrayList<Taxonomy>();
416 if ( _taxonomies.size() == 0 ) {
417 _taxonomies.add( taxonomy );
420 _taxonomies.set( 0, taxonomy );
425 public StringBuffer toNHX() {
426 final StringBuffer sb = new StringBuffer();
427 if ( isHasTaxonomy() ) {
428 sb.append( getTaxonomy().toNHX() );
430 if ( isHasSequence() ) {
431 sb.append( getSequence().toNHX() );
433 if ( isHasEvent() ) {
434 sb.append( getEvent().toNHX() );
440 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
441 if ( isHasTaxonomy() ) {
442 for( final Taxonomy t : getTaxonomies() ) {
443 if ( !t.isEmpty() ) {
444 t.toPhyloXML( writer, level, indentation );
448 if ( isHasSequence() ) {
449 for( final Sequence s : getSequences() ) {
450 if ( !s.isEmpty() ) {
451 s.toPhyloXML( writer, level, indentation );
455 if ( isHasEvent() ) {
456 getEvent().toPhyloXML( writer, level, indentation );
458 if ( isHasBinaryCharacters() ) {
459 getBinaryCharacters().toPhyloXML( writer, level, indentation );
461 if ( isHasDistribution() ) {
462 for( final Distribution d : getDistributions() ) {
463 d.toPhyloXML( writer, level, indentation );
467 getDate().toPhyloXML( writer, level, indentation );
469 if ( isHasReference() ) {
470 for( final Reference r : getReferences() ) {
471 r.toPhyloXML( writer, level, indentation );
474 if ( isHasProperties() ) {
475 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
477 if ( ( level == 0 ) && ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
478 getNodeVisualData().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
480 if ( ( getVector() != null )
481 && !getVector().isEmpty()
482 && ( ( getProperties() == null ) || getProperties()
483 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
484 final List<Property> ps = vectorToProperties( getVector() );
485 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
486 for( final Property p : ps ) {
487 p.toPhyloXML( writer, level, my_indent );
492 private List<Property> vectorToProperties( final List<Double> vector ) {
493 final List<Property> properties = new ArrayList<Property>();
494 for( int i = 0; i < vector.size(); ++i ) {
495 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
496 String.valueOf( vector.get( i ) ),
498 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
504 public void setVector( final List<Double> vector ) {
508 public List<Double> getVector() {
512 public String getNodeName() {
516 public void setNodeName( final String node_name ) {
517 _node_name = node_name;
520 public void setNodeVisualData( final NodeVisualData node_visual_data ) {
521 _node_visual_data = node_visual_data;
524 public NodeVisualData getNodeVisualData() {
525 return _node_visual_data;