in progress
[jalview.git] / forester / java / src / org / forester / surfacing / DomainSimilarity.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.awt.Color;
30 import java.util.Comparator;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Set;
37 import java.util.SortedMap;
38 import java.util.SortedSet;
39 import java.util.TreeMap;
40 import java.util.TreeSet;
41
42 import org.forester.phylogeny.Phylogeny;
43 import org.forester.species.Species;
44 import org.forester.surfacing.DomainSimilarityCalculator.Detailedness;
45 import org.forester.util.ForesterUtil;
46
47 public class DomainSimilarity implements Comparable<DomainSimilarity> {
48
49     final public static String                              SPECIES_SEPARATOR          = "  ";
50     final private static int                                EQUAL                      = 0;
51     final private static String                             NO_SPECIES                 = "     ";
52     private static final boolean                            OUTPUT_TAXCODES_PER_DOMAIN = false;
53     final private CombinableDomains                         _combinable_domains;
54     private DomainSimilarityCalculator.Detailedness         _detailedness;
55     final private double                                    _max;
56     private final int                                       _max_difference;
57     private final int                                       _max_difference_in_counts;
58     final private double                                    _mean;
59     final private double                                    _min;
60     final private int                                       _n;
61     final private double                                    _sd;
62     final private SortedMap<Species, SpeciesSpecificDcData> _species_data;
63     private List<Species>                                   _species_order;
64     private final boolean                                   _treat_as_binary_comparison;
65
66     public DomainSimilarity( final CombinableDomains combinable_domains,
67                              final double min,
68                              final double max,
69                              final double mean,
70                              final double median,
71                              final double sd,
72                              final int n,
73                              final int max_difference_in_counts,
74                              final int max_difference,
75                              final SortedMap<Species, SpeciesSpecificDcData> species_data,
76                              final boolean sort_by_species_count_first,
77                              final boolean treat_as_binary_comparison ) {
78         if ( combinable_domains == null ) {
79             throw new IllegalArgumentException( "attempt to use null combinable domains" );
80         }
81         if ( species_data == null ) {
82             throw new IllegalArgumentException( "attempt to use null species data" );
83         }
84         if ( species_data.size() < 1 ) {
85             throw new IllegalArgumentException( "attempt to use empty species data" );
86         }
87         if ( n < 0 ) {
88             throw new IllegalArgumentException( "attempt to use N less than 0" );
89         }
90         if ( ( species_data.size() > 1 ) && ( n < 1 ) ) {
91             throw new IllegalArgumentException( "attempt to use N less than 1" );
92         }
93         if ( sd < 0.0 ) {
94             throw new IllegalArgumentException( "attempt to use negative SD" );
95         }
96         if ( max < min ) {
97             throw new IllegalArgumentException( "attempt to use max smaller than min" );
98         }
99         init();
100         _combinable_domains = combinable_domains;
101         _min = min;
102         _max = max;
103         _mean = mean;
104         _sd = sd;
105         _n = n;
106         _max_difference_in_counts = max_difference_in_counts;
107         _max_difference = max_difference;
108         _species_data = species_data;
109         _treat_as_binary_comparison = treat_as_binary_comparison;
110         final int s = species_data.size();
111         if ( ( ( s * s ) - s ) != ( getN() * 2 ) ) {
112             throw new IllegalArgumentException( "illegal species count and n: species count:" + s + ", n:" + _n
113                     + " for domain " + combinable_domains.getKeyDomain() );
114         }
115         if ( s > 2 ) {
116             if ( getMaximalDifferenceInCounts() < 0 ) {
117                 throw new IllegalArgumentException( "attempt to use negative max difference in counts with more than two species" );
118             }
119             if ( getMaximalDifference() < 0 ) {
120                 throw new IllegalArgumentException( "attempt to use negative max difference with more than two species" );
121             }
122         }
123     }
124
125     public DomainSimilarity( final CombinableDomains combinable_domains,
126                              final int max_difference_in_counts,
127                              final int max_difference,
128                              final SortedMap<Species, SpeciesSpecificDcData> species_data,
129                              final boolean sort_by_species_count_first,
130                              final boolean treat_as_binary_comparison ) {
131         if ( combinable_domains == null ) {
132             throw new IllegalArgumentException( "attempt to use null combinable domains" );
133         }
134         if ( species_data == null ) {
135             throw new IllegalArgumentException( "attempt to use null species data" );
136         }
137         if ( species_data.size() < 1 ) {
138             throw new IllegalArgumentException( "attempt to use empty species data" );
139         }
140         init();
141         _combinable_domains = combinable_domains;
142         _min = -1;
143         _max = -1;
144         _mean = -1;
145         _sd = -1;
146         _n = -1;
147         _max_difference_in_counts = max_difference_in_counts;
148         _max_difference = max_difference;
149         _species_data = species_data;
150         _treat_as_binary_comparison = treat_as_binary_comparison;
151         final int s = species_data.size();
152         if ( s > 2 ) {
153             if ( getMaximalDifferenceInCounts() < 0 ) {
154                 throw new IllegalArgumentException( "attempt to use negative max difference in counts with more than two species" );
155             }
156             if ( getMaximalDifference() < 0 ) {
157                 throw new IllegalArgumentException( "attempt to use negative max difference with more than two species" );
158             }
159         }
160     }
161
162     public int compareTo( final DomainSimilarity domain_similarity ) {
163         if ( this == domain_similarity ) {
164             return EQUAL;
165         }
166         else if ( domain_similarity == null ) {
167             throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to null" );
168         }
169         else if ( domain_similarity.getClass() != this.getClass() ) {
170             throw new IllegalArgumentException( "attempt to compare " + this.getClass() + " to "
171                     + domain_similarity.getClass() );
172         }
173         return compareByDomainId( domain_similarity );
174     }
175
176     public SortedSet<String> getCombinableDomainIds( final Species species_of_combinable_domain ) {
177         final SortedSet<String> sorted_ids = new TreeSet<String>();
178         if ( getSpeciesData().containsKey( species_of_combinable_domain ) ) {
179             for( final String id : getSpeciesData().get( species_of_combinable_domain )
180                     .getCombinableDomainIdToCountsMap().keySet() ) {
181                 sorted_ids.add( id );
182             }
183         }
184         return sorted_ids;
185     }
186
187     public String getDomainId() {
188         return getCombinableDomains().getKeyDomain();
189     }
190
191     /**
192      * For pairwise similarities, this should return the "difference"; for example the difference in counts
193      * for copy number based features (the same as getMaximalDifferenceInCounts(), or the number
194      * of actually different domain combinations. 
195      * For pairwise similarities, this should return the difference,
196      * while for comparisons of more than two domains, this should return the maximal difference
197      * 
198      */
199     public int getMaximalDifference() {
200         return _max_difference;
201     }
202
203     /**
204      * For pairwise similarities, this should return the difference in counts,
205      * while for comparisons of more than two domains, this should return the maximal difference
206      * in counts
207      * 
208      * 
209      * @return the (maximal) difference in counts
210      */
211     public int getMaximalDifferenceInCounts() {
212         return _max_difference_in_counts;
213     }
214
215     public double getMaximalSimilarityScore() {
216         return _max;
217     }
218
219     public double getMeanSimilarityScore() {
220         return _mean;
221     }
222
223     public double getMinimalSimilarityScore() {
224         return _min;
225     }
226
227     /**
228      * This should return the number of pairwise distances used to calculate
229      * this similarity score
230      * 
231      * @return the number of pairwise distances
232      */
233     public int getN() {
234         return _n;
235     }
236
237     public SortedSet<Species> getSpecies() {
238         final SortedSet<Species> species = new TreeSet<Species>();
239         for( final Species s : getSpeciesData().keySet() ) {
240             species.add( s );
241         }
242         return species;
243     }
244
245     public List<Species> getSpeciesCustomOrder() {
246         return _species_order;
247     }
248
249     /**
250      * This should return a map, which maps species names to
251      * SpeciesSpecificDomainSimilariyData
252      * 
253      * 
254      * @return SortedMap<String, SpeciesSpecificDomainSimilariyData>
255      */
256     public SortedMap<Species, SpeciesSpecificDcData> getSpeciesData() {
257         return _species_data;
258     }
259
260     public double getStandardDeviationOfSimilarityScore() {
261         return _sd;
262     }
263
264     public void setDetailedness( final Detailedness detailedness ) {
265         _detailedness = detailedness;
266     }
267
268     public void setSpeciesOrder( final List<Species> species_order ) {
269         if ( !species_order.containsAll( getSpeciesData().keySet() ) ) {
270             throw new IllegalArgumentException( "list to order species must contain all species of multiple combinable domains similarity" );
271         }
272         _species_order = species_order;
273     }
274
275     public StringBuffer toStringBuffer( final DomainSimilarity.PRINT_OPTION print_option,
276                                         final Map<String, Integer> tax_code_to_id_map,
277                                         final Phylogeny phy ) {
278         switch ( print_option ) {
279             case SIMPLE_TAB_DELIMITED:
280                 return toStringBufferSimpleTabDelimited();
281             case HTML:
282                 return toStringBufferDetailedHTML( tax_code_to_id_map, phy, OUTPUT_TAXCODES_PER_DOMAIN );
283             default:
284                 throw new AssertionError( "Unknown print option: " + print_option );
285         }
286     }
287
288     private void addSpeciesSpecificDomainData( final StringBuffer sb,
289                                                final Species species,
290                                                final boolean html,
291                                                final Map<String, Integer> tax_code_to_id_map,
292                                                final Phylogeny phy ) {
293         if ( html ) {
294             sb.append( "<tr>" );
295             sb.append( "<td>" );
296             addTaxWithLink( sb, species.getSpeciesId(), tax_code_to_id_map, phy );
297             sb.append( "</td>" );
298         }
299         else {
300             sb.append( species.getSpeciesId() );
301         }
302         if ( getDetaildness() != DomainSimilarityCalculator.Detailedness.BASIC ) {
303             if ( html ) {
304                 //sb.append( ":" );
305             }
306             else {
307                 sb.append( "\t" );
308             }
309             sb.append( getSpeciesData().get( species ).toStringBuffer( getDetaildness(), html ) );
310         }
311         if ( html ) {
312             //sb.append( "<br>" );
313             sb.append( "</tr>" );
314         }
315         else {
316             sb.append( "\n\t" );
317         }
318     }
319
320     private void addTaxWithLink( final StringBuffer sb,
321                                  final String tax_code,
322                                  final Map<String, Integer> tax_code_to_id_map,
323                                  final Phylogeny phy ) {
324         String hex = null;
325         if ( ( phy != null ) && !phy.isEmpty() ) {
326             hex = SurfacingUtil.obtainHexColorStringDependingOnTaxonomyGroup( tax_code, phy );
327         }
328         sb.append( "<b>" );
329         if ( !ForesterUtil.isEmpty( tax_code )
330                 && ( ( tax_code_to_id_map != null ) && tax_code_to_id_map.containsKey( tax_code ) ) ) {
331             if ( !ForesterUtil.isEmpty( hex ) ) {
332                 sb.append( "<a href=\"" );
333                 sb.append( SurfacingConstants.UNIPROT_TAXONOMY_ID_LINK );
334                 sb.append( tax_code_to_id_map.get( tax_code ) );
335                 sb.append( "\" target=\"tw\"><span style=\"color:" );
336                 sb.append( hex );
337                 sb.append( "\">" );
338                 sb.append( tax_code );
339                 sb.append( "</span></a>" );
340             }
341             else {
342                 sb.append( "<a href=\"" );
343                 sb.append( SurfacingConstants.UNIPROT_TAXONOMY_ID_LINK );
344                 sb.append( tax_code_to_id_map.get( tax_code ) );
345                 sb.append( "\" target=\"tw\">" );
346                 sb.append( tax_code );
347                 sb.append( "</a>" );
348             }
349         }
350         else {
351             sb.append( tax_code );
352         }
353         sb.append( "</b>" );
354     }
355
356     private int compareByDomainId( final DomainSimilarity other ) {
357         return getDomainId().compareToIgnoreCase( other.getDomainId() );
358     }
359
360     private CombinableDomains getCombinableDomains() {
361         return _combinable_domains;
362     }
363
364     private DomainSimilarityCalculator.Detailedness getDetaildness() {
365         return _detailedness;
366     }
367
368     private StringBuffer getDomainDataInAlphabeticalOrder() {
369         final SortedMap<String, SortedSet<String>> m = new TreeMap<String, SortedSet<String>>();
370         final StringBuffer sb = new StringBuffer();
371         for( final Species species : getSpeciesData().keySet() ) {
372             for( final String combable_dom : getCombinableDomainIds( species ) ) {
373                 if ( !m.containsKey( combable_dom ) ) {
374                     m.put( combable_dom, new TreeSet<String>() );
375                 }
376                 m.get( combable_dom ).add( species.getSpeciesId() );
377             }
378         }
379         for( final Map.Entry<String, SortedSet<String>> e : m.entrySet() ) {
380             sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + e.getKey() + "\">" + e.getKey() + "</a>" );
381             sb.append( " " );
382             sb.append( "<span style=\"font-size:7px\">" );
383             for( final String tax : e.getValue() ) {
384                 final String hex = SurfacingUtil.obtainHexColorStringDependingOnTaxonomyGroup( tax, null );
385                 if ( !ForesterUtil.isEmpty( hex ) ) {
386                     sb.append( "<span style=\"color:" );
387                     sb.append( hex );
388                     sb.append( "\">" );
389                     sb.append( tax );
390                     sb.append( "</span>" );
391                 }
392                 else {
393                     sb.append( tax );
394                 }
395                 sb.append( " " );
396             }
397             sb.append( "</span>" );
398             sb.append( "<br>\n" );
399         }
400         return sb;
401     }
402
403     private StringBuffer getSpeciesDataInAlphabeticalOrder( final boolean html,
404                                                             final Map<String, Integer> tax_code_to_id_map,
405                                                             final Phylogeny phy ) {
406         final StringBuffer sb = new StringBuffer();
407         sb.append( "<table>" );
408         for( final Species species : getSpeciesData().keySet() ) {
409             addSpeciesSpecificDomainData( sb, species, html, tax_code_to_id_map, phy );
410         }
411         sb.append( "</table>" );
412         return sb;
413     }
414
415     private StringBuffer getSpeciesDataInCustomOrder( final boolean html,
416                                                       final Map<String, Integer> tax_code_to_id_map,
417                                                       final Phylogeny phy ) {
418         final StringBuffer sb = new StringBuffer();
419         for( final Species order_species : getSpeciesCustomOrder() ) {
420             if ( getSpeciesData().keySet().contains( order_species ) ) {
421                 addSpeciesSpecificDomainData( sb, order_species, html, tax_code_to_id_map, phy );
422             }
423             else {
424                 sb.append( DomainSimilarity.NO_SPECIES );
425                 sb.append( DomainSimilarity.SPECIES_SEPARATOR );
426             }
427         }
428         return sb;
429     }
430
431     private StringBuffer getTaxonomyGroupDistribution( final Phylogeny tol ) {
432         final SortedMap<String, Set<String>> domain_to_species_set_map = new TreeMap<String, Set<String>>();
433         for( final Species species : getSpeciesData().keySet() ) {
434             for( final String combable_dom : getCombinableDomainIds( species ) ) {
435                 if ( !domain_to_species_set_map.containsKey( combable_dom ) ) {
436                     domain_to_species_set_map.put( combable_dom, new HashSet<String>() );
437                 }
438                 domain_to_species_set_map.get( combable_dom ).add( species.getSpeciesId() );
439             }
440         }
441         final StringBuffer sb = new StringBuffer();
442         sb.append( "<table>" );
443         for( final Map.Entry<String, Set<String>> domain_to_species_set : domain_to_species_set_map.entrySet() ) {
444             final Map<String, Integer> counts = new HashMap<String, Integer>();
445             for( final String tax_code : domain_to_species_set.getValue() ) {
446                 final String group = SurfacingUtil.obtainTaxonomyGroup( tax_code, tol );
447                 if ( !ForesterUtil.isEmpty( group ) ) {
448                     if ( !counts.containsKey( group ) ) {
449                         counts.put( group, 1 );
450                     }
451                     else {
452                         counts.put( group, counts.get( group ) + 1 );
453                     }
454                 }
455                 else {
456                     return null;
457                 }
458             }
459             final SortedMap<Integer, SortedSet<String>> counts_to_groups = new TreeMap<Integer, SortedSet<String>>( new Comparator<Integer>() {
460
461                 @Override
462                 public int compare( final Integer first, final Integer second ) {
463                     return second.compareTo( first );
464                 }
465             } );
466             for( final Map.Entry<String, Integer> group_to_counts : counts.entrySet() ) {
467                 final int c = group_to_counts.getValue();
468                 if ( !counts_to_groups.containsKey( c ) ) {
469                     counts_to_groups.put( c, new TreeSet<String>() );
470                 }
471                 counts_to_groups.get( c ).add( group_to_counts.getKey() );
472             }
473             sb.append( "<tr>" );
474             sb.append( "<td>" );
475             sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + domain_to_species_set.getKey() + "\">"
476                     + domain_to_species_set.getKey() + "</a>" );
477             sb.append( " " );
478             sb.append( "</td>" );
479             boolean first = true;
480             for( final Entry<Integer, SortedSet<String>> count_to_groups : counts_to_groups.entrySet() ) {
481                 if ( first ) {
482                     first = false;
483                 }
484                 else {
485                     sb.append( "<tr>" );
486                     sb.append( "<td>" );
487                     sb.append( "</td>" );
488                 }
489                 sb.append( "<td>" );
490                 final SortedSet<String> groups = count_to_groups.getValue();
491                 sb.append( count_to_groups.getKey() );
492                 sb.append( " " );
493                 for( final String group : groups ) {
494                     final Color color = ForesterUtil.obtainColorDependingOnTaxonomyGroup( group );
495                     if ( color == null ) {
496                         throw new IllegalArgumentException( "no color found for taxonomy group\"" + group + "\"" );
497                     }
498                     final String hex = String.format( "#%02x%02x%02x",
499                                                       color.getRed(),
500                                                       color.getGreen(),
501                                                       color.getBlue() );
502                     sb.append( "<span style=\"color:" );
503                     sb.append( hex );
504                     sb.append( "\">" );
505                     sb.append( " " );
506                     sb.append( group );
507                     sb.append( "</span>" );
508                 }
509                 sb.append( "</td>" );
510                 sb.append( "</tr>" );
511             }
512             sb.append( ForesterUtil.getLineSeparator() );
513         }
514         sb.append( "</table>" );
515         return sb;
516     }
517
518     private void init() {
519         _detailedness = DomainSimilarityCalculator.Detailedness.PUNCTILIOUS;
520     }
521
522     private boolean isTreatAsBinaryComparison() {
523         return _treat_as_binary_comparison;
524     }
525
526     private StringBuffer toStringBufferDetailedHTML( final Map<String, Integer> tax_code_to_id_map,
527                                                      final Phylogeny phy,
528                                                      final boolean output_tax_codes_per_domain ) {
529         final StringBuffer sb = new StringBuffer();
530         sb.append( "<tr>" );
531         sb.append( "<td>" );
532         sb.append( "<b>" );
533         sb.append( "<a href=\"" + SurfacingConstants.PFAM_FAMILY_ID_LINK + getDomainId() + "\" target=\"pfam_window\">"
534                 + getDomainId() + "</a>" );
535         sb.append( "</b>" );
536         sb.append( "<a name=\"" + getDomainId() + "\">" );
537         sb.append( "</td>" );
538         sb.append( "<td>" );
539         sb.append( "<a href=\"" + SurfacingConstants.GOOGLE_SCHOLAR_SEARCH + getDomainId()
540                 + "\" target=\"gs_window\">gs</a>" );
541         sb.append( "</td>" );
542         if ( getMaximalSimilarityScore() > 0 ) {
543             sb.append( "<td>" );
544             sb.append( ForesterUtil.round( getMeanSimilarityScore(), 3 ) );
545             sb.append( "</td>" );
546             if ( SurfacingConstants.PRINT_MORE_DOM_SIMILARITY_INFO ) {
547                 if ( !isTreatAsBinaryComparison() ) {
548                     sb.append( "<td>" );
549                     sb.append( "(" );
550                     sb.append( ForesterUtil.round( getStandardDeviationOfSimilarityScore(), 3 ) );
551                     sb.append( ")" );
552                     sb.append( "</td>" );
553                     sb.append( "<td>" );
554                     sb.append( "[" );
555                     sb.append( ForesterUtil.round( getMinimalSimilarityScore(), 3 ) );
556                     sb.append( "-" );
557                     sb.append( ForesterUtil.round( getMaximalSimilarityScore(), 3 ) );
558                     sb.append( "]" );
559                     sb.append( "</td>" );
560                 }
561             }
562         }
563         sb.append( "<td>" );
564         sb.append( getMaximalDifference() );
565         sb.append( "</td>" );
566         sb.append( "<td>" );
567         if ( isTreatAsBinaryComparison() ) {
568             sb.append( getMaximalDifferenceInCounts() );
569         }
570         else {
571             sb.append( Math.abs( getMaximalDifferenceInCounts() ) );
572         }
573         sb.append( "</td>" );
574         if ( !isTreatAsBinaryComparison() ) {
575             sb.append( "<td>" );
576             sb.append( "<b>" );
577             sb.append( getSpeciesData().size() );
578             sb.append( "</b>" );
579             sb.append( "</td>" );
580         }
581         if ( ( getSpeciesCustomOrder() == null ) || getSpeciesCustomOrder().isEmpty() ) {
582             sb.append( "<td>" );
583             sb.append( getSpeciesDataInAlphabeticalOrder( true, tax_code_to_id_map, phy ) );
584             if ( output_tax_codes_per_domain ) {
585                 sb.append( getDomainDataInAlphabeticalOrder() );
586             }
587             sb.append( getTaxonomyGroupDistribution( phy ) );
588             sb.append( "</td>" );
589         }
590         else {
591             sb.append( "<td>" );
592             sb.append( getSpeciesDataInCustomOrder( true, tax_code_to_id_map, phy ) );
593             if ( output_tax_codes_per_domain ) {
594                 sb.append( getDomainDataInAlphabeticalOrder() );
595             }
596             sb.append( getTaxonomyGroupDistribution( phy ) );
597             sb.append( "</td>" );
598         }
599         sb.append( "</tr>" );
600         return sb;
601     }
602
603     private StringBuffer toStringBufferSimpleTabDelimited() {
604         final StringBuffer sb = new StringBuffer();
605         sb.append( getDomainId() );
606         sb.append( "\t" );
607         sb.append( getSpeciesDataInAlphabeticalOrder( false, null, null ) );
608         sb.append( "\n" );
609         return sb;
610     }
611
612     static public enum DomainSimilarityScoring {
613         COMBINATIONS, DOMAINS, PROTEINS;
614     }
615
616     public static enum DomainSimilaritySortField {
617         ABS_MAX_COUNTS_DIFFERENCE, DOMAIN_ID, MAX, MAX_COUNTS_DIFFERENCE, MAX_DIFFERENCE, MEAN, MIN, SD, SPECIES_COUNT,
618     }
619
620     public static enum PRINT_OPTION {
621         HTML, SIMPLE_TAB_DELIMITED;
622     }
623
624     class ValueComparator implements Comparator<String> {
625
626         final private Map<String, Integer> _base;
627
628         public ValueComparator( final Map<String, Integer> base ) {
629             _base = base;
630         }
631
632         public int compare( final String a, final String b ) {
633             if ( _base.get( a ) >= _base.get( b ) ) {
634                 return -1;
635             }
636             else {
637                 return 1;
638             } // returning 0 would merge keys
639         }
640     }
641 }