in progress...
[jalview.git] / forester / java / src / org / forester / util / ForesterUtil.java
index 3ba3887..dc89976 100644 (file)
@@ -99,7 +99,7 @@ public final class ForesterUtil {
     public final static String       OS_VERSION                       = System.getProperty( "os.version" );
     public static final String       PDB                              = "http://www.pdb.org/pdb/explore/explore.do?pdbId=";
     public final static String       UNIPROT_KB                       = "http://www.uniprot.org/uniprot/";
-    public final static double       ZERO_DIFF                        = 1.0E-9;
+    public final static double       ZERO_DIFF                        = 1.0E-12;
     private static final Pattern     PARANTHESESABLE_NH_CHARS_PATTERN = Pattern.compile( "[(),;\\s:\\[\\]]" );
     static {
         final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
@@ -593,12 +593,12 @@ public final class ForesterUtil {
     final public static boolean isEmpty( final String s ) {
         return ( ( s == null ) || ( s.length() < 1 ) );
     }
-    
+
     final public static boolean isEmptyTrimmed( final String s ) {
-       if ( s == null ) {
-           return true;
-       }
-       return ( ( s.trim().length() < 1 ) );
+        if ( s == null ) {
+            return true;
+        }
+        return ( ( s.trim().length() < 1 ) );
     }
 
     /**
@@ -622,6 +622,10 @@ public final class ForesterUtil {
         return ( ( Math.abs( a - b ) ) < ZERO_DIFF );
     }
 
+    final public static boolean isEqual( final double a, final double b, final double tolerance ) {
+        return ( ( Math.abs( a - b ) ) < tolerance );
+    }
+
     final public static boolean isEven( final int n ) {
         return ( n % 2 ) == 0;
     }
@@ -718,6 +722,10 @@ public final class ForesterUtil {
         }
         return null;
     }
+    
+    final public static String isWritableFile( final String s ) {
+        return isWritableFile( new File( s ) );
+    }
 
     /**
      * Helper for method "stringToColor".
@@ -1589,12 +1597,49 @@ public final class ForesterUtil {
         return a.substring( 0, min_length );
     }
 
+    public final static String greatestCommonPrefix( final String a, final String b, final String separator ) {
+        if ( ForesterUtil.isEmpty( separator ) ) {
+            throw new IllegalArgumentException( "separator must not be null or empty" );
+        }
+        final String[] as = a.split( Pattern.quote( separator ) );
+        final String[] bs = b.split( Pattern.quote( separator ) );
+        final int min_length = Math.min( as.length, bs.length );
+        for( int i = 0; i < min_length; ++i ) {
+            if ( !( as[ i ].equals( bs[ i ] ) ) ) {
+                StringBuilder sb = new StringBuilder();
+                boolean first = true;
+                for( int j = 0; j < i; ++j ) {
+                    if ( first ) {
+                        first = false;
+                    }
+                    else {
+                        sb.append( separator );
+                    }
+                    sb.append( as[ j ] );
+                }
+                return sb.toString();
+            }
+        }
+        StringBuilder sb = new StringBuilder();
+        boolean first = true;
+        for( int j = 0; j < min_length; ++j ) {
+            if ( first ) {
+                first = false;
+            }
+            else {
+                sb.append( separator );
+            }
+            sb.append( as[ j ] );
+        }
+        return sb.toString();
+    }
+
     public final static String greatestCommonPrefix( final List<String> strings ) {
         if ( strings == null ) {
-            throw new IllegalArgumentException( "list is null" );
+            throw new IllegalArgumentException( "list of strings is null" );
         }
         if ( strings.isEmpty() ) {
-            throw new IllegalArgumentException( "list is empty" );
+            throw new IllegalArgumentException( "list of strings is empty" );
         }
         String common = strings.get( 0 );
         for( int i = 1; i < strings.size(); ++i ) {
@@ -1603,6 +1648,46 @@ public final class ForesterUtil {
         return common;
     }
 
+    public final static String greatestCommonPrefix( final List<String> strings, final String separator ) {
+        if ( ForesterUtil.isEmpty( separator ) ) {
+            return greatestCommonPrefix( strings );
+        }
+        if ( strings == null ) {
+            throw new IllegalArgumentException( "list of strings is null" );
+        }
+        if ( strings.isEmpty() ) {
+            throw new IllegalArgumentException( "list of strings is empty" );
+        }
+        String common = strings.get( 0 );
+        for( int i = 1; i < strings.size(); ++i ) {
+            common = greatestCommonPrefix( common, strings.get( i ), separator );
+        }
+        return common;
+    }
+
     private ForesterUtil() {
     }
+
+    public static List<String> spliIntoPrefixes( final String prefix, final String separator ) {
+        final String[] a = prefix.split( Pattern.quote( separator ) );
+        final List<String> l = new ArrayList<String>();
+        for( int i = 0; i < a.length; ++i ) {
+            final StringBuilder sb = new StringBuilder();
+            for( int j = 0; j <= i; ++j ) {
+                sb.append( a[ j ] );
+                if ( j < i ) {
+                    sb.append( separator );
+                }
+            }
+            //  System.out.println( sb.toString() );
+            l.add( sb.toString() );
+        }
+        return l;
+    }
+
+    //
+    public static boolean isLooksLikeFasta( final File file ) throws IOException {
+        final String first_line = ForesterUtil.getFirstLine( file ).trim().toLowerCase();
+        return ( ( !isEmptyTrimmed( first_line ) && first_line.trim().startsWith( ">" ) ) );
+    }
 }