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: www.phylosoft.org/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.TreeSet;
35 import org.forester.go.GoId;
36 import org.forester.go.GoNameSpace;
37 import org.forester.go.GoTerm;
38 import org.forester.go.GoXRef;
39 import org.forester.surfacing.DomainSimilarityCalculator.Detailedness;
40 import org.forester.surfacing.DomainSimilarityCalculator.GoAnnotationOutput;
41 import org.forester.util.ForesterUtil;
43 public class PrintableDomainSimilarity implements DomainSimilarity {
45 final public static String SPECIES_SEPARATOR = " ";
46 final private static char TAB = '\t';
47 final private static int BEFORE = -1;
48 final private static int EQUAL = 0;
49 final private static int AFTER = 1;
50 final private static String NO_SPECIES = " ";
51 final private double _min;
52 final private double _max;
53 final private double _mean;
54 final private double _sd;
56 private final int _max_difference_in_counts;
57 private final int _max_difference;
58 private DomainSimilarityCalculator.GoAnnotationOutput _go_annotation_output;
59 final private CombinableDomains _combinable_domains;
60 final private SortedMap<Species, SpeciesSpecificDomainSimilariyData> _species_data;
61 final private DomainSimilaritySortField _sort_field;
62 private List<Species> _species_order;
63 private final boolean _sort_by_species_count_first;
64 private DomainSimilarityCalculator.Detailedness _detailedness;
65 private Map<GoId, GoTerm> _go_id_to_term_map;
66 private GoNameSpace _go_namespace_limit;
67 private final boolean _treat_as_binary_comparison;
70 * If go_id_to_term_map not null, detailed GO information is written,
71 * only GO ids otherwise.
75 public PrintableDomainSimilarity( final CombinableDomains combinable_domains,
82 final int max_difference_in_counts,
83 final int max_difference,
84 final SortedMap<Species, SpeciesSpecificDomainSimilariyData> species_data,
85 final DomainSimilaritySortField sort_field,
86 final boolean sort_by_species_count_first,
87 final boolean treat_as_binary_comparison ) {
88 if ( combinable_domains == null ) {
89 throw new IllegalArgumentException( "attempt to use null combinable domains" );
91 if ( sort_field == null ) {
92 throw new IllegalArgumentException( "attempt to use null sorting" );
94 if ( species_data == null ) {
95 throw new IllegalArgumentException( "attempt to use null species data" );
97 if ( species_data.size() < 1 ) {
98 throw new IllegalArgumentException( "attempt to use empty species data" );
101 throw new IllegalArgumentException( "attempt to use N less than 0" );
103 if ( ( species_data.size() > 1 ) && ( n < 1 ) ) {
104 throw new IllegalArgumentException( "attempt to use N less than 1" );
107 throw new IllegalArgumentException( "attempt to use negative SD" );
110 throw new IllegalArgumentException( "attempt to use max smaller than min" );
113 _combinable_domains = combinable_domains;
119 _max_difference_in_counts = max_difference_in_counts;
120 _max_difference = max_difference;
121 _species_data = species_data;
122 _sort_field = sort_field;
123 _sort_by_species_count_first = sort_by_species_count_first;
124 _treat_as_binary_comparison = treat_as_binary_comparison;
125 final int s = species_data.size();
126 if ( ( ( s * s ) - s ) != ( getN() * 2 ) ) {
127 throw new IllegalArgumentException( "illegal species count and n: species count:" + s + ", n:" + _n
128 + " for domain " + combinable_domains.getKeyDomain() );
131 if ( getMaximalDifferenceInCounts() < 0 ) {
132 throw new IllegalArgumentException( "attempt to use negative max difference in counts with more than two species" );
134 if ( getMaximalDifference() < 0 ) {
135 throw new IllegalArgumentException( "attempt to use negative max difference with more than two species" );
140 private void addGoInformation( final StringBuffer sb, final boolean for_table, final boolean html ) {
144 switch ( getGoAnnotationOutput() ) {
146 final int go_ids = getCombinableDomains().getKeyDomain().getNumberOfGoIds();
147 boolean first = true;
148 for( int i = 0; i < go_ids; ++i ) {
149 final GoId go_id = getCombinableDomains().getKeyDomain().getGoId( i );
150 if ( getGoIdToTermMap() != null ) {
151 if ( getGoIdToTermMap().containsKey( go_id ) ) {
152 first = appendGoTerm( sb, getGoIdToTermMap().get( go_id ), first, html );
155 sb.append( "go id \"" + go_id + "\" not found ["
156 + getCombinableDomains().getKeyDomain().getId() + "]" );
164 sb.append( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id
165 + "\" target=\"amigo_window\">" + go_id + "</a>" );
179 throw new RuntimeException( "unknown " + getGoAnnotationOutput() );
186 private void addSpeciesSpecificDomainData( final StringBuffer sb, final Species species, final boolean html ) {
187 if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
192 if ( ( SurfacingConstants.TAXONOMY_LINK != null ) && ( species.getSpeciesId().length() > 2 )
193 && ( species.getSpeciesId().length() < 6 ) ) {
194 sb.append( "<a href=\"" + SurfacingConstants.TAXONOMY_LINK + species.getSpeciesId()
195 + "\" target=\"taxonomy_window\">" + species.getSpeciesId() + "</a>" );
198 sb.append( species.getSpeciesId() );
203 sb.append( species.getSpeciesId() );
205 if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
207 sb.append( getSpeciesData().get( species ).toStringBuffer( getDetaildness(), html ) );
213 sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
216 private boolean appendGoTerm( final StringBuffer sb, final GoTerm go_term, final boolean first, final boolean html ) {
217 if ( ( getGoNamespaceLimit() == null ) || getGoNamespaceLimit().equals( go_term.getGoNameSpace() ) ) {
221 final GoId go_id = go_term.getGoId();
223 sb.append( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id + "\" target=\"amigo_window\">" + go_id
230 sb.append( go_term.getName() );
232 if ( getGoNamespaceLimit() == null ) {
234 sb.append( go_term.getGoNameSpace().toString() );
236 for( final GoXRef xref : go_term.getGoXRefs() ) {
238 sb.append( xref.toString() );
246 private void boldEndIfSortedBy( final DomainSimilaritySortField sort_field, final StringBuffer sb ) {
247 if ( getSortField() == sort_field ) {
252 private void boldStartIfSortedBy( final DomainSimilaritySortField sort_field, final StringBuffer sb ) {
253 if ( getSortField() == sort_field ) {
258 private int compareByDomainId( final DomainSimilarity other ) {
259 return getDomainId().compareTo( other.getDomainId() );
262 private int compareBySpeciesCount( final DomainSimilarity domain_similarity ) {
263 final int s_this = getSpeciesData().size();
264 final int s_other = domain_similarity.getSpeciesData().size();
265 if ( s_this < s_other ) {
266 return PrintableDomainSimilarity.BEFORE;
268 else if ( s_this > s_other ) {
269 return PrintableDomainSimilarity.AFTER;
272 return PrintableDomainSimilarity.EQUAL;
276 public int compareTo( final DomainSimilarity domain_similarity ) {
277 if ( this == domain_similarity ) {
278 return PrintableDomainSimilarity.EQUAL;
280 else if ( domain_similarity == null ) {
281 throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to null" );
283 else if ( domain_similarity.getClass() != this.getClass() ) {
284 throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to "
285 + domain_similarity.getClass() );
287 switch ( getSortField() ) {
289 if ( isSortBySpeciesCountFirst() ) {
290 final int i = compareBySpeciesCount( domain_similarity );
291 if ( i != PrintableDomainSimilarity.EQUAL ) {
295 if ( getMinimalSimilarityScore() < domain_similarity.getMinimalSimilarityScore() ) {
296 return PrintableDomainSimilarity.BEFORE;
298 else if ( getMinimalSimilarityScore() > domain_similarity.getMinimalSimilarityScore() ) {
299 return PrintableDomainSimilarity.AFTER;
302 return compareByDomainId( domain_similarity );
305 if ( isSortBySpeciesCountFirst() ) {
306 final int i = compareBySpeciesCount( domain_similarity );
307 if ( i != PrintableDomainSimilarity.EQUAL ) {
311 if ( getMaximalSimilarityScore() < domain_similarity.getMaximalSimilarityScore() ) {
312 return PrintableDomainSimilarity.BEFORE;
314 else if ( getMaximalSimilarityScore() > domain_similarity.getMaximalSimilarityScore() ) {
315 return PrintableDomainSimilarity.AFTER;
318 return compareByDomainId( domain_similarity );
321 if ( isSortBySpeciesCountFirst() ) {
322 final int i = compareBySpeciesCount( domain_similarity );
323 if ( i != PrintableDomainSimilarity.EQUAL ) {
327 if ( getMeanSimilarityScore() < domain_similarity.getMeanSimilarityScore() ) {
328 return PrintableDomainSimilarity.BEFORE;
330 else if ( getMeanSimilarityScore() > domain_similarity.getMeanSimilarityScore() ) {
331 return PrintableDomainSimilarity.AFTER;
334 return compareByDomainId( domain_similarity );
337 if ( isSortBySpeciesCountFirst() ) {
338 final int i = compareBySpeciesCount( domain_similarity );
339 if ( i != PrintableDomainSimilarity.EQUAL ) {
343 if ( getStandardDeviationOfSimilarityScore() < domain_similarity
344 .getStandardDeviationOfSimilarityScore() ) {
345 return PrintableDomainSimilarity.BEFORE;
347 else if ( getStandardDeviationOfSimilarityScore() > domain_similarity
348 .getStandardDeviationOfSimilarityScore() ) {
349 return PrintableDomainSimilarity.AFTER;
352 return compareByDomainId( domain_similarity );
355 if ( isSortBySpeciesCountFirst() ) {
356 final int i = compareBySpeciesCount( domain_similarity );
357 if ( i != PrintableDomainSimilarity.EQUAL ) {
361 if ( getMaximalDifference() > domain_similarity.getMaximalDifference() ) {
362 return PrintableDomainSimilarity.BEFORE;
364 else if ( getMaximalDifference() < domain_similarity.getMaximalDifference() ) {
365 return PrintableDomainSimilarity.AFTER;
368 return compareByDomainId( domain_similarity );
370 case ABS_MAX_COUNTS_DIFFERENCE:
371 if ( isSortBySpeciesCountFirst() ) {
372 final int i = compareBySpeciesCount( domain_similarity );
373 if ( i != PrintableDomainSimilarity.EQUAL ) {
377 if ( Math.abs( getMaximalDifferenceInCounts() ) > Math.abs( domain_similarity
378 .getMaximalDifferenceInCounts() ) ) {
379 return PrintableDomainSimilarity.BEFORE;
381 else if ( Math.abs( getMaximalDifferenceInCounts() ) < Math.abs( domain_similarity
382 .getMaximalDifferenceInCounts() ) ) {
383 return PrintableDomainSimilarity.AFTER;
386 return compareByDomainId( domain_similarity );
388 case MAX_COUNTS_DIFFERENCE:
389 if ( getSpeciesData().size() != 2 ) {
390 throw new RuntimeException( "attempt to sort by maximal difference with species not equal to two" );
392 if ( isSortBySpeciesCountFirst() ) {
393 final int i = compareBySpeciesCount( domain_similarity );
394 if ( i != PrintableDomainSimilarity.EQUAL ) {
398 if ( getMaximalDifferenceInCounts() > domain_similarity.getMaximalDifferenceInCounts() ) {
399 return PrintableDomainSimilarity.BEFORE;
401 else if ( getMaximalDifferenceInCounts() < domain_similarity.getMaximalDifferenceInCounts() ) {
402 return PrintableDomainSimilarity.AFTER;
405 return compareByDomainId( domain_similarity );
408 final int i = compareBySpeciesCount( domain_similarity );
409 if ( i != PrintableDomainSimilarity.EQUAL ) {
413 return compareByDomainId( domain_similarity );
416 return compareByDomainId( domain_similarity );
418 throw new AssertionError( "Unknown sort method: " + getSortField() );
421 public SortedSet<DomainId> getCombinableDomainIds( final Species species_of_combinable_domain ) {
422 final SortedSet<DomainId> sorted_ids = new TreeSet<DomainId>();
423 if ( getSpeciesData().containsKey( species_of_combinable_domain ) ) {
424 for( final DomainId id : getSpeciesData().get( species_of_combinable_domain )
425 .getCombinableDomainIdToCountsMap().keySet() ) {
426 sorted_ids.add( id );
432 private CombinableDomains getCombinableDomains() {
433 return _combinable_domains;
436 private DomainSimilarityCalculator.Detailedness getDetaildness() {
437 return _detailedness;
440 public DomainId getDomainId() {
441 return getCombinableDomains().getKeyDomain();
444 private DomainSimilarityCalculator.GoAnnotationOutput getGoAnnotationOutput() {
445 return _go_annotation_output;
448 private Map<GoId, GoTerm> getGoIdToTermMap() {
449 return _go_id_to_term_map;
452 public GoNameSpace getGoNamespaceLimit() {
453 return _go_namespace_limit;
456 public int getMaximalDifference() {
457 return _max_difference;
461 public int getMaximalDifferenceInCounts() {
462 return _max_difference_in_counts;
465 public double getMaximalSimilarityScore() {
469 public double getMeanSimilarityScore() {
473 public double getMinimalSimilarityScore() {
481 private DomainSimilaritySortField getSortField() {
485 public SortedSet<Species> getSpecies() {
486 final SortedSet<Species> species = new TreeSet<Species>();
487 for( final Species s : getSpeciesData().keySet() ) {
493 public List<Species> getSpeciesCustomOrder() {
494 return _species_order;
497 public SortedMap<Species, SpeciesSpecificDomainSimilariyData> getSpeciesData() {
498 return _species_data;
501 private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html ) {
502 final StringBuffer sb = new StringBuffer();
503 for( final Species species : getSpeciesData().keySet() ) {
504 addSpeciesSpecificDomainData( sb, species, html );
509 private StringBuffer getSpeciesDataInCustomOrder( final boolean html ) {
510 final StringBuffer sb = new StringBuffer();
511 for( final Species order_species : getSpeciesCustomOrder() ) {
512 if ( getSpeciesData().keySet().contains( order_species ) ) {
513 addSpeciesSpecificDomainData( sb, order_species, html );
516 sb.append( PrintableDomainSimilarity.NO_SPECIES );
517 sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
523 public double getStandardDeviationOfSimilarityScore() {
527 private void init() {
528 _detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
529 _go_annotation_output = null;
530 _go_id_to_term_map = null;
533 private boolean isSortBySpeciesCountFirst() {
534 return _sort_by_species_count_first;
537 private boolean isTreatAsBinaryComparison() {
538 return _treat_as_binary_comparison;
541 public void setDetailedness( final Detailedness detailedness ) {
542 _detailedness = detailedness;
545 public void setGoAnnotationOutput( final GoAnnotationOutput go_annotation_output ) {
546 _go_annotation_output = go_annotation_output;
549 public void setGoIdToTermMap( final Map<GoId, GoTerm> go_id_to_term_map ) {
550 _go_id_to_term_map = go_id_to_term_map;
553 public void setGoNamespaceLimit( final GoNameSpace go_namespace_limit ) {
554 _go_namespace_limit = go_namespace_limit;
557 public void setSpeciesOrder( final List<Species> species_order ) {
558 if ( !species_order.containsAll( getSpeciesData().keySet() ) ) {
559 throw new IllegalArgumentException( "list to order species must contain all species of multiple combinable domains similarity" );
561 _species_order = species_order;
565 public String toString() {
566 return toStringBuffer( null ).toString();
569 public StringBuffer toStringBuffer( final PrintableDomainSimilarity.PRINT_OPTION print_option ) {
570 switch ( print_option ) {
571 case SIMPLE_TAB_DELIMITED:
572 return toStringBufferSimpleTabDelimited();
574 return toStringBufferDetailedHTML();
576 throw new AssertionError( "Unknown print option: " + print_option );
580 private StringBuffer toStringBufferDetailedHTML() {
581 final StringBuffer sb = new StringBuffer();
584 boldStartIfSortedBy( DomainSimilaritySortField.DOMAIN_ID, sb );
585 sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + getDomainId() + "\">" + getDomainId()
587 boldEndIfSortedBy( DomainSimilaritySortField.DOMAIN_ID, sb );
588 sb.append( "</td>" );
590 boldStartIfSortedBy( DomainSimilaritySortField.MEAN, sb );
591 sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
592 boldEndIfSortedBy( DomainSimilaritySortField.MEAN, sb );
593 sb.append( "</td>" );
594 if ( !isTreatAsBinaryComparison() ) {
597 boldStartIfSortedBy( DomainSimilaritySortField.SD, sb );
598 sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
599 boldEndIfSortedBy( DomainSimilaritySortField.SD, sb );
601 sb.append( "</td>" );
604 boldStartIfSortedBy( DomainSimilaritySortField.MIN, sb );
605 sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
606 boldEndIfSortedBy( DomainSimilaritySortField.MIN, sb );
608 boldStartIfSortedBy( DomainSimilaritySortField.MAX, sb );
609 sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
610 boldEndIfSortedBy( DomainSimilaritySortField.MAX, sb );
612 sb.append( "</td>" );
615 boldStartIfSortedBy( DomainSimilaritySortField.MAX_DIFFERENCE, sb );
616 sb.append( getMaximalDifference() );
617 boldEndIfSortedBy( DomainSimilaritySortField.MAX_DIFFERENCE, sb );
618 sb.append( "</td>" );
620 if ( isTreatAsBinaryComparison() ) {
621 boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
622 boldStartIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
623 sb.append( getMaximalDifferenceInCounts() );
624 boldEndIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
625 boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
628 boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
629 boldStartIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
630 sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
631 boldEndIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
632 boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
634 sb.append( "</td>" );
635 if ( !isTreatAsBinaryComparison() ) {
637 if ( ( getSortField() == DomainSimilaritySortField.SPECIES_COUNT ) || isSortBySpeciesCountFirst() ) {
640 sb.append( getSpeciesData().size() );
641 if ( ( getSortField() == DomainSimilaritySortField.SPECIES_COUNT ) || isSortBySpeciesCountFirst() ) {
644 sb.append( "</td>" );
646 if ( getGoAnnotationOutput() != DomainSimilarityCalculator.GoAnnotationOutput.NONE ) {
648 addGoInformation( sb, true, true );
649 sb.append( "</td>" );
651 if ( ( getSpeciesCustomOrder() == null ) || getSpeciesCustomOrder().isEmpty() ) {
653 sb.append( getSpeciesDataInAlphabeticalOrder( true ) );
654 sb.append( "</td>" );
658 sb.append( getSpeciesDataInCustomOrder( true ) );
659 sb.append( "</td>" );
661 sb.append( "</tr>" );
665 private StringBuffer toStringBufferSimpleTabDelimited() {
666 final StringBuffer sb = new StringBuffer();
667 sb.append( getDomainId() );
668 switch ( getSortField() ) {
671 sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
675 sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
679 sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
683 sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
687 sb.append( getMaximalDifference() );
688 case ABS_MAX_COUNTS_DIFFERENCE:
689 case MAX_COUNTS_DIFFERENCE:
691 if ( isTreatAsBinaryComparison() ) {
692 sb.append( getMaximalDifferenceInCounts() );
695 sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
700 sb.append( getSpeciesData().size() );
705 throw new AssertionError( "Unknown sort method: " + getSortField() );
707 if ( getGoAnnotationOutput() != DomainSimilarityCalculator.GoAnnotationOutput.NONE ) {
709 addGoInformation( sb, true, false );
714 public static enum PRINT_OPTION {
715 SIMPLE_TAB_DELIMITED, HTML;