3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // Contact: phylosoft @ gmail . com
25 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
27 package org.forester.surfacing;
29 import java.util.List;
31 import java.util.SortedMap;
32 import java.util.SortedSet;
33 import java.util.TreeMap;
34 import java.util.TreeSet;
36 import org.forester.phylogeny.Phylogeny;
37 import org.forester.species.Species;
38 import org.forester.surfacing.DomainSimilarityCalculator.Detailedness;
39 import org.forester.util.ForesterUtil;
41 public class PrintableDomainSimilarity implements DomainSimilarity {
43 final public static String SPECIES_SEPARATOR = " ";
44 final private static int EQUAL = 0;
45 final private static String NO_SPECIES = " ";
46 final private CombinableDomains _combinable_domains;
47 private DomainSimilarityCalculator.Detailedness _detailedness;
48 final private double _max;
49 private final int _max_difference;
50 private final int _max_difference_in_counts;
51 final private double _mean;
52 final private double _min;
54 final private double _sd;
55 final private SortedMap<Species, SpeciesSpecificDcData> _species_data;
56 private List<Species> _species_order;
57 private final boolean _treat_as_binary_comparison;
59 public PrintableDomainSimilarity( final CombinableDomains combinable_domains,
66 final int max_difference_in_counts,
67 final int max_difference,
68 final SortedMap<Species, SpeciesSpecificDcData> species_data,
69 final boolean sort_by_species_count_first,
70 final boolean treat_as_binary_comparison ) {
71 if ( combinable_domains == null ) {
72 throw new IllegalArgumentException( "attempt to use null combinable domains" );
74 if ( species_data == null ) {
75 throw new IllegalArgumentException( "attempt to use null species data" );
77 if ( species_data.size() < 1 ) {
78 throw new IllegalArgumentException( "attempt to use empty species data" );
81 throw new IllegalArgumentException( "attempt to use N less than 0" );
83 if ( ( species_data.size() > 1 ) && ( n < 1 ) ) {
84 throw new IllegalArgumentException( "attempt to use N less than 1" );
87 throw new IllegalArgumentException( "attempt to use negative SD" );
90 throw new IllegalArgumentException( "attempt to use max smaller than min" );
93 _combinable_domains = combinable_domains;
99 _max_difference_in_counts = max_difference_in_counts;
100 _max_difference = max_difference;
101 _species_data = species_data;
102 _treat_as_binary_comparison = treat_as_binary_comparison;
103 final int s = species_data.size();
104 if ( ( ( s * s ) - s ) != ( getN() * 2 ) ) {
105 throw new IllegalArgumentException( "illegal species count and n: species count:" + s + ", n:" + _n
106 + " for domain " + combinable_domains.getKeyDomain() );
109 if ( getMaximalDifferenceInCounts() < 0 ) {
110 throw new IllegalArgumentException( "attempt to use negative max difference in counts with more than two species" );
112 if ( getMaximalDifference() < 0 ) {
113 throw new IllegalArgumentException( "attempt to use negative max difference with more than two species" );
118 public PrintableDomainSimilarity( final CombinableDomains combinable_domains,
119 final int max_difference_in_counts,
120 final int max_difference,
121 final SortedMap<Species, SpeciesSpecificDcData> species_data,
122 final boolean sort_by_species_count_first,
123 final boolean treat_as_binary_comparison ) {
124 if ( combinable_domains == null ) {
125 throw new IllegalArgumentException( "attempt to use null combinable domains" );
127 if ( species_data == null ) {
128 throw new IllegalArgumentException( "attempt to use null species data" );
130 if ( species_data.size() < 1 ) {
131 throw new IllegalArgumentException( "attempt to use empty species data" );
134 _combinable_domains = combinable_domains;
140 _max_difference_in_counts = max_difference_in_counts;
141 _max_difference = max_difference;
142 _species_data = species_data;
143 _treat_as_binary_comparison = treat_as_binary_comparison;
144 final int s = species_data.size();
146 if ( getMaximalDifferenceInCounts() < 0 ) {
147 throw new IllegalArgumentException( "attempt to use negative max difference in counts with more than two species" );
149 if ( getMaximalDifference() < 0 ) {
150 throw new IllegalArgumentException( "attempt to use negative max difference with more than two species" );
156 public int compareTo( final DomainSimilarity domain_similarity ) {
157 if ( this == domain_similarity ) {
160 else if ( domain_similarity == null ) {
161 throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to null" );
163 else if ( domain_similarity.getClass() != this.getClass() ) {
164 throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to "
165 + domain_similarity.getClass() );
167 return compareByDomainId( domain_similarity );
171 public SortedSet<String> getCombinableDomainIds( final Species species_of_combinable_domain ) {
172 final SortedSet<String> sorted_ids = new TreeSet<String>();
173 if ( getSpeciesData().containsKey( species_of_combinable_domain ) ) {
174 for( final String id : getSpeciesData().get( species_of_combinable_domain )
175 .getCombinableDomainIdToCountsMap().keySet() ) {
176 sorted_ids.add( id );
183 public String getDomainId() {
184 return getCombinableDomains().getKeyDomain();
188 public int getMaximalDifference() {
189 return _max_difference;
193 public int getMaximalDifferenceInCounts() {
194 return _max_difference_in_counts;
198 public double getMaximalSimilarityScore() {
203 public double getMeanSimilarityScore() {
208 public double getMinimalSimilarityScore() {
218 public SortedSet<Species> getSpecies() {
219 final SortedSet<Species> species = new TreeSet<Species>();
220 for( final Species s : getSpeciesData().keySet() ) {
226 public List<Species> getSpeciesCustomOrder() {
227 return _species_order;
231 public SortedMap<Species, SpeciesSpecificDcData> getSpeciesData() {
232 return _species_data;
236 public double getStandardDeviationOfSimilarityScore() {
240 public void setDetailedness( final Detailedness detailedness ) {
241 _detailedness = detailedness;
244 public void setSpeciesOrder( final List<Species> species_order ) {
245 if ( !species_order.containsAll( getSpeciesData().keySet() ) ) {
246 throw new IllegalArgumentException( "list to order species must contain all species of multiple combinable domains similarity" );
248 _species_order = species_order;
252 public StringBuffer toStringBuffer( final PrintableDomainSimilarity.PRINT_OPTION print_option,
253 final Map<String, Integer> tax_code_to_id_map,
254 final Phylogeny phy ) {
255 switch ( print_option ) {
256 case SIMPLE_TAB_DELIMITED:
257 return toStringBufferSimpleTabDelimited();
259 return toStringBufferDetailedHTML( tax_code_to_id_map, phy );
261 throw new AssertionError( "Unknown print option: " + print_option );
265 private void addSpeciesSpecificDomainData( final StringBuffer sb,
266 final Species species,
268 final Map<String, Integer> tax_code_to_id_map,
269 final Phylogeny phy ) {
271 addTaxWithLink( sb, species.getSpeciesId(), tax_code_to_id_map, phy );
274 sb.append( species.getSpeciesId() );
276 if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
283 sb.append( getSpeciesData().get( species ).toStringBuffer( getDetaildness(), html ) );
293 private void addTaxWithLink( final StringBuffer sb,
294 final String tax_code,
295 final Map<String, Integer> tax_code_to_id_map,
296 final Phylogeny phy ) {
298 if ( ( phy != null ) && !phy.isEmpty() ) {
299 hex = SurfacingUtil.obtainHexColorStringDependingOnTaxonomyGroup( tax_code, phy );
302 if ( !ForesterUtil.isEmpty( tax_code )
303 && ( ( tax_code_to_id_map != null ) && tax_code_to_id_map.containsKey( tax_code ) ) ) {
304 if ( !ForesterUtil.isEmpty( hex ) ) {
305 sb.append( "<a href=\"" );
306 sb.append( SurfacingConstants.UNIPROT_TAXONOMY_ID_LINK );
307 sb.append( tax_code_to_id_map.get( tax_code ) );
308 sb.append( "\" target=\"tw\"><span style=\"color:" );
311 sb.append( tax_code );
312 sb.append( "</span></a>" );
315 sb.append( "<a href=\"" );
316 sb.append( SurfacingConstants.UNIPROT_TAXONOMY_ID_LINK );
317 sb.append( tax_code_to_id_map.get( tax_code ) );
318 sb.append( "\" target=\"tw\">" );
319 sb.append( tax_code );
324 sb.append( tax_code );
329 private int compareByDomainId( final DomainSimilarity other ) {
330 return getDomainId().compareToIgnoreCase( other.getDomainId() );
333 private CombinableDomains getCombinableDomains() {
334 return _combinable_domains;
337 private DomainSimilarityCalculator.Detailedness getDetaildness() {
338 return _detailedness;
341 private StringBuffer getDomainDataInAlphabeticalOrder() {
342 final SortedMap<String, SortedSet<String>> m = new TreeMap<String, SortedSet<String>>();
343 final StringBuffer sb = new StringBuffer();
344 for( final Species species : getSpeciesData().keySet() ) {
345 for( final String combable_dom : getCombinableDomainIds( species ) ) {
346 if ( !m.containsKey( combable_dom ) ) {
347 m.put( combable_dom, new TreeSet<String>() );
349 m.get( combable_dom ).add( species.getSpeciesId() );
352 for( final Map.Entry<String, SortedSet<String>> e : m.entrySet() ) {
353 sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + e.getKey() + "\">" + e.getKey() + "</a>" );
355 sb.append( "<span style=\"font-size:7px\">" );
356 for( final String tax : e.getValue() ) {
357 final String hex = SurfacingUtil.obtainHexColorStringDependingOnTaxonomyGroup( tax, null );
358 if ( !ForesterUtil.isEmpty( hex ) ) {
359 sb.append( "<span style=\"color:" );
363 sb.append( "</span>" );
370 sb.append( "</span>" );
371 sb.append( "<br>\n" );
376 private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html,
377 final Map<String, Integer> tax_code_to_id_map,
378 final Phylogeny phy ) {
379 final StringBuffer sb = new StringBuffer();
380 for( final Species species : getSpeciesData().keySet() ) {
381 addSpeciesSpecificDomainData( sb, species, html, tax_code_to_id_map, phy );
386 private StringBuffer getSpeciesDataInCustomOrder( final boolean html,
387 final Map<String, Integer> tax_code_to_id_map,
388 final Phylogeny phy ) {
389 final StringBuffer sb = new StringBuffer();
390 for( final Species order_species : getSpeciesCustomOrder() ) {
391 if ( getSpeciesData().keySet().contains( order_species ) ) {
392 addSpeciesSpecificDomainData( sb, order_species, html, tax_code_to_id_map, phy );
395 sb.append( PrintableDomainSimilarity.NO_SPECIES );
396 sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
402 private void init() {
403 _detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
406 private boolean isTreatAsBinaryComparison() {
407 return _treat_as_binary_comparison;
410 private StringBuffer toStringBufferDetailedHTML( final Map<String, Integer> tax_code_to_id_map, final Phylogeny phy ) {
411 final StringBuffer sb = new StringBuffer();
415 sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + getDomainId() + "\" target=\"pfam_window\">"
416 + getDomainId() + "</a>" );
418 sb.append( "<a name=\"" + getDomainId() + "\">" );
419 sb.append( "</td>" );
421 sb.append( "<a href=\"" + SurfacingConstants.GOOGLE_SCHOLAR_SEARCH + getDomainId()
422 + "\" target=\"gs_window\">gs</a>" );
423 sb.append( "</td>" );
424 if ( getMaximalSimilarityScore() > 0 ) {
426 sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
427 sb.append( "</td>" );
428 if ( SurfacingConstants.PRINT_MORE_DOM_SIMILARITY_INFO ) {
429 if ( !isTreatAsBinaryComparison() ) {
432 sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
434 sb.append( "</td>" );
437 sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
439 sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
441 sb.append( "</td>" );
446 sb.append( getMaximalDifference() );
447 sb.append( "</td>" );
449 if ( isTreatAsBinaryComparison() ) {
450 sb.append( getMaximalDifferenceInCounts() );
453 sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
455 sb.append( "</td>" );
456 if ( !isTreatAsBinaryComparison() ) {
459 sb.append( getSpeciesData().size() );
461 sb.append( "</td>" );
463 if ( ( getSpeciesCustomOrder() == null ) || getSpeciesCustomOrder().isEmpty() ) {
465 sb.append( getSpeciesDataInAlphabeticalOrder( true, tax_code_to_id_map, phy ) );
466 sb.append( getDomainDataInAlphabeticalOrder() );
467 sb.append( "</td>" );
471 sb.append( getSpeciesDataInCustomOrder( true, tax_code_to_id_map, phy ) );
472 sb.append( getDomainDataInAlphabeticalOrder() );
473 sb.append( "</td>" );
475 sb.append( "</tr>" );
479 private StringBuffer toStringBufferSimpleTabDelimited() {
480 final StringBuffer sb = new StringBuffer();
481 sb.append( getDomainId() );
483 sb.append( getSpeciesDataInAlphabeticalOrder( false, null, null ) );
488 public static enum PRINT_OPTION {
489 HTML, SIMPLE_TAB_DELIMITED;