X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fsurfacing%2FPrintableDomainSimilarity.java;h=63db3b245e1897a5b8242ded0fd40f3027f595aa;hb=1c57d9dd98190445f28ff5b2d447614fb14dd1aa;hp=931fa3231dcdedba045d2d34b9e98b7914c3b4f5;hpb=0898ccf757a9e1b3c10f0a8ad51829eb809d3062;p=jalview.git diff --git a/forester/java/src/org/forester/surfacing/PrintableDomainSimilarity.java b/forester/java/src/org/forester/surfacing/PrintableDomainSimilarity.java index 931fa32..63db3b2 100644 --- a/forester/java/src/org/forester/surfacing/PrintableDomainSimilarity.java +++ b/forester/java/src/org/forester/surfacing/PrintableDomainSimilarity.java @@ -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_data; private List _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 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( "
" ); - } - else { - sb.append( "\n\t" ); - } - } - - private void addTaxWithLink( final StringBuffer sb, - final String tax_code, - final Map 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( "" ); - if ( !ForesterUtil.isEmpty( tax_code ) - && ( ( tax_code_to_id_map != null ) && tax_code_to_id_map.containsKey( tax_code ) ) ) { - sb.append( "" + tax_code + "" ); - } - else { - sb.append( tax_code ); - } - sb.append( "" ); - } - - private Color getColorDependingOnTaxonomy( final String tax_code, final Phylogeny phy ) { - List 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 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_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 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 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( "
" ); + } + else { + sb.append( "\n\t" ); + } + } + + private void addTaxWithLink( final StringBuffer sb, + final String tax_code, + final Map tax_code_to_id_map, + final Phylogeny phy ) { + String hex = null; + if ( ( phy != null ) && !phy.isEmpty() ) { + hex = SurfacingUtil.obtainHexColorStringDependingOnTaxonomyGroup( tax_code, phy ); + } + sb.append( "" ); + 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( "" ); + sb.append( tax_code ); + sb.append( "" ); + } + else { + sb.append( "" ); + sb.append( tax_code ); + sb.append( "" ); + } + } + else { + sb.append( tax_code ); + } + sb.append( "" ); + } + + 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> m = new TreeMap>(); 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() ); + } + m.get( combable_dom ).add( species.getSpeciesId() ); + } + } + for( final Map.Entry> e : m.entrySet() ) { + sb.append( "" + e.getKey() + "" ); + sb.append( ": " ); + sb.append( "" ); + for( final String tax : e.getValue() ) { + final String hex = SurfacingUtil.obtainHexColorStringDependingOnTaxonomyGroup( tax, null ); + if ( !ForesterUtil.isEmpty( hex ) ) { + sb.append( "" ); + sb.append( tax ); + sb.append( "" ); + } + else { + sb.append( tax ); + } + sb.append( " " ); + } + sb.append( "" ); + sb.append( "
\n" ); } return sb; } - private StringBuffer getDomainDataInAlphabeticalOrder() { + + + private StringBuffer getTaxonomyGroupDistribution( Phylogeny tol ) { + //TODO work on me + final SortedMap> m = new TreeMap>(); - 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 countz = new HashMap(); + for( final Map.Entry> 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> e : m.entrySet() ) { sb.append( "" + e.getKey() + "" ); sb.append( ": " ); - for( final String s : e.getValue() ) { - sb.append( s ); + sb.append( "" ); + for( final String tax : e.getValue() ) { + final String hex = SurfacingUtil.obtainHexColorStringDependingOnTaxonomyGroup( tax, null ); + if ( !ForesterUtil.isEmpty( hex ) ) { + sb.append( "" ); + sb.append( tax ); + sb.append( "" ); + } + else { + sb.append( tax ); + } sb.append( " " ); } - sb.append( "
" ); + sb.append( "
" ); + sb.append( "
\n" ); + } + return sb; + } + + + private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html, + final Map 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_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 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 tax_code_to_id_map, Phylogeny phy ) { + private StringBuffer toStringBufferDetailedHTML( final Map tax_code_to_id_map, final Phylogeny phy ) { final StringBuffer sb = new StringBuffer(); sb.append( "" ); sb.append( "" ); @@ -491,6 +549,6 @@ public class PrintableDomainSimilarity implements DomainSimilarity { } public static enum PRINT_OPTION { - SIMPLE_TAB_DELIMITED, HTML; + HTML, SIMPLE_TAB_DELIMITED; } }