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 TAXONOMY_SCIENTIFIC_NAME,
52 private String _node_name;
54 private List<Sequence> _sequences;
55 private Identifier _node_identifier;
56 private List<Taxonomy> _taxonomies;
57 private List<Distribution> _distributions;
59 private BinaryCharacters _binary_characters;
60 private PropertiesMap _properties;
61 private List<Reference> _references;
62 private List<Double> _vector;
63 private List<NodeVisualization> _node_visualizations;
73 public void addDistribution( final Distribution distribution ) {
74 if ( _distributions == null ) {
75 _distributions = new ArrayList<Distribution>();
77 _distributions.add( distribution );
80 public void addReference( final Reference reference ) {
81 if ( _references == null ) {
82 _references = new ArrayList<Reference>();
84 _references.add( reference );
87 public void addSequence( final Sequence sequence ) {
88 if ( _sequences == null ) {
89 _sequences = new ArrayList<Sequence>();
91 _sequences.add( sequence );
94 public void addTaxonomy( final Taxonomy taxonomy ) {
95 if ( _taxonomies == null ) {
96 _taxonomies = new ArrayList<Taxonomy>();
98 _taxonomies.add( taxonomy );
102 public StringBuffer asSimpleText() {
103 throw new UnsupportedOperationException();
107 public StringBuffer asText() {
108 throw new UnsupportedOperationException();
112 public PhylogenyData copy() {
113 final NodeData new_data = new NodeData();
114 new_data.setNodeName( getNodeName() );
115 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
116 new_data.setSequences( new ArrayList<Sequence>() );
117 for( final Sequence s : getSequences() ) {
119 new_data.addSequence( ( Sequence ) s.copy() );
123 if ( isHasEvent() ) {
124 new_data.setEvent( ( Event ) getEvent().copy() );
126 if ( isHasNodeIdentifier() ) {
127 new_data.setNodeIdentifier( ( Identifier ) getNodeIdentifier().copy() );
129 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
130 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
131 for( final Taxonomy t : getTaxonomies() ) {
133 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
137 if ( isHasBinaryCharacters() ) {
138 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
140 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
141 new_data.setReferences( new ArrayList<Reference>() );
142 for( final Reference r : getReferences() ) {
144 new_data.addReference( ( Reference ) r.copy() );
148 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
149 new_data.setDistributions( new ArrayList<Distribution>() );
150 for( final Distribution d : getDistributions() ) {
152 new_data.addDistribution( ( Distribution ) d.copy() );
156 if ( ( getNodeVisualizations() != null ) && ( getNodeVisualizations().size() > 0 ) ) {
157 new_data.setNodeVisualizations( new ArrayList<NodeVisualization>() );
158 for( final NodeVisualization v : getNodeVisualizations() ) {
160 new_data.getNodeVisualizations().add( ( NodeVisualization ) v.copy() );
165 new_data.setDate( ( Date ) getDate().copy() );
167 if ( isHasProperties() ) {
168 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
173 public BinaryCharacters getBinaryCharacters() {
174 return _binary_characters;
177 public Date getDate() {
182 * Convenience method -- always returns the first Distribution.
184 * @return Distribution
186 public Distribution getDistribution() {
187 return getDistribution( 0 );
190 public Distribution getDistribution( final int index ) {
191 return _distributions.get( index );
194 public List<Distribution> getDistributions() {
195 return _distributions;
198 public Event getEvent() {
202 public Identifier getNodeIdentifier() {
203 return _node_identifier;
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 UnsupportedOperationException();
268 public boolean isHasBinaryCharacters() {
269 return getBinaryCharacters() != null;
272 public boolean isHasDate() {
273 return ( getDate() != null )
274 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
275 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
276 .isEmpty( getDate().getUnit() ) );
279 public boolean isHasDistribution() {
280 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
281 .isEmpty( getDistribution().getDesc() ) )
282 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
283 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
286 public boolean isHasEvent() {
287 return getEvent() != null;
290 public boolean isHasNodeIdentifier() {
291 return getNodeIdentifier() != null;
294 public boolean isHasProperties() {
295 return ( getProperties() != null ) && ( getProperties().size() > 0 );
298 public boolean isHasReference() {
299 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
300 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
301 .getDescription() ) );
304 public boolean isHasSequence() {
305 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
308 public boolean isHasTaxonomy() {
309 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
312 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
313 _binary_characters = binary_characters;
316 public void setDate( final Date date ) {
321 * Convenience method -- always sets the first Distribution.
324 public void setDistribution( final Distribution distribution ) {
325 if ( _distributions == null ) {
326 _distributions = new ArrayList<Distribution>();
328 if ( _distributions.size() == 0 ) {
329 _distributions.add( distribution );
332 _distributions.set( 0, distribution );
336 public void setDistribution( final int index, final Distribution distribution ) {
337 if ( _distributions == null ) {
338 _distributions = new ArrayList<Distribution>();
340 _distributions.set( index, distribution );
343 private void setDistributions( final List<Distribution> distributions ) {
344 _distributions = distributions;
347 public void setEvent( final Event event ) {
351 public void setNodeIdentifier( final Identifier node_identifier ) {
352 _node_identifier = node_identifier;
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 ( isHasNodeIdentifier() ) {
458 writer.write( ForesterUtil.LINE_SEPARATOR );
459 writer.write( indentation );
460 // if ( !org.forester.util.ForesterUtil.isEmpty( getNodeIdentifier().getProvider() ) ) {
461 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
462 // .getValue(), PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR, getNodeIdentifier().getProvider() );
465 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
469 if ( isHasTaxonomy() ) {
470 for( final Taxonomy t : getTaxonomies() ) {
471 if ( !t.isEmpty() ) {
472 t.toPhyloXML( writer, level, indentation );
476 if ( isHasSequence() ) {
477 for( final Sequence s : getSequences() ) {
478 if ( !s.isEmpty() ) {
479 s.toPhyloXML( writer, level, indentation );
483 if ( isHasEvent() ) {
484 getEvent().toPhyloXML( writer, level, indentation );
486 if ( isHasBinaryCharacters() ) {
487 getBinaryCharacters().toPhyloXML( writer, level, indentation );
489 if ( isHasDistribution() ) {
490 for( final Distribution d : getDistributions() ) {
491 d.toPhyloXML( writer, level, indentation );
495 getDate().toPhyloXML( writer, level, indentation );
497 if ( isHasReference() ) {
498 for( final Reference r : getReferences() ) {
499 r.toPhyloXML( writer, level, indentation );
502 if ( isHasProperties() ) {
503 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
505 if ( ( getVector() != null )
506 && !getVector().isEmpty()
507 && ( ( getProperties() == null ) || getProperties()
508 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
509 final List<Property> ps = vectorToProperties( getVector() );
510 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
511 for( final Property p : ps ) {
512 p.toPhyloXML( writer, level, my_indent );
517 private List<Property> vectorToProperties( final List<Double> vector ) {
518 final List<Property> properties = new ArrayList<Property>();
519 for( int i = 0; i < vector.size(); ++i ) {
520 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
521 String.valueOf( vector.get( i ) ),
523 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
529 public void setVector( final List<Double> vector ) {
533 public List<Double> getVector() {
537 public String getNodeName() {
541 public void setNodeName( final String node_name ) {
542 _node_name = node_name;
545 public void setNodeVisualizations( final List<NodeVisualization> _node_visualizations ) {
546 this._node_visualizations = _node_visualizations;
549 public List<NodeVisualization> getNodeVisualizations() {
550 return _node_visualizations;