in progress
[jalview.git] / forester / java / src / org / forester / surfacing / PrintableDomainSimilarity.java
index 931fa32..63db3b2 100644 (file)
@@ -26,7 +26,7 @@
 
 package org.forester.surfacing;
 
-import java.awt.Color;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.SortedMap;
@@ -35,7 +35,6 @@ import java.util.TreeMap;
 import java.util.TreeSet;
 
 import org.forester.phylogeny.Phylogeny;
-import org.forester.phylogeny.PhylogenyNode;
 import org.forester.species.Species;
 import org.forester.surfacing.DomainSimilarityCalculator.Detailedness;
 import org.forester.util.ForesterUtil;
@@ -45,17 +44,17 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
     final public static String                              SPECIES_SEPARATOR = "  ";
     final private static int                                EQUAL             = 0;
     final private static String                             NO_SPECIES        = "     ";
-    final private double                                    _min;
+    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                                    _sd;
+    final private double                                    _min;
     final private int                                       _n;
-    private final int                                       _max_difference_in_counts;
-    private final int                                       _max_difference;
-    final private CombinableDomains                         _combinable_domains;
+    final private double                                    _sd;
     final private SortedMap<Species, SpeciesSpecificDcData> _species_data;
     private List<Species>                                   _species_order;
-    private DomainSimilarityCalculator.Detailedness         _detailedness;
     private final boolean                                   _treat_as_binary_comparison;
 
     public PrintableDomainSimilarity( final CombinableDomains combinable_domains,
@@ -154,89 +153,6 @@ 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,
-                                               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 ) {
-        Color c = null;
-        if ( phy != null && !phy.isEmpty() ) {
-            c = getColorDependingOnTaxonomy( tax_code, phy );
-        }
-        if ( c == null ) {
-            c = new Color( 0, 0, 0 );
-        }
-        final String hex = String.format( "#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue() );
-        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\" color=\"" + hex + "\">" + tax_code + "</a>" );
-        }
-        else {
-            sb.append( tax_code );
-        }
-        sb.append( "</b>" );
-    }
-
-    private Color getColorDependingOnTaxonomy( final String tax_code, final Phylogeny phy ) {
-        List<PhylogenyNode> nodes = phy.getNodesViaTaxonomyCode( tax_code );
-        Color c = null;
-        if ( nodes == null || nodes.isEmpty() ) {
-            throw new RuntimeException( tax_code + " is not found" );
-        }
-        if ( nodes.size() != 1 ) {
-            throw new RuntimeException( tax_code + " is not unique" );
-        }
-        PhylogenyNode n = nodes.get( 0 );
-        while ( n != null ) {
-            c = null;
-            if ( n.getNodeData().isHasTaxonomy()
-                    && !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
-                c = SurfacingUtil.getColorForTaxCode( n.getNodeData().getTaxonomy().getScientificName() );
-            }
-            if ( c == null && !ForesterUtil.isEmpty( n.getName() ) ) {
-                c = SurfacingUtil.getColorForTaxCode( n.getName() );
-            }
-            if ( c != null ) {
-                break;
-            }
-            n = n.getParent();
-        }
-        return c;
-    }
-
-    private int compareByDomainId( final DomainSimilarity other ) {
-        return getDomainId().compareToIgnoreCase( other.getDomainId() );
-    }
-
     @Override
     public int compareTo( final DomainSimilarity domain_similarity ) {
         if ( this == domain_similarity ) {
@@ -264,14 +180,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();
@@ -325,19 +233,153 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
         return _species_data;
     }
 
-    private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html,
-                                                            final Map<String, Integer> tax_code_to_id_map,
-                                                            final Phylogeny phy ) {
+    @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" );
+        }
+        _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() {
+        final SortedMap<String, SortedSet<String>> m = new TreeMap<String, SortedSet<String>>();
         final StringBuffer sb = new StringBuffer();
         for( final Species species : getSpeciesData().keySet() ) {
-            addSpeciesSpecificDomainData( sb, species, html, tax_code_to_id_map, phy );
+            for( final String combable_dom : getCombinableDomainIds( species ) ) {
+                if ( !m.containsKey( combable_dom ) ) {
+                    m.put( combable_dom, new TreeSet<String>() );
+                }
+                m.get( combable_dom ).add( species.getSpeciesId() );
+            }
+        }
+        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( ": " );
+            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( "</span>" );
+            sb.append( "<br>\n" );
         }
         return sb;
     }
 
-    private StringBuffer getDomainDataInAlphabeticalOrder() {
+    
+    
+    private StringBuffer getTaxonomyGroupDistribution( Phylogeny tol ) {
+        //TODO work on me    
+        
         final SortedMap<String, SortedSet<String>> m = new TreeMap<String, SortedSet<String>>();
-        final StringBuffer sb = new StringBuffer();
         for( final Species species : getSpeciesData().keySet() ) {
             for( final String combable_dom : getCombinableDomainIds( species ) ) {
                 if ( !m.containsKey( combable_dom ) ) {
@@ -346,14 +388,60 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
                 m.get( combable_dom ).add( species.getSpeciesId() );
             }
         }
+        Map<String,Integer> countz = new HashMap<String,Integer>();
+        for( final Map.Entry<String, SortedSet<String>> e : m.entrySet() ) {
+             for( final String tax_code : e.getValue() ) {
+             final String group = SurfacingUtil.obtainTaxonomyGroup( tax_code, tol );
+                if ( !ForesterUtil.isEmpty( group ) ) {
+                    if ( !countz.containsKey( group ) ) {
+                        countz.put( group, 1 );
+                    }
+                    else {
+                        countz.put( group, countz.get( group) + 1 );
+                    }
+                    
+                }
+                else {
+                    return null;
+                }
+                
+            }
+           
+        }
+        final StringBuffer sb = new StringBuffer();
+        
+        // i am just a template and need to be modified for "printout" TODO
         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:8px\">" );
+            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 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;
     }
@@ -374,11 +462,6 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
         return sb;
     }
 
-    @Override
-    public double getStandardDeviationOfSimilarityScore() {
-        return _sd;
-    }
-
     private void init() {
         _detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
     }
@@ -387,32 +470,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,
-                                        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 StringBuffer toStringBufferDetailedHTML( final Map<String, Integer> tax_code_to_id_map, Phylogeny phy ) {
+    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>" );
@@ -491,6 +549,6 @@ public class PrintableDomainSimilarity implements DomainSimilarity {
     }
 
     public static enum PRINT_OPTION {
-        SIMPLE_TAB_DELIMITED, HTML;
+        HTML, SIMPLE_TAB_DELIMITED;
     }
 }