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