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 {
47 SEQUENCE_MOL_SEQ_FASTA,
49 TAXONOMY_SCIENTIFIC_NAME,
53 private String _node_name;
55 private List<Sequence> _sequences;
56 private Identifier _node_identifier;
57 private List<Taxonomy> _taxonomies;
58 private List<Distribution> _distributions;
60 private BinaryCharacters _binary_characters;
61 private PropertiesMap _properties;
62 private List<Reference> _references;
63 private List<Double> _vector;
64 private List<NodeVisualization> _node_visualizations;
74 public void addDistribution( final Distribution distribution ) {
75 if ( _distributions == null ) {
76 _distributions = new ArrayList<Distribution>();
78 _distributions.add( distribution );
81 public void addReference( final Reference reference ) {
82 if ( _references == null ) {
83 _references = new ArrayList<Reference>();
85 _references.add( reference );
88 public void addSequence( final Sequence sequence ) {
89 if ( _sequences == null ) {
90 _sequences = new ArrayList<Sequence>();
92 _sequences.add( sequence );
95 public void addTaxonomy( final Taxonomy taxonomy ) {
96 if ( _taxonomies == null ) {
97 _taxonomies = new ArrayList<Taxonomy>();
99 _taxonomies.add( taxonomy );
103 public StringBuffer asSimpleText() {
104 throw new UnsupportedOperationException();
108 public StringBuffer asText() {
109 throw new UnsupportedOperationException();
113 public PhylogenyData copy() {
114 final NodeData new_data = new NodeData();
115 new_data.setNodeName( getNodeName() );
116 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
117 new_data.setSequences( new ArrayList<Sequence>() );
118 for( final Sequence s : getSequences() ) {
120 new_data.addSequence( ( Sequence ) s.copy() );
124 if ( isHasEvent() ) {
125 new_data.setEvent( ( Event ) getEvent().copy() );
127 if ( isHasNodeIdentifier() ) {
128 new_data.setNodeIdentifier( ( Identifier ) getNodeIdentifier().copy() );
130 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
131 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
132 for( final Taxonomy t : getTaxonomies() ) {
134 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
138 if ( isHasBinaryCharacters() ) {
139 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
141 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
142 new_data.setReferences( new ArrayList<Reference>() );
143 for( final Reference r : getReferences() ) {
145 new_data.addReference( ( Reference ) r.copy() );
149 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
150 new_data.setDistributions( new ArrayList<Distribution>() );
151 for( final Distribution d : getDistributions() ) {
153 new_data.addDistribution( ( Distribution ) d.copy() );
157 if ( ( getNodeVisualizations() != null ) && ( getNodeVisualizations().size() > 0 ) ) {
158 new_data.setNodeVisualizations( new ArrayList<NodeVisualization>() );
159 for( final NodeVisualization v : getNodeVisualizations() ) {
161 new_data.getNodeVisualizations().add( ( NodeVisualization ) v.copy() );
166 new_data.setDate( ( Date ) getDate().copy() );
168 if ( isHasProperties() ) {
169 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
174 public BinaryCharacters getBinaryCharacters() {
175 return _binary_characters;
178 public Date getDate() {
183 * Convenience method -- always returns the first Distribution.
185 * @return Distribution
187 public Distribution getDistribution() {
188 return getDistribution( 0 );
191 public Distribution getDistribution( final int index ) {
192 return _distributions.get( index );
195 public List<Distribution> getDistributions() {
196 return _distributions;
199 public Event getEvent() {
203 public Identifier getNodeIdentifier() {
204 return _node_identifier;
207 public PropertiesMap getProperties() {
212 * Convenience method -- always returns the first Reference.
217 public Reference getReference() {
218 return getReference( 0 );
221 public Reference getReference( final int index ) {
222 return _references.get( index );
225 public List<Reference> getReferences() {
230 * Convenience method -- always returns the first Sequence.
234 public Sequence getSequence() {
235 return getSequence( 0 );
238 public Sequence getSequence( final int index ) {
239 return _sequences.get( index );
242 public List<Sequence> getSequences() {
246 public List<Taxonomy> getTaxonomies() {
251 * Convenience method -- always returns the first Taxonomy.
256 public Taxonomy getTaxonomy() {
257 return getTaxonomy( 0 );
260 public Taxonomy getTaxonomy( final int index ) {
261 return _taxonomies.get( index );
265 public boolean isEqual( final PhylogenyData data ) {
266 throw new UnsupportedOperationException();
269 public boolean isHasBinaryCharacters() {
270 return getBinaryCharacters() != null;
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 isHasNodeIdentifier() {
292 return getNodeIdentifier() != null;
295 public boolean isHasProperties() {
296 return ( getProperties() != null ) && ( getProperties().size() > 0 );
299 public boolean isHasReference() {
300 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
301 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
302 .getDescription() ) );
305 public boolean isHasSequence() {
306 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
309 public boolean isHasTaxonomy() {
310 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
313 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
314 _binary_characters = binary_characters;
317 public void setDate( final Date date ) {
322 * Convenience method -- always sets the first Distribution.
325 public void setDistribution( final Distribution distribution ) {
326 if ( _distributions == null ) {
327 _distributions = new ArrayList<Distribution>();
329 if ( _distributions.size() == 0 ) {
330 _distributions.add( distribution );
333 _distributions.set( 0, distribution );
337 public void setDistribution( final int index, final Distribution distribution ) {
338 if ( _distributions == null ) {
339 _distributions = new ArrayList<Distribution>();
341 _distributions.set( index, distribution );
344 private void setDistributions( final List<Distribution> distributions ) {
345 _distributions = distributions;
348 public void setEvent( final Event event ) {
352 public void setNodeIdentifier( final Identifier node_identifier ) {
353 _node_identifier = node_identifier;
356 public void setProperties( final PropertiesMap custom_data ) {
357 _properties = custom_data;
360 public void setReference( final int index, final Reference reference ) {
361 if ( _references == null ) {
362 _references = new ArrayList<Reference>();
364 _references.set( index, reference );
368 * Convenience method -- always sets the first Reference.
371 public void setReference( final Reference reference ) {
372 if ( _references == null ) {
373 _references = new ArrayList<Reference>();
375 if ( _references.size() == 0 ) {
376 _references.add( reference );
379 _references.set( 0, reference );
383 private void setReferences( final List<Reference> references ) {
384 _references = references;
387 public void setSequence( final int index, final Sequence sequence ) {
388 if ( _sequences == null ) {
389 _sequences = new ArrayList<Sequence>();
391 _sequences.set( index, sequence );
395 * Convenience method -- always sets the first Sequence.
398 public void setSequence( final Sequence sequence ) {
399 if ( _sequences == null ) {
400 _sequences = new ArrayList<Sequence>();
402 if ( _sequences.size() == 0 ) {
403 _sequences.add( sequence );
406 _sequences.set( 0, sequence );
410 private void setSequences( final List<Sequence> sequences ) {
411 _sequences = sequences;
414 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
415 _taxonomies = taxonomies;
418 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
419 if ( _taxonomies == null ) {
420 _taxonomies = new ArrayList<Taxonomy>();
422 _taxonomies.set( index, taxonomy );
426 * Convenience method -- always sets the first Taxonomy.
429 public void setTaxonomy( final Taxonomy taxonomy ) {
430 if ( _taxonomies == null ) {
431 _taxonomies = new ArrayList<Taxonomy>();
433 if ( _taxonomies.size() == 0 ) {
434 _taxonomies.add( taxonomy );
437 _taxonomies.set( 0, taxonomy );
442 public StringBuffer toNHX() {
443 final StringBuffer sb = new StringBuffer();
444 if ( isHasTaxonomy() ) {
445 sb.append( getTaxonomy().toNHX() );
447 if ( isHasSequence() ) {
448 sb.append( getSequence().toNHX() );
450 if ( isHasEvent() ) {
451 sb.append( getEvent().toNHX() );
457 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
458 if ( isHasNodeIdentifier() ) {
459 writer.write( ForesterUtil.LINE_SEPARATOR );
460 writer.write( indentation );
461 // if ( !org.forester.util.ForesterUtil.isEmpty( getNodeIdentifier().getProvider() ) ) {
462 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
463 // .getValue(), PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR, getNodeIdentifier().getProvider() );
466 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
470 if ( isHasTaxonomy() ) {
471 for( final Taxonomy t : getTaxonomies() ) {
472 if ( !t.isEmpty() ) {
473 t.toPhyloXML( writer, level, indentation );
477 if ( isHasSequence() ) {
478 for( final Sequence s : getSequences() ) {
479 if ( !s.isEmpty() ) {
480 s.toPhyloXML( writer, level, indentation );
484 if ( isHasEvent() ) {
485 getEvent().toPhyloXML( writer, level, indentation );
487 if ( isHasBinaryCharacters() ) {
488 getBinaryCharacters().toPhyloXML( writer, level, indentation );
490 if ( isHasDistribution() ) {
491 for( final Distribution d : getDistributions() ) {
492 d.toPhyloXML( writer, level, indentation );
496 getDate().toPhyloXML( writer, level, indentation );
498 if ( isHasReference() ) {
499 for( final Reference r : getReferences() ) {
500 r.toPhyloXML( writer, level, indentation );
503 if ( isHasProperties() ) {
504 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
506 if ( ( getVector() != null )
507 && !getVector().isEmpty()
508 && ( ( getProperties() == null ) || getProperties()
509 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
510 final List<Property> ps = vectorToProperties( getVector() );
511 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
512 for( final Property p : ps ) {
513 p.toPhyloXML( writer, level, my_indent );
518 private List<Property> vectorToProperties( final List<Double> vector ) {
519 final List<Property> properties = new ArrayList<Property>();
520 for( int i = 0; i < vector.size(); ++i ) {
521 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
522 String.valueOf( vector.get( i ) ),
524 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
530 public void setVector( final List<Double> vector ) {
534 public List<Double> getVector() {
538 public String getNodeName() {
542 public void setNodeName( final String node_name ) {
543 _node_name = node_name;
546 public void setNodeVisualizations( final List<NodeVisualization> _node_visualizations ) {
547 this._node_visualizations = _node_visualizations;
550 public List<NodeVisualization> getNodeVisualizations() {
551 return _node_visualizations;