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: www.phylosoft.org/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 TAXONOMY_SCIENTIFIC_NAME,
51 private String _node_name;
53 private List<Sequence> _sequences;
54 private Identifier _node_identifier;
55 private List<Taxonomy> _taxonomies;
56 private List<Distribution> _distributions;
58 private BinaryCharacters _binary_characters;
59 private PropertiesMap _properties;
60 private List<Reference> _references;
61 private List<Double> _vector;
62 private List<NodeVisualization> _node_visualizations;
72 public void addDistribution( final Distribution distribution ) {
73 if ( _distributions == null ) {
74 _distributions = new ArrayList<Distribution>();
76 _distributions.add( distribution );
79 public void addReference( final Reference reference ) {
80 if ( _references == null ) {
81 _references = new ArrayList<Reference>();
83 _references.add( reference );
86 public void addSequence( final Sequence sequence ) {
87 if ( _sequences == null ) {
88 _sequences = new ArrayList<Sequence>();
90 _sequences.add( sequence );
93 public void addTaxonomy( final Taxonomy taxonomy ) {
94 if ( _taxonomies == null ) {
95 _taxonomies = new ArrayList<Taxonomy>();
97 _taxonomies.add( taxonomy );
101 public StringBuffer asSimpleText() {
102 throw new UnsupportedOperationException();
106 public StringBuffer asText() {
107 throw new UnsupportedOperationException();
111 public PhylogenyData copy() {
112 final NodeData new_data = new NodeData();
113 new_data.setNodeName( getNodeName() );
114 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
115 new_data.setSequences( new ArrayList<Sequence>() );
116 for( final Sequence s : getSequences() ) {
118 new_data.addSequence( ( Sequence ) s.copy() );
122 if ( isHasEvent() ) {
123 new_data.setEvent( ( Event ) getEvent().copy() );
125 if ( isHasNodeIdentifier() ) {
126 new_data.setNodeIdentifier( ( Identifier ) getNodeIdentifier().copy() );
128 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
129 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
130 for( final Taxonomy t : getTaxonomies() ) {
132 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
136 if ( isHasBinaryCharacters() ) {
137 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
139 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
140 new_data.setReferences( new ArrayList<Reference>() );
141 for( final Reference r : getReferences() ) {
143 new_data.addReference( ( Reference ) r.copy() );
147 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
148 new_data.setDistributions( new ArrayList<Distribution>() );
149 for( final Distribution d : getDistributions() ) {
151 new_data.addDistribution( ( Distribution ) d.copy() );
155 if ( ( getNodeVisualizations() != null ) && ( getNodeVisualizations().size() > 0 ) ) {
156 new_data.setNodeVisualizations( new ArrayList<NodeVisualization>() );
157 for( final NodeVisualization v : getNodeVisualizations() ) {
159 new_data.getNodeVisualizations().add( ( NodeVisualization ) v.copy() );
164 new_data.setDate( ( Date ) getDate().copy() );
166 if ( isHasProperties() ) {
167 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
172 public BinaryCharacters getBinaryCharacters() {
173 return _binary_characters;
176 public Date getDate() {
181 * Convenience method -- always returns the first Distribution.
183 * @return Distribution
185 public Distribution getDistribution() {
186 return getDistribution( 0 );
189 public Distribution getDistribution( final int index ) {
190 return _distributions.get( index );
193 public List<Distribution> getDistributions() {
194 return _distributions;
197 public Event getEvent() {
201 public Identifier getNodeIdentifier() {
202 return _node_identifier;
205 public PropertiesMap getProperties() {
210 * Convenience method -- always returns the first Reference.
215 public Reference getReference() {
216 return getReference( 0 );
219 public Reference getReference( final int index ) {
220 return _references.get( index );
223 public List<Reference> getReferences() {
228 * Convenience method -- always returns the first Sequence.
232 public Sequence getSequence() {
233 return getSequence( 0 );
236 public Sequence getSequence( final int index ) {
237 return _sequences.get( index );
240 public List<Sequence> getSequences() {
244 public List<Taxonomy> getTaxonomies() {
249 * Convenience method -- always returns the first Taxonomy.
254 public Taxonomy getTaxonomy() {
255 return getTaxonomy( 0 );
258 public Taxonomy getTaxonomy( final int index ) {
259 return _taxonomies.get( index );
263 public boolean isEqual( final PhylogenyData data ) {
264 throw new UnsupportedOperationException();
267 public boolean isHasBinaryCharacters() {
268 return getBinaryCharacters() != null;
271 public boolean isHasDate() {
272 return ( getDate() != null )
273 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
274 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
275 .isEmpty( getDate().getUnit() ) );
278 public boolean isHasDistribution() {
279 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
280 .isEmpty( getDistribution().getDesc() ) )
281 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
282 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
285 public boolean isHasEvent() {
286 return getEvent() != null;
289 public boolean isHasNodeIdentifier() {
290 return getNodeIdentifier() != null;
293 public boolean isHasProperties() {
294 return ( getProperties() != null ) && ( getProperties().size() > 0 );
297 public boolean isHasReference() {
298 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
299 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
300 .getDescription() ) );
303 public boolean isHasSequence() {
304 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
307 public boolean isHasTaxonomy() {
308 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
311 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
312 _binary_characters = binary_characters;
315 public void setDate( final Date date ) {
320 * Convenience method -- always sets the first Distribution.
323 public void setDistribution( final Distribution distribution ) {
324 if ( _distributions == null ) {
325 _distributions = new ArrayList<Distribution>();
327 if ( _distributions.size() == 0 ) {
328 _distributions.add( distribution );
331 _distributions.set( 0, distribution );
335 public void setDistribution( final int index, final Distribution distribution ) {
336 if ( _distributions == null ) {
337 _distributions = new ArrayList<Distribution>();
339 _distributions.set( index, distribution );
342 private void setDistributions( final List<Distribution> distributions ) {
343 _distributions = distributions;
346 public void setEvent( final Event event ) {
350 public void setNodeIdentifier( final Identifier node_identifier ) {
351 _node_identifier = node_identifier;
354 public void setProperties( final PropertiesMap custom_data ) {
355 _properties = custom_data;
358 public void setReference( final int index, final Reference reference ) {
359 if ( _references == null ) {
360 _references = new ArrayList<Reference>();
362 _references.set( index, reference );
366 * Convenience method -- always sets the first Reference.
369 public void setReference( final Reference reference ) {
370 if ( _references == null ) {
371 _references = new ArrayList<Reference>();
373 if ( _references.size() == 0 ) {
374 _references.add( reference );
377 _references.set( 0, reference );
381 private void setReferences( final List<Reference> references ) {
382 _references = references;
385 public void setSequence( final int index, final Sequence sequence ) {
386 if ( _sequences == null ) {
387 _sequences = new ArrayList<Sequence>();
389 _sequences.set( index, sequence );
393 * Convenience method -- always sets the first Sequence.
396 public void setSequence( final Sequence sequence ) {
397 if ( _sequences == null ) {
398 _sequences = new ArrayList<Sequence>();
400 if ( _sequences.size() == 0 ) {
401 _sequences.add( sequence );
404 _sequences.set( 0, sequence );
408 private void setSequences( final List<Sequence> sequences ) {
409 _sequences = sequences;
412 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
413 _taxonomies = taxonomies;
416 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
417 if ( _taxonomies == null ) {
418 _taxonomies = new ArrayList<Taxonomy>();
420 _taxonomies.set( index, taxonomy );
424 * Convenience method -- always sets the first Taxonomy.
427 public void setTaxonomy( final Taxonomy taxonomy ) {
428 if ( _taxonomies == null ) {
429 _taxonomies = new ArrayList<Taxonomy>();
431 if ( _taxonomies.size() == 0 ) {
432 _taxonomies.add( taxonomy );
435 _taxonomies.set( 0, taxonomy );
440 public StringBuffer toNHX() {
441 final StringBuffer sb = new StringBuffer();
442 if ( isHasNodeIdentifier() ) {
443 sb.append( getNodeIdentifier().toNHX() );
445 if ( isHasTaxonomy() ) {
446 sb.append( getTaxonomy().toNHX() );
448 if ( isHasSequence() ) {
449 sb.append( getSequence().toNHX() );
451 if ( isHasEvent() ) {
452 sb.append( getEvent().toNHX() );
454 if ( isHasProperties() ) {
455 sb.append( getProperties().toNHX() );
461 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
462 if ( isHasNodeIdentifier() ) {
463 writer.write( ForesterUtil.LINE_SEPARATOR );
464 writer.write( indentation );
465 // if ( !org.forester.util.ForesterUtil.isEmpty( getNodeIdentifier().getProvider() ) ) {
466 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
467 // .getValue(), PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR, getNodeIdentifier().getProvider() );
470 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
474 if ( isHasTaxonomy() ) {
475 for( final Taxonomy t : getTaxonomies() ) {
476 if ( !t.isEmpty() ) {
477 t.toPhyloXML( writer, level, indentation );
481 if ( isHasSequence() ) {
482 for( final Sequence s : getSequences() ) {
483 if ( !s.isEmpty() ) {
484 s.toPhyloXML( writer, level, indentation );
488 if ( isHasEvent() ) {
489 getEvent().toPhyloXML( writer, level, indentation );
491 if ( isHasBinaryCharacters() ) {
492 getBinaryCharacters().toPhyloXML( writer, level, indentation );
494 if ( isHasDistribution() ) {
495 for( final Distribution d : getDistributions() ) {
496 d.toPhyloXML( writer, level, indentation );
500 getDate().toPhyloXML( writer, level, indentation );
502 if ( isHasReference() ) {
503 for( final Reference r : getReferences() ) {
504 r.toPhyloXML( writer, level, indentation );
507 if ( isHasProperties() ) {
508 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
510 if ( ( getVector() != null )
511 && !getVector().isEmpty()
512 && ( ( getProperties() == null ) || getProperties()
513 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
514 final List<Property> ps = vectorToProperties( getVector() );
515 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
516 for( final Property p : ps ) {
517 p.toPhyloXML( writer, level, my_indent );
522 private List<Property> vectorToProperties( final List<Double> vector ) {
523 final List<Property> properties = new ArrayList<Property>();
524 for( int i = 0; i < vector.size(); ++i ) {
525 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
526 String.valueOf( vector.get( i ) ),
528 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
534 public void setVector( final List<Double> vector ) {
538 public List<Double> getVector() {
542 public String getNodeName() {
546 public void setNodeName( final String node_name ) {
547 _node_name = node_name;
550 public void setNodeVisualizations( final List<NodeVisualization> _node_visualizations ) {
551 this._node_visualizations = _node_visualizations;
554 public List<NodeVisualization> getNodeVisualizations() {
555 return _node_visualizations;