mb parsing
[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: www.phylosoft.org/forester
25
26 package org.forester.phylogeny.data;
27
28 import java.io.IOException;
29 import java.io.Writer;
30
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;
35
36 public class Confidence implements PhylogenyData, Comparable<Confidence> {
37
38     public final static int CONFIDENCE_DEFAULT_VALUE = -9999;
39     private double          _value;
40     private double          _sd;
41     private String          _type;
42
43     public Confidence() {
44         init();
45     }
46
47     public Confidence( final double value, final String type ) {
48         setValue( value );
49         setType( type );
50         setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
51     }
52
53     public Confidence( final double value, final String type, final double sd ) {
54         setValue( value );
55         setType( type );
56         setStandardDeviation( sd );
57     }
58
59     @Override
60     public StringBuffer asSimpleText() {
61         return new StringBuffer().append( ForesterUtil.FORMATTER_6.format( getValue() ) );
62     }
63
64     @Override
65     public StringBuffer asText() {
66         final StringBuffer sb = new StringBuffer();
67         if ( !ForesterUtil.isEmpty( getType() ) ) {
68             sb.append( "[" );
69             sb.append( getType() );
70             sb.append( "] " );
71         }
72         sb.append( ForesterUtil.FORMATTER_6.format( getValue() ) );
73         if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
74             sb.append( " (sd=" );
75             sb.append( getStandardDeviation() );
76             sb.append( ")" );
77         }
78         return sb;
79     }
80
81     @Override
82     public int compareTo( final Confidence confidence ) {
83         if ( this == confidence ) {
84             return 0;
85         }
86         return getType().compareToIgnoreCase( confidence.getType() );
87     }
88
89     @Override
90     public PhylogenyData copy() {
91         return new Confidence( getValue(), getType(), getStandardDeviation() );
92     }
93
94     public String getType() {
95         return _type;
96     }
97
98     public double getValue() {
99         return _value;
100     }
101
102     public double getStandardDeviation() {
103         return _sd;
104     }
105
106     public void init() {
107         setValue( CONFIDENCE_DEFAULT_VALUE );
108         setType( "" );
109         setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
110     }
111
112     @Override
113     public boolean isEqual( final PhylogenyData confidence ) {
114         if ( confidence == null ) {
115             return false;
116         }
117         if ( !( confidence instanceof Confidence ) ) {
118             return false;
119         }
120         final Confidence s = ( Confidence ) confidence;
121         if ( s.getValue() != getValue() ) {
122             return false;
123         }
124         if ( !s.getType().equals( getType() ) ) {
125             return false;
126         }
127         return true;
128     }
129
130     public void setType( final String type ) {
131         _type = type;
132     }
133
134     public void setValue( final double value ) {
135         _value = value;
136     }
137
138     public void setStandardDeviation( final double sd ) {
139         _sd = sd;
140     }
141
142     @Override
143     public StringBuffer toNHX() {
144         final StringBuffer sb = new StringBuffer();
145         sb.append( NHXtags.SUPPORT );
146         sb.append( getValue() );
147         return sb;
148     }
149
150     @Override
151     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
152         if ( getValue() == CONFIDENCE_DEFAULT_VALUE ) {
153             return;
154         }
155         writer.write( ForesterUtil.LINE_SEPARATOR );
156         writer.write( indentation );
157         if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
158             PhylogenyDataUtil
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 ) ) );
169         }
170         else {
171             PhylogenyDataUtil
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() );
178         }
179     }
180
181     @Override
182     public String toString() {
183         return asText().toString();
184     }
185 }