inprogress
[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: https://sites.google.com/site/cmzmasek/home/software/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,
189                                                final Species species,
190                                                final boolean html,
191                                                final Map<String, Integer> tax_code_to_id_map ) {
192         if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
193             sb.append( "[" );
194         }
195         if ( html ) {
196             sb.append( "<b>" );
197             final String tax_code = species.getSpeciesId();
198             if ( !ForesterUtil.isEmpty( tax_code )
199                     && ( ( tax_code_to_id_map != null ) && tax_code_to_id_map.containsKey( tax_code ) ) ) {
200                 sb.append( "<a href=\"" + SurfacingConstants.UNIPROT_TAXONOMY_ID_LINK
201                         + tax_code_to_id_map.get( tax_code ) + "\" target=\"taxonomy_window\">" + tax_code + "</a>" );
202             }
203             else {
204                 sb.append( tax_code );
205             }
206             sb.append( "</b>" );
207         }
208         else {
209             sb.append( species.getSpeciesId() );
210         }
211         if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
212             sb.append( ":" );
213             sb.append( getSpeciesData().get( species ).toStringBuffer( getDetaildness(), html ) );
214             sb.append( "]" );
215         }
216         if ( html ) {
217             sb.append( "<br>" );
218         }
219         sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
220     }
221
222     private boolean appendGoTerm( final StringBuffer sb, final GoTerm go_term, final boolean first, final boolean html ) {
223         if ( ( getGoNamespaceLimit() == null ) || getGoNamespaceLimit().equals( go_term.getGoNameSpace() ) ) {
224             if ( !first ) {
225                 sb.append( ", " );
226             }
227             final GoId go_id = go_term.getGoId();
228             if ( html ) {
229                 sb.append( "<a href=\"" + SurfacingConstants.AMIGO_LINK + go_id + "\" target=\"amigo_window\">" + go_id
230                         + "</a>" );
231             }
232             else {
233                 sb.append( go_id );
234             }
235             sb.append( ":" );
236             sb.append( go_term.getName() );
237             if ( !html ) {
238                 if ( getGoNamespaceLimit() == null ) {
239                     sb.append( ":" );
240                     sb.append( go_term.getGoNameSpace().toString() );
241                 }
242                 for( final GoXRef xref : go_term.getGoXRefs() ) {
243                     sb.append( ":" );
244                     sb.append( xref.toString() );
245                 }
246             }
247             return false;
248         }
249         return true;
250     }
251
252     private void boldEndIfSortedBy( final DomainSimilaritySortField sort_field, final StringBuffer sb ) {
253         if ( getSortField() == sort_field ) {
254             sb.append( "</b>" );
255         }
256     }
257
258     private void boldStartIfSortedBy( final DomainSimilaritySortField sort_field, final StringBuffer sb ) {
259         if ( getSortField() == sort_field ) {
260             sb.append( "<b>" );
261         }
262     }
263
264     private int compareByDomainId( final DomainSimilarity other ) {
265         return getDomainId().compareTo( other.getDomainId() );
266     }
267
268     private int compareBySpeciesCount( final DomainSimilarity domain_similarity ) {
269         final int s_this = getSpeciesData().size();
270         final int s_other = domain_similarity.getSpeciesData().size();
271         if ( s_this < s_other ) {
272             return PrintableDomainSimilarity.BEFORE;
273         }
274         else if ( s_this > s_other ) {
275             return PrintableDomainSimilarity.AFTER;
276         }
277         else {
278             return PrintableDomainSimilarity.EQUAL;
279         }
280     }
281
282     @Override
283     public int compareTo( final DomainSimilarity domain_similarity ) {
284         if ( this == domain_similarity ) {
285             return PrintableDomainSimilarity.EQUAL;
286         }
287         else if ( domain_similarity == null ) {
288             throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to null" );
289         }
290         else if ( domain_similarity.getClass() != this.getClass() ) {
291             throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to "
292                     + domain_similarity.getClass() );
293         }
294         switch ( getSortField() ) {
295             case MIN:
296                 if ( isSortBySpeciesCountFirst() ) {
297                     final int i = compareBySpeciesCount( domain_similarity );
298                     if ( i != PrintableDomainSimilarity.EQUAL ) {
299                         return i;
300                     }
301                 }
302                 if ( getMinimalSimilarityScore() < domain_similarity.getMinimalSimilarityScore() ) {
303                     return PrintableDomainSimilarity.BEFORE;
304                 }
305                 else if ( getMinimalSimilarityScore() > domain_similarity.getMinimalSimilarityScore() ) {
306                     return PrintableDomainSimilarity.AFTER;
307                 }
308                 else {
309                     return compareByDomainId( domain_similarity );
310                 }
311             case MAX:
312                 if ( isSortBySpeciesCountFirst() ) {
313                     final int i = compareBySpeciesCount( domain_similarity );
314                     if ( i != PrintableDomainSimilarity.EQUAL ) {
315                         return i;
316                     }
317                 }
318                 if ( getMaximalSimilarityScore() < domain_similarity.getMaximalSimilarityScore() ) {
319                     return PrintableDomainSimilarity.BEFORE;
320                 }
321                 else if ( getMaximalSimilarityScore() > domain_similarity.getMaximalSimilarityScore() ) {
322                     return PrintableDomainSimilarity.AFTER;
323                 }
324                 else {
325                     return compareByDomainId( domain_similarity );
326                 }
327             case MEAN:
328                 if ( isSortBySpeciesCountFirst() ) {
329                     final int i = compareBySpeciesCount( domain_similarity );
330                     if ( i != PrintableDomainSimilarity.EQUAL ) {
331                         return i;
332                     }
333                 }
334                 if ( getMeanSimilarityScore() < domain_similarity.getMeanSimilarityScore() ) {
335                     return PrintableDomainSimilarity.BEFORE;
336                 }
337                 else if ( getMeanSimilarityScore() > domain_similarity.getMeanSimilarityScore() ) {
338                     return PrintableDomainSimilarity.AFTER;
339                 }
340                 else {
341                     return compareByDomainId( domain_similarity );
342                 }
343             case SD:
344                 if ( isSortBySpeciesCountFirst() ) {
345                     final int i = compareBySpeciesCount( domain_similarity );
346                     if ( i != PrintableDomainSimilarity.EQUAL ) {
347                         return i;
348                     }
349                 }
350                 if ( getStandardDeviationOfSimilarityScore() < domain_similarity
351                         .getStandardDeviationOfSimilarityScore() ) {
352                     return PrintableDomainSimilarity.BEFORE;
353                 }
354                 else if ( getStandardDeviationOfSimilarityScore() > domain_similarity
355                         .getStandardDeviationOfSimilarityScore() ) {
356                     return PrintableDomainSimilarity.AFTER;
357                 }
358                 else {
359                     return compareByDomainId( domain_similarity );
360                 }
361             case MAX_DIFFERENCE:
362                 if ( isSortBySpeciesCountFirst() ) {
363                     final int i = compareBySpeciesCount( domain_similarity );
364                     if ( i != PrintableDomainSimilarity.EQUAL ) {
365                         return i;
366                     }
367                 }
368                 if ( getMaximalDifference() > domain_similarity.getMaximalDifference() ) {
369                     return PrintableDomainSimilarity.BEFORE;
370                 }
371                 else if ( getMaximalDifference() < domain_similarity.getMaximalDifference() ) {
372                     return PrintableDomainSimilarity.AFTER;
373                 }
374                 else {
375                     return compareByDomainId( domain_similarity );
376                 }
377             case ABS_MAX_COUNTS_DIFFERENCE:
378                 if ( isSortBySpeciesCountFirst() ) {
379                     final int i = compareBySpeciesCount( domain_similarity );
380                     if ( i != PrintableDomainSimilarity.EQUAL ) {
381                         return i;
382                     }
383                 }
384                 if ( Math.abs( getMaximalDifferenceInCounts() ) > Math.abs( domain_similarity
385                         .getMaximalDifferenceInCounts() ) ) {
386                     return PrintableDomainSimilarity.BEFORE;
387                 }
388                 else if ( Math.abs( getMaximalDifferenceInCounts() ) < Math.abs( domain_similarity
389                         .getMaximalDifferenceInCounts() ) ) {
390                     return PrintableDomainSimilarity.AFTER;
391                 }
392                 else {
393                     return compareByDomainId( domain_similarity );
394                 }
395             case MAX_COUNTS_DIFFERENCE:
396                 if ( getSpeciesData().size() != 2 ) {
397                     throw new RuntimeException( "attempt to sort by maximal difference with species not equal to two" );
398                 }
399                 if ( isSortBySpeciesCountFirst() ) {
400                     final int i = compareBySpeciesCount( domain_similarity );
401                     if ( i != PrintableDomainSimilarity.EQUAL ) {
402                         return i;
403                     }
404                 }
405                 if ( getMaximalDifferenceInCounts() > domain_similarity.getMaximalDifferenceInCounts() ) {
406                     return PrintableDomainSimilarity.BEFORE;
407                 }
408                 else if ( getMaximalDifferenceInCounts() < domain_similarity.getMaximalDifferenceInCounts() ) {
409                     return PrintableDomainSimilarity.AFTER;
410                 }
411                 else {
412                     return compareByDomainId( domain_similarity );
413                 }
414             case SPECIES_COUNT:
415                 final int i = compareBySpeciesCount( domain_similarity );
416                 if ( i != PrintableDomainSimilarity.EQUAL ) {
417                     return i;
418                 }
419                 else {
420                     return compareByDomainId( domain_similarity );
421                 }
422             case DOMAIN_ID:
423                 return compareByDomainId( domain_similarity );
424         }
425         throw new AssertionError( "Unknown sort method: " + getSortField() );
426     }
427
428     @Override
429     public SortedSet<DomainId> getCombinableDomainIds( final Species species_of_combinable_domain ) {
430         final SortedSet<DomainId> sorted_ids = new TreeSet<DomainId>();
431         if ( getSpeciesData().containsKey( species_of_combinable_domain ) ) {
432             for( final DomainId id : getSpeciesData().get( species_of_combinable_domain )
433                     .getCombinableDomainIdToCountsMap().keySet() ) {
434                 sorted_ids.add( id );
435             }
436         }
437         return sorted_ids;
438     }
439
440     private CombinableDomains getCombinableDomains() {
441         return _combinable_domains;
442     }
443
444     private DomainSimilarityCalculator.Detailedness getDetaildness() {
445         return _detailedness;
446     }
447
448     @Override
449     public DomainId getDomainId() {
450         return getCombinableDomains().getKeyDomain();
451     }
452
453     private DomainSimilarityCalculator.GoAnnotationOutput getGoAnnotationOutput() {
454         return _go_annotation_output;
455     }
456
457     private Map<GoId, GoTerm> getGoIdToTermMap() {
458         return _go_id_to_term_map;
459     }
460
461     public GoNameSpace getGoNamespaceLimit() {
462         return _go_namespace_limit;
463     }
464
465     @Override
466     public int getMaximalDifference() {
467         return _max_difference;
468     }
469
470     @Override
471     public int getMaximalDifferenceInCounts() {
472         return _max_difference_in_counts;
473     }
474
475     @Override
476     public double getMaximalSimilarityScore() {
477         return _max;
478     }
479
480     @Override
481     public double getMeanSimilarityScore() {
482         return _mean;
483     }
484
485     @Override
486     public double getMinimalSimilarityScore() {
487         return _min;
488     }
489
490     @Override
491     public int getN() {
492         return _n;
493     }
494
495     private DomainSimilaritySortField getSortField() {
496         return _sort_field;
497     }
498
499     @Override
500     public SortedSet<Species> getSpecies() {
501         final SortedSet<Species> species = new TreeSet<Species>();
502         for( final Species s : getSpeciesData().keySet() ) {
503             species.add( s );
504         }
505         return species;
506     }
507
508     public List<Species> getSpeciesCustomOrder() {
509         return _species_order;
510     }
511
512     @Override
513     public SortedMap<Species, SpeciesSpecificDomainSimilariyData> getSpeciesData() {
514         return _species_data;
515     }
516
517     private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html,
518                                                             final Map<String, Integer> tax_code_to_id_map ) {
519         final StringBuffer sb = new StringBuffer();
520         for( final Species species : getSpeciesData().keySet() ) {
521             addSpeciesSpecificDomainData( sb, species, html, tax_code_to_id_map );
522         }
523         return sb;
524     }
525
526     private StringBuffer getSpeciesDataInCustomOrder( final boolean html, final Map<String, Integer> tax_code_to_id_map ) {
527         final StringBuffer sb = new StringBuffer();
528         for( final Species order_species : getSpeciesCustomOrder() ) {
529             if ( getSpeciesData().keySet().contains( order_species ) ) {
530                 addSpeciesSpecificDomainData( sb, order_species, html, tax_code_to_id_map );
531             }
532             else {
533                 sb.append( PrintableDomainSimilarity.NO_SPECIES );
534                 sb.append( PrintableDomainSimilarity.SPECIES_SEPARATOR );
535             }
536         }
537         return sb;
538     }
539
540     @Override
541     public double getStandardDeviationOfSimilarityScore() {
542         return _sd;
543     }
544
545     private void init() {
546         _detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
547         _go_annotation_output = null;
548         _go_id_to_term_map = null;
549     }
550
551     private boolean isSortBySpeciesCountFirst() {
552         return _sort_by_species_count_first;
553     }
554
555     private boolean isTreatAsBinaryComparison() {
556         return _treat_as_binary_comparison;
557     }
558
559     public void setDetailedness( final Detailedness detailedness ) {
560         _detailedness = detailedness;
561     }
562
563     public void setGoAnnotationOutput( final GoAnnotationOutput go_annotation_output ) {
564         _go_annotation_output = go_annotation_output;
565     }
566
567     public void setGoIdToTermMap( final Map<GoId, GoTerm> go_id_to_term_map ) {
568         _go_id_to_term_map = go_id_to_term_map;
569     }
570
571     public void setGoNamespaceLimit( final GoNameSpace go_namespace_limit ) {
572         _go_namespace_limit = go_namespace_limit;
573     }
574
575     public void setSpeciesOrder( final List<Species> species_order ) {
576         if ( !species_order.containsAll( getSpeciesData().keySet() ) ) {
577             throw new IllegalArgumentException( "list to order species must contain all species of multiple combinable domains similarity" );
578         }
579         _species_order = species_order;
580     }
581
582     @Override
583     public StringBuffer toStringBuffer( final PrintableDomainSimilarity.PRINT_OPTION print_option,
584                                         final Map<String, Integer> tax_code_to_id_map ) {
585         switch ( print_option ) {
586             case SIMPLE_TAB_DELIMITED:
587                 return toStringBufferSimpleTabDelimited();
588             case HTML:
589                 return toStringBufferDetailedHTML( tax_code_to_id_map );
590             default:
591                 throw new AssertionError( "Unknown print option: " + print_option );
592         }
593     }
594
595     private StringBuffer toStringBufferDetailedHTML( final Map<String, Integer> tax_code_to_id_map ) {
596         final StringBuffer sb = new StringBuffer();
597         sb.append( "<tr>" );
598         sb.append( "<td>" );
599         boldStartIfSortedBy( DomainSimilaritySortField.DOMAIN_ID, sb );
600         sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + getDomainId() + "\" target=\"pfam_window\">"
601                 + getDomainId() + "</a>" );
602         boldEndIfSortedBy( DomainSimilaritySortField.DOMAIN_ID, sb );
603         sb.append( "</td>" );
604         sb.append( "<td>" );
605         sb.append( "<a href=\"" + SurfacingConstants.GOOGLE_SCHOLAR_SEARCH + getDomainId()
606                 + "\" target=\"gs_window\">gs</a>" );
607         sb.append( "</td>" );
608         sb.append( "<td>" );
609         boldStartIfSortedBy( DomainSimilaritySortField.MEAN, sb );
610         sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
611         boldEndIfSortedBy( DomainSimilaritySortField.MEAN, sb );
612         sb.append( "</td>" );
613         if ( !isTreatAsBinaryComparison() ) {
614             sb.append( "<td>" );
615             sb.append( "(" );
616             boldStartIfSortedBy( DomainSimilaritySortField.SD, sb );
617             sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
618             boldEndIfSortedBy( DomainSimilaritySortField.SD, sb );
619             sb.append( ")" );
620             sb.append( "</td>" );
621             sb.append( "<td>" );
622             sb.append( "[" );
623             boldStartIfSortedBy( DomainSimilaritySortField.MIN, sb );
624             sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
625             boldEndIfSortedBy( DomainSimilaritySortField.MIN, sb );
626             sb.append( "," );
627             boldStartIfSortedBy( DomainSimilaritySortField.MAX, sb );
628             sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
629             boldEndIfSortedBy( DomainSimilaritySortField.MAX, sb );
630             sb.append( "]" );
631             sb.append( "</td>" );
632         }
633         sb.append( "<td>" );
634         boldStartIfSortedBy( DomainSimilaritySortField.MAX_DIFFERENCE, sb );
635         sb.append( getMaximalDifference() );
636         boldEndIfSortedBy( DomainSimilaritySortField.MAX_DIFFERENCE, sb );
637         sb.append( "</td>" );
638         sb.append( "<td>" );
639         if ( isTreatAsBinaryComparison() ) {
640             boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
641             boldStartIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
642             sb.append( getMaximalDifferenceInCounts() );
643             boldEndIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
644             boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
645         }
646         else {
647             boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
648             boldStartIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
649             sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
650             boldEndIfSortedBy( DomainSimilaritySortField.ABS_MAX_COUNTS_DIFFERENCE, sb );
651             boldStartIfSortedBy( DomainSimilaritySortField.MAX_COUNTS_DIFFERENCE, sb );
652         }
653         sb.append( "</td>" );
654         if ( !isTreatAsBinaryComparison() ) {
655             sb.append( "<td>" );
656             if ( ( getSortField() == DomainSimilaritySortField.SPECIES_COUNT ) || isSortBySpeciesCountFirst() ) {
657                 sb.append( "<b>" );
658             }
659             sb.append( getSpeciesData().size() );
660             if ( ( getSortField() == DomainSimilaritySortField.SPECIES_COUNT ) || isSortBySpeciesCountFirst() ) {
661                 sb.append( "</b>" );
662             }
663             sb.append( "</td>" );
664         }
665         if ( getGoAnnotationOutput() != DomainSimilarityCalculator.GoAnnotationOutput.NONE ) {
666             sb.append( "<td>" );
667             addGoInformation( sb, true, true );
668             sb.append( "</td>" );
669         }
670         if ( ( getSpeciesCustomOrder() == null ) || getSpeciesCustomOrder().isEmpty() ) {
671             sb.append( "<td>" );
672             sb.append( getSpeciesDataInAlphabeticalOrder( true, tax_code_to_id_map ) );
673             sb.append( "</td>" );
674         }
675         else {
676             sb.append( "<td>" );
677             sb.append( getSpeciesDataInCustomOrder( true, tax_code_to_id_map ) );
678             sb.append( "</td>" );
679         }
680         sb.append( "</tr>" );
681         return sb;
682     }
683
684     private StringBuffer toStringBufferSimpleTabDelimited() {
685         final StringBuffer sb = new StringBuffer();
686         sb.append( getDomainId() );
687         switch ( getSortField() ) {
688             case MIN:
689                 sb.append( TAB );
690                 sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
691                 break;
692             case MAX:
693                 sb.append( TAB );
694                 sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
695                 break;
696             case MEAN:
697                 sb.append( TAB );
698                 sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
699                 break;
700             case SD:
701                 sb.append( TAB );
702                 sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
703                 break;
704             case MAX_DIFFERENCE:
705                 sb.append( TAB );
706                 sb.append( getMaximalDifference() );
707             case ABS_MAX_COUNTS_DIFFERENCE:
708             case MAX_COUNTS_DIFFERENCE:
709                 sb.append( TAB );
710                 if ( isTreatAsBinaryComparison() ) {
711                     sb.append( getMaximalDifferenceInCounts() );
712                 }
713                 else {
714                     sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
715                 }
716                 break;
717             case SPECIES_COUNT:
718                 sb.append( TAB );
719                 sb.append( getSpeciesData().size() );
720                 break;
721             case DOMAIN_ID:
722                 break;
723             default:
724                 throw new AssertionError( "Unknown sort method: " + getSortField() );
725         }
726         if ( getGoAnnotationOutput() != DomainSimilarityCalculator.GoAnnotationOutput.NONE ) {
727             sb.append( TAB );
728             addGoInformation( sb, true, false );
729         }
730         return sb;
731     }
732
733     public static enum PRINT_OPTION {
734         SIMPLE_TAB_DELIMITED, HTML;
735     }
736 }