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;
61 public void addDistribution( final Distribution distribution ) {
62 if ( _distributions == null ) {
63 _distributions = new ArrayList<Distribution>();
65 _distributions.add( distribution );
68 public void addReference( final Reference reference ) {
69 if ( _references == null ) {
70 _references = new ArrayList<Reference>();
72 _references.add( reference );
75 public void addSequence( final Sequence sequence ) {
76 if ( _sequences == null ) {
77 _sequences = new ArrayList<Sequence>();
79 _sequences.add( sequence );
82 public void addTaxonomy( final Taxonomy taxonomy ) {
83 if ( _taxonomies == null ) {
84 _taxonomies = new ArrayList<Taxonomy>();
86 _taxonomies.add( taxonomy );
90 public StringBuffer asSimpleText() {
91 throw new UnsupportedOperationException();
95 public StringBuffer asText() {
96 throw new UnsupportedOperationException();
100 public PhylogenyData copy() {
101 final NodeData new_data = new NodeData();
102 new_data.setNodeName( getNodeName() );
103 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
104 new_data.setSequences( new ArrayList<Sequence>() );
105 for( final Sequence s : getSequences() ) {
107 new_data.addSequence( ( Sequence ) s.copy() );
111 if ( isHasEvent() ) {
112 new_data.setEvent( ( Event ) getEvent().copy() );
114 if ( isHasNodeIdentifier() ) {
115 new_data.setNodeIdentifier( ( Identifier ) getNodeIdentifier().copy() );
117 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
118 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
119 for( final Taxonomy t : getTaxonomies() ) {
121 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
125 if ( isHasBinaryCharacters() ) {
126 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
128 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
129 new_data.setReferences( new ArrayList<Reference>() );
130 for( final Reference r : getReferences() ) {
132 new_data.addReference( ( Reference ) r.copy() );
136 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
137 new_data.setDistributions( new ArrayList<Distribution>() );
138 for( final Distribution d : getDistributions() ) {
140 new_data.addDistribution( ( Distribution ) d.copy() );
145 new_data.setDate( ( Date ) getDate().copy() );
147 if ( isHasProperties() ) {
148 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
153 public BinaryCharacters getBinaryCharacters() {
154 return _binary_characters;
157 public Date getDate() {
162 * Convenience method -- always returns the first Distribution.
164 * @return Distribution
166 public Distribution getDistribution() {
167 return getDistribution( 0 );
170 public Distribution getDistribution( final int index ) {
171 return _distributions.get( index );
174 public List<Distribution> getDistributions() {
175 return _distributions;
178 public Event getEvent() {
182 public Identifier getNodeIdentifier() {
183 return _node_identifier;
186 public PropertiesMap getProperties() {
191 * Convenience method -- always returns the first Reference.
196 public Reference getReference() {
197 return getReference( 0 );
200 public Reference getReference( final int index ) {
201 return _references.get( index );
204 public List<Reference> getReferences() {
209 * Convenience method -- always returns the first Sequence.
213 public Sequence getSequence() {
214 return getSequence( 0 );
217 public Sequence getSequence( final int index ) {
218 return _sequences.get( index );
221 public List<Sequence> getSequences() {
225 public List<Taxonomy> getTaxonomies() {
230 * Convenience method -- always returns the first Taxonomy.
235 public Taxonomy getTaxonomy() {
236 return getTaxonomy( 0 );
239 public Taxonomy getTaxonomy( final int index ) {
240 return _taxonomies.get( index );
244 public boolean isEqual( final PhylogenyData data ) {
245 throw new UnsupportedOperationException();
248 public boolean isHasBinaryCharacters() {
249 return getBinaryCharacters() != null;
252 public boolean isHasDate() {
253 return ( getDate() != null )
254 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
255 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
256 .isEmpty( getDate().getUnit() ) );
259 public boolean isHasDistribution() {
260 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
261 .isEmpty( getDistribution().getDesc() ) )
262 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
263 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
266 public boolean isHasEvent() {
267 return getEvent() != null;
270 public boolean isHasNodeIdentifier() {
271 return getNodeIdentifier() != null;
274 public boolean isHasProperties() {
275 return getProperties() != null;
278 public boolean isHasReference() {
279 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
280 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
281 .getDescription() ) );
284 public boolean isHasSequence() {
285 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
288 public boolean isHasTaxonomy() {
289 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
292 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
293 _binary_characters = binary_characters;
296 public void setDate( final Date date ) {
301 * Convenience method -- always sets the first Distribution.
304 public void setDistribution( final Distribution distribution ) {
305 if ( _distributions == null ) {
306 _distributions = new ArrayList<Distribution>();
308 if ( _distributions.size() == 0 ) {
309 _distributions.add( distribution );
312 _distributions.set( 0, distribution );
316 public void setDistribution( final int index, final Distribution distribution ) {
317 if ( _distributions == null ) {
318 _distributions = new ArrayList<Distribution>();
320 _distributions.set( index, distribution );
323 private void setDistributions( final List<Distribution> distributions ) {
324 _distributions = distributions;
327 public void setEvent( final Event event ) {
331 public void setNodeIdentifier( final Identifier node_identifier ) {
332 _node_identifier = node_identifier;
335 public void setProperties( final PropertiesMap custom_data ) {
336 _properties = custom_data;
339 public void setReference( final int index, final Reference reference ) {
340 if ( _references == null ) {
341 _references = new ArrayList<Reference>();
343 _references.set( index, reference );
347 * Convenience method -- always sets the first Reference.
350 public void setReference( final Reference reference ) {
351 if ( _references == null ) {
352 _references = new ArrayList<Reference>();
354 if ( _references.size() == 0 ) {
355 _references.add( reference );
358 _references.set( 0, reference );
362 private void setReferences( final List<Reference> references ) {
363 _references = references;
366 public void setSequence( final int index, final Sequence sequence ) {
367 if ( _sequences == null ) {
368 _sequences = new ArrayList<Sequence>();
370 _sequences.set( index, sequence );
374 * Convenience method -- always sets the first Sequence.
377 public void setSequence( final Sequence sequence ) {
378 if ( _sequences == null ) {
379 _sequences = new ArrayList<Sequence>();
381 if ( _sequences.size() == 0 ) {
382 _sequences.add( sequence );
385 _sequences.set( 0, sequence );
389 private void setSequences( final List<Sequence> sequences ) {
390 _sequences = sequences;
393 private void setTaxonomies( final List<Taxonomy> taxonomies ) {
394 _taxonomies = taxonomies;
397 public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
398 if ( _taxonomies == null ) {
399 _taxonomies = new ArrayList<Taxonomy>();
401 _taxonomies.set( index, taxonomy );
405 * Convenience method -- always sets the first Taxonomy.
408 public void setTaxonomy( final Taxonomy taxonomy ) {
409 if ( _taxonomies == null ) {
410 _taxonomies = new ArrayList<Taxonomy>();
412 if ( _taxonomies.size() == 0 ) {
413 _taxonomies.add( taxonomy );
416 _taxonomies.set( 0, taxonomy );
421 public StringBuffer toNHX() {
422 final StringBuffer sb = new StringBuffer();
423 if ( isHasNodeIdentifier() ) {
424 sb.append( getNodeIdentifier().toNHX() );
426 if ( isHasTaxonomy() ) {
427 sb.append( getTaxonomy().toNHX() );
429 if ( isHasSequence() ) {
430 sb.append( getSequence().toNHX() );
432 if ( isHasEvent() ) {
433 sb.append( getEvent().toNHX() );
435 if ( isHasProperties() ) {
436 sb.append( getProperties().toNHX() );
442 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
443 if ( isHasNodeIdentifier() ) {
444 writer.write( ForesterUtil.LINE_SEPARATOR );
445 writer.write( indentation );
446 // if ( !org.forester.util.ForesterUtil.isEmpty( getNodeIdentifier().getProvider() ) ) {
447 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
448 // .getValue(), PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR, getNodeIdentifier().getProvider() );
451 // PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.NODE_IDENTIFIER, getNodeIdentifier()
455 if ( isHasTaxonomy() ) {
456 for( final Taxonomy t : getTaxonomies() ) {
457 if ( !t.isEmpty() ) {
458 t.toPhyloXML( writer, level, indentation );
462 if ( isHasSequence() ) {
463 for( final Sequence s : getSequences() ) {
464 if ( !s.isEmpty() ) {
465 s.toPhyloXML( writer, level, indentation );
469 if ( isHasEvent() ) {
470 getEvent().toPhyloXML( writer, level, indentation );
472 if ( isHasBinaryCharacters() ) {
473 getBinaryCharacters().toPhyloXML( writer, level, indentation );
475 if ( isHasDistribution() ) {
476 for( final Distribution d : getDistributions() ) {
477 d.toPhyloXML( writer, level, indentation );
481 getDate().toPhyloXML( writer, level, indentation );
483 if ( isHasReference() ) {
484 for( final Reference r : getReferences() ) {
485 r.toPhyloXML( writer, level, indentation );
488 if ( isHasProperties() ) {
489 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
491 if ( ( getVector() != null )
492 && !getVector().isEmpty()
493 && ( ( getProperties() == null ) || getProperties()
494 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
495 final List<Property> ps = vectorToProperties( getVector() );
496 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
497 for( final Property p : ps ) {
498 p.toPhyloXML( writer, level, my_indent );
503 private List<Property> vectorToProperties( final List<Double> vector ) {
504 final List<Property> properties = new ArrayList<Property>();
505 for( int i = 0; i < vector.size(); ++i ) {
506 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
507 String.valueOf( vector.get( i ) ),
509 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
515 public void setVector( final List<Double> vector ) {
519 public List<Double> getVector() {
523 public String getNodeName() {
527 public void setNodeName( final String node_name ) {
528 _node_name = node_name;