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 if ( _distributions == null ) {
184 return _distributions.get( index );
187 public List<Distribution> getDistributions() {
188 return _distributions;
191 public Event getEvent() {
195 public PropertiesMap getProperties() {
200 * Convenience method -- always returns the first Reference.
205 public Reference getReference() {
206 return getReference( 0 );
209 public Reference getReference( final int index ) {
210 if ( _references == null ) {
213 return _references.get( index );
216 public List<Reference> getReferences() {
221 * Convenience method -- always returns the first Sequence.
225 public Sequence getSequence() {
226 return getSequence( 0 );
229 public Sequence getSequence( final int index ) {
230 if ( _sequences == null ) {
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 if ( _taxonomies == null ) {
258 return _taxonomies.get( index );
262 public boolean isEqual( final PhylogenyData data ) {
263 throw new NoSuchMethodError();
266 public boolean isHasBinaryCharacters() {
267 return getBinaryCharacters() != null;
270 public boolean isEmpty() {
271 return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
272 && !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
276 public boolean isHasDate() {
277 return ( getDate() != null )
278 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
279 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
280 .isEmpty( getDate().getUnit() ) );
283 public boolean isHasDistribution() {
284 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
285 .isEmpty( getDistribution().getDesc() ) )
286 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
287 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
290 public boolean isHasEvent() {
291 return getEvent() != null;
294 public boolean isHasProperties() {
295 return ( getProperties() != null ) && ( getProperties().size() > 0 );
298 public boolean isHasReference() {
299 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
300 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
301 .getDescription() ) );
304 public boolean isHasSequence() {
305 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
308 public boolean isHasTaxonomy() {
309 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
312 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
313 _binary_characters = binary_characters;
316 public void setDate( final Date date ) {
321 * Convenience method -- always sets the first Distribution.
324 public void setDistribution( final Distribution distribution ) {
325 if ( _distributions == null ) {
326 _distributions = new ArrayList<Distribution>();
328 if ( _distributions.size() == 0 ) {
329 _distributions.add( distribution );
332 _distributions.set( 0, distribution );
336 public void setDistribution( final int index, final Distribution distribution ) {
337 if ( _distributions == null ) {
338 _distributions = new ArrayList<Distribution>();
340 _distributions.set( index, distribution );
343 private void setDistributions( final List<Distribution> distributions ) {
344 _distributions = distributions;
347 public void setEvent( final Event event ) {
351 public void setProperties( final PropertiesMap custom_data ) {
352 _properties = custom_data;
355 public void setReference( final int index, final Reference reference ) {
356 if ( _references == null ) {
357 _references = new ArrayList<Reference>();
359 _references.set( index, reference );
363 * Convenience method -- always sets the first Reference.
366 public void setReference( final Reference reference ) {
367 if ( _references == null ) {
368 _references = new ArrayList<Reference>();
370 if ( _references.size() == 0 ) {
371 _references.add( reference );
374 _references.set( 0, reference );
378 private void setReferences( final List<Reference> references ) {
379 _references = references;
382 public void setSequence( final int index, final Sequence sequence ) {
383 if ( _sequences == null ) {
384 _sequences = new ArrayList<Sequence>();
386 _sequences.set( index, sequence );
390 * Convenience method -- always sets the first Sequence.
393 public void setSequence( final Sequence sequence ) {
394 if ( _sequences == null ) {
395 _sequences = new ArrayList<Sequence>();
397 if ( _sequences.size() == 0 ) {
398 _sequences.add( sequence );
401 _sequences.set( 0, sequence );
405 private void setSequences( final List<Sequence> sequences ) {
406 _sequences = sequences;
409 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
410 _taxonomies = taxonomies;
413 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
414 if ( _taxonomies == null ) {
415 _taxonomies = new ArrayList<Taxonomy>();
417 _taxonomies.set( index, taxonomy );
421 * Convenience method -- always sets the first Taxonomy.
424 public void setTaxonomy( final Taxonomy taxonomy ) {
425 if ( _taxonomies == null ) {
426 _taxonomies = new ArrayList<Taxonomy>();
428 if ( _taxonomies.size() == 0 ) {
429 _taxonomies.add( taxonomy );
432 _taxonomies.set( 0, taxonomy );
437 public StringBuffer toNHX() {
438 final StringBuffer sb = new StringBuffer();
439 if ( isHasTaxonomy() ) {
440 sb.append( getTaxonomy().toNHX() );
442 if ( isHasSequence() ) {
443 sb.append( getSequence().toNHX() );
445 if ( isHasEvent() ) {
446 sb.append( getEvent().toNHX() );
452 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
453 if ( isHasTaxonomy() ) {
454 for( final Taxonomy t : getTaxonomies() ) {
455 if ( !t.isEmpty() ) {
456 t.toPhyloXML( writer, level, indentation );
460 if ( isHasSequence() ) {
461 for( final Sequence s : getSequences() ) {
462 if ( !s.isEmpty() ) {
463 s.toPhyloXML( writer, level, indentation );
467 if ( isHasEvent() ) {
468 getEvent().toPhyloXML( writer, level, indentation );
470 if ( isHasBinaryCharacters() ) {
471 getBinaryCharacters().toPhyloXML( writer, level, indentation );
473 if ( isHasDistribution() ) {
474 for( final Distribution d : getDistributions() ) {
475 d.toPhyloXML( writer, level, indentation );
479 getDate().toPhyloXML( writer, level, indentation );
481 if ( isHasReference() ) {
482 for( final Reference r : getReferences() ) {
483 r.toPhyloXML( writer, level, indentation );
486 if ( isHasProperties() ) {
487 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
489 if ( ( level == 0 ) && ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
490 getNodeVisualData().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
492 if ( ( getVector() != null )
493 && !getVector().isEmpty()
494 && ( ( getProperties() == null ) || getProperties()
495 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
496 final List<Property> ps = vectorToProperties( getVector() );
497 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
498 for( final Property p : ps ) {
499 p.toPhyloXML( writer, level, my_indent );
504 private List<Property> vectorToProperties( final List<Double> vector ) {
505 final List<Property> properties = new ArrayList<Property>();
506 for( int i = 0; i < vector.size(); ++i ) {
507 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
508 String.valueOf( vector.get( i ) ),
510 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
516 public void setVector( final List<Double> vector ) {
520 public List<Double> getVector() {
524 public String getNodeName() {
528 public void setNodeName( final String node_name ) {
529 _node_name = node_name;
532 public void setNodeVisualData( final NodeVisualData node_visual_data ) {
533 _node_visual_data = node_visual_data;
536 public NodeVisualData getNodeVisualData() {
537 return _node_visual_data;