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 private String _node_name;
59 private List<Sequence> _sequences;
60 private List<Taxonomy> _taxonomies;
61 private List<Distribution> _distributions;
63 private BinaryCharacters _binary_characters;
64 private PropertiesMap _properties;
65 private List<Reference> _references;
66 private List<Double> _vector;
67 private NodeVisualData _node_visual_data;
78 _distributions = null;
80 _binary_characters = null;
84 _node_visual_data = null;
87 public void addDistribution( final Distribution distribution ) {
88 if ( _distributions == null ) {
89 _distributions = new ArrayList<Distribution>();
91 _distributions.add( distribution );
94 public void addReference( final Reference reference ) {
95 if ( _references == null ) {
96 _references = new ArrayList<Reference>();
98 _references.add( reference );
101 public void addSequence( final Sequence sequence ) {
102 if ( _sequences == null ) {
103 _sequences = new ArrayList<Sequence>();
105 _sequences.add( sequence );
108 public void addTaxonomy( final Taxonomy taxonomy ) {
109 if ( _taxonomies == null ) {
110 _taxonomies = new ArrayList<Taxonomy>();
112 _taxonomies.add( taxonomy );
116 public StringBuffer asSimpleText() {
117 throw new UnsupportedOperationException();
121 public StringBuffer asText() {
122 throw new UnsupportedOperationException();
126 public PhylogenyData copy() {
127 final NodeData new_data = new NodeData();
128 new_data.setNodeName( getNodeName() );
129 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
130 new_data.setSequences( new ArrayList<Sequence>() );
131 for( final Sequence s : getSequences() ) {
133 new_data.addSequence( ( Sequence ) s.copy() );
137 if ( isHasEvent() ) {
138 new_data.setEvent( ( Event ) getEvent().copy() );
140 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
141 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
142 for( final Taxonomy t : getTaxonomies() ) {
144 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
148 if ( isHasBinaryCharacters() ) {
149 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
151 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
152 new_data.setReferences( new ArrayList<Reference>() );
153 for( final Reference r : getReferences() ) {
155 new_data.addReference( ( Reference ) r.copy() );
159 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
160 new_data.setDistributions( new ArrayList<Distribution>() );
161 for( final Distribution d : getDistributions() ) {
163 new_data.addDistribution( ( Distribution ) d.copy() );
167 if ( ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
168 new_data.setNodeVisualData( ( NodeVisualData ) getNodeVisualData().copy() );
171 new_data.setDate( ( Date ) getDate().copy() );
173 if ( isHasProperties() ) {
174 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
179 public BinaryCharacters getBinaryCharacters() {
180 return _binary_characters;
183 public Date getDate() {
188 * Convenience method -- always returns the first Distribution.
190 * @return Distribution
192 public Distribution getDistribution() {
193 return getDistribution( 0 );
196 public Distribution getDistribution( final int index ) {
197 return _distributions.get( index );
200 public List<Distribution> getDistributions() {
201 return _distributions;
204 public Event getEvent() {
208 public PropertiesMap getProperties() {
213 * Convenience method -- always returns the first Reference.
218 public Reference getReference() {
219 return getReference( 0 );
222 public Reference getReference( final int index ) {
223 return _references.get( index );
226 public List<Reference> getReferences() {
231 * Convenience method -- always returns the first Sequence.
235 public Sequence getSequence() {
236 return getSequence( 0 );
239 public Sequence getSequence( final int index ) {
240 return _sequences.get( index );
243 public List<Sequence> getSequences() {
247 public List<Taxonomy> getTaxonomies() {
252 * Convenience method -- always returns the first Taxonomy.
257 public Taxonomy getTaxonomy() {
258 return getTaxonomy( 0 );
261 public Taxonomy getTaxonomy( final int index ) {
262 return _taxonomies.get( index );
266 public boolean isEqual( final PhylogenyData data ) {
267 throw new NoSuchMethodError();
270 public boolean isHasBinaryCharacters() {
271 return getBinaryCharacters() != null;
274 public boolean isEmpty() {
275 return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
276 && !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
280 public boolean isHasDate() {
281 return ( getDate() != null )
282 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
283 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
284 .isEmpty( getDate().getUnit() ) );
287 public boolean isHasDistribution() {
288 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
289 .isEmpty( getDistribution().getDesc() ) )
290 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
291 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
294 public boolean isHasEvent() {
295 return getEvent() != null;
298 public boolean isHasProperties() {
299 return ( getProperties() != null ) && ( getProperties().size() > 0 );
302 public boolean isHasReference() {
303 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
304 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
305 .getDescription() ) );
308 public boolean isHasSequence() {
309 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
312 public boolean isHasTaxonomy() {
313 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
316 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
317 _binary_characters = binary_characters;
320 public void setDate( final Date date ) {
325 * Convenience method -- always sets the first Distribution.
328 public void setDistribution( final Distribution distribution ) {
329 if ( _distributions == null ) {
330 _distributions = new ArrayList<Distribution>();
332 if ( _distributions.size() == 0 ) {
333 _distributions.add( distribution );
336 _distributions.set( 0, distribution );
340 public void setDistribution( final int index, final Distribution distribution ) {
341 if ( _distributions == null ) {
342 _distributions = new ArrayList<Distribution>();
344 _distributions.set( index, distribution );
347 private void setDistributions( final List<Distribution> distributions ) {
348 _distributions = distributions;
351 public void setEvent( final Event event ) {
355 public void setProperties( final PropertiesMap custom_data ) {
356 _properties = custom_data;
359 public void setReference( final int index, final Reference reference ) {
360 if ( _references == null ) {
361 _references = new ArrayList<Reference>();
363 _references.set( index, reference );
367 * Convenience method -- always sets the first Reference.
370 public void setReference( final Reference reference ) {
371 if ( _references == null ) {
372 _references = new ArrayList<Reference>();
374 if ( _references.size() == 0 ) {
375 _references.add( reference );
378 _references.set( 0, reference );
382 private void setReferences( final List<Reference> references ) {
383 _references = references;
386 public void setSequence( final int index, final Sequence sequence ) {
387 if ( _sequences == null ) {
388 _sequences = new ArrayList<Sequence>();
390 _sequences.set( index, sequence );
394 * Convenience method -- always sets the first Sequence.
397 public void setSequence( final Sequence sequence ) {
398 if ( _sequences == null ) {
399 _sequences = new ArrayList<Sequence>();
401 if ( _sequences.size() == 0 ) {
402 _sequences.add( sequence );
405 _sequences.set( 0, sequence );
409 private void setSequences( final List<Sequence> sequences ) {
410 _sequences = sequences;
413 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
414 _taxonomies = taxonomies;
417 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
418 if ( _taxonomies == null ) {
419 _taxonomies = new ArrayList<Taxonomy>();
421 _taxonomies.set( index, taxonomy );
425 * Convenience method -- always sets the first Taxonomy.
428 public void setTaxonomy( final Taxonomy taxonomy ) {
429 if ( _taxonomies == null ) {
430 _taxonomies = new ArrayList<Taxonomy>();
432 if ( _taxonomies.size() == 0 ) {
433 _taxonomies.add( taxonomy );
436 _taxonomies.set( 0, taxonomy );
441 public StringBuffer toNHX() {
442 final StringBuffer sb = new StringBuffer();
443 if ( isHasTaxonomy() ) {
444 sb.append( getTaxonomy().toNHX() );
446 if ( isHasSequence() ) {
447 sb.append( getSequence().toNHX() );
449 if ( isHasEvent() ) {
450 sb.append( getEvent().toNHX() );
456 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
457 if ( isHasTaxonomy() ) {
458 for( final Taxonomy t : getTaxonomies() ) {
459 if ( !t.isEmpty() ) {
460 t.toPhyloXML( writer, level, indentation );
464 if ( isHasSequence() ) {
465 for( final Sequence s : getSequences() ) {
466 if ( !s.isEmpty() ) {
467 s.toPhyloXML( writer, level, indentation );
471 if ( isHasEvent() ) {
472 getEvent().toPhyloXML( writer, level, indentation );
474 if ( isHasBinaryCharacters() ) {
475 getBinaryCharacters().toPhyloXML( writer, level, indentation );
477 if ( isHasDistribution() ) {
478 for( final Distribution d : getDistributions() ) {
479 d.toPhyloXML( writer, level, indentation );
483 getDate().toPhyloXML( writer, level, indentation );
485 if ( isHasReference() ) {
486 for( final Reference r : getReferences() ) {
487 r.toPhyloXML( writer, level, indentation );
490 if ( isHasProperties() ) {
491 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
493 if ( ( level == 0 ) && ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
494 getNodeVisualData().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
496 if ( ( getVector() != null )
497 && !getVector().isEmpty()
498 && ( ( getProperties() == null ) || getProperties()
499 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
500 final List<Property> ps = vectorToProperties( getVector() );
501 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
502 for( final Property p : ps ) {
503 p.toPhyloXML( writer, level, my_indent );
508 private List<Property> vectorToProperties( final List<Double> vector ) {
509 final List<Property> properties = new ArrayList<Property>();
510 for( int i = 0; i < vector.size(); ++i ) {
511 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
512 String.valueOf( vector.get( i ) ),
514 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
520 public void setVector( final List<Double> vector ) {
524 public List<Double> getVector() {
528 public String getNodeName() {
532 public void setNodeName( final String node_name ) {
533 _node_name = node_name;
536 public void setNodeVisualData( final NodeVisualData node_visual_data ) {
537 _node_visual_data = node_visual_data;
540 public NodeVisualData getNodeVisualData() {
541 return _node_visual_data;