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 {
47 SEQUENCE_MOL_SEQ_FASTA,
49 TAXONOMY_SCIENTIFIC_NAME,
54 private String _node_name;
56 private List<Sequence> _sequences;
57 private Identifier _node_identifier;
58 private List<Taxonomy> _taxonomies;
59 private List<Distribution> _distributions;
61 private BinaryCharacters _binary_characters;
62 private PropertiesMap _properties;
63 private List<Reference> _references;
64 private List<Double> _vector;
65 private List<NodeVisualization> _node_visualizations;
75 public void addDistribution( final Distribution distribution ) {
76 if ( _distributions == null ) {
77 _distributions = new ArrayList<Distribution>();
79 _distributions.add( distribution );
82 public void addReference( final Reference reference ) {
83 if ( _references == null ) {
84 _references = new ArrayList<Reference>();
86 _references.add( reference );
89 public void addSequence( final Sequence sequence ) {
90 if ( _sequences == null ) {
91 _sequences = new ArrayList<Sequence>();
93 _sequences.add( sequence );
96 public void addTaxonomy( final Taxonomy taxonomy ) {
97 if ( _taxonomies == null ) {
98 _taxonomies = new ArrayList<Taxonomy>();
100 _taxonomies.add( taxonomy );
104 public StringBuffer asSimpleText() {
105 throw new UnsupportedOperationException();
109 public StringBuffer asText() {
110 throw new UnsupportedOperationException();
114 public PhylogenyData copy() {
115 final NodeData new_data = new NodeData();
116 new_data.setNodeName( getNodeName() );
117 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
118 new_data.setSequences( new ArrayList<Sequence>() );
119 for( final Sequence s : getSequences() ) {
121 new_data.addSequence( ( Sequence ) s.copy() );
125 if ( isHasEvent() ) {
126 new_data.setEvent( ( Event ) getEvent().copy() );
128 if ( isHasNodeIdentifier() ) {
129 new_data.setNodeIdentifier( ( Identifier ) getNodeIdentifier().copy() );
131 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
132 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
133 for( final Taxonomy t : getTaxonomies() ) {
135 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
139 if ( isHasBinaryCharacters() ) {
140 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
142 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
143 new_data.setReferences( new ArrayList<Reference>() );
144 for( final Reference r : getReferences() ) {
146 new_data.addReference( ( Reference ) r.copy() );
150 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
151 new_data.setDistributions( new ArrayList<Distribution>() );
152 for( final Distribution d : getDistributions() ) {
154 new_data.addDistribution( ( Distribution ) d.copy() );
158 if ( ( getNodeVisualizations() != null ) && ( getNodeVisualizations().size() > 0 ) ) {
159 new_data.setNodeVisualizations( new ArrayList<NodeVisualization>() );
160 for( final NodeVisualization v : getNodeVisualizations() ) {
162 new_data.getNodeVisualizations().add( ( NodeVisualization ) v.copy() );
167 new_data.setDate( ( Date ) getDate().copy() );
169 if ( isHasProperties() ) {
170 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
175 public BinaryCharacters getBinaryCharacters() {
176 return _binary_characters;
179 public Date getDate() {
184 * Convenience method -- always returns the first Distribution.
186 * @return Distribution
188 public Distribution getDistribution() {
189 return getDistribution( 0 );
192 public Distribution getDistribution( final int index ) {
193 return _distributions.get( index );
196 public List<Distribution> getDistributions() {
197 return _distributions;
200 public Event getEvent() {
204 public Identifier getNodeIdentifier() {
205 return _node_identifier;
208 public PropertiesMap getProperties() {
213 * Convenience method -- always returns the first Reference.
218 public Reference getReference() {
219 return getReference( 0 );
222 public Reference getReference( final int index ) {
223 return _references.get( index );
226 public List<Reference> getReferences() {
231 * Convenience method -- always returns the first Sequence.
235 public Sequence getSequence() {
236 return getSequence( 0 );
239 public Sequence getSequence( final int index ) {
240 return _sequences.get( index );
243 public List<Sequence> getSequences() {
247 public List<Taxonomy> getTaxonomies() {
252 * Convenience method -- always returns the first Taxonomy.
257 public Taxonomy getTaxonomy() {
258 return getTaxonomy( 0 );
261 public Taxonomy getTaxonomy( final int index ) {
262 return _taxonomies.get( index );
266 public boolean isEqual( final PhylogenyData data ) {
267 throw new UnsupportedOperationException();
270 public boolean isHasBinaryCharacters() {
271 return getBinaryCharacters() != null;
274 public boolean isHasDate() {
275 return ( getDate() != null )
276 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
277 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
278 .isEmpty( getDate().getUnit() ) );
281 public boolean isHasDistribution() {
282 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
283 .isEmpty( getDistribution().getDesc() ) )
284 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
285 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
288 public boolean isHasEvent() {
289 return getEvent() != null;
292 public boolean isHasNodeIdentifier() {
293 return getNodeIdentifier() != null;
296 public boolean isHasProperties() {
297 return ( getProperties() != null ) && ( getProperties().size() > 0 );
300 public boolean isHasReference() {
301 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
302 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
303 .getDescription() ) );
306 public boolean isHasSequence() {
307 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
310 public boolean isHasTaxonomy() {
311 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
314 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
315 _binary_characters = binary_characters;
318 public void setDate( final Date date ) {
323 * Convenience method -- always sets the first Distribution.
326 public void setDistribution( final Distribution distribution ) {
327 if ( _distributions == null ) {
328 _distributions = new ArrayList<Distribution>();
330 if ( _distributions.size() == 0 ) {
331 _distributions.add( distribution );
334 _distributions.set( 0, distribution );
338 public void setDistribution( final int index, final Distribution distribution ) {
339 if ( _distributions == null ) {
340 _distributions = new ArrayList<Distribution>();
342 _distributions.set( index, distribution );
345 private void setDistributions( final List<Distribution> distributions ) {
346 _distributions = distributions;
349 public void setEvent( final Event event ) {
353 public void setNodeIdentifier( final Identifier node_identifier ) {
354 _node_identifier = node_identifier;
357 public void setProperties( final PropertiesMap custom_data ) {
358 _properties = custom_data;
361 public void setReference( final int index, final Reference reference ) {
362 if ( _references == null ) {
363 _references = new ArrayList<Reference>();
365 _references.set( index, reference );
369 * Convenience method -- always sets the first Reference.
372 public void setReference( final Reference reference ) {
373 if ( _references == null ) {
374 _references = new ArrayList<Reference>();
376 if ( _references.size() == 0 ) {
377 _references.add( reference );
380 _references.set( 0, reference );
384 private void setReferences( final List<Reference> references ) {
385 _references = references;
388 public void setSequence( final int index, final Sequence sequence ) {
389 if ( _sequences == null ) {
390 _sequences = new ArrayList<Sequence>();
392 _sequences.set( index, sequence );
396 * Convenience method -- always sets the first Sequence.
399 public void setSequence( final Sequence sequence ) {
400 if ( _sequences == null ) {
401 _sequences = new ArrayList<Sequence>();
403 if ( _sequences.size() == 0 ) {
404 _sequences.add( sequence );
407 _sequences.set( 0, sequence );
411 private void setSequences( final List<Sequence> sequences ) {
412 _sequences = sequences;
415 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
416 _taxonomies = taxonomies;
419 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
420 if ( _taxonomies == null ) {
421 _taxonomies = new ArrayList<Taxonomy>();
423 _taxonomies.set( index, taxonomy );
427 * Convenience method -- always sets the first Taxonomy.
430 public void setTaxonomy( final Taxonomy taxonomy ) {
431 if ( _taxonomies == null ) {
432 _taxonomies = new ArrayList<Taxonomy>();
434 if ( _taxonomies.size() == 0 ) {
435 _taxonomies.add( taxonomy );
438 _taxonomies.set( 0, taxonomy );
443 public StringBuffer toNHX() {
444 final StringBuffer sb = new StringBuffer();
445 if ( isHasTaxonomy() ) {
446 sb.append( getTaxonomy().toNHX() );
448 if ( isHasSequence() ) {
449 sb.append( getSequence().toNHX() );
451 if ( isHasEvent() ) {
452 sb.append( getEvent().toNHX() );
458 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
459 if ( isHasNodeIdentifier() ) {
460 writer.write( ForesterUtil.LINE_SEPARATOR );
461 writer.write( indentation );
462 // if ( !org.forester.util.ForesterUtil.isEmpty( getNodeIdentifier().getProvider() ) ) {
463 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
464 // .getValue(), PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR, getNodeIdentifier().getProvider() );
467 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
471 if ( isHasTaxonomy() ) {
472 for( final Taxonomy t : getTaxonomies() ) {
473 if ( !t.isEmpty() ) {
474 t.toPhyloXML( writer, level, indentation );
478 if ( isHasSequence() ) {
479 for( final Sequence s : getSequences() ) {
480 if ( !s.isEmpty() ) {
481 s.toPhyloXML( writer, level, indentation );
485 if ( isHasEvent() ) {
486 getEvent().toPhyloXML( writer, level, indentation );
488 if ( isHasBinaryCharacters() ) {
489 getBinaryCharacters().toPhyloXML( writer, level, indentation );
491 if ( isHasDistribution() ) {
492 for( final Distribution d : getDistributions() ) {
493 d.toPhyloXML( writer, level, indentation );
497 getDate().toPhyloXML( writer, level, indentation );
499 if ( isHasReference() ) {
500 for( final Reference r : getReferences() ) {
501 r.toPhyloXML( writer, level, indentation );
504 if ( isHasProperties() ) {
505 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
507 if ( ( getVector() != null )
508 && !getVector().isEmpty()
509 && ( ( getProperties() == null ) || getProperties()
510 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
511 final List<Property> ps = vectorToProperties( getVector() );
512 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
513 for( final Property p : ps ) {
514 p.toPhyloXML( writer, level, my_indent );
519 private List<Property> vectorToProperties( final List<Double> vector ) {
520 final List<Property> properties = new ArrayList<Property>();
521 for( int i = 0; i < vector.size(); ++i ) {
522 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
523 String.valueOf( vector.get( i ) ),
525 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
531 public void setVector( final List<Double> vector ) {
535 public List<Double> getVector() {
539 public String getNodeName() {
543 public void setNodeName( final String node_name ) {
544 _node_name = node_name;
547 public void setNodeVisualizations( final List<NodeVisualization> _node_visualizations ) {
548 this._node_visualizations = _node_visualizations;
551 public List<NodeVisualization> getNodeVisualizations() {
552 return _node_visualizations;