in progress....
[jalview.git] / forester / java / src / org / forester / util / ForesterUtil.java
index eb1ea8d..8fb4c68 100644 (file)
@@ -41,6 +41,9 @@ import java.io.StringReader;
 import java.io.Writer;
 import java.math.BigDecimal;
 import java.net.URL;
+import java.net.URLConnection;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
 import java.text.DateFormat;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
@@ -61,36 +64,42 @@ import java.util.TreeSet;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.forester.archaeopteryx.Constants;
+import org.forester.archaeopteryx.AptxConstants;
+import org.forester.io.parsers.PhylogenyParser;
+import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.data.Distribution;
 import org.forester.phylogeny.data.Sequence;
 import org.forester.phylogeny.data.Taxonomy;
+import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
 import org.forester.protein.BasicProtein;
 import org.forester.protein.Domain;
 import org.forester.protein.Protein;
+import org.forester.sequence.MolecularSequence;
+import org.forester.sequence.MolecularSequence.TYPE;
 import org.forester.surfacing.SurfacingUtil;
 
 public final class ForesterUtil {
 
     public final static String       FILE_SEPARATOR                   = System.getProperty( "file.separator" );
-    public final static String       LINE_SEPARATOR                   = System.getProperty( "line.separator" );
+    public static final NumberFormat FORMATTER_06;
+    public static final NumberFormat FORMATTER_3;
+    public static final NumberFormat FORMATTER_6;
+    public static final NumberFormat FORMATTER_9;
     public final static String       JAVA_VENDOR                      = System.getProperty( "java.vendor" );
     public final static String       JAVA_VERSION                     = System.getProperty( "java.version" );
+    public final static String       LINE_SEPARATOR                   = System.getProperty( "line.separator" );
+    public static final String       NCBI_GI                          = "http://www.ncbi.nlm.nih.gov/protein/gi:";
+    public static final String       NCBI_NUCCORE                     = "http://www.ncbi.nlm.nih.gov/nuccore/";
+    public static final String       NCBI_PROTEIN                     = "http://www.ncbi.nlm.nih.gov/protein/";
+    public static final BigDecimal   NULL_BD                          = new BigDecimal( 0 );
     public final static String       OS_ARCH                          = System.getProperty( "os.arch" );
     public final static String       OS_NAME                          = System.getProperty( "os.name" );
     public final static String       OS_VERSION                       = System.getProperty( "os.version" );
-    public final static Pattern      PARANTHESESABLE_NH_CHARS_PATTERN = Pattern.compile( "[(),;\\s]" );
-    public final static double       ZERO_DIFF                        = 1.0E-9;
-    public static final BigDecimal   NULL_BD                          = new BigDecimal( 0 );
-    public static final NumberFormat FORMATTER_9;
-    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 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 static final String       NCBI_GI                          = "http://www.ncbi.nlm.nih.gov/protein/gi:";
+    public final static double       ZERO_DIFF                        = 1.0E-9;
+    private static final Pattern     PARANTHESESABLE_NH_CHARS_PATTERN = Pattern.compile( "[(),;\\s:\\[\\]]" );
     static {
         final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
         dfs.setDecimalSeparator( '.' );
@@ -101,102 +110,30 @@ public final class ForesterUtil {
         FORMATTER_3 = new DecimalFormat( "#.###", dfs );
     }
 
-    private ForesterUtil() {
-    }
-
-    public static int calculateOverlap( final Domain domain, final List<Boolean> covered_positions ) {
-        int overlap_count = 0;
-        for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
-            if ( ( i < covered_positions.size() ) && ( covered_positions.get( i ) == true ) ) {
-                ++overlap_count;
-            }
-        }
-        return overlap_count;
-    }
-
     final public static void appendSeparatorIfNotEmpty( final StringBuffer sb, final char separator ) {
         if ( sb.length() > 0 ) {
             sb.append( separator );
         }
     }
 
-    /**
-     * 
-     * Example regarding engulfment: ------------0.1 ----------0.2 --0.3 =>
-     * domain with 0.3 is ignored
-     * 
-     * -----------0.1 ----------0.2 --0.3 => domain with 0.3 is ignored
-     * 
-     * 
-     * ------------0.1 ----------0.3 --0.2 => domains with 0.3 and 0.2 are _not_
-     * ignored
-     * 
-     * @param max_allowed_overlap
-     *            maximal allowed overlap (inclusive) to be still considered not
-     *            overlapping (zero or negative value to allow any overlap)
-     * @param remove_engulfed_domains
-     *            to remove domains which are completely engulfed by coverage of
-     *            domains with better support
-     * @param protein
-     * @return
-     */
-    public static Protein removeOverlappingDomains( final int max_allowed_overlap,
-                                                    final boolean remove_engulfed_domains,
-                                                    final Protein protein ) {
-        final Protein pruned_protein = new BasicProtein( protein.getProteinId().getId(), protein.getSpecies()
-                .getSpeciesId(), protein.getLength() );
-        final List<Domain> sorted = SurfacingUtil.sortDomainsWithAscendingConfidenceValues( protein );
-        final List<Boolean> covered_positions = new ArrayList<Boolean>();
-        for( final Domain domain : sorted ) {
-            if ( ( ( max_allowed_overlap < 0 ) || ( ForesterUtil.calculateOverlap( domain, covered_positions ) <= max_allowed_overlap ) )
-                    && ( !remove_engulfed_domains || !isEngulfed( domain, covered_positions ) ) ) {
-                final int covered_positions_size = covered_positions.size();
-                for( int i = covered_positions_size; i < domain.getFrom(); ++i ) {
-                    covered_positions.add( false );
-                }
-                final int new_covered_positions_size = covered_positions.size();
-                for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
-                    if ( i < new_covered_positions_size ) {
-                        covered_positions.set( i, true );
-                    }
-                    else {
-                        covered_positions.add( true );
-                    }
-                }
-                pruned_protein.addProteinDomain( domain );
-            }
-        }
-        return pruned_protein;
-    }
-
-    /**
-     * Returns true is Domain domain falls in an uninterrupted stretch of
-     * covered positions.
-     * 
-     * @param domain
-     * @param covered_positions
-     * @return
-     */
-    public static boolean isEngulfed( final Domain domain, final List<Boolean> covered_positions ) {
-        for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
-            if ( ( i >= covered_positions.size() ) || ( covered_positions.get( i ) != true ) ) {
-                return false;
-            }
+    final public static String removeFileExtension( final String file_name ) {
+        if ( file_name.indexOf( "." ) > 0 ) {
+            return file_name.substring( 0, file_name.lastIndexOf( "." ) );
         }
-        return true;
+        return file_name;
     }
 
     /**
      * 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,
      * otherwise a color 'proportional' to value is returned.
-     * 
+     *
      * @param value
-     *            the value 
+     *            the value
      * @param min
-     *            the smallest value 
+     *            the smallest value
      * @param max
-     *            the largest value 
+     *            the largest value
      * @param minColor
      *            the color for min
      * @param maxColor
@@ -227,15 +164,15 @@ public final class ForesterUtil {
      * value is equal to mean the returned color is meanColor, otherwise a color
      * 'proportional' to value is returned -- either between min-mean or
      * mean-max
-     * 
+     *
      * @param value
      *            the value
      * @param min
      *            the smallest value
      * @param max
-     *            the largest value 
+     *            the largest value
      * @param mean
-     *            the mean/median value 
+     *            the mean/median value
      * @param minColor
      *            the color for min
      * @param maxColor
@@ -276,6 +213,50 @@ public final class ForesterUtil {
         }
     }
 
+    /**
+     * Helper method for calcColor methods.
+     *
+     * @param smallercolor_component_x
+     *            color component the smaller color
+     * @param largercolor_component_x
+     *            color component the larger color
+     * @param x
+     *            factor
+     * @return an int representing a color component
+     */
+    final private static int calculateColorComponent( final double smallercolor_component_x,
+                                                      final double largercolor_component_x,
+                                                      final double x ) {
+        return ( int ) ( smallercolor_component_x
+                + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) );
+    }
+
+    /**
+     * Helper method for calcColor methods.
+     *
+     *
+     * @param value
+     *            the value
+     * @param larger
+     *            the largest value
+     * @param smaller
+     *            the smallest value
+     * @return a normalized value between larger and smaller
+     */
+    final private static double calculateColorFactor( final double value, final double larger, final double smaller ) {
+        return ( 255.0 * ( value - smaller ) ) / ( larger - smaller );
+    }
+
+    public static int calculateOverlap( final Domain domain, final List<Boolean> covered_positions ) {
+        int overlap_count = 0;
+        for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
+            if ( ( i < covered_positions.size() ) && ( covered_positions.get( i ) == true ) ) {
+                ++overlap_count;
+            }
+        }
+        return overlap_count;
+    }
+
     final public static String collapseWhiteSpace( final String s ) {
         return s.replaceAll( "[\\s]+", " " );
     }
@@ -424,16 +405,6 @@ public final class ForesterUtil {
         }
     }
 
-    public static String[] file2array( final File file ) throws IOException {
-        final List<String> list = file2list( file );
-        final String[] ary = new String[ list.size() ];
-        int i = 0;
-        for( final String s : list ) {
-            ary[ i++ ] = s;
-        }
-        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 ) );
@@ -462,6 +433,16 @@ public final class ForesterUtil {
         return ary;
     }
 
+    public static String[] file2array( final File file ) throws IOException {
+        final List<String> list = file2list( file );
+        final String[] ary = new String[ list.size() ];
+        int i = 0;
+        for( final String s : list ) {
+            ary[ i++ ] = 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 ) );
@@ -528,7 +509,9 @@ public final class ForesterUtil {
             reader = new BufferedReader( new StringReader( source.toString() ) );
         }
         else if ( source instanceof URL ) {
-            reader = new BufferedReader( new InputStreamReader( ( ( URL ) source ).openStream() ) );
+            final URLConnection url_connection = ( ( URL ) source ).openConnection();
+            url_connection.setDefaultUseCaches( false );
+            reader = new BufferedReader( new InputStreamReader( url_connection.getInputStream() ) );
         }
         else {
             throw new IllegalArgumentException( "dont know how to read [" + source.getClass() + "]" );
@@ -557,6 +540,22 @@ public final class ForesterUtil {
         return ForesterUtil.LINE_SEPARATOR;
     }
 
+    final public static MolecularSequence.TYPE guessMolecularSequenceType( final String mol_seq ) {
+        if ( mol_seq.contains( "L" ) || mol_seq.contains( "I" ) || mol_seq.contains( "E" ) || mol_seq.contains( "H" )
+                || mol_seq.contains( "D" ) || mol_seq.contains( "Q" ) ) {
+            return TYPE.AA;
+        }
+        else {
+            if ( mol_seq.contains( "T" ) ) {
+                return TYPE.DNA;
+            }
+            else if ( mol_seq.contains( "U" ) ) {
+                return TYPE.RNA;
+            }
+        }
+        return null;
+    }
+
     final public static void increaseCountingMap( final Map<String, Integer> counting_map, final String item_name ) {
         if ( !counting_map.containsKey( item_name ) ) {
             counting_map.put( item_name, 1 );
@@ -566,10 +565,6 @@ public final class ForesterUtil {
         }
     }
 
-    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;
@@ -598,6 +593,23 @@ public final class ForesterUtil {
         return ( ( s == null ) || ( s.length() < 1 ) );
     }
 
+    /**
+     * Returns true is Domain domain falls in an uninterrupted stretch of
+     * covered positions.
+     *
+     * @param domain
+     * @param covered_positions
+     * @return
+     */
+    public static boolean isEngulfed( final Domain domain, final List<Boolean> covered_positions ) {
+        for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
+            if ( ( i >= covered_positions.size() ) || ( covered_positions.get( i ) != true ) ) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     final public static boolean isEqual( final double a, final double b ) {
         return ( ( Math.abs( a - b ) ) < ZERO_DIFF );
     }
@@ -610,7 +622,7 @@ public final class ForesterUtil {
      * This determines whether String[] a and String[] b have at least one
      * String in common (intersect). Returns false if at least one String[] is
      * null or empty.
-     * 
+     *
      * @param a
      *            a String[] b a String[]
      * @return true if both a and b or not empty or null and contain at least
@@ -642,6 +654,16 @@ public final class ForesterUtil {
         }
     }
 
+    public final static boolean isMac() {
+        try {
+            return OS_NAME.toLowerCase().startsWith( "mac" );
+        }
+        catch ( final Exception e ) {
+            ForesterUtil.printWarningMessage( AptxConstants.PRG_NAME, "minor error: " + e );
+            return false;
+        }
+    }
+
     final public static boolean isNull( final BigDecimal s ) {
         return ( ( s == null ) || ( s.compareTo( NULL_BD ) == 0 ) );
     }
@@ -674,17 +696,7 @@ public final class ForesterUtil {
             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 );
+            ForesterUtil.printWarningMessage( AptxConstants.PRG_NAME, "minor error: " + e );
             return false;
         }
     }
@@ -730,7 +742,8 @@ public final class ForesterUtil {
     final public static void map2file( final File file,
                                        final Map<?, ?> data,
                                        final String entry_separator,
-                                       final String data_separator ) throws IOException {
+                                       final String data_separator )
+            throws IOException {
         final Writer writer = new BufferedWriter( new FileWriter( file ) );
         map2writer( writer, data, entry_separator, data_separator );
         writer.close();
@@ -739,7 +752,8 @@ public final class ForesterUtil {
     final public static void map2writer( final Writer writer,
                                          final Map<?, ?> data,
                                          final String entry_separator,
-                                         final String data_separator ) throws IOException {
+                                         final String data_separator )
+            throws IOException {
         boolean first = true;
         for( final Entry<?, ?> entry : data.entrySet() ) {
             if ( !first ) {
@@ -754,7 +768,8 @@ public final class ForesterUtil {
         }
     }
 
-    final public static StringBuffer mapToStringBuffer( final Map<Object, Object> map, final String key_value_separator ) {
+    final public static StringBuffer mapToStringBuffer( final Map<Object, Object> map,
+                                                        final String key_value_separator ) {
         final StringBuffer sb = new StringBuffer();
         for( final Object key : map.keySet() ) {
             sb.append( key.toString() );
@@ -786,6 +801,194 @@ public final class ForesterUtil {
         }
     }
 
+    public final static Color obtainColorDependingOnTaxonomyGroup( final String tax_group ) {
+        if ( !ForesterUtil.isEmpty( tax_group ) ) {
+            if ( tax_group.equals( TaxonomyGroups.DEUTEROSTOMIA ) ) {
+                return TaxonomyColors.DEUTEROSTOMIA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.PROTOSTOMIA ) ) {
+                return TaxonomyColors.PROTOSTOMIA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.CNIDARIA ) ) {
+                return TaxonomyColors.CNIDARIA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.PLACOZOA ) ) {
+                return TaxonomyColors.PLACOZOA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.CTENOPHORA ) ) {
+                return TaxonomyColors.CTENOPHORA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.PORIFERA ) ) {
+                return TaxonomyColors.PORIFERA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.CHOANOFLAGELLIDA ) ) {
+                return TaxonomyColors.CHOANOFLAGELLIDA;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.ICHTHYOPHONIDA_FILASTEREA ) ) {
+                return TaxonomyColors.ICHTHYOSPOREA_AND_FILASTEREA;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.DIKARYA ) ) {
+                return TaxonomyColors.DIKARYA_COLOR;
+            }
+            else if ( tax_group.equalsIgnoreCase( TaxonomyGroups.FUNGI )
+                    || tax_group.equalsIgnoreCase( TaxonomyGroups.OTHER_FUNGI ) ) {
+                return TaxonomyColors.OTHER_FUNGI_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.NUCLEARIIDAE_AND_FONTICULA_GROUP ) ) {
+                return TaxonomyColors.NUCLEARIIDAE_AND_FONTICULA_GROUP_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.AMOEBOZOA ) ) {
+                return TaxonomyColors.AMOEBOZOA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.EMBRYOPHYTA ) ) {
+                return TaxonomyColors.EMBRYOPHYTA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.CHLOROPHYTA ) ) {
+                return TaxonomyColors.CHLOROPHYTA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.RHODOPHYTA ) ) {
+                return TaxonomyColors.RHODOPHYTA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.HACROBIA ) ) {
+                return TaxonomyColors.HACROBIA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.GLAUCOCYSTOPHYCEAE ) ) {
+                return TaxonomyColors.GLAUCOPHYTA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.STRAMENOPILES ) ) {
+                return TaxonomyColors.STRAMENOPILES_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.ALVEOLATA ) ) {
+                return TaxonomyColors.ALVEOLATA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.RHIZARIA ) ) {
+                return TaxonomyColors.RHIZARIA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.EXCAVATA ) ) {
+                return TaxonomyColors.EXCAVATA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.APUSOZOA ) ) {
+                return TaxonomyColors.APUSOZOA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.ARCHAEA ) ) {
+                return TaxonomyColors.ARCHAEA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.BACTERIA ) ) {
+                return TaxonomyColors.BACTERIA_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.VIRUSES ) ) {
+                return TaxonomyColors.VIRUSES_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.ALPHAHERPESVIRINAE ) ) {
+                return TaxonomyColors.ALPHAHERPESVIRINAE_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.BETAHERPESVIRINAE ) ) {
+                return TaxonomyColors.BETAHERPESVIRINAE_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.GAMMAHERPESVIRINAE ) ) {
+                return TaxonomyColors.GAMMAHERPESVIRINAE_COLOR;
+            }
+            else if ( tax_group.equals( TaxonomyGroups.OTHER ) ) {
+                return TaxonomyColors.OTHER_COLOR;
+            }
+        }
+        return null;
+    }
+
+    public final static String obtainNormalizedTaxonomyGroup( final String tax ) {
+        if ( tax.equalsIgnoreCase( TaxonomyGroups.DEUTEROSTOMIA ) ) {
+            return TaxonomyGroups.DEUTEROSTOMIA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.PROTOSTOMIA ) ) {
+            return TaxonomyGroups.PROTOSTOMIA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.CNIDARIA ) ) {
+            return TaxonomyGroups.CNIDARIA;
+        }
+        else if ( tax.toLowerCase().startsWith( "trichoplax" ) || tax.equalsIgnoreCase( TaxonomyGroups.PLACOZOA ) ) {
+            return TaxonomyGroups.PLACOZOA;
+        }
+        else if ( tax.toLowerCase().startsWith( "mnemiopsis" ) || tax.equalsIgnoreCase( TaxonomyGroups.CTENOPHORA ) ) {
+            return TaxonomyGroups.CTENOPHORA;
+        }
+        else if ( tax.toLowerCase().startsWith( "amphimedon" ) || tax.equalsIgnoreCase( TaxonomyGroups.PORIFERA ) ) {
+            return TaxonomyGroups.PORIFERA;
+        }
+        else if ( tax.equalsIgnoreCase( "codonosigidae" ) || tax.equalsIgnoreCase( TaxonomyGroups.CHOANOFLAGELLIDA ) ) {
+            return TaxonomyGroups.CHOANOFLAGELLIDA;
+        }
+        else if ( tax.toLowerCase().startsWith( TaxonomyGroups.ICHTHYOPHONIDA_FILASTEREA )
+                || tax.toLowerCase().startsWith( "ichthyophonida and filasterea" )
+                || tax.toLowerCase().startsWith( "ichthyosporea & filasterea" )
+                || tax.toLowerCase().startsWith( "ichthyosporea and filasterea" ) ) {
+            return TaxonomyGroups.ICHTHYOPHONIDA_FILASTEREA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.DIKARYA ) ) {
+            return TaxonomyGroups.DIKARYA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.FUNGI ) || tax.equalsIgnoreCase( TaxonomyGroups.OTHER_FUNGI ) ) {
+            return TaxonomyGroups.OTHER_FUNGI;
+        }
+        else if ( tax.toLowerCase().startsWith( "nucleariidae and fonticula" ) ) {
+            return TaxonomyGroups.NUCLEARIIDAE_AND_FONTICULA_GROUP;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.AMOEBOZOA ) ) {
+            return TaxonomyGroups.AMOEBOZOA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.EMBRYOPHYTA ) ) {
+            return TaxonomyGroups.EMBRYOPHYTA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.CHLOROPHYTA ) ) {
+            return TaxonomyGroups.CHLOROPHYTA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.RHODOPHYTA ) ) {
+            return TaxonomyGroups.RHODOPHYTA;
+        }
+        else if ( tax.toLowerCase().startsWith( TaxonomyGroups.HACROBIA ) ) {
+            return TaxonomyGroups.HACROBIA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.GLAUCOCYSTOPHYCEAE ) || tax.equalsIgnoreCase( "glaucophyta" ) ) {
+            return TaxonomyGroups.GLAUCOCYSTOPHYCEAE;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.STRAMENOPILES ) ) {
+            return TaxonomyGroups.STRAMENOPILES;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.ALVEOLATA ) ) {
+            return TaxonomyGroups.ALVEOLATA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.RHIZARIA ) ) {
+            return TaxonomyGroups.RHIZARIA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.EXCAVATA ) ) {
+            return TaxonomyGroups.EXCAVATA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.APUSOZOA ) ) {
+            return TaxonomyGroups.APUSOZOA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.ARCHAEA ) ) {
+            return TaxonomyGroups.ARCHAEA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.BACTERIA ) ) {
+            return TaxonomyGroups.BACTERIA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.BACTERIA ) ) {
+            return TaxonomyGroups.BACTERIA;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.VIRUSES ) ) {
+            return TaxonomyGroups.VIRUSES;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.ALPHAHERPESVIRINAE ) ) {
+            return TaxonomyGroups.ALPHAHERPESVIRINAE;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.BETAHERPESVIRINAE ) ) {
+            return TaxonomyGroups.BETAHERPESVIRINAE;
+        }
+        else if ( tax.equalsIgnoreCase( TaxonomyGroups.GAMMAHERPESVIRINAE ) ) {
+            return TaxonomyGroups.GAMMAHERPESVIRINAE;
+        }
+        return null;
+    }
+
     final public static BufferedReader obtainReader( final Object source ) throws IOException, FileNotFoundException {
         BufferedReader reader = null;
         if ( source instanceof File ) {
@@ -817,11 +1020,26 @@ public final class ForesterUtil {
         return reader;
     }
 
-    final public static StringBuffer pad( final double number, final int size, final char pad, final boolean left_pad ) {
+    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 );
+    }
+
+    final public static StringBuffer pad( final double number,
+                                          final int size,
+                                          final char pad,
+                                          final boolean left_pad ) {
         return pad( new StringBuffer( number + "" ), size, pad, left_pad );
     }
 
-    final public static StringBuffer pad( final String string, final int size, final char pad, final boolean left_pad ) {
+    final public static StringBuffer pad( final String string,
+                                          final int size,
+                                          final char pad,
+                                          final boolean left_pad ) {
         return pad( new StringBuffer( string ), size, pad, left_pad );
     }
 
@@ -875,7 +1093,9 @@ public final class ForesterUtil {
         System.err.println( "[" + prg_name + "] > error: " + message );
     }
 
-    final public static void printProgramInformation( final String prg_name, final String prg_version, final String date ) {
+    final public static void printProgramInformation( final String prg_name,
+                                                      final String prg_version,
+                                                      final String date ) {
         final int l = prg_name.length() + prg_version.length() + date.length() + 4;
         System.out.println();
         System.out.println( prg_name + " " + prg_version + " (" + date + ")" );
@@ -919,7 +1139,8 @@ public final class ForesterUtil {
         }
         if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) {
             System.out.println();
-            System.out.println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" );
+            System.out
+                    .println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" );
         }
         System.out.println();
     }
@@ -932,6 +1153,71 @@ public final class ForesterUtil {
         System.out.println( "[" + prg_name + "] > " + message );
     }
 
+    public static List<String> readUrl( final String url_str ) throws IOException {
+        final URL url = new URL( url_str );
+        final URLConnection urlc = url.openConnection();
+        //urlc.setRequestProperty( "User-Agent", "" );
+        final BufferedReader in = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
+        String line;
+        final List<String> result = new ArrayList<String>();
+        while ( ( line = in.readLine() ) != null ) {
+            result.add( line );
+        }
+        in.close();
+        return result;
+    }
+
+    /**
+     *
+     * Example regarding engulfment: ------------0.1 ----------0.2 --0.3 =>
+     * domain with 0.3 is ignored
+     *
+     * -----------0.1 ----------0.2 --0.3 => domain with 0.3 is ignored
+     *
+     *
+     * ------------0.1 ----------0.3 --0.2 => domains with 0.3 and 0.2 are _not_
+     * ignored
+     *
+     * @param max_allowed_overlap
+     *            maximal allowed overlap (inclusive) to be still considered not
+     *            overlapping (zero or negative value to allow any overlap)
+     * @param remove_engulfed_domains
+     *            to remove domains which are completely engulfed by coverage of
+     *            domains with better support
+     * @param protein
+     * @return
+     */
+    public static Protein removeOverlappingDomains( final int max_allowed_overlap,
+                                                    final boolean remove_engulfed_domains,
+                                                    final Protein protein ) {
+        final Protein pruned_protein = new BasicProtein( protein.getProteinId().getId(),
+                                                         protein.getSpecies().getSpeciesId(),
+                                                         protein.getLength() );
+        final List<Domain> sorted = SurfacingUtil.sortDomainsWithAscendingConfidenceValues( protein );
+        final List<Boolean> covered_positions = new ArrayList<Boolean>();
+        for( final Domain domain : sorted ) {
+            if ( ( ( max_allowed_overlap < 0 )
+                    || ( ForesterUtil.calculateOverlap( domain, covered_positions ) <= max_allowed_overlap ) )
+                    && ( !remove_engulfed_domains || !isEngulfed( domain, covered_positions ) ) ) {
+                final int covered_positions_size = covered_positions.size();
+                for( int i = covered_positions_size; i < domain.getFrom(); ++i ) {
+                    covered_positions.add( false );
+                }
+                final int new_covered_positions_size = covered_positions.size();
+                for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
+                    if ( i < new_covered_positions_size ) {
+                        covered_positions.set( i, true );
+                    }
+                    else {
+                        covered_positions.add( true );
+                    }
+                }
+                pruned_protein.addProteinDomain( domain );
+            }
+        }
+        return pruned_protein;
+    }
+
     final public static String removeSuffix( final String file_name ) {
         final int i = file_name.lastIndexOf( '.' );
         if ( i > 1 ) {
@@ -942,7 +1228,7 @@ public final class ForesterUtil {
 
     /**
      * Removes all white space from String s.
-     * 
+     *
      * @return String s with white space removed
      */
     final public static String removeWhiteSpace( String s ) {
@@ -957,18 +1243,11 @@ public final class ForesterUtil {
         return s;
     }
 
-    final public static String replaceIllegalNhCharacters( final String nh ) {
-        if ( nh == null ) {
-            return "";
-        }
-        return nh.trim().replaceAll( "[\\[\\]:]+", "_" );
-    }
-
     final public static String replaceIllegalNhxCharacters( final String nhx ) {
         if ( nhx == null ) {
             return "";
         }
-        return nhx.trim().replaceAll( "[\\[\\](),:;\\s]+", "_" );
+        return nhx.trim().replaceAll( "[\\[\\]']+", "_" );
     }
 
     final public static double round( final double value, final int decimal_place ) {
@@ -1001,6 +1280,35 @@ public final class ForesterUtil {
         }
     }
 
+    public final static StringBuilder santitizeStringForNH( String data ) {
+        data = data.replaceAll( "\\s+", " " ).trim();
+        final StringBuilder sb = new StringBuilder();
+        if ( data.length() > 0 ) {
+            final boolean single_pars = data.indexOf( '\'' ) > -1;
+            final boolean double_pars = data.indexOf( '"' ) > -1;
+            if ( single_pars && double_pars ) {
+                data = data.replace( '\'', '`' );
+                sb.append( '\'' );
+                sb.append( data );
+                sb.append( '\'' );
+            }
+            else if ( single_pars ) {
+                sb.append( '"' );
+                sb.append( data );
+                sb.append( '"' );
+            }
+            else if ( PARANTHESESABLE_NH_CHARS_PATTERN.matcher( data ).find() ) {
+                sb.append( '\'' );
+                sb.append( data );
+                sb.append( '\'' );
+            }
+            else {
+                sb.append( data );
+            }
+        }
+        return sb;
+    }
+
     public static boolean seqIsLikelyToBeAa( final String s ) {
         final String seq = s.toLowerCase();
         if ( ( seq.indexOf( 'r' ) > -1 ) || ( seq.indexOf( 'd' ) > -1 ) || ( seq.indexOf( 'e' ) > -1 )
@@ -1012,6 +1320,11 @@ public final class ForesterUtil {
         return false;
     }
 
+    final private static String[] splitString( final String str ) {
+        final String regex = "[\\s;,]+";
+        return str.split( regex );
+    }
+
     final public static String stringArrayToString( final String[] a ) {
         return stringArrayToString( a, ", " );
     }
@@ -1059,17 +1372,17 @@ public final class ForesterUtil {
         return str_array;
     }
 
-    final public static void unexpectedFatalError( final Exception e ) {
+    final public static void unexpectedFatalError( final Error e ) {
         System.err.println();
-        System.err.println( "unexpected exception: should not have occured! Please contact program author(s)." );
+        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 Error e ) {
+    final public static void unexpectedFatalError( final Exception e ) {
         System.err.println();
-        System.err.println( "unexpected error: should not have occured! Please contact program author(s)." );
+        System.err.println( "unexpected exception: should not have occured! Please contact program author(s)." );
         e.printStackTrace( System.err );
         System.err.println();
         System.exit( -1 );
@@ -1157,207 +1470,42 @@ public final class ForesterUtil {
         return sb.toString();
     }
 
-    /**
-     * Helper method for calcColor methods.
-     * 
-     * @param smallercolor_component_x
-     *            color component the smaller color
-     * @param largercolor_component_x
-     *            color component the larger color
-     * @param x
-     *            factor
-     * @return an int representing a color component
-     */
-    final private static int calculateColorComponent( final double smallercolor_component_x,
-                                                      final double largercolor_component_x,
-                                                      final double x ) {
-        return ( int ) ( smallercolor_component_x + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) );
-    }
-
-    /**
-     * Helper method for calcColor methods.
-     * 
-     * 
-     * @param value
-     *            the value
-     * @param larger
-     *            the largest value
-     * @param smaller
-     *            the smallest value
-     * @return a normalized value between larger and smaller
-     */
-    final private static double calculateColorFactor( final double value, final double larger, final double smaller ) {
-        return ( 255.0 * ( value - smaller ) ) / ( larger - smaller );
-    }
-
-    final private static String[] splitString( final String str ) {
-        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 );
-    }
-
-    public final static Color obtainColorDependingOnTaxonomyGroup( final String tax_group ) {
-        if ( !ForesterUtil.isEmpty( tax_group ) ) {
-            if ( tax_group.equals( "deuterostomia" ) ) {
-                return TaxonomyColors.DEUTEROSTOMIA_COLOR;
-            }
-            else if ( tax_group.equals( "protostomia" ) ) {
-                return TaxonomyColors.PROTOSTOMIA_COLOR;
-            }
-            else if ( tax_group.equals( "cnidaria" ) ) {
-                return TaxonomyColors.CNIDARIA_COLOR;
-            }
-            else if ( tax_group.equals( "placozoa" ) ) {
-                return TaxonomyColors.PLACOZOA_COLOR;
-            }
-            else if ( tax_group.equals( "ctenophora" ) ) {
-                return TaxonomyColors.CTENOPHORA_COLOR;
-            }
-            else if ( tax_group.equals( "porifera" ) ) {
-                return TaxonomyColors.PORIFERA_COLOR;
-            }
-            else if ( tax_group.equals( "choanoflagellida" ) ) {
-                return TaxonomyColors.CHOANOFLAGELLIDA;
-            }
-            else if ( tax_group.equals( "ichthyophonida & filasterea" ) ) {
-                return TaxonomyColors.ICHTHYOSPOREA_AND_FILASTEREA;
-            }
-            else if ( tax_group.equals( "dikarya" ) ) {
-                return TaxonomyColors.DIKARYA_COLOR;
-            }
-            else if ( tax_group.equalsIgnoreCase( "fungi" ) || tax_group.equalsIgnoreCase( "other fungi" ) ) {
-                return TaxonomyColors.OTHER_FUNGI_COLOR;
-            }
-            else if ( tax_group.equals( "nucleariidae and fonticula" ) ) {
-                return TaxonomyColors.NUCLEARIIDAE_AND_FONTICULA_GROUP_COLOR;
-            }
-            else if ( tax_group.equals( "amoebozoa" ) ) {
-                return TaxonomyColors.AMOEBOZOA_COLOR;
-            }
-            else if ( tax_group.equals( "embryophyta" ) ) {
-                return TaxonomyColors.EMBRYOPHYTA_COLOR;
-            }
-            else if ( tax_group.equals( "chlorophyta" ) ) {
-                return TaxonomyColors.CHLOROPHYTA_COLOR;
-            }
-            else if ( tax_group.equals( "rhodophyta" ) ) {
-                return TaxonomyColors.RHODOPHYTA_COLOR;
-            }
-            else if ( tax_group.equals( "hacrobia" ) ) {
-                return TaxonomyColors.HACROBIA_COLOR;
-            }
-            else if ( tax_group.equals( "glaucocystophyceae" ) ) {
-                return TaxonomyColors.GLAUCOPHYTA_COLOR;
-            }
-            else if ( tax_group.equals( "stramenopiles" ) ) {
-                return TaxonomyColors.STRAMENOPILES_COLOR;
-            }
-            else if ( tax_group.equals( "alveolata" ) ) {
-                return TaxonomyColors.ALVEOLATA_COLOR;
-            }
-            else if ( tax_group.equals( "rhizaria" ) ) {
-                return TaxonomyColors.RHIZARIA_COLOR;
-            }
-            else if ( tax_group.equals( "excavata" ) ) {
-                return TaxonomyColors.EXCAVATA_COLOR;
-            }
-            else if ( tax_group.equals( "apusozoa" ) ) {
-                return TaxonomyColors.APUSOZOA_COLOR;
-            }
-            else if ( tax_group.equals( "archaea" ) ) {
-                return TaxonomyColors.ARCHAEA_COLOR;
-            }
-            else if ( tax_group.equals( "bacteria" ) ) {
-                return TaxonomyColors.BACTERIA_COLOR;
-            }
-        }
-        return null;
-    }
-
-    public final static String obtainNormalizedTaxonomyGroup( final String tax ) {
-        if ( tax.equalsIgnoreCase( "deuterostomia" ) ) {
-            return "deuterostomia";
-        }
-        else if ( tax.equalsIgnoreCase( "protostomia" ) ) {
-            return "protostomia";
-        }
-        else if ( tax.equalsIgnoreCase( "cnidaria" ) ) {
-            return "cnidaria";
-        }
-        else if ( tax.toLowerCase().startsWith( "trichoplax" ) || tax.equalsIgnoreCase( "placozoa" ) ) {
-            return "placozoa";
+    public final static Phylogeny[] readPhylogeniesFromUrl( final URL url, final PhylogenyParser parser )
+            throws NoSuchAlgorithmException, IOException, KeyManagementException {
+        if ( url == null ) {
+            throw new IllegalArgumentException( "URL to read from must not be null" );
         }
-        else if ( tax.toLowerCase().startsWith( "mnemiopsis" ) || tax.equalsIgnoreCase( "ctenophora" ) ) {
-            return "ctenophora";
+        else if ( parser == null ) {
+            throw new IllegalArgumentException( "parser to use to read from URL must not be null" );
         }
-        else if ( tax.toLowerCase().startsWith( "amphimedon" ) || tax.equalsIgnoreCase( "porifera" ) ) {
-            return "porifera";
+        final URLConnection con;
+        if ( url.toString().startsWith( "https:" ) ) {
+            con = TrustManager.makeHttpsURLConnection( url );
         }
-        else if ( tax.equalsIgnoreCase( "codonosigidae" ) || tax.equalsIgnoreCase( "choanoflagellida" ) ) {
-            return "choanoflagellida";
-        }
-        else if ( tax.toLowerCase().startsWith( "ichthyophonida & filasterea" )
-                || tax.toLowerCase().startsWith( "ichthyophonida and filasterea" )
-                || tax.toLowerCase().startsWith( "ichthyosporea & filasterea" )
-                || tax.toLowerCase().startsWith( "ichthyosporea and filasterea" ) ) {
-            return "ichthyophonida & filasterea";
-        }
-        else if ( tax.equalsIgnoreCase( "dikarya" ) ) {
-            return "dikarya";
-        }
-        else if ( tax.equalsIgnoreCase( "other fungi" ) ) {
-            return "other fungi";
-        }
-        else if ( tax.toLowerCase().startsWith( "nucleariidae and fonticula" ) ) {
-            return "nucleariidae and fonticula group";
+        else if ( url.toString().startsWith( "http:" ) ) {
+            con = url.openConnection();
         }
-        else if ( tax.equalsIgnoreCase( "amoebozoa" ) ) {
-            return "amoebozoa";
-        }
-        else if ( tax.equalsIgnoreCase( "embryophyta" ) ) {
-            return "embryophyta";
-        }
-        else if ( tax.equalsIgnoreCase( "chlorophyta" ) ) {
-            return "chlorophyta";
-        }
-        else if ( tax.equalsIgnoreCase( "rhodophyta" ) ) {
-            return "rhodophyta";
-        }
-        else if ( tax.toLowerCase().startsWith( "hacrobia" ) ) {
-            return "hacrobia";
-        }
-        else if ( tax.equalsIgnoreCase( "glaucocystophyceae" ) || tax.equalsIgnoreCase( "glaucophyta" ) ) {
-            return "glaucocystophyceae";
-        }
-        else if ( tax.equalsIgnoreCase( "stramenopiles" ) ) {
-            return "stramenopiles";
-        }
-        else if ( tax.equalsIgnoreCase( "alveolata" ) ) {
-            return "alveolata";
-        }
-        else if ( tax.equalsIgnoreCase( "rhizaria" ) ) {
-            return "rhizaria";
+        else {
+            throw new IllegalArgumentException( "Cannot deal with URL: " + url );
         }
-        else if ( tax.equalsIgnoreCase( "excavata" ) ) {
-            return "excavata";
+        if ( con == null ) {
+            throw new IOException( "could not create connection from " + url );
         }
-        else if ( tax.equalsIgnoreCase( "apusozoa" ) ) {
-            return "apusozoa";
+        con.setDefaultUseCaches( false );
+        final InputStream is = con.getInputStream();
+        if ( is == null ) {
+            throw new IOException( "could not create input stream from " + url );
         }
-        else if ( tax.equalsIgnoreCase( "archaea" ) ) {
-            return "archaea";
+        final Phylogeny[] trees = ParserBasedPhylogenyFactory.getInstance().create( is, parser );
+        try {
+            is.close();
         }
-        else if ( tax.equalsIgnoreCase( "bacteria" ) ) {
-            return "bacteria";
+        catch ( final Exception e ) {
+            // ignore  
         }
-        return null;
+        return trees;
+    }
+
+    private ForesterUtil() {
     }
 }