initial commit
[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 double CONFIDENCE_DEFAULT_VALUE = -9999.0;
39     private double             _value;
40     private String             _type;
41
42     public Confidence() {
43         init();
44     }
45
46     public Confidence( final double value, final String type ) {
47         setValue( value );
48         setType( type );
49     }
50
51     public StringBuffer asSimpleText() {
52         return new StringBuffer().append( ForesterUtil.FORMATTER_6.format( getValue() ) );
53     }
54
55     public StringBuffer asText() {
56         final StringBuffer sb = new StringBuffer();
57         if ( !ForesterUtil.isEmpty( getType() ) ) {
58             sb.append( "[" );
59             sb.append( getType() );
60             sb.append( "] " );
61         }
62         sb.append( ForesterUtil.FORMATTER_6.format( getValue() ) );
63         return sb;
64     }
65
66     @Override
67     public int compareTo( final Confidence confidence ) {
68         if ( this == confidence ) {
69             return 0;
70         }
71         return getType().compareToIgnoreCase( confidence.getType() );
72     }
73
74     public PhylogenyData copy() {
75         return new Confidence( getValue(), getType() );
76     }
77
78     public String getType() {
79         return _type;
80     }
81
82     public double getValue() {
83         return _value;
84     }
85
86     public void init() {
87         setValue( CONFIDENCE_DEFAULT_VALUE );
88         setType( "" );
89     }
90
91     public boolean isEqual( final PhylogenyData confidence ) {
92         if ( confidence == null ) {
93             return false;
94         }
95         if ( !( confidence instanceof Confidence ) ) {
96             return false;
97         }
98         final Confidence s = ( Confidence ) confidence;
99         if ( s.getValue() != getValue() ) {
100             return false;
101         }
102         if ( !s.getType().equals( getType() ) ) {
103             return false;
104         }
105         return true;
106     }
107
108     public void setType( final String type ) {
109         _type = type;
110     }
111
112     public void setValue( final double value ) {
113         _value = value;
114     }
115
116     public StringBuffer toNHX() {
117         final StringBuffer sb = new StringBuffer();
118         sb.append( NHXtags.SUPPORT );
119         sb.append( getValue() );
120         return sb;
121     }
122
123     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
124         if ( getValue() == CONFIDENCE_DEFAULT_VALUE ) {
125             return;
126         }
127         writer.write( ForesterUtil.LINE_SEPARATOR );
128         writer.write( indentation );
129         PhylogenyDataUtil.appendElement( writer,
130                                          PhyloXmlMapping.CONFIDENCE,
131                                          String.valueOf( ForesterUtil
132                                                  .round( getValue(),
133                                                          PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ),
134                                          PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
135                                          ForesterUtil.isEmpty( getType() ) ? "unknown" : getType() );
136     }
137
138     @Override
139     public String toString() {
140         return asText().toString();
141     }
142 }