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 private String _node_name;
43 private List<Sequence> _sequences;
44 private Identifier _node_identifier;
45 private List<Taxonomy> _taxonomies;
46 private List<Distribution> _distributions;
48 private BinaryCharacters _binary_characters;
49 private PropertiesMap _properties;
50 private List<Reference> _references;
51 private List<Double> _vector;
52 private List<NodeVisualization> _node_visualizations;
62 public void addDistribution( final Distribution distribution ) {
63 if ( _distributions == null ) {
64 _distributions = new ArrayList<Distribution>();
66 _distributions.add( distribution );
69 public void addReference( final Reference reference ) {
70 if ( _references == null ) {
71 _references = new ArrayList<Reference>();
73 _references.add( reference );
76 public void addSequence( final Sequence sequence ) {
77 if ( _sequences == null ) {
78 _sequences = new ArrayList<Sequence>();
80 _sequences.add( sequence );
83 public void addTaxonomy( final Taxonomy taxonomy ) {
84 if ( _taxonomies == null ) {
85 _taxonomies = new ArrayList<Taxonomy>();
87 _taxonomies.add( taxonomy );
91 public StringBuffer asSimpleText() {
92 throw new UnsupportedOperationException();
96 public StringBuffer asText() {
97 throw new UnsupportedOperationException();
101 public PhylogenyData copy() {
102 final NodeData new_data = new NodeData();
103 new_data.setNodeName( getNodeName() );
104 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
105 new_data.setSequences( new ArrayList<Sequence>() );
106 for( final Sequence s : getSequences() ) {
108 new_data.addSequence( ( Sequence ) s.copy() );
112 if ( isHasEvent() ) {
113 new_data.setEvent( ( Event ) getEvent().copy() );
115 if ( isHasNodeIdentifier() ) {
116 new_data.setNodeIdentifier( ( Identifier ) getNodeIdentifier().copy() );
118 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
119 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
120 for( final Taxonomy t : getTaxonomies() ) {
122 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
126 if ( isHasBinaryCharacters() ) {
127 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
129 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
130 new_data.setReferences( new ArrayList<Reference>() );
131 for( final Reference r : getReferences() ) {
133 new_data.addReference( ( Reference ) r.copy() );
137 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
138 new_data.setDistributions( new ArrayList<Distribution>() );
139 for( final Distribution d : getDistributions() ) {
141 new_data.addDistribution( ( Distribution ) d.copy() );
145 if ( ( getNodeVisualizations() != null ) && ( getNodeVisualizations().size() > 0 ) ) {
146 new_data.setNodeVisualizations( new ArrayList<NodeVisualization>() );
147 for( final NodeVisualization v : getNodeVisualizations() ) {
149 new_data.getNodeVisualizations().add( ( NodeVisualization ) v.copy() );
154 new_data.setDate( ( Date ) getDate().copy() );
156 if ( isHasProperties() ) {
157 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
162 public BinaryCharacters getBinaryCharacters() {
163 return _binary_characters;
166 public Date getDate() {
171 * Convenience method -- always returns the first Distribution.
173 * @return Distribution
175 public Distribution getDistribution() {
176 return getDistribution( 0 );
179 public Distribution getDistribution( final int index ) {
180 return _distributions.get( index );
183 public List<Distribution> getDistributions() {
184 return _distributions;
187 public Event getEvent() {
191 public Identifier getNodeIdentifier() {
192 return _node_identifier;
195 public PropertiesMap getProperties() {
200 * Convenience method -- always returns the first Reference.
205 public Reference getReference() {
206 return getReference( 0 );
209 public Reference getReference( final int index ) {
210 return _references.get( index );
213 public List<Reference> getReferences() {
218 * Convenience method -- always returns the first Sequence.
222 public Sequence getSequence() {
223 return getSequence( 0 );
226 public Sequence getSequence( final int index ) {
227 return _sequences.get( index );
230 public List<Sequence> getSequences() {
234 public List<Taxonomy> getTaxonomies() {
239 * Convenience method -- always returns the first Taxonomy.
244 public Taxonomy getTaxonomy() {
245 return getTaxonomy( 0 );
248 public Taxonomy getTaxonomy( final int index ) {
249 return _taxonomies.get( index );
253 public boolean isEqual( final PhylogenyData data ) {
254 throw new UnsupportedOperationException();
257 public boolean isHasBinaryCharacters() {
258 return getBinaryCharacters() != null;
261 public boolean isHasDate() {
262 return ( getDate() != null )
263 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
264 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
265 .isEmpty( getDate().getUnit() ) );
268 public boolean isHasDistribution() {
269 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
270 .isEmpty( getDistribution().getDesc() ) )
271 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
272 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
275 public boolean isHasEvent() {
276 return getEvent() != null;
279 public boolean isHasNodeIdentifier() {
280 return getNodeIdentifier() != null;
283 public boolean isHasProperties() {
284 return ( getProperties() != null ) && ( getProperties().size() > 0 );
287 public boolean isHasReference() {
288 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
289 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
290 .getDescription() ) );
293 public boolean isHasSequence() {
294 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
297 public boolean isHasTaxonomy() {
298 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
301 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
302 _binary_characters = binary_characters;
305 public void setDate( final Date date ) {
310 * Convenience method -- always sets the first Distribution.
313 public void setDistribution( final Distribution distribution ) {
314 if ( _distributions == null ) {
315 _distributions = new ArrayList<Distribution>();
317 if ( _distributions.size() == 0 ) {
318 _distributions.add( distribution );
321 _distributions.set( 0, distribution );
325 public void setDistribution( final int index, final Distribution distribution ) {
326 if ( _distributions == null ) {
327 _distributions = new ArrayList<Distribution>();
329 _distributions.set( index, distribution );
332 private void setDistributions( final List<Distribution> distributions ) {
333 _distributions = distributions;
336 public void setEvent( final Event event ) {
340 public void setNodeIdentifier( final Identifier node_identifier ) {
341 _node_identifier = node_identifier;
344 public void setProperties( final PropertiesMap custom_data ) {
345 _properties = custom_data;
348 public void setReference( final int index, final Reference reference ) {
349 if ( _references == null ) {
350 _references = new ArrayList<Reference>();
352 _references.set( index, reference );
356 * Convenience method -- always sets the first Reference.
359 public void setReference( final Reference reference ) {
360 if ( _references == null ) {
361 _references = new ArrayList<Reference>();
363 if ( _references.size() == 0 ) {
364 _references.add( reference );
367 _references.set( 0, reference );
371 private void setReferences( final List<Reference> references ) {
372 _references = references;
375 public void setSequence( final int index, final Sequence sequence ) {
376 if ( _sequences == null ) {
377 _sequences = new ArrayList<Sequence>();
379 _sequences.set( index, sequence );
383 * Convenience method -- always sets the first Sequence.
386 public void setSequence( final Sequence sequence ) {
387 if ( _sequences == null ) {
388 _sequences = new ArrayList<Sequence>();
390 if ( _sequences.size() == 0 ) {
391 _sequences.add( sequence );
394 _sequences.set( 0, sequence );
398 private void setSequences( final List<Sequence> sequences ) {
399 _sequences = sequences;
402 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
403 _taxonomies = taxonomies;
406 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
407 if ( _taxonomies == null ) {
408 _taxonomies = new ArrayList<Taxonomy>();
410 _taxonomies.set( index, taxonomy );
414 * Convenience method -- always sets the first Taxonomy.
417 public void setTaxonomy( final Taxonomy taxonomy ) {
418 if ( _taxonomies == null ) {
419 _taxonomies = new ArrayList<Taxonomy>();
421 if ( _taxonomies.size() == 0 ) {
422 _taxonomies.add( taxonomy );
425 _taxonomies.set( 0, taxonomy );
430 public StringBuffer toNHX() {
431 final StringBuffer sb = new StringBuffer();
432 if ( isHasNodeIdentifier() ) {
433 sb.append( getNodeIdentifier().toNHX() );
435 if ( isHasTaxonomy() ) {
436 sb.append( getTaxonomy().toNHX() );
438 if ( isHasSequence() ) {
439 sb.append( getSequence().toNHX() );
441 if ( isHasEvent() ) {
442 sb.append( getEvent().toNHX() );
444 if ( isHasProperties() ) {
445 sb.append( getProperties().toNHX() );
451 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
452 if ( isHasNodeIdentifier() ) {
453 writer.write( ForesterUtil.LINE_SEPARATOR );
454 writer.write( indentation );
455 // if ( !org.forester.util.ForesterUtil.isEmpty( getNodeIdentifier().getProvider() ) ) {
456 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
457 // .getValue(), PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR, getNodeIdentifier().getProvider() );
460 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
464 if ( isHasTaxonomy() ) {
465 for( final Taxonomy t : getTaxonomies() ) {
466 if ( !t.isEmpty() ) {
467 t.toPhyloXML( writer, level, indentation );
471 if ( isHasSequence() ) {
472 for( final Sequence s : getSequences() ) {
473 if ( !s.isEmpty() ) {
474 s.toPhyloXML( writer, level, indentation );
478 if ( isHasEvent() ) {
479 getEvent().toPhyloXML( writer, level, indentation );
481 if ( isHasBinaryCharacters() ) {
482 getBinaryCharacters().toPhyloXML( writer, level, indentation );
484 if ( isHasDistribution() ) {
485 for( final Distribution d : getDistributions() ) {
486 d.toPhyloXML( writer, level, indentation );
490 getDate().toPhyloXML( writer, level, indentation );
492 if ( isHasReference() ) {
493 for( final Reference r : getReferences() ) {
494 r.toPhyloXML( writer, level, indentation );
497 if ( isHasProperties() ) {
498 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
500 if ( ( getVector() != null )
501 && !getVector().isEmpty()
502 && ( ( getProperties() == null ) || getProperties()
503 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
504 final List<Property> ps = vectorToProperties( getVector() );
505 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
506 for( final Property p : ps ) {
507 p.toPhyloXML( writer, level, my_indent );
512 private List<Property> vectorToProperties( final List<Double> vector ) {
513 final List<Property> properties = new ArrayList<Property>();
514 for( int i = 0; i < vector.size(); ++i ) {
515 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
516 String.valueOf( vector.get( i ) ),
518 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
524 public void setVector( final List<Double> vector ) {
528 public List<Double> getVector() {
532 public String getNodeName() {
536 public void setNodeName( final String node_name ) {
537 _node_name = node_name;
540 public void setNodeVisualizations( final List<NodeVisualization> _node_visualizations ) {
541 this._node_visualizations = _node_visualizations;
544 public List<NodeVisualization> getNodeVisualizations() {
545 return _node_visualizations;