in progress
[jalview.git] / forester / java / src / org / forester / util / ForesterUtil.java
index ada203b..64f3f81 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
@@ -15,7 +15,7 @@
 // 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
@@ -64,14 +64,19 @@ import org.forester.io.parsers.PhylogenyParser;
 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
 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;
 import org.forester.phylogeny.data.Confidence;
 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 {
@@ -109,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,
@@ -674,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 ) );
     }
@@ -996,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 ) {
@@ -1021,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 "";
@@ -1056,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 "";
@@ -1070,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() ) {
@@ -1173,6 +1227,38 @@ public final class ForesterUtil {
                         }
                         n.getNodeData().getSequence().setName( name );
                         break;
+                    case TAXONOMY_ID_UNIPROT_1: {
+                        if ( !n.getNodeData().isHasTaxonomy() ) {
+                            n.getNodeData().setTaxonomy( new Taxonomy() );
+                        }
+                        String id = name;
+                        final int i = name.indexOf( '_' );
+                        if ( i > 0 ) {
+                            id = name.substring( 0, i );
+                        }
+                        else {
+                            n.setName( "" );
+                        }
+                        n.getNodeData().getTaxonomy()
+                                .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
+                        break;
+                    }
+                    case TAXONOMY_ID_UNIPROT_2: {
+                        if ( !n.getNodeData().isHasTaxonomy() ) {
+                            n.getNodeData().setTaxonomy( new Taxonomy() );
+                        }
+                        String id = name;
+                        final int i = name.indexOf( '_' );
+                        if ( i > 0 ) {
+                            id = name.substring( i + 1, name.length() );
+                        }
+                        else {
+                            n.setName( "" );
+                        }
+                        n.getNodeData().getTaxonomy()
+                                .setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
+                        break;
+                    }
                 }
             }
         }
@@ -1236,7 +1322,14 @@ public final class ForesterUtil {
     }
 
     public static enum PhylogenyNodeField {
-        CLADE_NAME, TAXONOMY_CODE, TAXONOMY_SCIENTIFIC_NAME, TAXONOMY_COMMON_NAME, SEQUENCE_SYMBOL, SEQUENCE_NAME;
+        CLADE_NAME,
+        TAXONOMY_CODE,
+        TAXONOMY_SCIENTIFIC_NAME,
+        TAXONOMY_COMMON_NAME,
+        SEQUENCE_SYMBOL,
+        SEQUENCE_NAME,
+        TAXONOMY_ID_UNIPROT_1,
+        TAXONOMY_ID_UNIPROT_2;
     }
 
     public static enum TAXONOMY_EXTRACTION {