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: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.phylogeny.data;
28 import java.io.IOException;
29 import java.io.Writer;
30 import java.text.DecimalFormat;
31 import java.text.DecimalFormatSymbols;
32 import java.text.NumberFormat;
34 import org.forester.io.parsers.nhx.NHXtags;
35 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
36 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
37 import org.forester.util.ForesterUtil;
39 public class Confidence implements PhylogenyData, Comparable<Confidence> {
41 public final static int CONFIDENCE_DEFAULT_VALUE = -9999;
42 private double _value;
45 public final static NumberFormat FORMATTER;
47 final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
48 dfs.setDecimalSeparator( '.' );
49 FORMATTER = new DecimalFormat( "#.#########", dfs );
50 FORMATTER.setMaximumFractionDigits( PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT );
57 public Confidence( final double value, final String type ) {
60 setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
63 public Confidence( final double value, final String type, final double sd ) {
66 setStandardDeviation( sd );
70 public StringBuffer asSimpleText() {
71 return new StringBuffer().append( ForesterUtil.FORMATTER_6.format( getValue() ) );
75 public StringBuffer asText() {
76 final StringBuffer sb = new StringBuffer();
77 if ( !ForesterUtil.isEmpty( getType() ) ) {
79 sb.append( getType() );
82 sb.append( ForesterUtil.FORMATTER_6.format( getValue() ) );
83 if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
85 sb.append( getStandardDeviation() );
92 public int compareTo( final Confidence confidence ) {
93 if ( this == confidence ) {
96 return getType().compareToIgnoreCase( confidence.getType() );
100 public PhylogenyData copy() {
101 return new Confidence( getValue(), getType(), getStandardDeviation() );
104 public String getType() {
108 public double getValue() {
112 public double getStandardDeviation() {
117 setValue( CONFIDENCE_DEFAULT_VALUE );
119 setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
123 public boolean isEqual( final PhylogenyData confidence ) {
124 if ( confidence == null ) {
127 if ( !( confidence instanceof Confidence ) ) {
130 final Confidence s = ( Confidence ) confidence;
131 if ( s.getValue() != getValue() ) {
134 if ( !s.getType().equals( getType() ) ) {
140 public void setType( final String type ) {
144 public void setValue( final double value ) {
148 public void setStandardDeviation( final double sd ) {
153 public StringBuffer toNHX() {
154 final StringBuffer sb = new StringBuffer();
155 sb.append( NHXtags.SUPPORT );
156 sb.append( FORMATTER.format( ForesterUtil.round( getValue(),
157 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
162 public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
163 if ( getValue() == CONFIDENCE_DEFAULT_VALUE ) {
166 writer.write( ForesterUtil.LINE_SEPARATOR );
167 writer.write( indentation );
168 if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
170 .appendElement( writer,
171 PhyloXmlMapping.CONFIDENCE,
172 FORMATTER.format( ForesterUtil
173 .round( getValue(), PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ),
174 PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
175 ForesterUtil.isEmpty( getType() ) ? "unknown" : getType(),
176 PhyloXmlMapping.CONFIDENCE_SD_ATTR,
177 String.valueOf( ForesterUtil
178 .round( getStandardDeviation(),
179 PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
183 .appendElement( writer,
184 PhyloXmlMapping.CONFIDENCE,
185 FORMATTER.format( ForesterUtil
186 .round( getValue(), PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ),
187 PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
188 ForesterUtil.isEmpty( getType() ) ? "unknown" : getType() );
193 public String toString() {
194 return asText().toString();