moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Confidence.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.phylogeny.data;
27
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;
33
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;
38
39 public class Confidence implements PhylogenyData, Comparable<Confidence> {
40
41     public final static int          CONFIDENCE_DEFAULT_VALUE = -9999;
42     private double                   _value;
43     private double                   _sd;
44     private String                   _type;
45     public final static NumberFormat FORMATTER;
46     static {
47         final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
48         dfs.setDecimalSeparator( '.' );
49         FORMATTER = new DecimalFormat( "#.#########", dfs );
50         FORMATTER.setMaximumFractionDigits( PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT );
51     }
52
53     public Confidence() {
54         init();
55     }
56
57     public Confidence( final double value, final String type ) {
58         setValue( value );
59         setType( type );
60         setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
61     }
62
63     public Confidence( final double value, final String type, final double sd ) {
64         setValue( value );
65         setType( type );
66         setStandardDeviation( sd );
67     }
68
69     @Override
70     public StringBuffer asSimpleText() {
71         return new StringBuffer().append( ForesterUtil.FORMATTER_6.format( getValue() ) );
72     }
73
74     @Override
75     public StringBuffer asText() {
76         final StringBuffer sb = new StringBuffer();
77         if ( !ForesterUtil.isEmpty( getType() ) ) {
78             sb.append( "[" );
79             sb.append( getType() );
80             sb.append( "] " );
81         }
82         sb.append( ForesterUtil.FORMATTER_6.format( getValue() ) );
83         if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
84             sb.append( " (sd=" );
85             sb.append( getStandardDeviation() );
86             sb.append( ")" );
87         }
88         return sb;
89     }
90
91     @Override
92     public int compareTo( final Confidence confidence ) {
93         if ( this == confidence ) {
94             return 0;
95         }
96         return getType().compareToIgnoreCase( confidence.getType() );
97     }
98
99     @Override
100     public PhylogenyData copy() {
101         return new Confidence( getValue(), getType(), getStandardDeviation() );
102     }
103
104     public String getType() {
105         return _type;
106     }
107
108     public double getValue() {
109         return _value;
110     }
111
112     public double getStandardDeviation() {
113         return _sd;
114     }
115
116     public void init() {
117         setValue( CONFIDENCE_DEFAULT_VALUE );
118         setType( "" );
119         setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
120     }
121
122     @Override
123     public boolean isEqual( final PhylogenyData confidence ) {
124         if ( confidence == null ) {
125             return false;
126         }
127         if ( !( confidence instanceof Confidence ) ) {
128             return false;
129         }
130         final Confidence s = ( Confidence ) confidence;
131         if ( s.getValue() != getValue() ) {
132             return false;
133         }
134         if ( !s.getType().equals( getType() ) ) {
135             return false;
136         }
137         return true;
138     }
139
140     public void setType( final String type ) {
141         _type = type;
142     }
143
144     public void setValue( final double value ) {
145         _value = value;
146     }
147
148     public void setStandardDeviation( final double sd ) {
149         _sd = sd;
150     }
151
152     @Override
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 ) ) );
158         return sb;
159     }
160
161     @Override
162     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
163         if ( getValue() == CONFIDENCE_DEFAULT_VALUE ) {
164             return;
165         }
166         writer.write( ForesterUtil.LINE_SEPARATOR );
167         writer.write( indentation );
168         if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
169             PhylogenyDataUtil
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 ) ) );
180         }
181         else {
182             PhylogenyDataUtil
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() );
189         }
190     }
191
192     @Override
193     public String toString() {
194         return asText().toString();
195     }
196 }