in progress
[jalview.git] / forester / java / src / org / forester / util / ForesterUtil.java
index 43700ef..8036c2d 100644 (file)
@@ -67,6 +67,10 @@ 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.protein.BasicProtein;
+import org.forester.protein.Domain;
+import org.forester.protein.Protein;
+import org.forester.surfacing.SurfacingUtil;
 
 public final class ForesterUtil {
 
@@ -88,6 +92,24 @@ public final class ForesterUtil {
     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:";
+    public final static Color        DEUTEROSTOMIA_COLOR              = new Color( 255, 0, 0 );
+    public final static Color        PROTOSTOMIA_COLOR                = new Color( 204, 0, 0 );
+    public final static Color        METAZOA_COLOR                    = new Color( 204, 0, 102 );
+    public final static Color        HOLOZOA_COLOR                    = new Color( 127, 0, 255 );
+    public final static Color        FUNGI_COLOR                      = new Color( 255, 153, 0 );
+    public final static Color        HOLOMYCOTA_COLOR                 = new Color( 204, 102, 0 );
+    public final static Color        AMOEBOZOA_COLOR                  = new Color( 255, 0, 255 );
+    public final static Color        VIRIDPLANTAE_COLOR               = new Color( 0, 255, 0 );
+    public final static Color        RHODOPHYTA_COLOR                 = new Color( 0, 153, 76 );
+    public final static Color        HACROBIA_COLOR                   = new Color( 0, 102, 51 );
+    public final static Color        GLAUCOPHYTA_COLOR                = new Color( 0, 102, 51 );
+    public final static Color        STRAMENOPILES_COLOR              = new Color( 0, 0, 255 );
+    public final static Color        ALVEOLATA_COLOR                  = new Color( 0, 128, 255 );
+    public final static Color        RHIZARIA_COLOR                   = new Color( 0, 255, 255 );
+    public static final Color        APUSOZOA_COLOR                   = new Color( 204, 255, 255 );
+    public final static Color        EXCAVATA_COLOR                   = new Color( 204, 204, 0 );
+    public final static Color        ARCHAEA_COLOR                    = new Color( 160, 160, 160 );
+    public final static Color        BACTERIA_COLOR                   = new Color( 64, 64, 64 );
     static {
         final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
         dfs.setDecimalSeparator( '.' );
@@ -101,6 +123,16 @@ public final class ForesterUtil {
     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 );
@@ -108,6 +140,72 @@ public final class ForesterUtil {
     }
 
     /**
+     * 
+     * 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;
+            }
+        }
+        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,
      * otherwise a color 'proportional' to value is returned.
@@ -1125,4 +1223,62 @@ public final class ForesterUtil {
         System.err.println();
         System.exit( -1 );
     }
+
+    public final static Color obtainColorDependingOnTaxonomyGroup( final String tax ) {
+        if ( tax.equalsIgnoreCase( "deuterostomia" ) ) {
+            return DEUTEROSTOMIA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "protostomia" ) ) {
+            return PROTOSTOMIA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "metazoa" ) ) {
+            return METAZOA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "holozoa" ) ) {
+            return HOLOZOA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "fungi" ) ) {
+            return FUNGI_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "holomycota" ) ) {
+            return HOLOMYCOTA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "amoebozoa" ) ) {
+            return AMOEBOZOA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "viridiplantae" ) ) {
+            return VIRIDPLANTAE_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "rhodophyta" ) ) {
+            return RHODOPHYTA_COLOR;
+        }
+        else if ( tax.toLowerCase().startsWith( "hacrobia" ) ) {
+            return HACROBIA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "glaucocystophyceae" ) || tax.equalsIgnoreCase( "glaucophyta" ) ) {
+            return GLAUCOPHYTA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "stramenopiles" ) ) {
+            return STRAMENOPILES_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "alveolata" ) ) {
+            return ALVEOLATA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "rhizaria" ) ) {
+            return RHIZARIA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "excavata" ) ) {
+            return EXCAVATA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "apusozoa" ) ) {
+            return APUSOZOA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "archaea" ) ) {
+            return ARCHAEA_COLOR;
+        }
+        else if ( tax.equalsIgnoreCase( "bacteria" ) ) {
+            return BACTERIA_COLOR;
+        }
+        return null;
+    }
 }