1dc9f0e0071b72df20106d7a8e95e6f635a95596
[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   
70
71     void setGreatestCommonPrefixUp( final String greatest_common_prefix_up ) {
72         _greatest_common_prefix_up = greatest_common_prefix_up;
73     }
74
75     void setGreatestCommonPrefixDown( final String greatest_common_prefix_down ) {
76         _greatest_common_prefix_down = greatest_common_prefix_down;
77     }
78
79     void setGreatestCommonCladeSubtreeConfidence( final String greatest_common_clade_confidence ) {
80         _greatest_common_clade_subtree_confidence = greatest_common_clade_confidence;
81     }
82
83     void setGreatestCommonCladeUpSubtreeConfidence( final String greatest_common_clade_confidence_up ) {
84         _greatest_common_clade_subtree_confidence_up = greatest_common_clade_confidence_up;
85     }
86
87     void setGreatestCommonCladeDownSubtreeConfidence( final String greatest_common_clade_confidence_down ) {
88         _greatest_common_clade_subtree_confidence_down = greatest_common_clade_confidence_down;
89     }
90
91     //  public String getGreatestCommonPrefix() {
92     //      return _greatest_common_prefix;
93     //  }
94     public String getGreatestCommonPrefixUp() {
95         return _greatest_common_prefix_up;
96     }
97
98     public String getGreatestCommonPrefixDown() {
99         return _greatest_common_prefix_down;
100     }
101
102     public String getGreatestCommonCladeSubtreeConfidence() {
103         return _greatest_common_clade_subtree_confidence;
104     }
105
106     public String getGreatestCommonCladeUpSubtreeConfidence() {
107         return _greatest_common_clade_subtree_confidence_up;
108     }
109
110     public String getGreatestCommonCladeDownSubtreeConfidence() {
111         return _greatest_common_clade_subtree_confidence_down;
112     }
113
114     public List<String> getWarnings() {
115         return _warnings;
116     }
117
118     void setLeastEncompassingCladeSize( final int lec_ext_nodes ) {
119         _lec_ext_nodes = lec_ext_nodes;
120     }
121
122     void setTreeSize( final int p_ext_nodes ) {
123         _p_ext_nodes = p_ext_nodes;
124     }
125
126     public int getLeastEncompassingCladeSize() {
127         return _lec_ext_nodes;
128     }
129
130     public int getTreeSize() {
131         return _p_ext_nodes;
132     }
133     
134     public void analyzeGreatestCommonPrefixes( ) {
135         analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator );
136     }
137
138     public final static void analyzeGreatestCommonPrefixes( List<Prefix> greatest_common_prefixes, final String separator ) {
139         final SortedMap<String, Double> map = new TreeMap<>();
140         for( final Prefix prefix : greatest_common_prefixes ) {
141             final List<String> prefixes = ForesterUtil.spliIntoPrefixes( prefix.getPrefix(), separator );
142             for( final String p : prefixes ) {
143                 map.put( p, 0.0 );
144             }
145         }
146         // System.out.println( map );
147         for( final String key : map.keySet() ) {
148             //System.out.println(key);
149             for( final Prefix prefix : greatest_common_prefixes ) {
150                 if ( prefix.getPrefix().startsWith( key ) ) {
151                     map.put( key, map.get( key ) + prefix.getConfidence() );
152                 }
153             }
154         }
155         //System.out.println( map );
156         final List<Prefix> l = new ArrayList<>();
157         for( final Entry<String, Double> entry : map.entrySet() ) {
158             // System.out.println( entry.getKey() + "->" + entry.getValue() );
159             l.add( new Prefix( entry.getKey(), entry.getValue(), separator ) );
160         }
161         Collections.sort( l, new Comparator<Prefix>() {
162
163             @Override
164             public int compare( final Prefix x, final Prefix y ) {
165                 final int start_comparison = compare( x.getConfidence(), y.getConfidence() );
166                 return start_comparison;
167                 //return startComparison != 0 ? startComparison
168                 //                            : compare(x.timeEnded, y.timeEnded);
169             }
170
171             private int compare( final double a, final double b ) {
172                 return a > b ? -1 : a > b ? 1 : 0;
173             }
174         } );
175         System.out.println();
176         for( final Prefix prefix : l ) {
177             // System.out.println( prefix );
178         }
179         final List<Prefix> cleaned = new ArrayList<>();
180         for( final Prefix o : l ) {
181             boolean ok = true;
182             for( final Prefix i : l ) {
183                 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) )
184                         && ForesterUtil.isEqual( i.getConfidence(),
185                                                  o.getConfidence() ) ) {
186                     ok = false;
187                     break;
188                 }
189             }
190             if ( ok ) {
191                 cleaned.add( o );
192             }
193         }
194         System.out.println();
195         for( final Prefix prefix : cleaned ) {
196             System.out.println( prefix );
197         }
198         final List<Prefix> collapsed = new ArrayList<>();
199         final Set<String> firsts = new HashSet<>();
200         double confidence_sum = 0;
201         for( final Prefix prefix : cleaned ) {
202             final String f = prefix.getPrefixFirstElement();
203             if ( !firsts.contains( f ) ) {
204                 firsts.add( f );
205                 collapsed.add( prefix );
206                 confidence_sum += prefix.getConfidence();
207             }
208         }
209         if ( !ForesterUtil.isEqual( confidence_sum, 1.0 ) ) {
210             throw new IllegalArgumentException( "Confidences add up to " + confidence_sum + " instead of 1.0" );
211         }
212         System.out.println();
213         for( final Prefix prefix : collapsed ) {
214             System.out.println( prefix );
215         }
216     }
217 }