inprogress
[jalview.git] / forester / java / src / org / forester / util / ForesterUtil.java
index 1ca9ba2..43700ef 100644 (file)
@@ -21,7 +21,7 @@
 // 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.util;
 
@@ -59,8 +59,10 @@ import java.util.SortedMap;
 import java.util.SortedSet;
 import java.util.TreeMap;
 import java.util.TreeSet;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.forester.archaeopteryx.Constants;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.data.Distribution;
 import org.forester.phylogeny.data.Sequence;
@@ -82,6 +84,10 @@ public final class ForesterUtil {
     public static final NumberFormat FORMATTER_6;
     public static final NumberFormat FORMATTER_06;
     public static final NumberFormat FORMATTER_3;
+    public static final String       NCBI_PROTEIN                     = "http://www.ncbi.nlm.nih.gov/protein/";
+    public static final String       NCBI_NUCCORE                     = "http://www.ncbi.nlm.nih.gov/nuccore/";
+    public final static String       UNIPROT_KB                       = "http://www.uniprot.org/uniprot/";
+    public static final String       NCBI_GI                          = "http://www.ncbi.nlm.nih.gov/protein/gi:";
     static {
         final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
         dfs.setDecimalSeparator( '.' );
@@ -305,6 +311,13 @@ public final class ForesterUtil {
         }
     }
 
+    public static void fatalError( final String message ) {
+        System.err.println();
+        System.err.println( "error: " + message );
+        System.err.println();
+        System.exit( -1 );
+    }
+
     public static void fatalError( final String prg_name, final String message ) {
         System.err.println();
         System.err.println( "[" + prg_name + "] > " + message );
@@ -312,6 +325,16 @@ public final class ForesterUtil {
         System.exit( -1 );
     }
 
+    public static void fatalErrorIfFileNotReadable( final File file ) {
+        final String error = isReadableFile( file );
+        if ( !isEmpty( error ) ) {
+            System.err.println();
+            System.err.println( "error: " + error );
+            System.err.println();
+            System.exit( -1 );
+        }
+    }
+
     public static void fatalErrorIfFileNotReadable( final String prg_name, final File file ) {
         final String error = isReadableFile( file );
         if ( !isEmpty( error ) ) {
@@ -332,6 +355,34 @@ public final class ForesterUtil {
         return ary;
     }
 
+    public static String[][] file22dArray( final File file ) throws IOException {
+        final List<String> list = new ArrayList<String>();
+        final BufferedReader in = new BufferedReader( new FileReader( file ) );
+        String str;
+        while ( ( str = in.readLine() ) != null ) {
+            str = str.trim();
+            if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
+                list.add( str );
+            }
+        }
+        in.close();
+        final String[][] ary = new String[ list.size() ][ 2 ];
+        final Pattern pa = Pattern.compile( "(\\S+)\\s+(\\S+)" );
+        int i = 0;
+        for( final String s : list ) {
+            final Matcher m = pa.matcher( s );
+            if ( m.matches() ) {
+                ary[ i ][ 0 ] = m.group( 1 );
+                ary[ i ][ 1 ] = m.group( 2 );
+                ++i;
+            }
+            else {
+                throw new IOException( "unexpcted format: " + s );
+            }
+        }
+        return ary;
+    }
+
     final public static List<String> file2list( final File file ) throws IOException {
         final List<String> list = new ArrayList<String>();
         final BufferedReader in = new BufferedReader( new FileReader( file ) );
@@ -539,8 +590,24 @@ public final class ForesterUtil {
         return isReadableFile( new File( s ) );
     }
 
-    public static boolean isWindowns() {
-        return ForesterUtil.OS_NAME.toLowerCase().indexOf( "win" ) > -1;
+    public final static boolean isWindows() {
+        try {
+            return OS_NAME.toLowerCase().indexOf( "win" ) > -1;
+        }
+        catch ( final Exception e ) {
+            ForesterUtil.printWarningMessage( Constants.PRG_NAME, "minor error: " + e );
+            return false;
+        }
+    }
+
+    public final static boolean isMac() {
+        try {
+            return OS_NAME.toLowerCase().startsWith( "mac" );
+        }
+        catch ( final Exception e ) {
+            ForesterUtil.printWarningMessage( Constants.PRG_NAME, "minor error: " + e );
+            return false;
+        }
     }
 
     final public static String isWritableFile( final File f ) {
@@ -914,10 +981,34 @@ public final class ForesterUtil {
         return str_array;
     }
 
+    final public static void unexpectedFatalError( final Exception e ) {
+        System.err.println();
+        System.err.println( "unexpected exception: should not have occured! Please contact program author(s)." );
+        e.printStackTrace( System.err );
+        System.err.println();
+        System.exit( -1 );
+    }
+
+    final public static void unexpectedFatalError( final Error e ) {
+        System.err.println();
+        System.err.println( "unexpected error: should not have occured! Please contact program author(s)." );
+        e.printStackTrace( System.err );
+        System.err.println();
+        System.exit( -1 );
+    }
+
+    final public static void unexpectedFatalError( final String message ) {
+        System.err.println();
+        System.err.println( "unexpected error: should not have occured! Please contact program author(s)." );
+        System.err.println( message );
+        System.err.println();
+        System.exit( -1 );
+    }
+
     final public static void unexpectedFatalError( final String prg_name, final Exception e ) {
         System.err.println();
         System.err.println( "[" + prg_name
-                + "] > Unexpected error. Should not have occured! Please contact program author(s)." );
+                + "] > unexpected error; should not have occured! Please contact program author(s)." );
         e.printStackTrace( System.err );
         System.err.println();
         System.exit( -1 );
@@ -926,7 +1017,7 @@ public final class ForesterUtil {
     final public static void unexpectedFatalError( final String prg_name, final String message ) {
         System.err.println();
         System.err.println( "[" + prg_name
-                + "] > Unexpected error. Should not have occured! Please contact program author(s)." );
+                + "] > unexpected error: should not have occured! Please contact program author(s)." );
         System.err.println( message );
         System.err.println();
         System.exit( -1 );
@@ -935,7 +1026,7 @@ public final class ForesterUtil {
     final public static void unexpectedFatalError( final String prg_name, final String message, final Exception e ) {
         System.err.println();
         System.err.println( "[" + prg_name
-                + "] > Unexpected error. Should not have occured! Please contact program author(s)." );
+                + "] > unexpected error: should not have occured! Please contact program author(s)." );
         System.err.println( message );
         e.printStackTrace( System.err );
         System.err.println();
@@ -946,7 +1037,7 @@ public final class ForesterUtil {
         final int width = 50;
         System.out.print( "\r[" );
         int i = 0;
-        for( ; i <= ( int ) ( progress_percentage * width ); i++ ) {
+        for( ; i <= ForesterUtil.roundToInt( progress_percentage * width ); i++ ) {
             System.out.print( "." );
         }
         for( ; i < width; i++ ) {
@@ -955,6 +1046,10 @@ public final class ForesterUtil {
         System.out.print( "]" );
     }
 
+    public final static void updateProgress( final int i, final DecimalFormat f ) {
+        System.out.print( "\r[" + f.format( i ) + "]" );
+    }
+
     public final static String wordWrap( final String str, final int width ) {
         final StringBuilder sb = new StringBuilder( str );
         int start = 0;
@@ -1021,4 +1116,13 @@ public final class ForesterUtil {
         final String regex = "[\\s;,]+";
         return str.split( regex );
     }
+
+    public final static void outOfMemoryError( final OutOfMemoryError e ) {
+        System.err.println();
+        System.err.println( "Java memory allocation might be too small, try \"-Xmx2048m\" java command line option" );
+        System.err.println();
+        e.printStackTrace( System.err );
+        System.err.println();
+        System.exit( -1 );
+    }
 }