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 GO_ANNOTATIONS_WITH_COUNTS,
57 DOMAINS_COLLAPSED_PER_PROTEIN;
59 private String _node_name;
61 private List<Sequence> _sequences;
62 private List<Taxonomy> _taxonomies;
63 private List<Distribution> _distributions;
65 private BinaryCharacters _binary_characters;
66 private PropertiesMap _properties;
67 private List<Reference> _references;
68 private List<Double> _vector;
69 private NodeVisualData _node_visual_data;
80 _distributions = null;
82 _binary_characters = null;
86 _node_visual_data = null;
89 public void addDistribution( final Distribution distribution ) {
90 if ( _distributions == null ) {
91 _distributions = new ArrayList<Distribution>();
93 _distributions.add( distribution );
96 public void addReference( final Reference reference ) {
97 if ( _references == null ) {
98 _references = new ArrayList<Reference>();
100 _references.add( reference );
103 public void addSequence( final Sequence sequence ) {
104 if ( _sequences == null ) {
105 _sequences = new ArrayList<Sequence>();
107 _sequences.add( sequence );
110 public void addTaxonomy( final Taxonomy taxonomy ) {
111 if ( _taxonomies == null ) {
112 _taxonomies = new ArrayList<Taxonomy>();
114 _taxonomies.add( taxonomy );
118 public StringBuffer asSimpleText() {
119 throw new UnsupportedOperationException();
123 public StringBuffer asText() {
124 throw new UnsupportedOperationException();
128 public PhylogenyData copy() {
129 final NodeData new_data = new NodeData();
130 new_data.setNodeName( getNodeName() );
131 if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
132 new_data.setSequences( new ArrayList<Sequence>() );
133 for( final Sequence s : getSequences() ) {
135 new_data.addSequence( ( Sequence ) s.copy() );
139 if ( isHasEvent() ) {
140 new_data.setEvent( ( Event ) getEvent().copy() );
142 if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
143 new_data.setTaxonomies( new ArrayList<Taxonomy>() );
144 for( final Taxonomy t : getTaxonomies() ) {
146 new_data.addTaxonomy( ( Taxonomy ) t.copy() );
150 if ( isHasBinaryCharacters() ) {
151 new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
153 if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
154 new_data.setReferences( new ArrayList<Reference>() );
155 for( final Reference r : getReferences() ) {
157 new_data.addReference( ( Reference ) r.copy() );
161 if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
162 new_data.setDistributions( new ArrayList<Distribution>() );
163 for( final Distribution d : getDistributions() ) {
165 new_data.addDistribution( ( Distribution ) d.copy() );
169 if ( ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
170 new_data.setNodeVisualData( ( NodeVisualData ) getNodeVisualData().copy() );
173 new_data.setDate( ( Date ) getDate().copy() );
175 if ( isHasProperties() ) {
176 new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
181 public BinaryCharacters getBinaryCharacters() {
182 return _binary_characters;
185 public Date getDate() {
190 * Convenience method -- always returns the first Distribution.
192 * @return Distribution
194 public Distribution getDistribution() {
195 return getDistribution( 0 );
198 public Distribution getDistribution( final int index ) {
199 return _distributions.get( index );
202 public List<Distribution> getDistributions() {
203 return _distributions;
206 public Event getEvent() {
210 public PropertiesMap getProperties() {
215 * Convenience method -- always returns the first Reference.
220 public Reference getReference() {
221 return getReference( 0 );
224 public Reference getReference( final int index ) {
225 return _references.get( index );
228 public List<Reference> getReferences() {
233 * Convenience method -- always returns the first Sequence.
237 public Sequence getSequence() {
238 return getSequence( 0 );
241 public Sequence getSequence( final int index ) {
242 return _sequences.get( index );
245 public List<Sequence> getSequences() {
249 public List<Taxonomy> getTaxonomies() {
254 * Convenience method -- always returns the first Taxonomy.
259 public Taxonomy getTaxonomy() {
260 return getTaxonomy( 0 );
263 public Taxonomy getTaxonomy( final int index ) {
264 return _taxonomies.get( index );
268 public boolean isEqual( final PhylogenyData data ) {
269 throw new NoSuchMethodError();
272 public boolean isHasBinaryCharacters() {
273 return getBinaryCharacters() != null;
276 public boolean isEmpty() {
277 return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
278 && !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
282 public boolean isHasDate() {
283 return ( getDate() != null )
284 && ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
285 || !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
286 .isEmpty( getDate().getUnit() ) );
289 public boolean isHasDistribution() {
290 return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
291 .isEmpty( getDistribution().getDesc() ) )
292 || ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
293 .getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
296 public boolean isHasEvent() {
297 return getEvent() != null;
300 public boolean isHasProperties() {
301 return ( getProperties() != null ) && ( getProperties().size() > 0 );
304 public boolean isHasReference() {
305 return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
306 && ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
307 .getDescription() ) );
310 public boolean isHasSequence() {
311 return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
314 public boolean isHasTaxonomy() {
315 return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
318 public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
319 _binary_characters = binary_characters;
322 public void setDate( final Date date ) {
327 * Convenience method -- always sets the first Distribution.
330 public void setDistribution( final Distribution distribution ) {
331 if ( _distributions == null ) {
332 _distributions = new ArrayList<Distribution>();
334 if ( _distributions.size() == 0 ) {
335 _distributions.add( distribution );
338 _distributions.set( 0, distribution );
342 public void setDistribution( final int index, final Distribution distribution ) {
343 if ( _distributions == null ) {
344 _distributions = new ArrayList<Distribution>();
346 _distributions.set( index, distribution );
349 private void setDistributions( final List<Distribution> distributions ) {
350 _distributions = distributions;
353 public void setEvent( final Event event ) {
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 ( isHasTaxonomy() ) {
460 for( final Taxonomy t : getTaxonomies() ) {
461 if ( !t.isEmpty() ) {
462 t.toPhyloXML( writer, level, indentation );
466 if ( isHasSequence() ) {
467 for( final Sequence s : getSequences() ) {
468 if ( !s.isEmpty() ) {
469 s.toPhyloXML( writer, level, indentation );
473 if ( isHasEvent() ) {
474 getEvent().toPhyloXML( writer, level, indentation );
476 if ( isHasBinaryCharacters() ) {
477 getBinaryCharacters().toPhyloXML( writer, level, indentation );
479 if ( isHasDistribution() ) {
480 for( final Distribution d : getDistributions() ) {
481 d.toPhyloXML( writer, level, indentation );
485 getDate().toPhyloXML( writer, level, indentation );
487 if ( isHasReference() ) {
488 for( final Reference r : getReferences() ) {
489 r.toPhyloXML( writer, level, indentation );
492 if ( isHasProperties() ) {
493 getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
495 if ( ( level == 0 ) && ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
496 getNodeVisualData().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
498 if ( ( getVector() != null )
499 && !getVector().isEmpty()
500 && ( ( getProperties() == null ) || getProperties()
501 .getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
502 final List<Property> ps = vectorToProperties( getVector() );
503 final String my_indent = indentation.substring( 0, indentation.length() - 2 );
504 for( final Property p : ps ) {
505 p.toPhyloXML( writer, level, my_indent );
510 private List<Property> vectorToProperties( final List<Double> vector ) {
511 final List<Property> properties = new ArrayList<Property>();
512 for( int i = 0; i < vector.size(); ++i ) {
513 properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
514 String.valueOf( vector.get( i ) ),
516 PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
522 public void setVector( final List<Double> vector ) {
526 public List<Double> getVector() {
530 public String getNodeName() {
534 public void setNodeName( final String node_name ) {
535 _node_name = node_name;
538 public void setNodeVisualData( final NodeVisualData node_visual_data ) {
539 _node_visual_data = node_visual_data;
542 public NodeVisualData getNodeVisualData() {
543 return _node_visual_data;