X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fsurfacing%2FPrintableDomainSimilarity.java;h=22ddb1ecb6cb0106480d25d2deb695b843a80a0b;hb=41ea5973f93687513d29e5b7cad1abff8f3adb4b;hp=c785ff7ea136a03bd13d51d280ebbb1aacdcf6d9;hpb=f47d5c382bc4e329ff6977ebcc46b75b57ffc901;p=jalview.git diff --git a/forester/java/src/org/forester/surfacing/PrintableDomainSimilarity.java b/forester/java/src/org/forester/surfacing/PrintableDomainSimilarity.java index c785ff7..22ddb1e 100644 --- a/forester/java/src/org/forester/surfacing/PrintableDomainSimilarity.java +++ b/forester/java/src/org/forester/surfacing/PrintableDomainSimilarity.java @@ -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_data; - private List _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_data; + private List _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_data, + final SortedMap 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 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( "
" ); + public PrintableDomainSimilarity( final CombinableDomains combinable_domains, + final int max_difference_in_counts, + final int max_difference, + final SortedMap 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 tax_code_to_id_map ) { - 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 + "" ); + 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( "" ); - } - - 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 getSpeciesData() { + public SortedMap getSpeciesData() { return _species_data; } - private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html, - final Map 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_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 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() { @@ -267,20 +352,44 @@ public class PrintableDomainSimilarity implements DomainSimilarity { 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 getSpeciesDataInCustomOrder( final boolean html, final Map tax_code_to_id_map ) { + 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; + } + + private StringBuffer getSpeciesDataInCustomOrder( final boolean html, + final Map 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_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 ) { - 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 tax_code_to_id_map ) { + private StringBuffer toStringBufferDetailedHTML( final Map tax_code_to_id_map, final Phylogeny phy ) { final StringBuffer sb = new StringBuffer(); sb.append( "" ); sb.append( "" ); @@ -341,23 +421,25 @@ public class PrintableDomainSimilarity implements DomainSimilarity { sb.append( "gs" ); sb.append( "" ); - sb.append( "" ); - sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) ); - sb.append( "" ); - if ( PRINT_MORE_INFO ) { - if ( !isTreatAsBinaryComparison() ) { - sb.append( "" ); - sb.append( "(" ); - sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) ); - sb.append( ")" ); - sb.append( "" ); - sb.append( "" ); - sb.append( "[" ); - sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) ); - sb.append( "-" ); - sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) ); - sb.append( "]" ); - sb.append( "" ); + if ( getMaximalSimilarityScore() > 0 ) { + sb.append( "" ); + sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) ); + sb.append( "" ); + if ( SurfacingConstants.PRINT_MORE_DOM_SIMILARITY_INFO ) { + if ( !isTreatAsBinaryComparison() ) { + sb.append( "" ); + sb.append( "(" ); + sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) ); + sb.append( ")" ); + sb.append( "" ); + sb.append( "" ); + sb.append( "[" ); + sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) ); + sb.append( "-" ); + sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) ); + sb.append( "]" ); + sb.append( "" ); + } } } sb.append( "" ); @@ -380,13 +462,13 @@ public class PrintableDomainSimilarity implements DomainSimilarity { } if ( ( getSpeciesCustomOrder() == null ) || getSpeciesCustomOrder().isEmpty() ) { sb.append( "" ); - sb.append( getSpeciesDataInAlphabeticalOrder( true, tax_code_to_id_map ) ); + sb.append( getSpeciesDataInAlphabeticalOrder( true, tax_code_to_id_map, phy ) ); sb.append( getDomainDataInAlphabeticalOrder() ); sb.append( "" ); } else { sb.append( "" ); - sb.append( getSpeciesDataInCustomOrder( true, tax_code_to_id_map ) ); + sb.append( getSpeciesDataInCustomOrder( true, tax_code_to_id_map, phy ) ); sb.append( getDomainDataInAlphabeticalOrder() ); sb.append( "" ); } @@ -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; } }