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