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.species.Species;
37 import org.forester.surfacing.DomainSimilarityCalculator.Detailedness;
38 import org.forester.util.ForesterUtil;
40 public class PrintableDomainSimilarity implements DomainSimilarity {
42 final public static String SPECIES_SEPARATOR = " ";
43 final private static int EQUAL = 0;
44 final private static String NO_SPECIES = " ";
45 final private double _min;
46 final private double _max;
47 final private double _mean;
48 final private double _sd;
50 private final int _max_difference_in_counts;
51 private final int _max_difference;
52 final private CombinableDomains _combinable_domains;
53 final private SortedMap<Species, SpeciesSpecificDcData> _species_data;
54 private List<Species> _species_order;
55 private DomainSimilarityCalculator.Detailedness _detailedness;
56 private final boolean _treat_as_binary_comparison;
58 public PrintableDomainSimilarity( final CombinableDomains combinable_domains,
65 final int max_difference_in_counts,
66 final int max_difference,
67 final SortedMap<Species, SpeciesSpecificDcData> species_data,
68 final boolean sort_by_species_count_first,
69 final boolean treat_as_binary_comparison ) {
70 if ( combinable_domains == null ) {
71 throw new IllegalArgumentException( "attempt to use null combinable domains" );
73 if ( species_data == null ) {
74 throw new IllegalArgumentException( "attempt to use null species data" );
76 if ( species_data.size() < 1 ) {
77 throw new IllegalArgumentException( "attempt to use empty species data" );
80 throw new IllegalArgumentException( "attempt to use N less than 0" );
82 if ( ( species_data.size() > 1 ) && ( n < 1 ) ) {
83 throw new IllegalArgumentException( "attempt to use N less than 1" );
86 throw new IllegalArgumentException( "attempt to use negative SD" );
89 throw new IllegalArgumentException( "attempt to use max smaller than min" );
92 _combinable_domains = combinable_domains;
98 _max_difference_in_counts = max_difference_in_counts;
99 _max_difference = max_difference;
100 _species_data = species_data;
101 _treat_as_binary_comparison = treat_as_binary_comparison;
102 final int s = species_data.size();
103 if ( ( ( s * s ) - s ) != ( getN() * 2 ) ) {
104 throw new IllegalArgumentException( "illegal species count and n: species count:" + s + ", n:" + _n
105 + " for domain " + combinable_domains.getKeyDomain() );
108 if ( getMaximalDifferenceInCounts() < 0 ) {
109 throw new IllegalArgumentException( "attempt to use negative max difference in counts with more than two species" );
111 if ( getMaximalDifference() < 0 ) {
112 throw new IllegalArgumentException( "attempt to use negative max difference with more than two species" );
117 public PrintableDomainSimilarity( final CombinableDomains combinable_domains,
118 final int max_difference_in_counts,
119 final int max_difference,
120 final SortedMap<Species, SpeciesSpecificDcData> species_data,
121 final boolean sort_by_species_count_first,
122 final boolean treat_as_binary_comparison ) {
123 if ( combinable_domains == null ) {
124 throw new IllegalArgumentException( "attempt to use null combinable domains" );
126 if ( species_data == null ) {
127 throw new IllegalArgumentException( "attempt to use null species data" );
129 if ( species_data.size() < 1 ) {
130 throw new IllegalArgumentException( "attempt to use empty species data" );
133 _combinable_domains = combinable_domains;
139 _max_difference_in_counts = max_difference_in_counts;
140 _max_difference = max_difference;
141 _species_data = species_data;
142 _treat_as_binary_comparison = treat_as_binary_comparison;
143 final int s = species_data.size();
145 if ( getMaximalDifferenceInCounts() < 0 ) {
146 throw new IllegalArgumentException( "attempt to use negative max difference in counts with more than two species" );
148 if ( getMaximalDifference() < 0 ) {
149 throw new IllegalArgumentException( "attempt to use negative max difference with more than two species" );
154 private void addSpeciesSpecificDomainData( final StringBuffer sb,
155 final Species species,
157 final Map<String, Integer> tax_code_to_id_map ) {
159 addTaxWithLink( sb, species.getSpeciesId(), tax_code_to_id_map );
162 sb.append( species.getSpeciesId() );
164 if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
171 sb.append( getSpeciesData().get( species ).toStringBuffer( getDetaildness(), html ) );
181 private void addTaxWithLink( final StringBuffer sb,
182 final String tax_code,
183 final Map<String, Integer> tax_code_to_id_map ) {
185 if ( !ForesterUtil.isEmpty( tax_code )
186 && ( ( tax_code_to_id_map != null ) && tax_code_to_id_map.containsKey( tax_code ) ) ) {
187 sb.append( "<a href=\"" + SurfacingConstants.UNIPROT_TAXONOMY_ID_LINK + tax_code_to_id_map.get( tax_code )
188 + "\" target=\"taxonomy_window\">" + tax_code + "</a>" );
191 sb.append( tax_code );
196 private int compareByDomainId( final DomainSimilarity other ) {
197 return getDomainId().compareToIgnoreCase( other.getDomainId() );
201 public int compareTo( final DomainSimilarity domain_similarity ) {
202 if ( this == domain_similarity ) {
205 else if ( domain_similarity == null ) {
206 throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to null" );
208 else if ( domain_similarity.getClass() != this.getClass() ) {
209 throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to "
210 + domain_similarity.getClass() );
212 return compareByDomainId( domain_similarity );
216 public SortedSet<String> getCombinableDomainIds( final Species species_of_combinable_domain ) {
217 final SortedSet<String> sorted_ids = new TreeSet<String>();
218 if ( getSpeciesData().containsKey( species_of_combinable_domain ) ) {
219 for( final String id : getSpeciesData().get( species_of_combinable_domain )
220 .getCombinableDomainIdToCountsMap().keySet() ) {
221 sorted_ids.add( id );
227 private CombinableDomains getCombinableDomains() {
228 return _combinable_domains;
231 private DomainSimilarityCalculator.Detailedness getDetaildness() {
232 return _detailedness;
236 public String getDomainId() {
237 return getCombinableDomains().getKeyDomain();
241 public int getMaximalDifference() {
242 return _max_difference;
246 public int getMaximalDifferenceInCounts() {
247 return _max_difference_in_counts;
251 public double getMaximalSimilarityScore() {
256 public double getMeanSimilarityScore() {
261 public double getMinimalSimilarityScore() {
271 public SortedSet<Species> getSpecies() {
272 final SortedSet<Species> species = new TreeSet<Species>();
273 for( final Species s : getSpeciesData().keySet() ) {
279 public List<Species> getSpeciesCustomOrder() {
280 return _species_order;
284 public SortedMap<Species, SpeciesSpecificDcData> getSpeciesData() {
285 return _species_data;
288 private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html,
289 final Map<String, Integer> tax_code_to_id_map ) {
290 final StringBuffer sb = new StringBuffer();
291 for( final Species species : getSpeciesData().keySet() ) {
292 addSpeciesSpecificDomainData( sb, species, html, tax_code_to_id_map );
297 private StringBuffer getDomainDataInAlphabeticalOrder() {
298 final SortedMap<String, SortedSet<String>> m = new TreeMap<String, SortedSet<String>>();
299 final StringBuffer sb = new StringBuffer();
300 for( final Species species : getSpeciesData().keySet() ) {
301 for( final String combable_dom : getCombinableDomainIds( species ) ) {
302 if ( !m.containsKey( combable_dom ) ) {
303 m.put( combable_dom, new TreeSet<String>() );
305 m.get( combable_dom ).add( species.getSpeciesId() );
308 for( final Map.Entry<String, SortedSet<String>> e : m.entrySet() ) {
309 sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + e.getKey() + "\">" + e.getKey() + "</a>" );
311 for( final String s : e.getValue() ) {
320 private StringBuffer getSpeciesDataInCustomOrder( final boolean html, final Map<String, Integer> tax_code_to_id_map ) {
321 final StringBuffer sb = new StringBuffer();
322 for( final Species order_species : getSpeciesCustomOrder() ) {
323 if ( getSpeciesData().keySet().contains( order_species ) ) {
324 addSpeciesSpecificDomainData( sb, order_species, html, tax_code_to_id_map );
327 sb.append( PrintableDomainSimilarity.NO_SPECIES );
328 sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
335 public double getStandardDeviationOfSimilarityScore() {
339 private void init() {
340 _detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
343 private boolean isTreatAsBinaryComparison() {
344 return _treat_as_binary_comparison;
347 public void setDetailedness( final Detailedness detailedness ) {
348 _detailedness = detailedness;
351 public void setSpeciesOrder( final List<Species> species_order ) {
352 if ( !species_order.containsAll( getSpeciesData().keySet() ) ) {
353 throw new IllegalArgumentException( "list to order species must contain all species of multiple combinable domains similarity" );
355 _species_order = species_order;
359 public StringBuffer toStringBuffer( final PrintableDomainSimilarity.PRINT_OPTION print_option,
360 final Map<String, Integer> tax_code_to_id_map ) {
361 switch ( print_option ) {
362 case SIMPLE_TAB_DELIMITED:
363 return toStringBufferSimpleTabDelimited();
365 return toStringBufferDetailedHTML( tax_code_to_id_map );
367 throw new AssertionError( "Unknown print option: " + print_option );
371 private StringBuffer toStringBufferDetailedHTML( final Map<String, Integer> tax_code_to_id_map ) {
372 final StringBuffer sb = new StringBuffer();
376 sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + getDomainId() + "\" target=\"pfam_window\">"
377 + getDomainId() + "</a>" );
379 sb.append( "<a name=\"" + getDomainId() + "\">" );
380 sb.append( "</td>" );
382 sb.append( "<a href=\"" + SurfacingConstants.GOOGLE_SCHOLAR_SEARCH + getDomainId()
383 + "\" target=\"gs_window\">gs</a>" );
384 sb.append( "</td>" );
385 if ( getMaximalSimilarityScore() > 0 ) {
387 sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
388 sb.append( "</td>" );
389 if ( SurfacingConstants.PRINT_MORE_DOM_SIMILARITY_INFO ) {
390 if ( !isTreatAsBinaryComparison() ) {
393 sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
395 sb.append( "</td>" );
398 sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
400 sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
402 sb.append( "</td>" );
407 sb.append( getMaximalDifference() );
408 sb.append( "</td>" );
410 if ( isTreatAsBinaryComparison() ) {
411 sb.append( getMaximalDifferenceInCounts() );
414 sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
416 sb.append( "</td>" );
417 if ( !isTreatAsBinaryComparison() ) {
420 sb.append( getSpeciesData().size() );
422 sb.append( "</td>" );
424 if ( ( getSpeciesCustomOrder() == null ) || getSpeciesCustomOrder().isEmpty() ) {
426 sb.append( getSpeciesDataInAlphabeticalOrder( true, tax_code_to_id_map ) );
427 sb.append( getDomainDataInAlphabeticalOrder() );
428 sb.append( "</td>" );
432 sb.append( getSpeciesDataInCustomOrder( true, tax_code_to_id_map ) );
433 sb.append( getDomainDataInAlphabeticalOrder() );
434 sb.append( "</td>" );
436 sb.append( "</tr>" );
440 private StringBuffer toStringBufferSimpleTabDelimited() {
441 final StringBuffer sb = new StringBuffer();
442 sb.append( getDomainId() );
444 sb.append( getSpeciesDataInAlphabeticalOrder( false, null ) );
449 public static enum PRINT_OPTION {
450 SIMPLE_TAB_DELIMITED, HTML;