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
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
26 package org.forester.phylogeny.data;
28 import java.io.IOException;
29 import java.io.Writer;
31 import org.forester.io.parsers.nhx.NHXtags;
32 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
33 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
34 import org.forester.util.ForesterUtil;
36 public class Confidence implements PhylogenyData, Comparable<Confidence> {
38 public final static int CONFIDENCE_DEFAULT_VALUE = -9999;
39 private double _value;
47 public Confidence( final double value, final String type ) {
50 setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
53 public Confidence( final double value, final String type, final double sd ) {
56 setStandardDeviation( sd );
60 public StringBuffer asSimpleText() {
61 return new StringBuffer().append( ForesterUtil.FORMATTER_6.format( getValue() ) );
65 public StringBuffer asText() {
66 final StringBuffer sb = new StringBuffer();
67 if ( !ForesterUtil.isEmpty( getType() ) ) {
69 sb.append( getType() );
72 sb.append( ForesterUtil.FORMATTER_6.format( getValue() ) );
73 if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
75 sb.append( getStandardDeviation() );
82 public int compareTo( final Confidence confidence ) {
83 if ( this == confidence ) {
86 return getType().compareToIgnoreCase( confidence.getType() );
90 public PhylogenyData copy() {
91 return new Confidence( getValue(), getType(), getStandardDeviation() );
94 public String getType() {
98 public double getValue() {
102 public double getStandardDeviation() {
107 setValue( CONFIDENCE_DEFAULT_VALUE );
109 setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
113 public boolean isEqual( final PhylogenyData confidence ) {
114 if ( confidence == null ) {
117 if ( !( confidence instanceof Confidence ) ) {
120 final Confidence s = ( Confidence ) confidence;
121 if ( s.getValue() != getValue() ) {
124 if ( !s.getType().equals( getType() ) ) {
130 public void setType( final String type ) {
134 public void setValue( final double value ) {
138 public void setStandardDeviation( final double sd ) {
143 public StringBuffer toNHX() {
144 final StringBuffer sb = new StringBuffer();
145 sb.append( NHXtags.SUPPORT );
146 sb.append( getValue() );
151 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
152 if ( getValue() == CONFIDENCE_DEFAULT_VALUE ) {
155 writer.write( ForesterUtil.LINE_SEPARATOR );
156 writer.write( indentation );
157 if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
159 .appendElement( writer,
160 PhyloXmlMapping.CONFIDENCE,
161 String.valueOf( ForesterUtil
162 .round( getValue(), PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ),
163 PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
164 ForesterUtil.isEmpty( getType() ) ? "unknown" : getType(),
165 PhyloXmlMapping.CONFIDENCE_SD_ATTR,
166 String.valueOf( ForesterUtil
167 .round( getStandardDeviation(),
168 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
172 .appendElement( writer,
173 PhyloXmlMapping.CONFIDENCE,
174 String.valueOf( ForesterUtil
175 .round( getValue(), PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ),
176 PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
177 ForesterUtil.isEmpty( getType() ) ? "unknown" : getType() );
182 public String toString() {
183 return asText().toString();