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
40 public final class ResultMulti {
41
42     private final String       _separator;
43     private final List<Prefix> _greatest_common_prefixes      = new ArrayList<>();
44     private final List<Prefix> _greatest_common_prefixes_up   = new ArrayList<>();
45     private final List<Prefix> _greatest_common_prefixes_down = new ArrayList<>();
46     private List<Prefix>       _all                           = null;
47     private List<Prefix>       _collapsed                     = null;
48     private List<Prefix>       _cleaned_spec                  = null;
49     private boolean            _has_specifics                 = false;
50     private List<Prefix>       _all_up                        = null;
51     private List<Prefix>       _collapsed_up                  = null;
52     private List<Prefix>       _cleaned_spec_up               = null;
53     private boolean            _has_specifics_up              = false;
54     private List<Prefix>       _all_down                      = null;
55     private List<Prefix>       _collapsed_down                = null;
56     private List<Prefix>       _cleaned_spec_down             = null;
57     private boolean            _has_specifics_down            = false;
58
59     ResultMulti( final String separator ) {
60         _separator = separator;
61         reset();
62     }
63
64     ResultMulti() {
65         _separator = AnalysisMulti.DEFAULT_SEPARATOR;
66         reset();
67     }
68
69     public List<Prefix> getAllMultiHitPrefixesUp() {
70         return _all_up;
71     }
72
73     public List<Prefix> getCollapsedMultiHitPrefixesUp() {
74         return _collapsed_up;
75     }
76
77     public List<Prefix> getSpecificMultiHitPrefixesUp() {
78         return _cleaned_spec_up;
79     }
80
81     public boolean isHasSpecificMultiHitsPrefixesUp() {
82         return _has_specifics_up;
83     }
84
85     public List<Prefix> getAllMultiHitPrefixesDown() {
86         return _all_down;
87     }
88
89     public List<Prefix> getCollapsedMultiHitPrefixesDown() {
90         return _collapsed_down;
91     }
92
93     public List<Prefix> getSpecificMultiHitPrefixesDown() {
94         return _cleaned_spec_down;
95     }
96
97     public boolean isHasSpecificMultiHitsPrefixesDown() {
98         return _has_specifics_down;
99     }
100
101     public List<Prefix> getAllMultiHitPrefixes() {
102         return _all;
103     }
104
105     public List<Prefix> getCollapsedMultiHitPrefixes() {
106         return _collapsed;
107     }
108
109     public List<Prefix> getSpecificMultiHitPrefixes() {
110         return _cleaned_spec;
111     }
112
113     public boolean isHasSpecificMultiHitsPrefixes() {
114         return _has_specifics;
115     }
116
117     @Override
118     public final String toString() {
119         final StringBuilder sb = new StringBuilder();
120         sb.append( "Matching Clade(s):" );
121         sb.append( ForesterUtil.LINE_SEPARATOR );
122         for( final Prefix prefix : _collapsed ) {
123             sb.append( prefix );
124             sb.append( ForesterUtil.LINE_SEPARATOR );
125         }
126         if ( _has_specifics ) {
127             sb.append( ForesterUtil.LINE_SEPARATOR );
128             sb.append( "Specific-hit(s):" );
129             sb.append( ForesterUtil.LINE_SEPARATOR );
130             for( final Prefix prefix : _cleaned_spec ) {
131                 sb.append( prefix );
132                 sb.append( ForesterUtil.LINE_SEPARATOR );
133             }
134             sb.append( ForesterUtil.LINE_SEPARATOR );
135             sb.append( "Matching Clade(s) with Specific-hit(s):" );
136             sb.append( ForesterUtil.LINE_SEPARATOR );
137             for( final Prefix prefix : _collapsed ) {
138                 sb.append( prefix );
139                 sb.append( ForesterUtil.LINE_SEPARATOR );
140                 for( final Prefix spec : _cleaned_spec ) {
141                     if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
142                         sb.append( "    " + spec );
143                         sb.append( ForesterUtil.LINE_SEPARATOR );
144                     }
145                 }
146             }
147         }
148         if ( !ForesterUtil.isEmpty( _all_down ) ) {
149             sb.append( ForesterUtil.LINE_SEPARATOR );
150             sb.append( "Matching Down-tree Bracketing Clade(s):" );
151             sb.append( ForesterUtil.LINE_SEPARATOR );
152             for( final Prefix prefix : _collapsed_down ) {
153                 sb.append( prefix );
154                 sb.append( ForesterUtil.LINE_SEPARATOR );
155             }
156         }
157         if ( !ForesterUtil.isEmpty( _all_up ) ) {
158             sb.append( ForesterUtil.LINE_SEPARATOR );
159             sb.append( "Matching Up-tree Bracketing Clade(s):" );
160             sb.append( ForesterUtil.LINE_SEPARATOR );
161             for( final Prefix prefix : _collapsed_up ) {
162                 sb.append( prefix );
163                 sb.append( ForesterUtil.LINE_SEPARATOR );
164             }
165         }
166         return sb.toString();
167     }
168
169     void addGreatestCommonPrefix( final String prefix, final double confidence ) {
170         _greatest_common_prefixes.add( new Prefix( prefix, confidence, _separator ) );
171     }
172
173     void addGreatestCommonPrefixUp( final String prefix_up, final double confidence ) {
174         _greatest_common_prefixes_up.add( new Prefix( prefix_up, confidence, _separator ) );
175     }
176
177     void addGreatestCommonPrefixDown( final String prefix_down, final double confidence ) {
178         _greatest_common_prefixes_down.add( new Prefix( prefix_down, confidence, _separator ) );
179     }
180
181     final void analyze( final double cutoff_for_specifics ) {
182         reset();
183         analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator, cutoff_for_specifics );
184         analyzeGreatestCommonPrefixesUp( _greatest_common_prefixes_up, _separator, cutoff_for_specifics );
185         analyzeGreatestCommonPrefixesDown( _greatest_common_prefixes_down, _separator, cutoff_for_specifics );
186     }
187
188     private final void reset() {
189         _all = new ArrayList<>();
190         _collapsed = new ArrayList<>();
191         _cleaned_spec = new ArrayList<>();
192         _has_specifics = false;
193         _all_up = new ArrayList<>();
194         _collapsed_up = new ArrayList<>();
195         _cleaned_spec_up = new ArrayList<>();
196         _has_specifics_up = false;
197         _all_down = new ArrayList<>();
198         _collapsed_down = new ArrayList<>();
199         _cleaned_spec_down = new ArrayList<>();
200         _has_specifics_down = false;
201     }
202
203     private final void analyzeGreatestCommonPrefixes( final List<Prefix> greatest_common_prefixes,
204                                                       final String separator,
205                                                       final double cutoff ) {
206         final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes, separator );
207         if ( !ForesterUtil.isEmpty( l ) ) {
208             sortPrefixesAccordingToConfidence( l );
209             _all = removeLessSpecificPrefixes( l );
210             _collapsed = collapse( _all );
211             _has_specifics = false;
212             if ( cutoff >= 0 ) {
213                 _cleaned_spec = obtainSpecifics( cutoff, _all, _collapsed );
214                 if ( !ForesterUtil.isEmpty( _cleaned_spec ) ) {
215                     _has_specifics = true;
216                 }
217             }
218         }
219     }
220
221     private final void analyzeGreatestCommonPrefixesUp( final List<Prefix> greatest_common_prefixes_up,
222                                                         final String separator,
223                                                         final double cutoff ) {
224         final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_up, separator );
225         if ( !ForesterUtil.isEmpty( l ) ) {
226             sortPrefixesAccordingToConfidence( l );
227             _all_up = removeLessSpecificPrefixes( l );
228             _collapsed_up = collapse( _all_up );
229             _has_specifics_up = false;
230             if ( cutoff >= 0 ) {
231                 _cleaned_spec_up = obtainSpecifics( cutoff, _all_up, _collapsed_up );
232                 if ( !ForesterUtil.isEmpty( _cleaned_spec_up ) ) {
233                     _has_specifics_up = true;
234                 }
235             }
236         }
237     }
238
239     final void analyzeGreatestCommonPrefixesDown( final List<Prefix> greatest_common_prefixes_down,
240                                                   final String separator,
241                                                   final double cutoff ) {
242         final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_down, separator );
243         if ( !ForesterUtil.isEmpty( l ) ) {
244             sortPrefixesAccordingToConfidence( l );
245             _all_down = removeLessSpecificPrefixes( l );
246             _collapsed_down = collapse( _all_down );
247             _has_specifics_down = false;
248             if ( cutoff >= 0 ) {
249                 _cleaned_spec_down = obtainSpecifics( cutoff, _all_down, _collapsed_down );
250                 if ( !ForesterUtil.isEmpty( _cleaned_spec_down ) ) {
251                     _has_specifics_down = true;
252                 }
253             }
254         }
255     }
256
257     final static List<Prefix> obtainSpecifics( final double cutoff,
258                                                final List<Prefix> cleaned,
259                                                final List<Prefix> collapsed ) {
260         final List<Prefix> cleaned_spec = new ArrayList<>();
261         final Set<String> collapsed_set = new HashSet<>();
262         for( final Prefix prefix : collapsed ) {
263             collapsed_set.add( prefix.getPrefix() );
264         }
265         final List<Prefix> spec = new ArrayList<>();
266         for( final Prefix prefix : cleaned ) {
267             if ( ( prefix.getConfidence() >= cutoff ) && !collapsed_set.contains( prefix.getPrefix() ) ) {
268                 spec.add( prefix );
269             }
270         }
271         for( final Prefix o : spec ) {
272             boolean ok = true;
273             for( final Prefix i : spec ) {
274                 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) ) ) {
275                     ok = false;
276                     break;
277                 }
278             }
279             if ( ok ) {
280                 cleaned_spec.add( o );
281             }
282         }
283         return cleaned_spec;
284     }
285
286     private final static List<Prefix> collapse( final List<Prefix> cleaned ) {
287         final List<Prefix> collapsed = new ArrayList<>();
288         final Set<String> firsts = new HashSet<>();
289         double confidence_sum = 0;
290         for( final Prefix prefix : cleaned ) {
291             final String f = prefix.getPrefixFirstElement();
292             if ( !firsts.contains( f ) ) {
293                 firsts.add( f );
294                 collapsed.add( prefix );
295                 confidence_sum += prefix.getConfidence();
296             }
297         }
298         if ( !ForesterUtil.isEqual( confidence_sum, 1.0, 1E-5 ) ) {
299             throw new IllegalArgumentException( "Confidences add up to " + confidence_sum + " instead of 1.0" );
300         }
301         return collapsed;
302     }
303
304     /*
305      * This replaces (by way of example)
306      * A.1.1 0.9
307      * A.1   0.9
308      * with
309      * A.1.1 0.9
310      *
311      * I.e. it removes less specific prefixes.
312      *
313      */
314     private final static List<Prefix> removeLessSpecificPrefixes( final List<Prefix> l ) {
315         final List<Prefix> cleaned = new ArrayList<>();
316         for( final Prefix o : l ) {
317             boolean ok = true;
318             for( final Prefix i : l ) {
319                 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) )
320                         && ForesterUtil.isEqual( i.getConfidence(),
321                                                  o.getConfidence() ) ) {
322                     ok = false;
323                     break;
324                 }
325             }
326             if ( ok ) {
327                 cleaned.add( o );
328             }
329         }
330         return cleaned;
331     }
332
333     private final static void sortPrefixesAccordingToConfidence( final List<Prefix> l ) {
334         Collections.sort( l, new Comparator<Prefix>() {
335
336             @Override
337             public int compare( final Prefix x, final Prefix y ) {
338                 return compare( x.getConfidence(), y.getConfidence() );
339             }
340
341             private int compare( final double a, final double b ) {
342                 return a > b ? -1 : a > b ? 1 : 0;
343             }
344         } );
345     }
346
347     private final static List<Prefix> obtainAllPrefixes( final List<Prefix> greatest_common_prefixes,
348                                                          final String separator ) {
349         final SortedMap<String, Double> map = new TreeMap<>();
350         for( final Prefix prefix : greatest_common_prefixes ) {
351             final List<String> prefixes = ForesterUtil.spliIntoPrefixes( prefix.getPrefix(), separator );
352             for( final String p : prefixes ) {
353                 map.put( p, 0.0 );
354             }
355         }
356         for( final String key : map.keySet() ) {
357             for( final Prefix prefix : greatest_common_prefixes ) {
358                 if ( prefix.getPrefix().startsWith( key ) ) {
359                     map.put( key, map.get( key ) + prefix.getConfidence() );
360                 }
361             }
362         }
363         final List<Prefix> l = new ArrayList<>();
364         for( final Entry<String, Double> entry : map.entrySet() ) {
365             l.add( new Prefix( entry.getKey(), entry.getValue(), separator ) );
366         }
367         return l;
368     }
369 }