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