in progress...
[jalview.git] / forester / java / src / org / forester / clade_analysis / ResultMulti.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2017 Christian M. Zmasek
6 // Copyright (C) 2017 J. Craig Venter Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phyloxml @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.clade_analysis;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map.Entry;
34 import java.util.Set;
35 import java.util.SortedMap;
36 import java.util.TreeMap;
37
38 import org.forester.util.ForesterUtil;
39 import org.forester.util.UserException;
40
41 public final class ResultMulti {
42
43     private final String       _separator;
44     private final List<Prefix> _greatest_common_prefixes      = new ArrayList<>();
45     private final List<Prefix> _greatest_common_prefixes_up   = new ArrayList<>();
46     private final List<Prefix> _greatest_common_prefixes_down = new ArrayList<>();
47     private List<Prefix>       _all                           = null;
48     private List<Prefix>       _collapsed                     = null;
49     private List<Prefix>       _cleaned_spec                  = null;
50     private boolean            _has_specifics                 = false;
51     private List<Prefix>       _all_up                        = null;
52     private List<Prefix>       _collapsed_up                  = null;
53     private List<Prefix>       _cleaned_spec_up               = null;
54     private boolean            _has_specifics_up              = false;
55     private List<Prefix>       _all_down                      = null;
56     private List<Prefix>       _collapsed_down                = null;
57     private List<Prefix>       _cleaned_spec_down             = null;
58     private boolean            _has_specifics_down            = false;
59
60     ResultMulti( final String separator ) {
61         _separator = separator;
62         reset();
63     }
64
65     ResultMulti() {
66         _separator = AnalysisMulti.DEFAULT_SEPARATOR;
67         reset();
68     }
69
70     public List<Prefix> getAllMultiHitPrefixesUp() {
71         return _all_up;
72     }
73
74     public List<Prefix> getCollapsedMultiHitPrefixesUp() {
75         return _collapsed_up;
76     }
77
78     public List<Prefix> getSpecificMultiHitPrefixesUp() {
79         return _cleaned_spec_up;
80     }
81
82     public boolean isHasSpecificMultiHitsPrefixesUp() {
83         return _has_specifics_up;
84     }
85
86     public List<Prefix> getAllMultiHitPrefixesDown() {
87         return _all_down;
88     }
89
90     public List<Prefix> getCollapsedMultiHitPrefixesDown() {
91         return _collapsed_down;
92     }
93
94     public List<Prefix> getSpecificMultiHitPrefixesDown() {
95         return _cleaned_spec_down;
96     }
97
98     public boolean isHasSpecificMultiHitsPrefixesDown() {
99         return _has_specifics_down;
100     }
101
102     public List<Prefix> getAllMultiHitPrefixes() {
103         return _all;
104     }
105
106     public List<Prefix> getCollapsedMultiHitPrefixes() {
107         return _collapsed;
108     }
109
110     public List<Prefix> getSpecificMultiHitPrefixes() {
111         return _cleaned_spec;
112     }
113
114     public boolean isHasSpecificMultiHitsPrefixes() {
115         return _has_specifics;
116     }
117
118     @Override
119     public final String toString() {
120         final StringBuilder sb = new StringBuilder();
121         sb.append( "Matching Clade(s):" );
122         sb.append( ForesterUtil.LINE_SEPARATOR );
123         for( final Prefix prefix : _collapsed ) {
124             sb.append( prefix );
125             sb.append( ForesterUtil.LINE_SEPARATOR );
126         }
127         if ( _has_specifics ) {
128             sb.append( ForesterUtil.LINE_SEPARATOR );
129             sb.append( "Specific-hit(s):" );
130             sb.append( ForesterUtil.LINE_SEPARATOR );
131             for( final Prefix prefix : _cleaned_spec ) {
132                 sb.append( prefix );
133                 sb.append( ForesterUtil.LINE_SEPARATOR );
134             }
135             sb.append( ForesterUtil.LINE_SEPARATOR );
136             sb.append( "Matching Clade(s) with Specific-hit(s):" );
137             sb.append( ForesterUtil.LINE_SEPARATOR );
138             for( final Prefix prefix : _collapsed ) {
139                 sb.append( prefix );
140                 sb.append( ForesterUtil.LINE_SEPARATOR );
141                 for( final Prefix spec : _cleaned_spec ) {
142                     if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
143                         sb.append( "    " + spec );
144                         sb.append( ForesterUtil.LINE_SEPARATOR );
145                     }
146                 }
147             }
148         }
149         if ( !ForesterUtil.isEmpty( _all_down ) ) {
150             sb.append( ForesterUtil.LINE_SEPARATOR );
151             sb.append( "Matching Down-tree Bracketing Clade(s):" );
152             sb.append( ForesterUtil.LINE_SEPARATOR );
153             for( final Prefix prefix : _collapsed_down ) {
154                 sb.append( prefix );
155                 sb.append( ForesterUtil.LINE_SEPARATOR );
156             }
157         }
158         if ( !ForesterUtil.isEmpty( _all_up ) ) {
159             sb.append( ForesterUtil.LINE_SEPARATOR );
160             sb.append( "Matching Up-tree Bracketing Clade(s):" );
161             sb.append( ForesterUtil.LINE_SEPARATOR );
162             for( final Prefix prefix : _collapsed_up ) {
163                 sb.append( prefix );
164                 sb.append( ForesterUtil.LINE_SEPARATOR );
165             }
166         }
167         return sb.toString();
168     }
169
170     void addGreatestCommonPrefix( final String prefix, final double confidence ) {
171         _greatest_common_prefixes.add( new Prefix( prefix, confidence, _separator ) );
172     }
173
174     void addGreatestCommonPrefixUp( final String prefix_up, final double confidence ) {
175         _greatest_common_prefixes_up.add( new Prefix( prefix_up, confidence, _separator ) );
176     }
177
178     void addGreatestCommonPrefixDown( final String prefix_down, final double confidence ) {
179         _greatest_common_prefixes_down.add( new Prefix( prefix_down, confidence, _separator ) );
180     }
181
182     final void analyze( final double cutoff_for_specifics ) throws UserException {
183         reset();
184         analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator, cutoff_for_specifics );
185         analyzeGreatestCommonPrefixesUp( _greatest_common_prefixes_up, _separator, cutoff_for_specifics );
186         analyzeGreatestCommonPrefixesDown( _greatest_common_prefixes_down, _separator, cutoff_for_specifics );
187     }
188
189     private final void reset() {
190         _all = new ArrayList<>();
191         _collapsed = new ArrayList<>();
192         _cleaned_spec = new ArrayList<>();
193         _has_specifics = false;
194         _all_up = new ArrayList<>();
195         _collapsed_up = new ArrayList<>();
196         _cleaned_spec_up = new ArrayList<>();
197         _has_specifics_up = false;
198         _all_down = new ArrayList<>();
199         _collapsed_down = new ArrayList<>();
200         _cleaned_spec_down = new ArrayList<>();
201         _has_specifics_down = false;
202     }
203
204     private final void analyzeGreatestCommonPrefixes( final List<Prefix> greatest_common_prefixes,
205                                                       final String separator,
206                                                       final double cutoff )
207             throws UserException {
208         final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes, separator );
209         if ( !ForesterUtil.isEmpty( l ) ) {
210             sortPrefixesAccordingToConfidence( l );
211             _all = removeLessSpecificPrefixes( l );
212             _collapsed = collapse( _all );
213             _has_specifics = false;
214             if ( cutoff >= 0 ) {
215                 _cleaned_spec = obtainSpecifics( cutoff, _all, _collapsed );
216                 if ( !ForesterUtil.isEmpty( _cleaned_spec ) ) {
217                     _has_specifics = true;
218                 }
219             }
220         }
221     }
222
223     private final void analyzeGreatestCommonPrefixesUp( final List<Prefix> greatest_common_prefixes_up,
224                                                         final String separator,
225                                                         final double cutoff )
226             throws UserException {
227         final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_up, separator );
228         if ( !ForesterUtil.isEmpty( l ) ) {
229             sortPrefixesAccordingToConfidence( l );
230             _all_up = removeLessSpecificPrefixes( l );
231             _collapsed_up = collapse( _all_up );
232             _has_specifics_up = false;
233             if ( cutoff >= 0 ) {
234                 _cleaned_spec_up = obtainSpecifics( cutoff, _all_up, _collapsed_up );
235                 if ( !ForesterUtil.isEmpty( _cleaned_spec_up ) ) {
236                     _has_specifics_up = true;
237                 }
238             }
239         }
240     }
241
242     final void analyzeGreatestCommonPrefixesDown( final List<Prefix> greatest_common_prefixes_down,
243                                                   final String separator,
244                                                   final double cutoff )
245             throws UserException {
246         final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_down, separator );
247         if ( !ForesterUtil.isEmpty( l ) ) {
248             sortPrefixesAccordingToConfidence( l );
249             _all_down = removeLessSpecificPrefixes( l );
250             _collapsed_down = collapse( _all_down );
251             _has_specifics_down = false;
252             if ( cutoff >= 0 ) {
253                 _cleaned_spec_down = obtainSpecifics( cutoff, _all_down, _collapsed_down );
254                 if ( !ForesterUtil.isEmpty( _cleaned_spec_down ) ) {
255                     _has_specifics_down = true;
256                 }
257             }
258         }
259     }
260
261     final static List<Prefix> obtainSpecifics( final double cutoff,
262                                                final List<Prefix> cleaned,
263                                                final List<Prefix> collapsed ) {
264         final List<Prefix> cleaned_spec = new ArrayList<>();
265         final Set<String> collapsed_set = new HashSet<>();
266         for( final Prefix prefix : collapsed ) {
267             collapsed_set.add( prefix.getPrefix() );
268         }
269         final List<Prefix> spec = new ArrayList<>();
270         for( final Prefix prefix : cleaned ) {
271             if ( ( prefix.getConfidence() >= cutoff ) && !collapsed_set.contains( prefix.getPrefix() ) ) {
272                 spec.add( prefix );
273             }
274         }
275         for( final Prefix o : spec ) {
276             boolean ok = true;
277             for( final Prefix i : spec ) {
278                 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) ) ) {
279                     ok = false;
280                     break;
281                 }
282             }
283             if ( ok ) {
284                 cleaned_spec.add( o );
285             }
286         }
287         return cleaned_spec;
288     }
289
290     private final static List<Prefix> collapse( final List<Prefix> cleaned ) throws UserException {
291         final List<Prefix> collapsed = new ArrayList<>();
292         final Set<String> firsts = new HashSet<>();
293         double confidence_sum = 0;
294         for( final Prefix prefix : cleaned ) {
295             final String f = prefix.getPrefixFirstElement();
296             if ( !firsts.contains( f ) ) {
297                 firsts.add( f );
298                 collapsed.add( prefix );
299                 confidence_sum += prefix.getConfidence();
300             }
301         }
302         if ( !ForesterUtil.isEqual( confidence_sum, 1.0, 1E-5 ) ) {
303             throw new UserException( "confidences add up to " + confidence_sum + " instead of 1.0" );
304         }
305         return collapsed;
306     }
307
308     /*
309      * This replaces (by way of example)
310      * A.1.1 0.9
311      * A.1   0.9
312      * with
313      * A.1.1 0.9
314      *
315      * I.e. it removes less specific prefixes.
316      *
317      */
318     private final static List<Prefix> removeLessSpecificPrefixes( final List<Prefix> l ) {
319         final List<Prefix> cleaned = new ArrayList<>();
320         for( final Prefix o : l ) {
321             boolean ok = true;
322             for( final Prefix i : l ) {
323                 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) )
324                         && ForesterUtil.isEqual( i.getConfidence(),
325                                                  o.getConfidence() ) ) {
326                     ok = false;
327                     break;
328                 }
329             }
330             if ( ok ) {
331                 cleaned.add( o );
332             }
333         }
334         return cleaned;
335     }
336
337     private final static void sortPrefixesAccordingToConfidence( final List<Prefix> l ) {
338         Collections.sort( l, new Comparator<Prefix>() {
339
340             @Override
341             public int compare( final Prefix x, final Prefix y ) {
342                 return compare( x.getConfidence(), y.getConfidence() );
343             }
344
345             private int compare( final double a, final double b ) {
346                 return a > b ? -1 : a > b ? 1 : 0;
347             }
348         } );
349     }
350
351     private final static List<Prefix> obtainAllPrefixes( final List<Prefix> greatest_common_prefixes,
352                                                          final String separator ) {
353         final SortedMap<String, Double> map = new TreeMap<>();
354         for( final Prefix prefix : greatest_common_prefixes ) {
355             final List<String> prefixes = ForesterUtil.spliIntoPrefixes( prefix.getPrefix(), separator );
356             for( final String p : prefixes ) {
357                 map.put( p, 0.0 );
358             }
359         }
360         for( final String key : map.keySet() ) {
361             for( final Prefix prefix : greatest_common_prefixes ) {
362                 if ( prefix.getPrefix().startsWith( key ) ) {
363                     map.put( key, map.get( key ) + prefix.getConfidence() );
364                 }
365             }
366         }
367         final List<Prefix> l = new ArrayList<>();
368         for( final Entry<String, Double> entry : map.entrySet() ) {
369             l.add( new Prefix( entry.getKey(), entry.getValue(), separator ) );
370         }
371         return l;
372     }
373 }