1cfc3ff24b927eb0f248058c0898cf9f3219617f
[jalview.git] / forester / java / src / org / forester / clade_analysis / Result2.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 Result2 {
41
42     private final String       _separator;
43     private final List<Prefix> _greatest_common_prefixes                      = new ArrayList<>();
44     private String             _greatest_common_prefix_up                     = "";
45     private String             _greatest_common_prefix_down                   = "";
46     private final List<String> _warnings                                      = new ArrayList<>();
47     private int                _lec_ext_nodes                                 = 0;
48     private int                _p_ext_nodes                                   = 0;
49     private String             _greatest_common_clade_subtree_confidence      = "";
50     private String             _greatest_common_clade_subtree_confidence_up   = "";
51     private String             _greatest_common_clade_subtree_confidence_down = "";
52     private List<Prefix>       _all                                           = null;
53     private List<Prefix>       _collapsed                                     = null;
54     private List<Prefix>       _cleaned_spec                                  = null;
55     private boolean            _has_specifics;
56
57     public Result2( final String separator ) {
58         _separator = separator;
59     }
60
61     public Result2() {
62         _separator = ".";//TODO make const somewhere
63     }
64
65     public List<Prefix> getAllMultiHitPrefixes() {
66         return _all;
67     }
68     
69     public List<Prefix> getCollapsedMultiHitPrefixes() {
70         return _collapsed;
71     }
72     
73     public List<Prefix> getSpecificMultiHitPrefixes() {
74         return _cleaned_spec;
75     }
76     
77     public boolean isHasSpecificMultiHitsPrefixes() {
78         return _has_specifics;
79     }
80     
81     
82     void addWarning( final String warning ) {
83         _warnings.add( warning );
84     }
85
86     void addGreatestCommonPrefix( final String prefix, final double confidence ) {
87         _greatest_common_prefixes.add( new Prefix( prefix, confidence, _separator ) );
88     }
89
90     void setGreatestCommonPrefixUp( final String greatest_common_prefix_up ) {
91         _greatest_common_prefix_up = greatest_common_prefix_up;
92     }
93
94     void setGreatestCommonPrefixDown( final String greatest_common_prefix_down ) {
95         _greatest_common_prefix_down = greatest_common_prefix_down;
96     }
97
98     void setGreatestCommonCladeSubtreeConfidence( final String greatest_common_clade_confidence ) {
99         _greatest_common_clade_subtree_confidence = greatest_common_clade_confidence;
100     }
101
102     void setGreatestCommonCladeUpSubtreeConfidence( final String greatest_common_clade_confidence_up ) {
103         _greatest_common_clade_subtree_confidence_up = greatest_common_clade_confidence_up;
104     }
105
106     void setGreatestCommonCladeDownSubtreeConfidence( final String greatest_common_clade_confidence_down ) {
107         _greatest_common_clade_subtree_confidence_down = greatest_common_clade_confidence_down;
108     }
109
110     //  public String getGreatestCommonPrefix() {
111     //      return _greatest_common_prefix;
112     //  }
113     public String getGreatestCommonPrefixUp() {
114         return _greatest_common_prefix_up;
115     }
116
117     public String getGreatestCommonPrefixDown() {
118         return _greatest_common_prefix_down;
119     }
120
121     public String getGreatestCommonCladeSubtreeConfidence() {
122         return _greatest_common_clade_subtree_confidence;
123     }
124
125     public String getGreatestCommonCladeUpSubtreeConfidence() {
126         return _greatest_common_clade_subtree_confidence_up;
127     }
128
129     public String getGreatestCommonCladeDownSubtreeConfidence() {
130         return _greatest_common_clade_subtree_confidence_down;
131     }
132
133     public List<String> getWarnings() {
134         return _warnings;
135     }
136
137     void setLeastEncompassingCladeSize( final int lec_ext_nodes ) {
138         _lec_ext_nodes = lec_ext_nodes;
139     }
140
141     void setTreeSize( final int p_ext_nodes ) {
142         _p_ext_nodes = p_ext_nodes;
143     }
144
145     public int getLeastEncompassingCladeSize() {
146         return _lec_ext_nodes;
147     }
148
149     public int getTreeSize() {
150         return _p_ext_nodes;
151     }
152
153     public void analyzeGreatestCommonPrefixes( final double cutoff ) {
154         analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator, cutoff );
155     }
156
157     public void analyzeGreatestCommonPrefixes() {
158         analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator, -1 );
159     }
160
161     private final void analyzeGreatestCommonPrefixes( final List<Prefix> greatest_common_prefixes,
162                                                       final String separator,
163                                                       final double cutoff ) {
164         final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes, separator );
165         sortPrefixesAccordingToConfidence( l );
166         _all = removeLessSpecificPrefixes( l );
167         _collapsed = collapse( _all );
168         _has_specifics = false;
169         if ( cutoff >= 0 ) {
170             _cleaned_spec = obtainSpecifics( cutoff, _all, _collapsed );
171             if ( _cleaned_spec != null && _cleaned_spec.size() > 0 ) {
172                 _has_specifics = true;
173             }
174         }
175         else {
176             _cleaned_spec = null;
177         }
178     }
179
180     private final static List<Prefix> obtainSpecifics( final double cutoff,
181                                                        final List<Prefix> cleaned,
182                                                        final List<Prefix> collapsed ) {
183         final List<Prefix> cleaned_spec = new ArrayList<>();
184         final Set<String> collapsed_set = new HashSet<>();
185         for( final Prefix prefix : collapsed ) {
186             collapsed_set.add( prefix.getPrefix() );
187         }
188         final List<Prefix> spec = new ArrayList<>();
189         for( final Prefix prefix : cleaned ) {
190             if ( ( prefix.getConfidence() >= cutoff ) && !collapsed_set.contains( prefix.getPrefix() ) ) {
191                 spec.add( prefix );
192             }
193         }
194         for( final Prefix o : spec ) {
195             boolean ok = true;
196             for( final Prefix i : spec ) {
197                 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) ) ) {
198                     ok = false;
199                     break;
200                 }
201             }
202             if ( ok ) {
203                 cleaned_spec.add( o );
204             }
205         }
206         return cleaned_spec;
207     }
208
209     private final static List<Prefix> collapse( final List<Prefix> cleaned ) {
210         final List<Prefix> collapsed = new ArrayList<>();
211         final Set<String> firsts = new HashSet<>();
212         double confidence_sum = 0;
213         for( final Prefix prefix : cleaned ) {
214             final String f = prefix.getPrefixFirstElement();
215             if ( !firsts.contains( f ) ) {
216                 firsts.add( f );
217                 collapsed.add( prefix );
218                 confidence_sum += prefix.getConfidence();
219             }
220         }
221         if ( !ForesterUtil.isEqual( confidence_sum, 1.0, 1E-5 ) ) {
222             throw new IllegalArgumentException( "Confidences add up to " + confidence_sum + " instead of 1.0" );
223         }
224         return collapsed;
225     }
226
227     /*
228      * This replaces (by way of example)
229      * A.1.1 0.9
230      * A.1   0.9
231      * with
232      * A.1.1 0.9
233      *
234      * I.e. it removes less specific prefixes.
235      *
236      */
237     private final static List<Prefix> removeLessSpecificPrefixes( final List<Prefix> l ) {
238         final List<Prefix> cleaned = new ArrayList<>();
239         for( final Prefix o : l ) {
240             boolean ok = true;
241             for( final Prefix i : l ) {
242                 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) )
243                         && ForesterUtil.isEqual( i.getConfidence(),
244                                                  o.getConfidence() ) ) {
245                     ok = false;
246                     break;
247                 }
248             }
249             if ( ok ) {
250                 cleaned.add( o );
251             }
252         }
253         return cleaned;
254     }
255
256     private static void sortPrefixesAccordingToConfidence( final List<Prefix> l ) {
257         Collections.sort( l, new Comparator<Prefix>() {
258
259             @Override
260             public int compare( final Prefix x, final Prefix y ) {
261                 return compare( x.getConfidence(), y.getConfidence() );
262             }
263
264             private int compare( final double a, final double b ) {
265                 return a > b ? -1 : a > b ? 1 : 0;
266             }
267         } );
268     }
269
270     private final static List<Prefix> obtainAllPrefixes( final List<Prefix> greatest_common_prefixes,
271                                                          final String separator ) {
272         final SortedMap<String, Double> map = new TreeMap<>();
273         for( final Prefix prefix : greatest_common_prefixes ) {
274             final List<String> prefixes = ForesterUtil.spliIntoPrefixes( prefix.getPrefix(), separator );
275             for( final String p : prefixes ) {
276                 map.put( p, 0.0 );
277             }
278         }
279         for( final String key : map.keySet() ) {
280             for( final Prefix prefix : greatest_common_prefixes ) {
281                 if ( prefix.getPrefix().startsWith( key ) ) {
282                     map.put( key, map.get( key ) + prefix.getConfidence() );
283                 }
284             }
285         }
286         final List<Prefix> l = new ArrayList<>();
287         for( final Entry<String, Double> entry : map.entrySet() ) {
288             l.add( new Prefix( entry.getKey(), entry.getValue(), separator ) );
289         }
290         return l;
291     }
292
293     public final String toString() {
294         final StringBuilder sb = new StringBuilder();
295         //TODO add all other stuff
296         sb.append( "Cleaned:" );
297         sb.append( ForesterUtil.LINE_SEPARATOR );
298         for( final Prefix prefix : _all ) {
299             sb.append( prefix );
300             sb.append( ForesterUtil.LINE_SEPARATOR );
301         }
302         sb.append( ForesterUtil.LINE_SEPARATOR );
303         sb.append( "Collapsed:" );
304         sb.append( ForesterUtil.LINE_SEPARATOR );
305         for( final Prefix prefix : _collapsed ) {
306             sb.append( prefix );
307             sb.append( ForesterUtil.LINE_SEPARATOR );
308         }
309         if ( _has_specifics ) {
310             sb.append( ForesterUtil.LINE_SEPARATOR );
311             sb.append( "Specifics:" );
312             sb.append( ForesterUtil.LINE_SEPARATOR );
313             for( final Prefix prefix : _cleaned_spec ) {
314                 sb.append( prefix );
315                 sb.append( ForesterUtil.LINE_SEPARATOR );
316             }
317             sb.append( ForesterUtil.LINE_SEPARATOR );
318             sb.append( "Collapsed with specifics:" );
319             sb.append( ForesterUtil.LINE_SEPARATOR );
320             for( final Prefix prefix : _collapsed ) {
321                 sb.append( prefix );
322                 sb.append( ForesterUtil.LINE_SEPARATOR );
323                 for( final Prefix spec : _cleaned_spec ) {
324                     if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
325                         sb.append( "    " + spec );
326                         sb.append( ForesterUtil.LINE_SEPARATOR );
327                     }
328                 }
329             }
330         }
331         return sb.toString();
332     }
333 }