in progress
[jalview.git] / forester / java / src / org / forester / util / ForesterUtil.java
index 9aba8cc..64f3f81 100644 (file)
@@ -66,6 +66,7 @@ import org.forester.io.parsers.nhx.NHXParser;
 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
 import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
 import org.forester.io.parsers.tol.TolParser;
+import org.forester.io.parsers.util.PhylogenyParserException;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
@@ -74,6 +75,8 @@ import org.forester.phylogeny.data.Distribution;
 import org.forester.phylogeny.data.Identifier;
 import org.forester.phylogeny.data.Sequence;
 import org.forester.phylogeny.data.Taxonomy;
+import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
+import org.forester.phylogeny.factories.PhylogenyFactory;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
 
 public final class ForesterUtil {
@@ -111,30 +114,6 @@ public final class ForesterUtil {
         }
     }
 
-    final public static boolean isEmpty( final List<?> l ) {
-        if ( ( l == null ) || l.isEmpty() ) {
-            return true;
-        }
-        for( final Object o : l ) {
-            if ( o != null ) {
-                return false;
-            }
-        }
-        return true;
-    }
-
-    final public static boolean isEmpty( final Set<?> s ) {
-        if ( ( s == null ) || s.isEmpty() ) {
-            return true;
-        }
-        for( final Object o : s ) {
-            if ( o != null ) {
-                return false;
-            }
-        }
-        return true;
-    }
-
     /**
      * This calculates a color. If value is equal to min the returned color is
      * minColor, if value is equal to max the returned color is maxColor,
@@ -676,6 +655,34 @@ public final class ForesterUtil {
         return true;
     }
 
+    final public static boolean isContainsParanthesesableNhCharacter( final String nh ) {
+        return PARANTHESESABLE_NH_CHARS_PATTERN.matcher( nh ).find();
+    }
+
+    final public static boolean isEmpty( final List<?> l ) {
+        if ( ( l == null ) || l.isEmpty() ) {
+            return true;
+        }
+        for( final Object o : l ) {
+            if ( o != null ) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    final public static boolean isEmpty( final Set<?> s ) {
+        if ( ( s == null ) || s.isEmpty() ) {
+            return true;
+        }
+        for( final Object o : s ) {
+            if ( o != null ) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     final public static boolean isEmpty( final String s ) {
         return ( ( s == null ) || ( s.length() < 1 ) );
     }
@@ -998,6 +1005,15 @@ public final class ForesterUtil {
         System.out.println( "[" + prg_name + "] > " + message );
     }
 
+    public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file ) throws IOException {
+        final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
+        final Phylogeny[] trees = factory.create( file, parser );
+        if ( ( trees == null ) || ( trees.length == 0 ) ) {
+            throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
+        }
+        return trees;
+    }
+
     final public static String removeSuffix( final String file_name ) {
         final int i = file_name.lastIndexOf( '.' );
         if ( i > 1 ) {
@@ -1023,10 +1039,6 @@ public final class ForesterUtil {
         return s;
     }
 
-    final public static boolean isContainsParanthesesableNhCharacter( final String nh ) {
-        return PARANTHESESABLE_NH_CHARS_PATTERN.matcher( nh ).find();
-    }
-
     final public static String replaceIllegalNhCharacters( final String nh ) {
         if ( nh == null ) {
             return "";
@@ -1058,6 +1070,10 @@ public final class ForesterUtil {
         return ( int ) ( f + 0.5f );
     }
 
+    final public static short roundToShort( final double d ) {
+        return ( short ) ( d + 0.5 );
+    }
+
     final public static String sanitizeString( final String s ) {
         if ( s == null ) {
             return "";
@@ -1072,17 +1088,53 @@ public final class ForesterUtil {
         return str.split( regex );
     }
 
-    final public static String stringArrayToString( final String[] a ) {
-        final StringBuffer sb = new StringBuffer();
+    final public static String stringArrayToString( final String[] a, final String separator ) {
+        final StringBuilder sb = new StringBuilder();
         if ( ( a != null ) && ( a.length > 0 ) ) {
             for( int i = 0; i < a.length - 1; ++i ) {
-                sb.append( a[ i ] + ", " );
+                sb.append( a[ i ] + separator );
             }
             sb.append( a[ a.length - 1 ] );
         }
         return sb.toString();
     }
 
+    final public static String stringListToString( final List<String> l, final String separator ) {
+        final StringBuilder sb = new StringBuilder();
+        if ( ( l != null ) && ( l.size() > 0 ) ) {
+            for( int i = 0; i < l.size() - 1; ++i ) {
+                sb.append( l.get( i ) + separator );
+            }
+            sb.append( l.get( l.size() - 1 ) );
+        }
+        return sb.toString();
+    }
+
+    final public static String stringArrayToString( final String[] a ) {
+        return stringArrayToString( a, ", " );
+    }
+
+    final public static String[] stringSetToArray( final Set<String> strings ) {
+        final String[] str_array = new String[ strings.size() ];
+        int i = 0;
+        for( final String e : strings ) {
+            str_array[ i++ ] = e;
+        }
+        return str_array;
+    }
+
+    final public static String[] stringListToArray( final List<String> list ) {
+        if ( list != null ) {
+            final String[] str = new String[ list.size() ];
+            int i = 0;
+            for( final String l : list ) {
+                str[ i++ ] = l;
+            }
+            return str;
+        }
+        return null;
+    }
+
     final static public void transferInternalNamesToBootstrapSupport( final Phylogeny phy ) {
         final PhylogenyNodeIterator it = phy.iteratorPostorder();
         while ( it.hasNext() ) {