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