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 SEQUENCE_MOL_SEQ_FASTA,
50 TAXONOMY_SCIENTIFIC_NAME,
55 private String _node_name;
57 private List<Sequence> _sequences;
58 private Identifier _node_identifier;
59 private List<Taxonomy> _taxonomies;
60 private List<Distribution> _distributions;
62 private BinaryCharacters _binary_characters;
63 private PropertiesMap _properties;
64 private List<Reference> _references;
65 private List<Double> _vector;
66 private List<NodeVisualization> _node_visualizations;
76 public void addDistribution( final Distribution distribution ) {
77 if ( _distributions == null ) {
78 _distributions = new ArrayList<Distribution>();
80 _distributions.add( distribution );
83 public void addReference( final Reference reference ) {
84 if ( _references == null ) {
85 _references = new ArrayList<Reference>();
87 _references.add( reference );
90 public void addSequence( final Sequence sequence ) {
91 if ( _sequences == null ) {
92 _sequences = new ArrayList<Sequence>();
94 _sequences.add( sequence );
97 public void addTaxonomy( final Taxonomy taxonomy ) {
98 if ( _taxonomies == null ) {
99 _taxonomies = new ArrayList<Taxonomy>();
101 _taxonomies.add( taxonomy );
105 public StringBuffer asSimpleText() {
106 throw new UnsupportedOperationException();
110 public StringBuffer asText() {
111 throw new UnsupportedOperationException();
115 public PhylogenyData copy() {
116 final NodeData new_data = new NodeData();
117 new_data.setNodeName( getNodeName() );
118 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
119 new_data.setSequences( new ArrayList<Sequence>() );
120 for( final Sequence s : getSequences() ) {
122 new_data.addSequence( ( Sequence ) s.copy() );
126 if ( isHasEvent() ) {
127 new_data.setEvent( ( Event ) getEvent().copy() );
129 if ( isHasNodeIdentifier() ) {
130 new_data.setNodeIdentifier( ( Identifier ) getNodeIdentifier().copy() );
132 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
133 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
134 for( final Taxonomy t : getTaxonomies() ) {
136 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
140 if ( isHasBinaryCharacters() ) {
141 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
143 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
144 new_data.setReferences( new ArrayList<Reference>() );
145 for( final Reference r : getReferences() ) {
147 new_data.addReference( ( Reference ) r.copy() );
151 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
152 new_data.setDistributions( new ArrayList<Distribution>() );
153 for( final Distribution d : getDistributions() ) {
155 new_data.addDistribution( ( Distribution ) d.copy() );
159 if ( ( getNodeVisualizations() != null ) && ( getNodeVisualizations().size() > 0 ) ) {
160 new_data.setNodeVisualizations( new ArrayList<NodeVisualization>() );
161 for( final NodeVisualization v : getNodeVisualizations() ) {
163 new_data.getNodeVisualizations().add( ( NodeVisualization ) v.copy() );
168 new_data.setDate( ( Date ) getDate().copy() );
170 if ( isHasProperties() ) {
171 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
176 public BinaryCharacters getBinaryCharacters() {
177 return _binary_characters;
180 public Date getDate() {
185 * Convenience method -- always returns the first Distribution.
187 * @return Distribution
189 public Distribution getDistribution() {
190 return getDistribution( 0 );
193 public Distribution getDistribution( final int index ) {
194 return _distributions.get( index );
197 public List<Distribution> getDistributions() {
198 return _distributions;
201 public Event getEvent() {
205 public Identifier getNodeIdentifier() {
206 return _node_identifier;
209 public PropertiesMap getProperties() {
214 * Convenience method -- always returns the first Reference.
219 public Reference getReference() {
220 return getReference( 0 );
223 public Reference getReference( final int index ) {
224 return _references.get( index );
227 public List<Reference> getReferences() {
232 * Convenience method -- always returns the first Sequence.
236 public Sequence getSequence() {
237 return getSequence( 0 );
240 public Sequence getSequence( final int index ) {
241 return _sequences.get( index );
244 public List<Sequence> getSequences() {
248 public List<Taxonomy> getTaxonomies() {
253 * Convenience method -- always returns the first Taxonomy.
258 public Taxonomy getTaxonomy() {
259 return getTaxonomy( 0 );
262 public Taxonomy getTaxonomy( final int index ) {
263 return _taxonomies.get( index );
267 public boolean isEqual( final PhylogenyData data ) {
268 throw new UnsupportedOperationException();
271 public boolean isHasBinaryCharacters() {
272 return getBinaryCharacters() != null;
275 public boolean isHasDate() {
276 return ( getDate() != null )
277 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
278 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
279 .isEmpty( getDate().getUnit() ) );
282 public boolean isHasDistribution() {
283 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
284 .isEmpty( getDistribution().getDesc() ) )
285 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
286 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
289 public boolean isHasEvent() {
290 return getEvent() != null;
293 public boolean isHasNodeIdentifier() {
294 return getNodeIdentifier() != null;
297 public boolean isHasProperties() {
298 return ( getProperties() != null ) && ( getProperties().size() > 0 );
301 public boolean isHasReference() {
302 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
303 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
304 .getDescription() ) );
307 public boolean isHasSequence() {
308 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
311 public boolean isHasTaxonomy() {
312 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
315 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
316 _binary_characters = binary_characters;
319 public void setDate( final Date date ) {
324 * Convenience method -- always sets the first Distribution.
327 public void setDistribution( final Distribution distribution ) {
328 if ( _distributions == null ) {
329 _distributions = new ArrayList<Distribution>();
331 if ( _distributions.size() == 0 ) {
332 _distributions.add( distribution );
335 _distributions.set( 0, distribution );
339 public void setDistribution( final int index, final Distribution distribution ) {
340 if ( _distributions == null ) {
341 _distributions = new ArrayList<Distribution>();
343 _distributions.set( index, distribution );
346 private void setDistributions( final List<Distribution> distributions ) {
347 _distributions = distributions;
350 public void setEvent( final Event event ) {
354 public void setNodeIdentifier( final Identifier node_identifier ) {
355 _node_identifier = node_identifier;
358 public void setProperties( final PropertiesMap custom_data ) {
359 _properties = custom_data;
362 public void setReference( final int index, final Reference reference ) {
363 if ( _references == null ) {
364 _references = new ArrayList<Reference>();
366 _references.set( index, reference );
370 * Convenience method -- always sets the first Reference.
373 public void setReference( final Reference reference ) {
374 if ( _references == null ) {
375 _references = new ArrayList<Reference>();
377 if ( _references.size() == 0 ) {
378 _references.add( reference );
381 _references.set( 0, reference );
385 private void setReferences( final List<Reference> references ) {
386 _references = references;
389 public void setSequence( final int index, final Sequence sequence ) {
390 if ( _sequences == null ) {
391 _sequences = new ArrayList<Sequence>();
393 _sequences.set( index, sequence );
397 * Convenience method -- always sets the first Sequence.
400 public void setSequence( final Sequence sequence ) {
401 if ( _sequences == null ) {
402 _sequences = new ArrayList<Sequence>();
404 if ( _sequences.size() == 0 ) {
405 _sequences.add( sequence );
408 _sequences.set( 0, sequence );
412 private void setSequences( final List<Sequence> sequences ) {
413 _sequences = sequences;
416 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
417 _taxonomies = taxonomies;
420 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
421 if ( _taxonomies == null ) {
422 _taxonomies = new ArrayList<Taxonomy>();
424 _taxonomies.set( index, taxonomy );
428 * Convenience method -- always sets the first Taxonomy.
431 public void setTaxonomy( final Taxonomy taxonomy ) {
432 if ( _taxonomies == null ) {
433 _taxonomies = new ArrayList<Taxonomy>();
435 if ( _taxonomies.size() == 0 ) {
436 _taxonomies.add( taxonomy );
439 _taxonomies.set( 0, taxonomy );
444 public StringBuffer toNHX() {
445 final StringBuffer sb = new StringBuffer();
446 if ( isHasTaxonomy() ) {
447 sb.append( getTaxonomy().toNHX() );
449 if ( isHasSequence() ) {
450 sb.append( getSequence().toNHX() );
452 if ( isHasEvent() ) {
453 sb.append( getEvent().toNHX() );
459 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
460 if ( isHasNodeIdentifier() ) {
461 writer.write( ForesterUtil.LINE_SEPARATOR );
462 writer.write( indentation );
463 // if ( !org.forester.util.ForesterUtil.isEmpty( getNodeIdentifier().getProvider() ) ) {
464 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
465 // .getValue(), PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR, getNodeIdentifier().getProvider() );
468 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
472 if ( isHasTaxonomy() ) {
473 for( final Taxonomy t : getTaxonomies() ) {
474 if ( !t.isEmpty() ) {
475 t.toPhyloXML( writer, level, indentation );
479 if ( isHasSequence() ) {
480 for( final Sequence s : getSequences() ) {
481 if ( !s.isEmpty() ) {
482 s.toPhyloXML( writer, level, indentation );
486 if ( isHasEvent() ) {
487 getEvent().toPhyloXML( writer, level, indentation );
489 if ( isHasBinaryCharacters() ) {
490 getBinaryCharacters().toPhyloXML( writer, level, indentation );
492 if ( isHasDistribution() ) {
493 for( final Distribution d : getDistributions() ) {
494 d.toPhyloXML( writer, level, indentation );
498 getDate().toPhyloXML( writer, level, indentation );
500 if ( isHasReference() ) {
501 for( final Reference r : getReferences() ) {
502 r.toPhyloXML( writer, level, indentation );
505 if ( isHasProperties() ) {
506 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
508 if ( ( getVector() != null )
509 && !getVector().isEmpty()
510 && ( ( getProperties() == null ) || getProperties()
511 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
512 final List<Property> ps = vectorToProperties( getVector() );
513 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
514 for( final Property p : ps ) {
515 p.toPhyloXML( writer, level, my_indent );
520 private List<Property> vectorToProperties( final List<Double> vector ) {
521 final List<Property> properties = new ArrayList<Property>();
522 for( int i = 0; i < vector.size(); ++i ) {
523 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
524 String.valueOf( vector.get( i ) ),
526 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
532 public void setVector( final List<Double> vector ) {
536 public List<Double> getVector() {
540 public String getNodeName() {
544 public void setNodeName( final String node_name ) {
545 _node_name = node_name;
548 public void setNodeVisualizations( final List<NodeVisualization> _node_visualizations ) {
549 this._node_visualizations = _node_visualizations;
552 public List<NodeVisualization> getNodeVisualizations() {
553 return _node_visualizations;