moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[jalview.git] / forester / java / src / org / forester / phylogeny / data / Confidence.java
index dbb9d07..4ea14ef 100644 (file)
@@ -5,7 +5,7 @@
 // Copyright (C) 2008-2009 Christian M. Zmasek
 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
 // All rights reserved
-// 
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 // Lesser General Public License for more details.
-// 
+//
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.phylogeny.data;
 
 import java.io.IOException;
 import java.io.Writer;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.NumberFormat;
 
 import org.forester.io.parsers.nhx.NHXtags;
 import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
@@ -35,9 +38,17 @@ import org.forester.util.ForesterUtil;
 
 public class Confidence implements PhylogenyData, Comparable<Confidence> {
 
-    public final static double CONFIDENCE_DEFAULT_VALUE = -9999.0;
-    private double             _value;
-    private String             _type;
+    public final static int          CONFIDENCE_DEFAULT_VALUE = -9999;
+    private double                   _value;
+    private double                   _sd;
+    private String                   _type;
+    public final static NumberFormat FORMATTER;
+    static {
+        final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
+        dfs.setDecimalSeparator( '.' );
+        FORMATTER = new DecimalFormat( "#.#########", dfs );
+        FORMATTER.setMaximumFractionDigits( PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT );
+    }
 
     public Confidence() {
         init();
@@ -46,12 +57,21 @@ public class Confidence implements PhylogenyData, Comparable<Confidence> {
     public Confidence( final double value, final String type ) {
         setValue( value );
         setType( type );
+        setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
+    }
+
+    public Confidence( final double value, final String type, final double sd ) {
+        setValue( value );
+        setType( type );
+        setStandardDeviation( sd );
     }
 
+    @Override
     public StringBuffer asSimpleText() {
         return new StringBuffer().append( ForesterUtil.FORMATTER_6.format( getValue() ) );
     }
 
+    @Override
     public StringBuffer asText() {
         final StringBuffer sb = new StringBuffer();
         if ( !ForesterUtil.isEmpty( getType() ) ) {
@@ -60,6 +80,11 @@ public class Confidence implements PhylogenyData, Comparable<Confidence> {
             sb.append( "] " );
         }
         sb.append( ForesterUtil.FORMATTER_6.format( getValue() ) );
+        if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
+            sb.append( " (sd=" );
+            sb.append( getStandardDeviation() );
+            sb.append( ")" );
+        }
         return sb;
     }
 
@@ -71,8 +96,9 @@ public class Confidence implements PhylogenyData, Comparable<Confidence> {
         return getType().compareToIgnoreCase( confidence.getType() );
     }
 
+    @Override
     public PhylogenyData copy() {
-        return new Confidence( getValue(), getType() );
+        return new Confidence( getValue(), getType(), getStandardDeviation() );
     }
 
     public String getType() {
@@ -83,11 +109,17 @@ public class Confidence implements PhylogenyData, Comparable<Confidence> {
         return _value;
     }
 
+    public double getStandardDeviation() {
+        return _sd;
+    }
+
     public void init() {
         setValue( CONFIDENCE_DEFAULT_VALUE );
         setType( "" );
+        setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
     }
 
+    @Override
     public boolean isEqual( final PhylogenyData confidence ) {
         if ( confidence == null ) {
             return false;
@@ -113,26 +145,48 @@ public class Confidence implements PhylogenyData, Comparable<Confidence> {
         _value = value;
     }
 
+    public void setStandardDeviation( final double sd ) {
+        _sd = sd;
+    }
+
+    @Override
     public StringBuffer toNHX() {
         final StringBuffer sb = new StringBuffer();
         sb.append( NHXtags.SUPPORT );
-        sb.append( getValue() );
+        sb.append( FORMATTER.format( ForesterUtil.round( getValue(),
+                                                         PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
         return sb;
     }
 
+    @Override
     public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
         if ( getValue() == CONFIDENCE_DEFAULT_VALUE ) {
             return;
         }
         writer.write( ForesterUtil.LINE_SEPARATOR );
         writer.write( indentation );
-        PhylogenyDataUtil.appendElement( writer,
-                                         PhyloXmlMapping.CONFIDENCE,
-                                         String.valueOf( ForesterUtil
-                                                 .round( getValue(),
-                                                         PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ),
-                                         PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
-                                         ForesterUtil.isEmpty( getType() ) ? "unknown" : getType() );
+        if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
+            PhylogenyDataUtil
+                    .appendElement( writer,
+                                    PhyloXmlMapping.CONFIDENCE,
+                                    FORMATTER.format( ForesterUtil
+                                            .round( getValue(), PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ),
+                                    PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
+                                    ForesterUtil.isEmpty( getType() ) ? "unknown" : getType(),
+                                    PhyloXmlMapping.CONFIDENCE_SD_ATTR,
+                                    String.valueOf( ForesterUtil
+                                            .round( getStandardDeviation(),
+                                                    PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
+        }
+        else {
+            PhylogenyDataUtil
+                    .appendElement( writer,
+                                    PhyloXmlMapping.CONFIDENCE,
+                                    FORMATTER.format( ForesterUtil
+                                            .round( getValue(), PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ),
+                                    PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
+                                    ForesterUtil.isEmpty( getType() ) ? "unknown" : getType() );
+        }
     }
 
     @Override