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 {
42 NODE_NAME, EVENT, SEQUENCE_NAME, SEQUENCE_SYMBOL, SEQUENCE_MOL_SEQ, SEQUENCE_ACC;
44 private String _node_name;
46 private List<Sequence> _sequences;
47 private Identifier _node_identifier;
48 private List<Taxonomy> _taxonomies;
49 private List<Distribution> _distributions;
51 private BinaryCharacters _binary_characters;
52 private PropertiesMap _properties;
53 private List<Reference> _references;
54 private List<Double> _vector;
55 private List<NodeVisualization> _node_visualizations;
65 public void addDistribution( final Distribution distribution ) {
66 if ( _distributions == null ) {
67 _distributions = new ArrayList<Distribution>();
69 _distributions.add( distribution );
72 public void addReference( final Reference reference ) {
73 if ( _references == null ) {
74 _references = new ArrayList<Reference>();
76 _references.add( reference );
79 public void addSequence( final Sequence sequence ) {
80 if ( _sequences == null ) {
81 _sequences = new ArrayList<Sequence>();
83 _sequences.add( sequence );
86 public void addTaxonomy( final Taxonomy taxonomy ) {
87 if ( _taxonomies == null ) {
88 _taxonomies = new ArrayList<Taxonomy>();
90 _taxonomies.add( taxonomy );
94 public StringBuffer asSimpleText() {
95 throw new UnsupportedOperationException();
99 public StringBuffer asText() {
100 throw new UnsupportedOperationException();
104 public PhylogenyData copy() {
105 final NodeData new_data = new NodeData();
106 new_data.setNodeName( getNodeName() );
107 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
108 new_data.setSequences( new ArrayList<Sequence>() );
109 for( final Sequence s : getSequences() ) {
111 new_data.addSequence( ( Sequence ) s.copy() );
115 if ( isHasEvent() ) {
116 new_data.setEvent( ( Event ) getEvent().copy() );
118 if ( isHasNodeIdentifier() ) {
119 new_data.setNodeIdentifier( ( Identifier ) getNodeIdentifier().copy() );
121 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
122 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
123 for( final Taxonomy t : getTaxonomies() ) {
125 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
129 if ( isHasBinaryCharacters() ) {
130 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
132 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
133 new_data.setReferences( new ArrayList<Reference>() );
134 for( final Reference r : getReferences() ) {
136 new_data.addReference( ( Reference ) r.copy() );
140 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
141 new_data.setDistributions( new ArrayList<Distribution>() );
142 for( final Distribution d : getDistributions() ) {
144 new_data.addDistribution( ( Distribution ) d.copy() );
148 if ( ( getNodeVisualizations() != null ) && ( getNodeVisualizations().size() > 0 ) ) {
149 new_data.setNodeVisualizations( new ArrayList<NodeVisualization>() );
150 for( final NodeVisualization v : getNodeVisualizations() ) {
152 new_data.getNodeVisualizations().add( ( NodeVisualization ) v.copy() );
157 new_data.setDate( ( Date ) getDate().copy() );
159 if ( isHasProperties() ) {
160 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
165 public BinaryCharacters getBinaryCharacters() {
166 return _binary_characters;
169 public Date getDate() {
174 * Convenience method -- always returns the first Distribution.
176 * @return Distribution
178 public Distribution getDistribution() {
179 return getDistribution( 0 );
182 public Distribution getDistribution( final int index ) {
183 return _distributions.get( index );
186 public List<Distribution> getDistributions() {
187 return _distributions;
190 public Event getEvent() {
194 public Identifier getNodeIdentifier() {
195 return _node_identifier;
198 public PropertiesMap getProperties() {
203 * Convenience method -- always returns the first Reference.
208 public Reference getReference() {
209 return getReference( 0 );
212 public Reference getReference( final int index ) {
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 return _sequences.get( index );
233 public List<Sequence> getSequences() {
237 public List<Taxonomy> getTaxonomies() {
242 * Convenience method -- always returns the first Taxonomy.
247 public Taxonomy getTaxonomy() {
248 return getTaxonomy( 0 );
251 public Taxonomy getTaxonomy( final int index ) {
252 return _taxonomies.get( index );
256 public boolean isEqual( final PhylogenyData data ) {
257 throw new UnsupportedOperationException();
260 public boolean isHasBinaryCharacters() {
261 return getBinaryCharacters() != null;
264 public boolean isHasDate() {
265 return ( getDate() != null )
266 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
267 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
268 .isEmpty( getDate().getUnit() ) );
271 public boolean isHasDistribution() {
272 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
273 .isEmpty( getDistribution().getDesc() ) )
274 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
275 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
278 public boolean isHasEvent() {
279 return getEvent() != null;
282 public boolean isHasNodeIdentifier() {
283 return getNodeIdentifier() != null;
286 public boolean isHasProperties() {
287 return ( getProperties() != null ) && ( getProperties().size() > 0 );
290 public boolean isHasReference() {
291 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
292 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
293 .getDescription() ) );
296 public boolean isHasSequence() {
297 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
300 public boolean isHasTaxonomy() {
301 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
304 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
305 _binary_characters = binary_characters;
308 public void setDate( final Date date ) {
313 * Convenience method -- always sets the first Distribution.
316 public void setDistribution( final Distribution distribution ) {
317 if ( _distributions == null ) {
318 _distributions = new ArrayList<Distribution>();
320 if ( _distributions.size() == 0 ) {
321 _distributions.add( distribution );
324 _distributions.set( 0, distribution );
328 public void setDistribution( final int index, final Distribution distribution ) {
329 if ( _distributions == null ) {
330 _distributions = new ArrayList<Distribution>();
332 _distributions.set( index, distribution );
335 private void setDistributions( final List<Distribution> distributions ) {
336 _distributions = distributions;
339 public void setEvent( final Event event ) {
343 public void setNodeIdentifier( final Identifier node_identifier ) {
344 _node_identifier = node_identifier;
347 public void setProperties( final PropertiesMap custom_data ) {
348 _properties = custom_data;
351 public void setReference( final int index, final Reference reference ) {
352 if ( _references == null ) {
353 _references = new ArrayList<Reference>();
355 _references.set( index, reference );
359 * Convenience method -- always sets the first Reference.
362 public void setReference( final Reference reference ) {
363 if ( _references == null ) {
364 _references = new ArrayList<Reference>();
366 if ( _references.size() == 0 ) {
367 _references.add( reference );
370 _references.set( 0, reference );
374 private void setReferences( final List<Reference> references ) {
375 _references = references;
378 public void setSequence( final int index, final Sequence sequence ) {
379 if ( _sequences == null ) {
380 _sequences = new ArrayList<Sequence>();
382 _sequences.set( index, sequence );
386 * Convenience method -- always sets the first Sequence.
389 public void setSequence( final Sequence sequence ) {
390 if ( _sequences == null ) {
391 _sequences = new ArrayList<Sequence>();
393 if ( _sequences.size() == 0 ) {
394 _sequences.add( sequence );
397 _sequences.set( 0, sequence );
401 private void setSequences( final List<Sequence> sequences ) {
402 _sequences = sequences;
405 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
406 _taxonomies = taxonomies;
409 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
410 if ( _taxonomies == null ) {
411 _taxonomies = new ArrayList<Taxonomy>();
413 _taxonomies.set( index, taxonomy );
417 * Convenience method -- always sets the first Taxonomy.
420 public void setTaxonomy( final Taxonomy taxonomy ) {
421 if ( _taxonomies == null ) {
422 _taxonomies = new ArrayList<Taxonomy>();
424 if ( _taxonomies.size() == 0 ) {
425 _taxonomies.add( taxonomy );
428 _taxonomies.set( 0, taxonomy );
433 public StringBuffer toNHX() {
434 final StringBuffer sb = new StringBuffer();
435 if ( isHasNodeIdentifier() ) {
436 sb.append( getNodeIdentifier().toNHX() );
438 if ( isHasTaxonomy() ) {
439 sb.append( getTaxonomy().toNHX() );
441 if ( isHasSequence() ) {
442 sb.append( getSequence().toNHX() );
444 if ( isHasEvent() ) {
445 sb.append( getEvent().toNHX() );
447 if ( isHasProperties() ) {
448 sb.append( getProperties().toNHX() );
454 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
455 if ( isHasNodeIdentifier() ) {
456 writer.write( ForesterUtil.LINE_SEPARATOR );
457 writer.write( indentation );
458 // if ( !org.forester.util.ForesterUtil.isEmpty( getNodeIdentifier().getProvider() ) ) {
459 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
460 // .getValue(), PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR, getNodeIdentifier().getProvider() );
463 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
467 if ( isHasTaxonomy() ) {
468 for( final Taxonomy t : getTaxonomies() ) {
469 if ( !t.isEmpty() ) {
470 t.toPhyloXML( writer, level, indentation );
474 if ( isHasSequence() ) {
475 for( final Sequence s : getSequences() ) {
476 if ( !s.isEmpty() ) {
477 s.toPhyloXML( writer, level, indentation );
481 if ( isHasEvent() ) {
482 getEvent().toPhyloXML( writer, level, indentation );
484 if ( isHasBinaryCharacters() ) {
485 getBinaryCharacters().toPhyloXML( writer, level, indentation );
487 if ( isHasDistribution() ) {
488 for( final Distribution d : getDistributions() ) {
489 d.toPhyloXML( writer, level, indentation );
493 getDate().toPhyloXML( writer, level, indentation );
495 if ( isHasReference() ) {
496 for( final Reference r : getReferences() ) {
497 r.toPhyloXML( writer, level, indentation );
500 if ( isHasProperties() ) {
501 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
503 if ( ( getVector() != null )
504 && !getVector().isEmpty()
505 && ( ( getProperties() == null ) || getProperties()
506 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
507 final List<Property> ps = vectorToProperties( getVector() );
508 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
509 for( final Property p : ps ) {
510 p.toPhyloXML( writer, level, my_indent );
515 private List<Property> vectorToProperties( final List<Double> vector ) {
516 final List<Property> properties = new ArrayList<Property>();
517 for( int i = 0; i < vector.size(); ++i ) {
518 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
519 String.valueOf( vector.get( i ) ),
521 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
527 public void setVector( final List<Double> vector ) {
531 public List<Double> getVector() {
535 public String getNodeName() {
539 public void setNodeName( final String node_name ) {
540 _node_name = node_name;
543 public void setNodeVisualizations( final List<NodeVisualization> _node_visualizations ) {
544 this._node_visualizations = _node_visualizations;
547 public List<NodeVisualization> getNodeVisualizations() {
548 return _node_visualizations;