0fb075eb93ebbfacee5636726729f43d22a6e85e
[jalview.git] / 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     public int compareTo( final DomainSimilarity domain_similarity ) {
277         if ( this == domain_similarity ) {
278             return PrintableDomainSimilarity.EQUAL;
279         }
280         else if ( domain_similarity == null ) {
281             throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to null" );
282         }
283         else if ( domain_similarity.getClass() != this.getClass() ) {
284             throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to "
285                     + domain_similarity.getClass() );
286         }
287         switch ( getSortField() ) {
288             case MIN:
289                 if ( isSortBySpeciesCountFirst() ) {
290                     final int i = compareBySpeciesCount( domain_similarity );
291                     if ( i != PrintableDomainSimilarity.EQUAL ) {
292                         return i;
293                     }
294                 }
295                 if ( getMinimalSimilarityScore() < domain_similarity.getMinimalSimilarityScore() ) {
296                     return PrintableDomainSimilarity.BEFORE;
297                 }
298                 else if ( getMinimalSimilarityScore() > domain_similarity.getMinimalSimilarityScore() ) {
299                     return PrintableDomainSimilarity.AFTER;
300                 }
301                 else {
302                     return compareByDomainId( domain_similarity );
303                 }
304             case MAX:
305                 if ( isSortBySpeciesCountFirst() ) {
306                     final int i = compareBySpeciesCount( domain_similarity );
307                     if ( i != PrintableDomainSimilarity.EQUAL ) {
308                         return i;
309                     }
310                 }
311                 if ( getMaximalSimilarityScore() < domain_similarity.getMaximalSimilarityScore() ) {
312                     return PrintableDomainSimilarity.BEFORE;
313                 }
314                 else if ( getMaximalSimilarityScore() > domain_similarity.getMaximalSimilarityScore() ) {
315                     return PrintableDomainSimilarity.AFTER;
316                 }
317                 else {
318                     return compareByDomainId( domain_similarity );
319                 }
320             case MEAN:
321                 if ( isSortBySpeciesCountFirst() ) {
322                     final int i = compareBySpeciesCount( domain_similarity );
323                     if ( i != PrintableDomainSimilarity.EQUAL ) {
324                         return i;
325                     }
326                 }
327                 if ( getMeanSimilarityScore() < domain_similarity.getMeanSimilarityScore() ) {
328                     return PrintableDomainSimilarity.BEFORE;
329                 }
330                 else if ( getMeanSimilarityScore() > domain_similarity.getMeanSimilarityScore() ) {
331                     return PrintableDomainSimilarity.AFTER;
332                 }
333                 else {
334                     return compareByDomainId( domain_similarity );
335                 }
336             case SD:
337                 if ( isSortBySpeciesCountFirst() ) {
338                     final int i = compareBySpeciesCount( domain_similarity );
339                     if ( i != PrintableDomainSimilarity.EQUAL ) {
340                         return i;
341                     }
342                 }
343                 if ( getStandardDeviationOfSimilarityScore() < domain_similarity
344                         .getStandardDeviationOfSimilarityScore() ) {
345                     return PrintableDomainSimilarity.BEFORE;
346                 }
347                 else if ( getStandardDeviationOfSimilarityScore() > domain_similarity
348                         .getStandardDeviationOfSimilarityScore() ) {
349                     return PrintableDomainSimilarity.AFTER;
350                 }
351                 else {
352                     return compareByDomainId( domain_similarity );
353                 }
354             case MAX_DIFFERENCE:
355                 if ( isSortBySpeciesCountFirst() ) {
356                     final int i = compareBySpeciesCount( domain_similarity );
357                     if ( i != PrintableDomainSimilarity.EQUAL ) {
358                         return i;
359                     }
360                 }
361                 if ( getMaximalDifference() > domain_similarity.getMaximalDifference() ) {
362                     return PrintableDomainSimilarity.BEFORE;
363                 }
364                 else if ( getMaximalDifference() < domain_similarity.getMaximalDifference() ) {
365                     return PrintableDomainSimilarity.AFTER;
366                 }
367                 else {
368                     return compareByDomainId( domain_similarity );
369                 }
370             case ABS_MAX_COUNTS_DIFFERENCE:
371                 if ( isSortBySpeciesCountFirst() ) {
372                     final int i = compareBySpeciesCount( domain_similarity );
373                     if ( i != PrintableDomainSimilarity.EQUAL ) {
374                         return i;
375                     }
376                 }
377                 if ( Math.abs( getMaximalDifferenceInCounts() ) > Math.abs( domain_similarity
378                         .getMaximalDifferenceInCounts() ) ) {
379                     return PrintableDomainSimilarity.BEFORE;
380                 }
381                 else if ( Math.abs( getMaximalDifferenceInCounts() ) < Math.abs( domain_similarity
382                         .getMaximalDifferenceInCounts() ) ) {
383                     return PrintableDomainSimilarity.AFTER;
384                 }
385                 else {
386                     return compareByDomainId( domain_similarity );
387                 }
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" );
391                 }
392                 if ( isSortBySpeciesCountFirst() ) {
393                     final int i = compareBySpeciesCount( domain_similarity );
394                     if ( i != PrintableDomainSimilarity.EQUAL ) {
395                         return i;
396                     }
397                 }
398                 if ( getMaximalDifferenceInCounts() > domain_similarity.getMaximalDifferenceInCounts() ) {
399                     return PrintableDomainSimilarity.BEFORE;
400                 }
401                 else if ( getMaximalDifferenceInCounts() < domain_similarity.getMaximalDifferenceInCounts() ) {
402                     return PrintableDomainSimilarity.AFTER;
403                 }
404                 else {
405                     return compareByDomainId( domain_similarity );
406                 }
407             case SPECIES_COUNT:
408                 final int i = compareBySpeciesCount( domain_similarity );
409                 if ( i != PrintableDomainSimilarity.EQUAL ) {
410                     return i;
411                 }
412                 else {
413                     return compareByDomainId( domain_similarity );
414                 }
415             case DOMAIN_ID:
416                 return compareByDomainId( domain_similarity );
417         }
418         throw new AssertionError( "Unknown sort method: " + getSortField() );
419     }
420
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 );
427             }
428         }
429         return sorted_ids;
430     }
431
432     private CombinableDomains getCombinableDomains() {
433         return _combinable_domains;
434     }
435
436     private DomainSimilarityCalculator.Detailedness getDetaildness() {
437         return _detailedness;
438     }
439
440     public DomainId getDomainId() {
441         return getCombinableDomains().getKeyDomain();
442     }
443
444     private DomainSimilarityCalculator.GoAnnotationOutput getGoAnnotationOutput() {
445         return _go_annotation_output;
446     }
447
448     private Map<GoId, GoTerm> getGoIdToTermMap() {
449         return _go_id_to_term_map;
450     }
451
452     public GoNameSpace getGoNamespaceLimit() {
453         return _go_namespace_limit;
454     }
455
456     public int getMaximalDifference() {
457         return _max_difference;
458     }
459
460     @Override
461     public int getMaximalDifferenceInCounts() {
462         return _max_difference_in_counts;
463     }
464
465     public double getMaximalSimilarityScore() {
466         return _max;
467     }
468
469     public double getMeanSimilarityScore() {
470         return _mean;
471     }
472
473     public double getMinimalSimilarityScore() {
474         return _min;
475     }
476
477     public int getN() {
478         return _n;
479     }
480
481     private DomainSimilaritySortField getSortField() {
482         return _sort_field;
483     }
484
485     public SortedSet<Species> getSpecies() {
486         final SortedSet<Species> species = new TreeSet<Species>();
487         for( final Species s : getSpeciesData().keySet() ) {
488             species.add( s );
489         }
490         return species;
491     }
492
493     public List<Species> getSpeciesCustomOrder() {
494         return _species_order;
495     }
496
497     public SortedMap<Species, SpeciesSpecificDomainSimilariyData> getSpeciesData() {
498         return _species_data;
499     }
500
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 );
505         }
506         return sb;
507     }
508
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 );
514             }
515             else {
516                 sb.append( PrintableDomainSimilarity.NO_SPECIES );
517                 sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
518             }
519         }
520         return sb;
521     }
522
523     public double getStandardDeviationOfSimilarityScore() {
524         return _sd;
525     }
526
527     private void init() {
528         _detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
529         _go_annotation_output = null;
530         _go_id_to_term_map = null;
531     }
532
533     private boolean isSortBySpeciesCountFirst() {
534         return _sort_by_species_count_first;
535     }
536
537     private boolean isTreatAsBinaryComparison() {
538         return _treat_as_binary_comparison;
539     }
540
541     public void setDetailedness( final Detailedness detailedness ) {
542         _detailedness = detailedness;
543     }
544
545     public void setGoAnnotationOutput( final GoAnnotationOutput go_annotation_output ) {
546         _go_annotation_output = go_annotation_output;
547     }
548
549     public void setGoIdToTermMap( final Map<GoId, GoTerm> go_id_to_term_map ) {
550         _go_id_to_term_map = go_id_to_term_map;
551     }
552
553     public void setGoNamespaceLimit( final GoNameSpace go_namespace_limit ) {
554         _go_namespace_limit = go_namespace_limit;
555     }
556
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" );
560         }
561         _species_order = species_order;
562     }
563
564     @Override
565     public String toString() {
566         return toStringBuffer( null ).toString();
567     }
568
569     public StringBuffer toStringBuffer( final PrintableDomainSimilarity.PRINT_OPTION print_option ) {
570         switch ( print_option ) {
571             case SIMPLE_TAB_DELIMITED:
572                 return toStringBufferSimpleTabDelimited();
573             case HTML:
574                 return toStringBufferDetailedHTML();
575             default:
576                 throw new AssertionError( "Unknown print option: " + print_option );
577         }
578     }
579
580     private StringBuffer toStringBufferDetailedHTML() {
581         final StringBuffer sb = new StringBuffer();
582         sb.append( "<tr>" );
583         sb.append( "<td>" );
584         boldStartIfSortedBy( DomainSimilaritySortField.DOMAIN_ID, sb );
585         sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + getDomainId() + "\">" + getDomainId()
586                 + "</a>" );
587         boldEndIfSortedBy( DomainSimilaritySortField.DOMAIN_ID, sb );
588         sb.append( "</td>" );
589         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() ) {
595             sb.append( "<td>" );
596             sb.append( "(" );
597             boldStartIfSortedBy( DomainSimilaritySortField.SD, sb );
598             sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
599             boldEndIfSortedBy( DomainSimilaritySortField.SD, sb );
600             sb.append( ")" );
601             sb.append( "</td>" );
602             sb.append( "<td>" );
603             sb.append( "[" );
604             boldStartIfSortedBy( DomainSimilaritySortField.MIN, sb );
605             sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
606             boldEndIfSortedBy( DomainSimilaritySortField.MIN, sb );
607             sb.append( "," );
608             boldStartIfSortedBy( DomainSimilaritySortField.MAX, sb );
609             sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
610             boldEndIfSortedBy( DomainSimilaritySortField.MAX, sb );
611             sb.append( "]" );
612             sb.append( "</td>" );
613         }
614         sb.append( "<td>" );
615         boldStartIfSortedBy( DomainSimilaritySortField.MAX_DIFFERENCE, sb );
616         sb.append( getMaximalDifference() );
617         boldEndIfSortedBy( DomainSimilaritySortField.MAX_DIFFERENCE, sb );
618         sb.append( "</td>" );
619         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 );
626         }
627         else {
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 );
633         }
634         sb.append( "</td>" );
635         if ( !isTreatAsBinaryComparison() ) {
636             sb.append( "<td>" );
637             if ( ( getSortField() == DomainSimilaritySortField.SPECIES_COUNT ) || isSortBySpeciesCountFirst() ) {
638                 sb.append( "<b>" );
639             }
640             sb.append( getSpeciesData().size() );
641             if ( ( getSortField() == DomainSimilaritySortField.SPECIES_COUNT ) || isSortBySpeciesCountFirst() ) {
642                 sb.append( "</b>" );
643             }
644             sb.append( "</td>" );
645         }
646         if ( getGoAnnotationOutput() != DomainSimilarityCalculator.GoAnnotationOutput.NONE ) {
647             sb.append( "<td>" );
648             addGoInformation( sb, true, true );
649             sb.append( "</td>" );
650         }
651         if ( ( getSpeciesCustomOrder() == null ) || getSpeciesCustomOrder().isEmpty() ) {
652             sb.append( "<td>" );
653             sb.append( getSpeciesDataInAlphabeticalOrder( true ) );
654             sb.append( "</td>" );
655         }
656         else {
657             sb.append( "<td>" );
658             sb.append( getSpeciesDataInCustomOrder( true ) );
659             sb.append( "</td>" );
660         }
661         sb.append( "</tr>" );
662         return sb;
663     }
664
665     private StringBuffer toStringBufferSimpleTabDelimited() {
666         final StringBuffer sb = new StringBuffer();
667         sb.append( getDomainId() );
668         switch ( getSortField() ) {
669             case MIN:
670                 sb.append( TAB );
671                 sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
672                 break;
673             case MAX:
674                 sb.append( TAB );
675                 sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
676                 break;
677             case MEAN:
678                 sb.append( TAB );
679                 sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
680                 break;
681             case SD:
682                 sb.append( TAB );
683                 sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
684                 break;
685             case MAX_DIFFERENCE:
686                 sb.append( TAB );
687                 sb.append( getMaximalDifference() );
688             case ABS_MAX_COUNTS_DIFFERENCE:
689             case MAX_COUNTS_DIFFERENCE:
690                 sb.append( TAB );
691                 if ( isTreatAsBinaryComparison() ) {
692                     sb.append( getMaximalDifferenceInCounts() );
693                 }
694                 else {
695                     sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
696                 }
697                 break;
698             case SPECIES_COUNT:
699                 sb.append( TAB );
700                 sb.append( getSpeciesData().size() );
701                 break;
702             case DOMAIN_ID:
703                 break;
704             default:
705                 throw new AssertionError( "Unknown sort method: " + getSortField() );
706         }
707         if ( getGoAnnotationOutput() != DomainSimilarityCalculator.GoAnnotationOutput.NONE ) {
708             sb.append( TAB );
709             addGoInformation( sb, true, false );
710         }
711         return sb;
712     }
713
714     public static enum PRINT_OPTION {
715         SIMPLE_TAB_DELIMITED, HTML;
716     }
717 }