in progress
[jalview.git] / forester / java / src / org / forester / surfacing / PrintableDomainSimilarity.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
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.
14 //
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.
19 //
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
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.surfacing;
28
29 import java.util.List;
30 import java.util.Map;
31 import java.util.SortedMap;
32 import java.util.SortedSet;
33 import java.util.TreeSet;
34
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;
42
43 public class PrintableDomainSimilarity implements DomainSimilarity {
44
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;
55     final private int                                                    _n;
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;
68
69     /**
70      * If go_id_to_term_map not null, detailed GO information is written,
71      * only GO ids otherwise.
72      * 
73      * 
74      */
75     public PrintableDomainSimilarity( final CombinableDomains combinable_domains,
76                                       final double min,
77                                       final double max,
78                                       final double mean,
79                                       final double median,
80                                       final double sd,
81                                       final int n,
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" );
90         }
91         if ( sort_field == null ) {
92             throw new IllegalArgumentException( "attempt to use null sorting" );
93         }
94         if ( species_data == null ) {
95             throw new IllegalArgumentException( "attempt to use null species data" );
96         }
97         if ( species_data.size() < 1 ) {
98             throw new IllegalArgumentException( "attempt to use empty species data" );
99         }
100         if ( n < 0 ) {
101             throw new IllegalArgumentException( "attempt to use N less than 0" );
102         }
103         if ( ( species_data.size() > 1 ) && ( n < 1 ) ) {
104             throw new IllegalArgumentException( "attempt to use N less than 1" );
105         }
106         if ( sd < 0.0 ) {
107             throw new IllegalArgumentException( "attempt to use negative SD" );
108         }
109         if ( max < min ) {
110             throw new IllegalArgumentException( "attempt to use max smaller than min" );
111         }
112         init();
113         _combinable_domains = combinable_domains;
114         _min = min;
115         _max = max;
116         _mean = mean;
117         _sd = sd;
118         _n = n;
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() );
129         }
130         if ( s > 2 ) {
131             if ( getMaximalDifferenceInCounts() < 0 ) {
132                 throw new IllegalArgumentException( "attempt to use negative max difference in counts with more than two species" );
133             }
134             if ( getMaximalDifference() < 0 ) {
135                 throw new IllegalArgumentException( "attempt to use negative max difference with more than two species" );
136             }
137         }
138     }
139
140     private void addGoInformation( final StringBuffer sb, final boolean for_table, final boolean html ) {
141         if ( !for_table ) {
142             sb.append( "<" );
143         }
144         switch ( getGoAnnotationOutput() ) {
145             case ALL: {
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 );
153                         }
154                         else {
155                             sb.append( "go id \"" + go_id + "\" not found ["
156                                     + getCombinableDomains().getKeyDomain().getId() + "]" );
157                         }
158                     }
159                     else {
160                         if ( !first ) {
161                             sb.append( ", " );
162                         }
163                         if ( html ) {
164                             sb.append( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id
165                                     + "\" target=\"amigo_window\">" + go_id + "</a>" );
166                         }
167                         else {
168                             sb.append( go_id );
169                         }
170                         first = false;
171                     }
172                 }
173                 break;
174             }
175             case NONE: {
176                 break;
177             }
178             default:
179                 throw new RuntimeException( "unknown " + getGoAnnotationOutput() );
180         }
181         if ( !for_table ) {
182             sb.append( ">: " );
183         }
184     }
185
186     private void addSpeciesSpecificDomainData( final StringBuffer sb, final Species species, final boolean html ) {
187         if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
188             sb.append( "[" );
189         }
190         if ( html ) {
191             sb.append( "<b>" );
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>" );
196             }
197             else {
198                 sb.append( species.getSpeciesId() );
199             }
200             sb.append( "</b>" );
201         }
202         else {
203             sb.append( species.getSpeciesId() );
204         }
205         if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
206             sb.append( ":" );
207             sb.append( getSpeciesData().get( species ).toStringBuffer( getDetaildness(), html ) );
208             sb.append( "]" );
209         }
210         if ( html ) {
211             sb.append( "<br>" );
212         }
213         sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
214     }
215
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() ) ) {
218             if ( !first ) {
219                 sb.append( ", " );
220             }
221             final GoId go_id = go_term.getGoId();
222             if ( html ) {
223                 sb.append( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id + "\" target=\"amigo_window\">" + go_id
224                         + "</a>" );
225             }
226             else {
227                 sb.append( go_id );
228             }
229             sb.append( ":" );
230             sb.append( go_term.getName() );
231             if ( !html ) {
232                 if ( getGoNamespaceLimit() == null ) {
233                     sb.append( ":" );
234                     sb.append( go_term.getGoNameSpace().toString() );
235                 }
236                 for( final GoXRef xref : go_term.getGoXRefs() ) {
237                     sb.append( ":" );
238                     sb.append( xref.toString() );
239                 }
240             }
241             return false;
242         }
243         return true;
244     }
245
246     private void boldEndIfSortedBy( final DomainSimilaritySortField sort_field, final StringBuffer sb ) {
247         if ( getSortField() == sort_field ) {
248             sb.append( "</b>" );
249         }
250     }
251
252     private void boldStartIfSortedBy( final DomainSimilaritySortField sort_field, final StringBuffer sb ) {
253         if ( getSortField() == sort_field ) {
254             sb.append( "<b>" );
255         }
256     }
257
258     private int compareByDomainId( final DomainSimilarity other ) {
259         return getDomainId().compareTo( other.getDomainId() );
260     }
261
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;
267         }
268         else if ( s_this > s_other ) {
269             return PrintableDomainSimilarity.AFTER;
270         }
271         else {
272             return PrintableDomainSimilarity.EQUAL;
273         }
274     }
275
276     @Override
277     public int compareTo( final DomainSimilarity domain_similarity ) {
278         if ( this == domain_similarity ) {
279             return PrintableDomainSimilarity.EQUAL;
280         }
281         else if ( domain_similarity == null ) {
282             throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to null" );
283         }
284         else if ( domain_similarity.getClass() != this.getClass() ) {
285             throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to "
286                     + domain_similarity.getClass() );
287         }
288         switch ( getSortField() ) {
289             case MIN:
290                 if ( isSortBySpeciesCountFirst() ) {
291                     final int i = compareBySpeciesCount( domain_similarity );
292                     if ( i != PrintableDomainSimilarity.EQUAL ) {
293                         return i;
294                     }
295                 }
296                 if ( getMinimalSimilarityScore() < domain_similarity.getMinimalSimilarityScore() ) {
297                     return PrintableDomainSimilarity.BEFORE;
298                 }
299                 else if ( getMinimalSimilarityScore() > domain_similarity.getMinimalSimilarityScore() ) {
300                     return PrintableDomainSimilarity.AFTER;
301                 }
302                 else {
303                     return compareByDomainId( domain_similarity );
304                 }
305             case MAX:
306                 if ( isSortBySpeciesCountFirst() ) {
307                     final int i = compareBySpeciesCount( domain_similarity );
308                     if ( i != PrintableDomainSimilarity.EQUAL ) {
309                         return i;
310                     }
311                 }
312                 if ( getMaximalSimilarityScore() < domain_similarity.getMaximalSimilarityScore() ) {
313                     return PrintableDomainSimilarity.BEFORE;
314                 }
315                 else if ( getMaximalSimilarityScore() > domain_similarity.getMaximalSimilarityScore() ) {
316                     return PrintableDomainSimilarity.AFTER;
317                 }
318                 else {
319                     return compareByDomainId( domain_similarity );
320                 }
321             case MEAN:
322                 if ( isSortBySpeciesCountFirst() ) {
323                     final int i = compareBySpeciesCount( domain_similarity );
324                     if ( i != PrintableDomainSimilarity.EQUAL ) {
325                         return i;
326                     }
327                 }
328                 if ( getMeanSimilarityScore() < domain_similarity.getMeanSimilarityScore() ) {
329                     return PrintableDomainSimilarity.BEFORE;
330                 }
331                 else if ( getMeanSimilarityScore() > domain_similarity.getMeanSimilarityScore() ) {
332                     return PrintableDomainSimilarity.AFTER;
333                 }
334                 else {
335                     return compareByDomainId( domain_similarity );
336                 }
337             case SD:
338                 if ( isSortBySpeciesCountFirst() ) {
339                     final int i = compareBySpeciesCount( domain_similarity );
340                     if ( i != PrintableDomainSimilarity.EQUAL ) {
341                         return i;
342                     }
343                 }
344                 if ( getStandardDeviationOfSimilarityScore() < domain_similarity
345                         .getStandardDeviationOfSimilarityScore() ) {
346                     return PrintableDomainSimilarity.BEFORE;
347                 }
348                 else if ( getStandardDeviationOfSimilarityScore() > domain_similarity
349                         .getStandardDeviationOfSimilarityScore() ) {
350                     return PrintableDomainSimilarity.AFTER;
351                 }
352                 else {
353                     return compareByDomainId( domain_similarity );
354                 }
355             case MAX_DIFFERENCE:
356                 if ( isSortBySpeciesCountFirst() ) {
357                     final int i = compareBySpeciesCount( domain_similarity );
358                     if ( i != PrintableDomainSimilarity.EQUAL ) {
359                         return i;
360                     }
361                 }
362                 if ( getMaximalDifference() > domain_similarity.getMaximalDifference() ) {
363                     return PrintableDomainSimilarity.BEFORE;
364                 }
365                 else if ( getMaximalDifference() < domain_similarity.getMaximalDifference() ) {
366                     return PrintableDomainSimilarity.AFTER;
367                 }
368                 else {
369                     return compareByDomainId( domain_similarity );
370                 }
371             case ABS_MAX_COUNTS_DIFFERENCE:
372                 if ( isSortBySpeciesCountFirst() ) {
373                     final int i = compareBySpeciesCount( domain_similarity );
374                     if ( i != PrintableDomainSimilarity.EQUAL ) {
375                         return i;
376                     }
377                 }
378                 if ( Math.abs( getMaximalDifferenceInCounts() ) > Math.abs( domain_similarity
379                         .getMaximalDifferenceInCounts() ) ) {
380                     return PrintableDomainSimilarity.BEFORE;
381                 }
382                 else if ( Math.abs( getMaximalDifferenceInCounts() ) < Math.abs( domain_similarity
383                         .getMaximalDifferenceInCounts() ) ) {
384                     return PrintableDomainSimilarity.AFTER;
385                 }
386                 else {
387                     return compareByDomainId( domain_similarity );
388                 }
389             case MAX_COUNTS_DIFFERENCE:
390                 if ( getSpeciesData().size() != 2 ) {
391                     throw new RuntimeException( "attempt to sort by maximal difference with species not equal to two" );
392                 }
393                 if ( isSortBySpeciesCountFirst() ) {
394                     final int i = compareBySpeciesCount( domain_similarity );
395                     if ( i != PrintableDomainSimilarity.EQUAL ) {
396                         return i;
397                     }
398                 }
399                 if ( getMaximalDifferenceInCounts() > domain_similarity.getMaximalDifferenceInCounts() ) {
400                     return PrintableDomainSimilarity.BEFORE;
401                 }
402                 else if ( getMaximalDifferenceInCounts() < domain_similarity.getMaximalDifferenceInCounts() ) {
403                     return PrintableDomainSimilarity.AFTER;
404                 }
405                 else {
406                     return compareByDomainId( domain_similarity );
407                 }
408             case SPECIES_COUNT:
409                 final int i = compareBySpeciesCount( domain_similarity );
410                 if ( i != PrintableDomainSimilarity.EQUAL ) {
411                     return i;
412                 }
413                 else {
414                     return compareByDomainId( domain_similarity );
415                 }
416             case DOMAIN_ID:
417                 return compareByDomainId( domain_similarity );
418         }
419         throw new AssertionError( "Unknown sort method: " + getSortField() );
420     }
421
422     @Override
423     public SortedSet<DomainId> getCombinableDomainIds( final Species species_of_combinable_domain ) {
424         final SortedSet<DomainId> sorted_ids = new TreeSet<DomainId>();
425         if ( getSpeciesData().containsKey( species_of_combinable_domain ) ) {
426             for( final DomainId id : getSpeciesData().get( species_of_combinable_domain )
427                     .getCombinableDomainIdToCountsMap().keySet() ) {
428                 sorted_ids.add( id );
429             }
430         }
431         return sorted_ids;
432     }
433
434     private CombinableDomains getCombinableDomains() {
435         return _combinable_domains;
436     }
437
438     private DomainSimilarityCalculator.Detailedness getDetaildness() {
439         return _detailedness;
440     }
441
442     @Override
443     public DomainId getDomainId() {
444         return getCombinableDomains().getKeyDomain();
445     }
446
447     private DomainSimilarityCalculator.GoAnnotationOutput getGoAnnotationOutput() {
448         return _go_annotation_output;
449     }
450
451     private Map<GoId, GoTerm> getGoIdToTermMap() {
452         return _go_id_to_term_map;
453     }
454
455     public GoNameSpace getGoNamespaceLimit() {
456         return _go_namespace_limit;
457     }
458
459     @Override
460     public int getMaximalDifference() {
461         return _max_difference;
462     }
463
464     @Override
465     public int getMaximalDifferenceInCounts() {
466         return _max_difference_in_counts;
467     }
468
469     @Override
470     public double getMaximalSimilarityScore() {
471         return _max;
472     }
473
474     @Override
475     public double getMeanSimilarityScore() {
476         return _mean;
477     }
478
479     @Override
480     public double getMinimalSimilarityScore() {
481         return _min;
482     }
483
484     @Override
485     public int getN() {
486         return _n;
487     }
488
489     private DomainSimilaritySortField getSortField() {
490         return _sort_field;
491     }
492
493     @Override
494     public SortedSet<Species> getSpecies() {
495         final SortedSet<Species> species = new TreeSet<Species>();
496         for( final Species s : getSpeciesData().keySet() ) {
497             species.add( s );
498         }
499         return species;
500     }
501
502     public List<Species> getSpeciesCustomOrder() {
503         return _species_order;
504     }
505
506     @Override
507     public SortedMap<Species, SpeciesSpecificDomainSimilariyData> getSpeciesData() {
508         return _species_data;
509     }
510
511     private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html ) {
512         final StringBuffer sb = new StringBuffer();
513         for( final Species species : getSpeciesData().keySet() ) {
514             addSpeciesSpecificDomainData( sb, species, html );
515         }
516         return sb;
517     }
518
519     private StringBuffer getSpeciesDataInCustomOrder( final boolean html ) {
520         final StringBuffer sb = new StringBuffer();
521         for( final Species order_species : getSpeciesCustomOrder() ) {
522             if ( getSpeciesData().keySet().contains( order_species ) ) {
523                 addSpeciesSpecificDomainData( sb, order_species, html );
524             }
525             else {
526                 sb.append( PrintableDomainSimilarity.NO_SPECIES );
527                 sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
528             }
529         }
530         return sb;
531     }
532
533     @Override
534     public double getStandardDeviationOfSimilarityScore() {
535         return _sd;
536     }
537
538     private void init() {
539         _detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
540         _go_annotation_output = null;
541         _go_id_to_term_map = null;
542     }
543
544     private boolean isSortBySpeciesCountFirst() {
545         return _sort_by_species_count_first;
546     }
547
548     private boolean isTreatAsBinaryComparison() {
549         return _treat_as_binary_comparison;
550     }
551
552     public void setDetailedness( final Detailedness detailedness ) {
553         _detailedness = detailedness;
554     }
555
556     public void setGoAnnotationOutput( final GoAnnotationOutput go_annotation_output ) {
557         _go_annotation_output = go_annotation_output;
558     }
559
560     public void setGoIdToTermMap( final Map<GoId, GoTerm> go_id_to_term_map ) {
561         _go_id_to_term_map = go_id_to_term_map;
562     }
563
564     public void setGoNamespaceLimit( final GoNameSpace go_namespace_limit ) {
565         _go_namespace_limit = go_namespace_limit;
566     }
567
568     public void setSpeciesOrder( final List<Species> species_order ) {
569         if ( !species_order.containsAll( getSpeciesData().keySet() ) ) {
570             throw new IllegalArgumentException( "list to order species must contain all species of multiple combinable domains similarity" );
571         }
572         _species_order = species_order;
573     }
574
575     @Override
576     public String toString() {
577         return toStringBuffer( null ).toString();
578     }
579
580     @Override
581     public StringBuffer toStringBuffer( final PrintableDomainSimilarity.PRINT_OPTION print_option ) {
582         switch ( print_option ) {
583             case SIMPLE_TAB_DELIMITED:
584                 return toStringBufferSimpleTabDelimited();
585             case HTML:
586                 return toStringBufferDetailedHTML();
587             default:
588                 throw new AssertionError( "Unknown print option: " + print_option );
589         }
590     }
591
592     private StringBuffer toStringBufferDetailedHTML() {
593         final StringBuffer sb = new StringBuffer();
594         sb.append( "<tr>" );
595         sb.append( "<td>" );
596         boldStartIfSortedBy( DomainSimilaritySortField.DOMAIN_ID, sb );
597         sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + getDomainId() + "\">" + getDomainId()
598                 + "</a>" );
599         boldEndIfSortedBy( DomainSimilaritySortField.DOMAIN_ID, sb );
600         sb.append( "</td>" );
601         sb.append( "<td>" );
602         boldStartIfSortedBy( DomainSimilaritySortField.MEAN, sb );
603         sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
604         boldEndIfSortedBy( DomainSimilaritySortField.MEAN, sb );
605         sb.append( "</td>" );
606         if ( !isTreatAsBinaryComparison() ) {
607             sb.append( "<td>" );
608             sb.append( "(" );
609             boldStartIfSortedBy( DomainSimilaritySortField.SD, sb );
610             sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
611             boldEndIfSortedBy( DomainSimilaritySortField.SD, sb );
612             sb.append( ")" );
613             sb.append( "</td>" );
614             sb.append( "<td>" );
615             sb.append( "[" );
616             boldStartIfSortedBy( DomainSimilaritySortField.MIN, sb );
617             sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
618             boldEndIfSortedBy( DomainSimilaritySortField.MIN, sb );
619             sb.append( "," );
620             boldStartIfSortedBy( DomainSimilaritySortField.MAX, sb );
621             sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
622             boldEndIfSortedBy( DomainSimilaritySortField.MAX, sb );
623             sb.append( "]" );
624             sb.append( "</td>" );
625         }
626         sb.append( "<td>" );
627         boldStartIfSortedBy( DomainSimilaritySortField.MAX_DIFFERENCE, sb );
628         sb.append( getMaximalDifference() );
629         boldEndIfSortedBy( DomainSimilaritySortField.MAX_DIFFERENCE, sb );
630         sb.append( "</td>" );
631         sb.append( "<td>" );
632         if ( isTreatAsBinaryComparison() ) {
633             boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
634             boldStartIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
635             sb.append( getMaximalDifferenceInCounts() );
636             boldEndIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
637             boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
638         }
639         else {
640             boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
641             boldStartIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
642             sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
643             boldEndIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
644             boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
645         }
646         sb.append( "</td>" );
647         if ( !isTreatAsBinaryComparison() ) {
648             sb.append( "<td>" );
649             if ( ( getSortField() == DomainSimilaritySortField.SPECIES_COUNT ) || isSortBySpeciesCountFirst() ) {
650                 sb.append( "<b>" );
651             }
652             sb.append( getSpeciesData().size() );
653             if ( ( getSortField() == DomainSimilaritySortField.SPECIES_COUNT ) || isSortBySpeciesCountFirst() ) {
654                 sb.append( "</b>" );
655             }
656             sb.append( "</td>" );
657         }
658         if ( getGoAnnotationOutput() != DomainSimilarityCalculator.GoAnnotationOutput.NONE ) {
659             sb.append( "<td>" );
660             addGoInformation( sb, true, true );
661             sb.append( "</td>" );
662         }
663         if ( ( getSpeciesCustomOrder() == null ) || getSpeciesCustomOrder().isEmpty() ) {
664             sb.append( "<td>" );
665             sb.append( getSpeciesDataInAlphabeticalOrder( true ) );
666             sb.append( "</td>" );
667         }
668         else {
669             sb.append( "<td>" );
670             sb.append( getSpeciesDataInCustomOrder( true ) );
671             sb.append( "</td>" );
672         }
673         sb.append( "</tr>" );
674         return sb;
675     }
676
677     private StringBuffer toStringBufferSimpleTabDelimited() {
678         final StringBuffer sb = new StringBuffer();
679         sb.append( getDomainId() );
680         switch ( getSortField() ) {
681             case MIN:
682                 sb.append( TAB );
683                 sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
684                 break;
685             case MAX:
686                 sb.append( TAB );
687                 sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
688                 break;
689             case MEAN:
690                 sb.append( TAB );
691                 sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
692                 break;
693             case SD:
694                 sb.append( TAB );
695                 sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
696                 break;
697             case MAX_DIFFERENCE:
698                 sb.append( TAB );
699                 sb.append( getMaximalDifference() );
700             case ABS_MAX_COUNTS_DIFFERENCE:
701             case MAX_COUNTS_DIFFERENCE:
702                 sb.append( TAB );
703                 if ( isTreatAsBinaryComparison() ) {
704                     sb.append( getMaximalDifferenceInCounts() );
705                 }
706                 else {
707                     sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
708                 }
709                 break;
710             case SPECIES_COUNT:
711                 sb.append( TAB );
712                 sb.append( getSpeciesData().size() );
713                 break;
714             case DOMAIN_ID:
715                 break;
716             default:
717                 throw new AssertionError( "Unknown sort method: " + getSortField() );
718         }
719         if ( getGoAnnotationOutput() != DomainSimilarityCalculator.GoAnnotationOutput.NONE ) {
720             sb.append( TAB );
721             addGoInformation( sb, true, false );
722         }
723         return sb;
724     }
725
726     public static enum PRINT_OPTION {
727         SIMPLE_TAB_DELIMITED, HTML;
728     }
729 }