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.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.protein.DomainId;
40 import org.forester.species.Species;
41 import org.forester.surfacing.DomainSimilarityCalculator.Detailedness;
42 import org.forester.surfacing.DomainSimilarityCalculator.GoAnnotationOutput;
43 import org.forester.util.ForesterUtil;
45 public class PrintableDomainSimilarity implements DomainSimilarity {
47 final public static String SPECIES_SEPARATOR = " ";
48 final private static char TAB = '\t';
49 final private static int BEFORE = -1;
50 final private static int EQUAL = 0;
51 final private static int AFTER = 1;
52 final private static String NO_SPECIES = " ";
53 final private double _min;
54 final private double _max;
55 final private double _mean;
56 final private double _sd;
58 private final int _max_difference_in_counts;
59 private final int _max_difference;
60 private DomainSimilarityCalculator.GoAnnotationOutput _go_annotation_output;
61 final private CombinableDomains _combinable_domains;
62 final private SortedMap<Species, SpeciesSpecificDomainSimilariyData> _species_data;
63 final private DomainSimilaritySortField _sort_field;
64 private List<Species> _species_order;
65 private final boolean _sort_by_species_count_first;
66 private DomainSimilarityCalculator.Detailedness _detailedness;
67 private Map<GoId, GoTerm> _go_id_to_term_map;
68 private GoNameSpace _go_namespace_limit;
69 private final boolean _treat_as_binary_comparison;
72 * If go_id_to_term_map not null, detailed GO information is written,
73 * only GO ids otherwise.
77 public PrintableDomainSimilarity( final CombinableDomains combinable_domains,
84 final int max_difference_in_counts,
85 final int max_difference,
86 final SortedMap<Species, SpeciesSpecificDomainSimilariyData> species_data,
87 final DomainSimilaritySortField sort_field,
88 final boolean sort_by_species_count_first,
89 final boolean treat_as_binary_comparison ) {
90 if ( combinable_domains == null ) {
91 throw new IllegalArgumentException( "attempt to use null combinable domains" );
93 if ( sort_field == null ) {
94 throw new IllegalArgumentException( "attempt to use null sorting" );
96 if ( species_data == null ) {
97 throw new IllegalArgumentException( "attempt to use null species data" );
99 if ( species_data.size() < 1 ) {
100 throw new IllegalArgumentException( "attempt to use empty species data" );
103 throw new IllegalArgumentException( "attempt to use N less than 0" );
105 if ( ( species_data.size() > 1 ) && ( n < 1 ) ) {
106 throw new IllegalArgumentException( "attempt to use N less than 1" );
109 throw new IllegalArgumentException( "attempt to use negative SD" );
112 throw new IllegalArgumentException( "attempt to use max smaller than min" );
115 _combinable_domains = combinable_domains;
121 _max_difference_in_counts = max_difference_in_counts;
122 _max_difference = max_difference;
123 _species_data = species_data;
124 _sort_field = sort_field;
125 _sort_by_species_count_first = sort_by_species_count_first;
126 _treat_as_binary_comparison = treat_as_binary_comparison;
127 final int s = species_data.size();
128 if ( ( ( s * s ) - s ) != ( getN() * 2 ) ) {
129 throw new IllegalArgumentException( "illegal species count and n: species count:" + s + ", n:" + _n
130 + " for domain " + combinable_domains.getKeyDomain() );
133 if ( getMaximalDifferenceInCounts() < 0 ) {
134 throw new IllegalArgumentException( "attempt to use negative max difference in counts with more than two species" );
136 if ( getMaximalDifference() < 0 ) {
137 throw new IllegalArgumentException( "attempt to use negative max difference with more than two species" );
142 private void addGoInformation( final StringBuffer sb, final boolean for_table, final boolean html ) {
146 switch ( getGoAnnotationOutput() ) {
148 final int go_ids = getCombinableDomains().getKeyDomain().getNumberOfGoIds();
149 boolean first = true;
150 for( int i = 0; i < go_ids; ++i ) {
151 final GoId go_id = getCombinableDomains().getKeyDomain().getGoId( i );
152 if ( getGoIdToTermMap() != null ) {
153 if ( getGoIdToTermMap().containsKey( go_id ) ) {
154 first = appendGoTerm( sb, getGoIdToTermMap().get( go_id ), first, html );
157 sb.append( "go id \"" + go_id + "\" not found ["
158 + getCombinableDomains().getKeyDomain().getId() + "]" );
166 sb.append( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id
167 + "\" target=\"amigo_window\">" + go_id + "</a>" );
181 throw new RuntimeException( "unknown " + getGoAnnotationOutput() );
188 private void addSpeciesSpecificDomainData( final StringBuffer sb, final Species species, final boolean html ) {
189 if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
194 if ( ( SurfacingConstants.TAXONOMY_LINK != null ) && ( species.getSpeciesId().length() > 2 )
195 && ( species.getSpeciesId().length() < 6 ) ) {
196 sb.append( "<a href=\"" + SurfacingConstants.TAXONOMY_LINK + species.getSpeciesId()
197 + "\" target=\"taxonomy_window\">" + species.getSpeciesId() + "</a>" );
200 sb.append( species.getSpeciesId() );
205 sb.append( species.getSpeciesId() );
207 if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
209 sb.append( getSpeciesData().get( species ).toStringBuffer( getDetaildness(), html ) );
215 sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
218 private boolean appendGoTerm( final StringBuffer sb, final GoTerm go_term, final boolean first, final boolean html ) {
219 if ( ( getGoNamespaceLimit() == null ) || getGoNamespaceLimit().equals( go_term.getGoNameSpace() ) ) {
223 final GoId go_id = go_term.getGoId();
225 sb.append( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id + "\" target=\"amigo_window\">" + go_id
232 sb.append( go_term.getName() );
234 if ( getGoNamespaceLimit() == null ) {
236 sb.append( go_term.getGoNameSpace().toString() );
238 for( final GoXRef xref : go_term.getGoXRefs() ) {
240 sb.append( xref.toString() );
248 private void boldEndIfSortedBy( final DomainSimilaritySortField sort_field, final StringBuffer sb ) {
249 if ( getSortField() == sort_field ) {
254 private void boldStartIfSortedBy( final DomainSimilaritySortField sort_field, final StringBuffer sb ) {
255 if ( getSortField() == sort_field ) {
260 private int compareByDomainId( final DomainSimilarity other ) {
261 return getDomainId().compareTo( other.getDomainId() );
264 private int compareBySpeciesCount( final DomainSimilarity domain_similarity ) {
265 final int s_this = getSpeciesData().size();
266 final int s_other = domain_similarity.getSpeciesData().size();
267 if ( s_this < s_other ) {
268 return PrintableDomainSimilarity.BEFORE;
270 else if ( s_this > s_other ) {
271 return PrintableDomainSimilarity.AFTER;
274 return PrintableDomainSimilarity.EQUAL;
279 public int compareTo( final DomainSimilarity domain_similarity ) {
280 if ( this == domain_similarity ) {
281 return PrintableDomainSimilarity.EQUAL;
283 else if ( domain_similarity == null ) {
284 throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to null" );
286 else if ( domain_similarity.getClass() != this.getClass() ) {
287 throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to "
288 + domain_similarity.getClass() );
290 switch ( getSortField() ) {
292 if ( isSortBySpeciesCountFirst() ) {
293 final int i = compareBySpeciesCount( domain_similarity );
294 if ( i != PrintableDomainSimilarity.EQUAL ) {
298 if ( getMinimalSimilarityScore() < domain_similarity.getMinimalSimilarityScore() ) {
299 return PrintableDomainSimilarity.BEFORE;
301 else if ( getMinimalSimilarityScore() > domain_similarity.getMinimalSimilarityScore() ) {
302 return PrintableDomainSimilarity.AFTER;
305 return compareByDomainId( domain_similarity );
308 if ( isSortBySpeciesCountFirst() ) {
309 final int i = compareBySpeciesCount( domain_similarity );
310 if ( i != PrintableDomainSimilarity.EQUAL ) {
314 if ( getMaximalSimilarityScore() < domain_similarity.getMaximalSimilarityScore() ) {
315 return PrintableDomainSimilarity.BEFORE;
317 else if ( getMaximalSimilarityScore() > domain_similarity.getMaximalSimilarityScore() ) {
318 return PrintableDomainSimilarity.AFTER;
321 return compareByDomainId( domain_similarity );
324 if ( isSortBySpeciesCountFirst() ) {
325 final int i = compareBySpeciesCount( domain_similarity );
326 if ( i != PrintableDomainSimilarity.EQUAL ) {
330 if ( getMeanSimilarityScore() < domain_similarity.getMeanSimilarityScore() ) {
331 return PrintableDomainSimilarity.BEFORE;
333 else if ( getMeanSimilarityScore() > domain_similarity.getMeanSimilarityScore() ) {
334 return PrintableDomainSimilarity.AFTER;
337 return compareByDomainId( domain_similarity );
340 if ( isSortBySpeciesCountFirst() ) {
341 final int i = compareBySpeciesCount( domain_similarity );
342 if ( i != PrintableDomainSimilarity.EQUAL ) {
346 if ( getStandardDeviationOfSimilarityScore() < domain_similarity
347 .getStandardDeviationOfSimilarityScore() ) {
348 return PrintableDomainSimilarity.BEFORE;
350 else if ( getStandardDeviationOfSimilarityScore() > domain_similarity
351 .getStandardDeviationOfSimilarityScore() ) {
352 return PrintableDomainSimilarity.AFTER;
355 return compareByDomainId( domain_similarity );
358 if ( isSortBySpeciesCountFirst() ) {
359 final int i = compareBySpeciesCount( domain_similarity );
360 if ( i != PrintableDomainSimilarity.EQUAL ) {
364 if ( getMaximalDifference() > domain_similarity.getMaximalDifference() ) {
365 return PrintableDomainSimilarity.BEFORE;
367 else if ( getMaximalDifference() < domain_similarity.getMaximalDifference() ) {
368 return PrintableDomainSimilarity.AFTER;
371 return compareByDomainId( domain_similarity );
373 case ABS_MAX_COUNTS_DIFFERENCE:
374 if ( isSortBySpeciesCountFirst() ) {
375 final int i = compareBySpeciesCount( domain_similarity );
376 if ( i != PrintableDomainSimilarity.EQUAL ) {
380 if ( Math.abs( getMaximalDifferenceInCounts() ) > Math.abs( domain_similarity
381 .getMaximalDifferenceInCounts() ) ) {
382 return PrintableDomainSimilarity.BEFORE;
384 else if ( Math.abs( getMaximalDifferenceInCounts() ) < Math.abs( domain_similarity
385 .getMaximalDifferenceInCounts() ) ) {
386 return PrintableDomainSimilarity.AFTER;
389 return compareByDomainId( domain_similarity );
391 case MAX_COUNTS_DIFFERENCE:
392 if ( getSpeciesData().size() != 2 ) {
393 throw new RuntimeException( "attempt to sort by maximal difference with species not equal to two" );
395 if ( isSortBySpeciesCountFirst() ) {
396 final int i = compareBySpeciesCount( domain_similarity );
397 if ( i != PrintableDomainSimilarity.EQUAL ) {
401 if ( getMaximalDifferenceInCounts() > domain_similarity.getMaximalDifferenceInCounts() ) {
402 return PrintableDomainSimilarity.BEFORE;
404 else if ( getMaximalDifferenceInCounts() < domain_similarity.getMaximalDifferenceInCounts() ) {
405 return PrintableDomainSimilarity.AFTER;
408 return compareByDomainId( domain_similarity );
411 final int i = compareBySpeciesCount( domain_similarity );
412 if ( i != PrintableDomainSimilarity.EQUAL ) {
416 return compareByDomainId( domain_similarity );
419 return compareByDomainId( domain_similarity );
421 throw new AssertionError( "Unknown sort method: " + getSortField() );
425 public SortedSet<DomainId> getCombinableDomainIds( final Species species_of_combinable_domain ) {
426 final SortedSet<DomainId> sorted_ids = new TreeSet<DomainId>();
427 if ( getSpeciesData().containsKey( species_of_combinable_domain ) ) {
428 for( final DomainId id : getSpeciesData().get( species_of_combinable_domain )
429 .getCombinableDomainIdToCountsMap().keySet() ) {
430 sorted_ids.add( id );
436 private CombinableDomains getCombinableDomains() {
437 return _combinable_domains;
440 private DomainSimilarityCalculator.Detailedness getDetaildness() {
441 return _detailedness;
445 public DomainId getDomainId() {
446 return getCombinableDomains().getKeyDomain();
449 private DomainSimilarityCalculator.GoAnnotationOutput getGoAnnotationOutput() {
450 return _go_annotation_output;
453 private Map<GoId, GoTerm> getGoIdToTermMap() {
454 return _go_id_to_term_map;
457 public GoNameSpace getGoNamespaceLimit() {
458 return _go_namespace_limit;
462 public int getMaximalDifference() {
463 return _max_difference;
467 public int getMaximalDifferenceInCounts() {
468 return _max_difference_in_counts;
472 public double getMaximalSimilarityScore() {
477 public double getMeanSimilarityScore() {
482 public double getMinimalSimilarityScore() {
491 private DomainSimilaritySortField getSortField() {
496 public SortedSet<Species> getSpecies() {
497 final SortedSet<Species> species = new TreeSet<Species>();
498 for( final Species s : getSpeciesData().keySet() ) {
504 public List<Species> getSpeciesCustomOrder() {
505 return _species_order;
509 public SortedMap<Species, SpeciesSpecificDomainSimilariyData> getSpeciesData() {
510 return _species_data;
513 private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html ) {
514 final StringBuffer sb = new StringBuffer();
515 for( final Species species : getSpeciesData().keySet() ) {
516 addSpeciesSpecificDomainData( sb, species, html );
521 private StringBuffer getSpeciesDataInCustomOrder( final boolean html ) {
522 final StringBuffer sb = new StringBuffer();
523 for( final Species order_species : getSpeciesCustomOrder() ) {
524 if ( getSpeciesData().keySet().contains( order_species ) ) {
525 addSpeciesSpecificDomainData( sb, order_species, html );
528 sb.append( PrintableDomainSimilarity.NO_SPECIES );
529 sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
536 public double getStandardDeviationOfSimilarityScore() {
540 private void init() {
541 _detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
542 _go_annotation_output = null;
543 _go_id_to_term_map = null;
546 private boolean isSortBySpeciesCountFirst() {
547 return _sort_by_species_count_first;
550 private boolean isTreatAsBinaryComparison() {
551 return _treat_as_binary_comparison;
554 public void setDetailedness( final Detailedness detailedness ) {
555 _detailedness = detailedness;
558 public void setGoAnnotationOutput( final GoAnnotationOutput go_annotation_output ) {
559 _go_annotation_output = go_annotation_output;
562 public void setGoIdToTermMap( final Map<GoId, GoTerm> go_id_to_term_map ) {
563 _go_id_to_term_map = go_id_to_term_map;
566 public void setGoNamespaceLimit( final GoNameSpace go_namespace_limit ) {
567 _go_namespace_limit = go_namespace_limit;
570 public void setSpeciesOrder( final List<Species> species_order ) {
571 if ( !species_order.containsAll( getSpeciesData().keySet() ) ) {
572 throw new IllegalArgumentException( "list to order species must contain all species of multiple combinable domains similarity" );
574 _species_order = species_order;
578 public String toString() {
579 return toStringBuffer( null ).toString();
583 public StringBuffer toStringBuffer( final PrintableDomainSimilarity.PRINT_OPTION print_option ) {
584 switch ( print_option ) {
585 case SIMPLE_TAB_DELIMITED:
586 return toStringBufferSimpleTabDelimited();
588 return toStringBufferDetailedHTML();
590 throw new AssertionError( "Unknown print option: " + print_option );
594 private StringBuffer toStringBufferDetailedHTML() {
595 final StringBuffer sb = new StringBuffer();
598 boldStartIfSortedBy( DomainSimilaritySortField.DOMAIN_ID, sb );
599 sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + getDomainId() + "\">" + getDomainId()
601 boldEndIfSortedBy( DomainSimilaritySortField.DOMAIN_ID, sb );
602 sb.append( "</td>" );
604 boldStartIfSortedBy( DomainSimilaritySortField.MEAN, sb );
605 sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
606 boldEndIfSortedBy( DomainSimilaritySortField.MEAN, sb );
607 sb.append( "</td>" );
608 if ( !isTreatAsBinaryComparison() ) {
611 boldStartIfSortedBy( DomainSimilaritySortField.SD, sb );
612 sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
613 boldEndIfSortedBy( DomainSimilaritySortField.SD, sb );
615 sb.append( "</td>" );
618 boldStartIfSortedBy( DomainSimilaritySortField.MIN, sb );
619 sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
620 boldEndIfSortedBy( DomainSimilaritySortField.MIN, sb );
622 boldStartIfSortedBy( DomainSimilaritySortField.MAX, sb );
623 sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
624 boldEndIfSortedBy( DomainSimilaritySortField.MAX, sb );
626 sb.append( "</td>" );
629 boldStartIfSortedBy( DomainSimilaritySortField.MAX_DIFFERENCE, sb );
630 sb.append( getMaximalDifference() );
631 boldEndIfSortedBy( DomainSimilaritySortField.MAX_DIFFERENCE, sb );
632 sb.append( "</td>" );
634 if ( isTreatAsBinaryComparison() ) {
635 boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
636 boldStartIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
637 sb.append( getMaximalDifferenceInCounts() );
638 boldEndIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
639 boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
642 boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
643 boldStartIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
644 sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
645 boldEndIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
646 boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
648 sb.append( "</td>" );
649 if ( !isTreatAsBinaryComparison() ) {
651 if ( ( getSortField() == DomainSimilaritySortField.SPECIES_COUNT ) || isSortBySpeciesCountFirst() ) {
654 sb.append( getSpeciesData().size() );
655 if ( ( getSortField() == DomainSimilaritySortField.SPECIES_COUNT ) || isSortBySpeciesCountFirst() ) {
658 sb.append( "</td>" );
660 if ( getGoAnnotationOutput() != DomainSimilarityCalculator.GoAnnotationOutput.NONE ) {
662 addGoInformation( sb, true, true );
663 sb.append( "</td>" );
665 if ( ( getSpeciesCustomOrder() == null ) || getSpeciesCustomOrder().isEmpty() ) {
667 sb.append( getSpeciesDataInAlphabeticalOrder( true ) );
668 sb.append( "</td>" );
672 sb.append( getSpeciesDataInCustomOrder( true ) );
673 sb.append( "</td>" );
675 sb.append( "</tr>" );
679 private StringBuffer toStringBufferSimpleTabDelimited() {
680 final StringBuffer sb = new StringBuffer();
681 sb.append( getDomainId() );
682 switch ( getSortField() ) {
685 sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
689 sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
693 sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
697 sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
701 sb.append( getMaximalDifference() );
702 case ABS_MAX_COUNTS_DIFFERENCE:
703 case MAX_COUNTS_DIFFERENCE:
705 if ( isTreatAsBinaryComparison() ) {
706 sb.append( getMaximalDifferenceInCounts() );
709 sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
714 sb.append( getSpeciesData().size() );
719 throw new AssertionError( "Unknown sort method: " + getSortField() );
721 if ( getGoAnnotationOutput() != DomainSimilarityCalculator.GoAnnotationOutput.NONE ) {
723 addGoInformation( sb, true, false );
728 public static enum PRINT_OPTION {
729 SIMPLE_TAB_DELIMITED, HTML;