cleanup
[jalview.git] / forester / java / src / org / forester / surfacing / PrintableDomainSimilarity.java
index c785ff7..22ddb1e 100644 (file)
@@ -33,28 +33,28 @@ import java.util.SortedSet;
 import java.util.TreeMap;
 import java.util.TreeSet;
 
+import org.forester.phylogeny.Phylogeny;
 import org.forester.species.Species;
 import org.forester.surfacing.DomainSimilarityCalculator.Detailedness;
 import org.forester.util.ForesterUtil;
 
 public class PrintableDomainSimilarity implements DomainSimilarity {
 
-    final public static String                                           SPECIES_SEPARATOR = "  ";
-    final private static int                                             EQUAL             = 0;
-    final private static String                                          NO_SPECIES        = "     ";
-    private static final boolean                                         PRINT_MORE_INFO   = false;
-    final private double                                                 _min;
-    final private double                                                 _max;
-    final private double                                                 _mean;
-    final private double                                                 _sd;
-    final private int                                                    _n;
-    private final int                                                    _max_difference_in_counts;
-    private final int                                                    _max_difference;
-    final private CombinableDomains                                      _combinable_domains;
-    final private SortedMap<Species, SpeciesSpecificDomainSimilariyData> _species_data;
-    private List<Species>                                                _species_order;
-    private DomainSimilarityCalculator.Detailedness                      _detailedness;
-    private final boolean                                                _treat_as_binary_comparison;
+    final public static String                              SPECIES_SEPARATOR = "  ";
+    final private static int                                EQUAL             = 0;
+    final private static String                             NO_SPECIES        = "     ";
+    final private CombinableDomains                         _combinable_domains;
+    private DomainSimilarityCalculator.Detailedness         _detailedness;
+    final private double                                    _max;
+    private final int                                       _max_difference;
+    private final int                                       _max_difference_in_counts;
+    final private double                                    _mean;
+    final private double                                    _min;
+    final private int                                       _n;
+    final private double                                    _sd;
+    final private SortedMap<Species, SpeciesSpecificDcData> _species_data;
+    private List<Species>                                   _species_order;
+    private final boolean                                   _treat_as_binary_comparison;
 
     public PrintableDomainSimilarity( final CombinableDomains combinable_domains,
                                       final double min,
@@ -65,7 +65,7 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
                                       final int n,
                                       final int max_difference_in_counts,
                                       final int max_difference,
-                                      final SortedMap<Species, SpeciesSpecificDomainSimilariyData> species_data,
+                                      final SortedMap<Species, SpeciesSpecificDcData> species_data,
                                       final boolean sort_by_species_count_first,
                                       final boolean treat_as_binary_comparison ) {
         if ( combinable_domains == null ) {
@@ -115,45 +115,41 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
         }
     }
 
-    private void addSpeciesSpecificDomainData( final StringBuffer sb,
-                                               final Species species,
-                                               final boolean html,
-                                               final Map<String, Integer> tax_code_to_id_map ) {
-        if ( html ) {
-            addTaxWithLink( sb, species.getSpeciesId(), tax_code_to_id_map );
-        }
-        else {
-            sb.append( species.getSpeciesId() );
-        }
-        if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
-            sb.append( ":" );
-            sb.append( getSpeciesData().get( species ).toStringBuffer( getDetaildness(), html ) );
-        }
-        if ( html ) {
-            sb.append( "<br>" );
+    public PrintableDomainSimilarity( final CombinableDomains combinable_domains,
+                                      final int max_difference_in_counts,
+                                      final int max_difference,
+                                      final SortedMap<Species, SpeciesSpecificDcData> species_data,
+                                      final boolean sort_by_species_count_first,
+                                      final boolean treat_as_binary_comparison ) {
+        if ( combinable_domains == null ) {
+            throw new IllegalArgumentException( "attempt to use null combinable domains" );
         }
-        else {
-            sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
+        if ( species_data == null ) {
+            throw new IllegalArgumentException( "attempt to use null species data" );
         }
-    }
-
-    private void addTaxWithLink( final StringBuffer sb,
-                                 final String tax_code,
-                                 final Map<String, Integer> tax_code_to_id_map ) {
-        sb.append( "<b>" );
-        if ( !ForesterUtil.isEmpty( tax_code )
-                && ( ( tax_code_to_id_map != null ) && tax_code_to_id_map.containsKey( tax_code ) ) ) {
-            sb.append( "<a href=\"" + SurfacingConstants.UNIPROT_TAXONOMY_ID_LINK + tax_code_to_id_map.get( tax_code )
-                    + "\" target=\"taxonomy_window\">" + tax_code + "</a>" );
+        if ( species_data.size() < 1 ) {
+            throw new IllegalArgumentException( "attempt to use empty species data" );
         }
-        else {
-            sb.append( tax_code );
+        init();
+        _combinable_domains = combinable_domains;
+        _min = -1;
+        _max = -1;
+        _mean = -1;
+        _sd = -1;
+        _n = -1;
+        _max_difference_in_counts = max_difference_in_counts;
+        _max_difference = max_difference;
+        _species_data = species_data;
+        _treat_as_binary_comparison = treat_as_binary_comparison;
+        final int s = species_data.size();
+        if ( s > 2 ) {
+            if ( getMaximalDifferenceInCounts() < 0 ) {
+                throw new IllegalArgumentException( "attempt to use negative max difference in counts with more than two species" );
+            }
+            if ( getMaximalDifference() < 0 ) {
+                throw new IllegalArgumentException( "attempt to use negative max difference with more than two species" );
+            }
         }
-        sb.append( "</b>" );
-    }
-
-    private int compareByDomainId( final DomainSimilarity other ) {
-        return getDomainId().compareToIgnoreCase( other.getDomainId() );
     }
 
     @Override
@@ -183,14 +179,6 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
         return sorted_ids;
     }
 
-    private CombinableDomains getCombinableDomains() {
-        return _combinable_domains;
-    }
-
-    private DomainSimilarityCalculator.Detailedness getDetaildness() {
-        return _detailedness;
-    }
-
     @Override
     public String getDomainId() {
         return getCombinableDomains().getKeyDomain();
@@ -240,17 +228,114 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
     }
 
     @Override
-    public SortedMap<Species, SpeciesSpecificDomainSimilariyData> getSpeciesData() {
+    public SortedMap<Species, SpeciesSpecificDcData> getSpeciesData() {
         return _species_data;
     }
 
-    private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html,
-                                                            final Map<String, Integer> tax_code_to_id_map ) {
-        final StringBuffer sb = new StringBuffer();
-        for( final Species species : getSpeciesData().keySet() ) {
-            addSpeciesSpecificDomainData( sb, species, html, tax_code_to_id_map );
+    @Override
+    public double getStandardDeviationOfSimilarityScore() {
+        return _sd;
+    }
+
+    public void setDetailedness( final Detailedness detailedness ) {
+        _detailedness = detailedness;
+    }
+
+    public void setSpeciesOrder( final List<Species> species_order ) {
+        if ( !species_order.containsAll( getSpeciesData().keySet() ) ) {
+            throw new IllegalArgumentException( "list to order species must contain all species of multiple combinable domains similarity" );
         }
-        return sb;
+        _species_order = species_order;
+    }
+
+    @Override
+    public StringBuffer toStringBuffer( final PrintableDomainSimilarity.PRINT_OPTION print_option,
+                                        final Map<String, Integer> tax_code_to_id_map,
+                                        final Phylogeny phy ) {
+        switch ( print_option ) {
+            case SIMPLE_TAB_DELIMITED:
+                return toStringBufferSimpleTabDelimited();
+            case HTML:
+                return toStringBufferDetailedHTML( tax_code_to_id_map, phy );
+            default:
+                throw new AssertionError( "Unknown print option: " + print_option );
+        }
+    }
+
+    private void addSpeciesSpecificDomainData( final StringBuffer sb,
+                                               final Species species,
+                                               final boolean html,
+                                               final Map<String, Integer> tax_code_to_id_map,
+                                               final Phylogeny phy ) {
+        if ( html ) {
+            addTaxWithLink( sb, species.getSpeciesId(), tax_code_to_id_map, phy );
+        }
+        else {
+            sb.append( species.getSpeciesId() );
+        }
+        if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
+            if ( html ) {
+                sb.append( ":" );
+            }
+            else {
+                sb.append( "\t" );
+            }
+            sb.append( getSpeciesData().get( species ).toStringBuffer( getDetaildness(), html ) );
+        }
+        if ( html ) {
+            sb.append( "<br>" );
+        }
+        else {
+            sb.append( "\n\t" );
+        }
+    }
+
+    private void addTaxWithLink( final StringBuffer sb,
+                                 final String tax_code,
+                                 final Map<String, Integer> tax_code_to_id_map,
+                                 final Phylogeny phy ) {
+        String hex = null;
+        if ( ( phy != null ) && !phy.isEmpty() ) {
+            hex = SurfacingUtil.obtainHexColorStringDependingOnTaxonomyGroup( tax_code, phy );
+        }
+        sb.append( "<b>" );
+        if ( !ForesterUtil.isEmpty( tax_code )
+                && ( ( tax_code_to_id_map != null ) && tax_code_to_id_map.containsKey( tax_code ) ) ) {
+            if ( !ForesterUtil.isEmpty( hex ) ) {
+                sb.append( "<a href=\"" );
+                sb.append( SurfacingConstants.UNIPROT_TAXONOMY_ID_LINK );
+                sb.append( tax_code_to_id_map.get( tax_code ) );
+                sb.append( "\" target=\"tw\"><span style=\"color:" );
+                sb.append( hex );
+                sb.append( "\">" );
+                sb.append( tax_code );
+                sb.append( "</span></a>" );
+            }
+            else {
+                sb.append( "<a href=\"" );
+                sb.append( SurfacingConstants.UNIPROT_TAXONOMY_ID_LINK );
+                sb.append( tax_code_to_id_map.get( tax_code ) );
+                sb.append( "\" target=\"tw\">" );
+                sb.append( tax_code );
+                sb.append( "</a>" );
+            }
+        }
+        else {
+            sb.append( tax_code );
+        }
+        sb.append( "</b>" );
+    }
+
+    private int compareByDomainId( final DomainSimilarity other ) {
+        return getDomainId().compareToIgnoreCase( other.getDomainId() );
+    }
+
+    private CombinableDomains getCombinableDomains() {
+        return _combinable_domains;
+    }
+
+    private DomainSimilarityCalculator.Detailedness getDetaildness() {
+        return _detailedness;
     }
 
     private StringBuffer getDomainDataInAlphabeticalOrder() {
@@ -267,20 +352,44 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
         for( final Map.Entry<String, SortedSet<String>> e : m.entrySet() ) {
             sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + e.getKey() + "\">" + e.getKey() + "</a>" );
             sb.append( ": " );
-            for( final String s : e.getValue() ) {
-                sb.append( s );
+            sb.append( "<span style=\"font-size:7px\">" );
+            for( final String tax : e.getValue() ) {
+                final String hex = SurfacingUtil.obtainHexColorStringDependingOnTaxonomyGroup( tax, null );
+                if ( !ForesterUtil.isEmpty( hex ) ) {
+                    sb.append( "<span style=\"color:" );
+                    sb.append( hex );
+                    sb.append( "\">" );
+                    sb.append( tax );
+                    sb.append( "</span>" );
+                }
+                else {
+                    sb.append( tax );
+                }
                 sb.append( " " );
             }
-            sb.append( "<br>" );
+            sb.append( "</span>" );
+            sb.append( "<br>\n" );
         }
         return sb;
     }
 
-    private StringBuffer getSpeciesDataInCustomOrder( final boolean html, final Map<String, Integer> tax_code_to_id_map ) {
+    private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html,
+                                                            final Map<String, Integer> tax_code_to_id_map,
+                                                            final Phylogeny phy ) {
+        final StringBuffer sb = new StringBuffer();
+        for( final Species species : getSpeciesData().keySet() ) {
+            addSpeciesSpecificDomainData( sb, species, html, tax_code_to_id_map, phy );
+        }
+        return sb;
+    }
+
+    private StringBuffer getSpeciesDataInCustomOrder( final boolean html,
+                                                      final Map<String, Integer> tax_code_to_id_map,
+                                                      final Phylogeny phy ) {
         final StringBuffer sb = new StringBuffer();
         for( final Species order_species : getSpeciesCustomOrder() ) {
             if ( getSpeciesData().keySet().contains( order_species ) ) {
-                addSpeciesSpecificDomainData( sb, order_species, html, tax_code_to_id_map );
+                addSpeciesSpecificDomainData( sb, order_species, html, tax_code_to_id_map, phy );
             }
             else {
                 sb.append( PrintableDomainSimilarity.NO_SPECIES );
@@ -290,11 +399,6 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
         return sb;
     }
 
-    @Override
-    public double getStandardDeviationOfSimilarityScore() {
-        return _sd;
-    }
-
     private void init() {
         _detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
     }
@@ -303,31 +407,7 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
         return _treat_as_binary_comparison;
     }
 
-    public void setDetailedness( final Detailedness detailedness ) {
-        _detailedness = detailedness;
-    }
-
-    public void setSpeciesOrder( final List<Species> species_order ) {
-        if ( !species_order.containsAll( getSpeciesData().keySet() ) ) {
-            throw new IllegalArgumentException( "list to order species must contain all species of multiple combinable domains similarity" );
-        }
-        _species_order = species_order;
-    }
-
-    @Override
-    public StringBuffer toStringBuffer( final PrintableDomainSimilarity.PRINT_OPTION print_option,
-                                        final Map<String, Integer> tax_code_to_id_map ) {
-        switch ( print_option ) {
-            case SIMPLE_TAB_DELIMITED:
-                return toStringBufferSimpleTabDelimited();
-            case HTML:
-                return toStringBufferDetailedHTML( tax_code_to_id_map );
-            default:
-                throw new AssertionError( "Unknown print option: " + print_option );
-        }
-    }
-
-    private StringBuffer toStringBufferDetailedHTML( final Map<String, Integer> tax_code_to_id_map ) {
+    private StringBuffer toStringBufferDetailedHTML( final Map<String, Integer> tax_code_to_id_map, final Phylogeny phy ) {
         final StringBuffer sb = new StringBuffer();
         sb.append( "<tr>" );
         sb.append( "<td>" );
@@ -341,23 +421,25 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
         sb.append( "<a href=\"" + SurfacingConstants.GOOGLE_SCHOLAR_SEARCH + getDomainId()
                 + "\" target=\"gs_window\">gs</a>" );
         sb.append( "</td>" );
-        sb.append( "<td>" );
-        sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
-        sb.append( "</td>" );
-        if ( PRINT_MORE_INFO ) {
-            if ( !isTreatAsBinaryComparison() ) {
-                sb.append( "<td>" );
-                sb.append( "(" );
-                sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
-                sb.append( ")" );
-                sb.append( "</td>" );
-                sb.append( "<td>" );
-                sb.append( "[" );
-                sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
-                sb.append( "-" );
-                sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
-                sb.append( "]" );
-                sb.append( "</td>" );
+        if ( getMaximalSimilarityScore() > 0 ) {
+            sb.append( "<td>" );
+            sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
+            sb.append( "</td>" );
+            if ( SurfacingConstants.PRINT_MORE_DOM_SIMILARITY_INFO ) {
+                if ( !isTreatAsBinaryComparison() ) {
+                    sb.append( "<td>" );
+                    sb.append( "(" );
+                    sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
+                    sb.append( ")" );
+                    sb.append( "</td>" );
+                    sb.append( "<td>" );
+                    sb.append( "[" );
+                    sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
+                    sb.append( "-" );
+                    sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
+                    sb.append( "]" );
+                    sb.append( "</td>" );
+                }
             }
         }
         sb.append( "<td>" );
@@ -380,13 +462,13 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
         }
         if ( ( getSpeciesCustomOrder() == null ) || getSpeciesCustomOrder().isEmpty() ) {
             sb.append( "<td>" );
-            sb.append( getSpeciesDataInAlphabeticalOrder( true, tax_code_to_id_map ) );
+            sb.append( getSpeciesDataInAlphabeticalOrder( true, tax_code_to_id_map, phy ) );
             sb.append( getDomainDataInAlphabeticalOrder() );
             sb.append( "</td>" );
         }
         else {
             sb.append( "<td>" );
-            sb.append( getSpeciesDataInCustomOrder( true, tax_code_to_id_map ) );
+            sb.append( getSpeciesDataInCustomOrder( true, tax_code_to_id_map, phy ) );
             sb.append( getDomainDataInAlphabeticalOrder() );
             sb.append( "</td>" );
         }
@@ -397,10 +479,13 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
     private StringBuffer toStringBufferSimpleTabDelimited() {
         final StringBuffer sb = new StringBuffer();
         sb.append( getDomainId() );
+        sb.append( "\t" );
+        sb.append( getSpeciesDataInAlphabeticalOrder( false, null, null ) );
+        sb.append( "\n" );
         return sb;
     }
 
     public static enum PRINT_OPTION {
-        SIMPLE_TAB_DELIMITED, HTML;
+        HTML, SIMPLE_TAB_DELIMITED;
     }
 }