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 NodeVisualData _node_visual_data;
76 _distributions = null;
78 _binary_characters = null;
82 _node_visual_data = null;
85 public void addDistribution( final Distribution distribution ) {
86 if ( _distributions == null ) {
87 _distributions = new ArrayList<Distribution>();
89 _distributions.add( distribution );
92 public void addReference( final Reference reference ) {
93 if ( _references == null ) {
94 _references = new ArrayList<Reference>();
96 _references.add( reference );
99 public void addSequence( final Sequence sequence ) {
100 if ( _sequences == null ) {
101 _sequences = new ArrayList<Sequence>();
103 _sequences.add( sequence );
106 public void addTaxonomy( final Taxonomy taxonomy ) {
107 if ( _taxonomies == null ) {
108 _taxonomies = new ArrayList<Taxonomy>();
110 _taxonomies.add( taxonomy );
114 public StringBuffer asSimpleText() {
115 throw new UnsupportedOperationException();
119 public StringBuffer asText() {
120 throw new UnsupportedOperationException();
124 public PhylogenyData copy() {
125 final NodeData new_data = new NodeData();
126 new_data.setNodeName( getNodeName() );
127 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
128 new_data.setSequences( new ArrayList<Sequence>() );
129 for( final Sequence s : getSequences() ) {
131 new_data.addSequence( ( Sequence ) s.copy() );
135 if ( isHasEvent() ) {
136 new_data.setEvent( ( Event ) getEvent().copy() );
138 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
139 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
140 for( final Taxonomy t : getTaxonomies() ) {
142 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
146 if ( isHasBinaryCharacters() ) {
147 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
149 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
150 new_data.setReferences( new ArrayList<Reference>() );
151 for( final Reference r : getReferences() ) {
153 new_data.addReference( ( Reference ) r.copy() );
157 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
158 new_data.setDistributions( new ArrayList<Distribution>() );
159 for( final Distribution d : getDistributions() ) {
161 new_data.addDistribution( ( Distribution ) d.copy() );
165 if ( ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
166 new_data.setNodeVisualData( ( NodeVisualData ) getNodeVisualData().copy() );
169 new_data.setDate( ( Date ) getDate().copy() );
171 if ( isHasProperties() ) {
172 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
177 public BinaryCharacters getBinaryCharacters() {
178 return _binary_characters;
181 public Date getDate() {
186 * Convenience method -- always returns the first Distribution.
188 * @return Distribution
190 public Distribution getDistribution() {
191 return getDistribution( 0 );
194 public Distribution getDistribution( final int index ) {
195 return _distributions.get( index );
198 public List<Distribution> getDistributions() {
199 return _distributions;
202 public Event getEvent() {
206 public PropertiesMap getProperties() {
211 * Convenience method -- always returns the first Reference.
216 public Reference getReference() {
217 return getReference( 0 );
220 public Reference getReference( final int index ) {
221 return _references.get( index );
224 public List<Reference> getReferences() {
229 * Convenience method -- always returns the first Sequence.
233 public Sequence getSequence() {
234 return getSequence( 0 );
237 public Sequence getSequence( final int index ) {
238 return _sequences.get( index );
241 public List<Sequence> getSequences() {
245 public List<Taxonomy> getTaxonomies() {
250 * Convenience method -- always returns the first Taxonomy.
255 public Taxonomy getTaxonomy() {
256 return getTaxonomy( 0 );
259 public Taxonomy getTaxonomy( final int index ) {
260 return _taxonomies.get( index );
264 public boolean isEqual( final PhylogenyData data ) {
265 throw new NoSuchMethodError();
268 public boolean isHasBinaryCharacters() {
269 return getBinaryCharacters() != null;
272 public boolean isEmpty() {
273 return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
274 && !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
278 public boolean isHasDate() {
279 return ( getDate() != null )
280 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
281 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
282 .isEmpty( getDate().getUnit() ) );
285 public boolean isHasDistribution() {
286 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
287 .isEmpty( getDistribution().getDesc() ) )
288 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
289 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
292 public boolean isHasEvent() {
293 return getEvent() != null;
296 public boolean isHasProperties() {
297 return ( getProperties() != null ) && ( getProperties().size() > 0 );
300 public boolean isHasReference() {
301 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
302 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
303 .getDescription() ) );
306 public boolean isHasSequence() {
307 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
310 public boolean isHasTaxonomy() {
311 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
314 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
315 _binary_characters = binary_characters;
318 public void setDate( final Date date ) {
323 * Convenience method -- always sets the first Distribution.
326 public void setDistribution( final Distribution distribution ) {
327 if ( _distributions == null ) {
328 _distributions = new ArrayList<Distribution>();
330 if ( _distributions.size() == 0 ) {
331 _distributions.add( distribution );
334 _distributions.set( 0, distribution );
338 public void setDistribution( final int index, final Distribution distribution ) {
339 if ( _distributions == null ) {
340 _distributions = new ArrayList<Distribution>();
342 _distributions.set( index, distribution );
345 private void setDistributions( final List<Distribution> distributions ) {
346 _distributions = distributions;
349 public void setEvent( final Event event ) {
353 public void setProperties( final PropertiesMap custom_data ) {
354 _properties = custom_data;
357 public void setReference( final int index, final Reference reference ) {
358 if ( _references == null ) {
359 _references = new ArrayList<Reference>();
361 _references.set( index, reference );
365 * Convenience method -- always sets the first Reference.
368 public void setReference( final Reference reference ) {
369 if ( _references == null ) {
370 _references = new ArrayList<Reference>();
372 if ( _references.size() == 0 ) {
373 _references.add( reference );
376 _references.set( 0, reference );
380 private void setReferences( final List<Reference> references ) {
381 _references = references;
384 public void setSequence( final int index, final Sequence sequence ) {
385 if ( _sequences == null ) {
386 _sequences = new ArrayList<Sequence>();
388 _sequences.set( index, sequence );
392 * Convenience method -- always sets the first Sequence.
395 public void setSequence( final Sequence sequence ) {
396 if ( _sequences == null ) {
397 _sequences = new ArrayList<Sequence>();
399 if ( _sequences.size() == 0 ) {
400 _sequences.add( sequence );
403 _sequences.set( 0, sequence );
407 private void setSequences( final List<Sequence> sequences ) {
408 _sequences = sequences;
411 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
412 _taxonomies = taxonomies;
415 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
416 if ( _taxonomies == null ) {
417 _taxonomies = new ArrayList<Taxonomy>();
419 _taxonomies.set( index, taxonomy );
423 * Convenience method -- always sets the first Taxonomy.
426 public void setTaxonomy( final Taxonomy taxonomy ) {
427 if ( _taxonomies == null ) {
428 _taxonomies = new ArrayList<Taxonomy>();
430 if ( _taxonomies.size() == 0 ) {
431 _taxonomies.add( taxonomy );
434 _taxonomies.set( 0, taxonomy );
439 public StringBuffer toNHX() {
440 final StringBuffer sb = new StringBuffer();
441 if ( isHasTaxonomy() ) {
442 sb.append( getTaxonomy().toNHX() );
444 if ( isHasSequence() ) {
445 sb.append( getSequence().toNHX() );
447 if ( isHasEvent() ) {
448 sb.append( getEvent().toNHX() );
454 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
455 if ( isHasTaxonomy() ) {
456 for( final Taxonomy t : getTaxonomies() ) {
457 if ( !t.isEmpty() ) {
458 t.toPhyloXML( writer, level, indentation );
462 if ( isHasSequence() ) {
463 for( final Sequence s : getSequences() ) {
464 if ( !s.isEmpty() ) {
465 s.toPhyloXML( writer, level, indentation );
469 if ( isHasEvent() ) {
470 getEvent().toPhyloXML( writer, level, indentation );
472 if ( isHasBinaryCharacters() ) {
473 getBinaryCharacters().toPhyloXML( writer, level, indentation );
475 if ( isHasDistribution() ) {
476 for( final Distribution d : getDistributions() ) {
477 d.toPhyloXML( writer, level, indentation );
481 getDate().toPhyloXML( writer, level, indentation );
483 if ( isHasReference() ) {
484 for( final Reference r : getReferences() ) {
485 r.toPhyloXML( writer, level, indentation );
488 if ( isHasProperties() ) {
489 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
491 if ( ( level == 0 ) && ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
492 getNodeVisualData().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
494 if ( ( getVector() != null )
495 && !getVector().isEmpty()
496 && ( ( getProperties() == null ) || getProperties()
497 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
498 final List<Property> ps = vectorToProperties( getVector() );
499 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
500 for( final Property p : ps ) {
501 p.toPhyloXML( writer, level, my_indent );
506 private List<Property> vectorToProperties( final List<Double> vector ) {
507 final List<Property> properties = new ArrayList<Property>();
508 for( int i = 0; i < vector.size(); ++i ) {
509 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
510 String.valueOf( vector.get( i ) ),
512 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
518 public void setVector( final List<Double> vector ) {
522 public List<Double> getVector() {
526 public String getNodeName() {
530 public void setNodeName( final String node_name ) {
531 _node_name = node_name;
534 public void setNodeVisualData( final NodeVisualData node_visual_data ) {
535 _node_visual_data = node_visual_data;
538 public NodeVisualData getNodeVisualData() {
539 return _node_visual_data;