2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2017 Christian M. Zmasek
6 // Copyright (C) 2017 J. Craig Venter Institute
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.
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.
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
23 // Contact: phyloxml @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.clade_analysis;
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;
35 import java.util.SortedMap;
36 import java.util.TreeMap;
38 import org.forester.util.ForesterUtil;
40 public final class Result2 {
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 = "";
53 public Result2( final String separator ) {
54 _separator = separator;
58 _separator = ".";//TODO make const somewhere
61 void addWarning( final String warning ) {
62 _warnings.add( warning );
65 void addGreatestCommonPrefix( final String prefix, final double confidence ) {
66 _greatest_common_prefixes.add( new Prefix( prefix, confidence, _separator ) );
69 void setGreatestCommonPrefixUp( final String greatest_common_prefix_up ) {
70 _greatest_common_prefix_up = greatest_common_prefix_up;
73 void setGreatestCommonPrefixDown( final String greatest_common_prefix_down ) {
74 _greatest_common_prefix_down = greatest_common_prefix_down;
77 void setGreatestCommonCladeSubtreeConfidence( final String greatest_common_clade_confidence ) {
78 _greatest_common_clade_subtree_confidence = greatest_common_clade_confidence;
81 void setGreatestCommonCladeUpSubtreeConfidence( final String greatest_common_clade_confidence_up ) {
82 _greatest_common_clade_subtree_confidence_up = greatest_common_clade_confidence_up;
85 void setGreatestCommonCladeDownSubtreeConfidence( final String greatest_common_clade_confidence_down ) {
86 _greatest_common_clade_subtree_confidence_down = greatest_common_clade_confidence_down;
89 // public String getGreatestCommonPrefix() {
90 // return _greatest_common_prefix;
92 public String getGreatestCommonPrefixUp() {
93 return _greatest_common_prefix_up;
96 public String getGreatestCommonPrefixDown() {
97 return _greatest_common_prefix_down;
100 public String getGreatestCommonCladeSubtreeConfidence() {
101 return _greatest_common_clade_subtree_confidence;
104 public String getGreatestCommonCladeUpSubtreeConfidence() {
105 return _greatest_common_clade_subtree_confidence_up;
108 public String getGreatestCommonCladeDownSubtreeConfidence() {
109 return _greatest_common_clade_subtree_confidence_down;
112 public List<String> getWarnings() {
116 void setLeastEncompassingCladeSize( final int lec_ext_nodes ) {
117 _lec_ext_nodes = lec_ext_nodes;
120 void setTreeSize( final int p_ext_nodes ) {
121 _p_ext_nodes = p_ext_nodes;
124 public int getLeastEncompassingCladeSize() {
125 return _lec_ext_nodes;
128 public int getTreeSize() {
132 public void analyzeGreatestCommonPrefixes() {
133 analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator, 0.3 );
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 );
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 );
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 );
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 );
166 System.out.println( "------" );
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() );
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 );
184 for( final Prefix o : spec ) {
186 for( final Prefix i : spec ) {
187 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) ) ) {
193 //System.out.println(">" + o );
194 cleaned_spec.add( o );
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 ) ) {
208 collapsed.add( prefix );
209 confidence_sum += prefix.getConfidence();
212 if ( !ForesterUtil.isEqual( confidence_sum, 1.0 ) ) {
213 throw new IllegalArgumentException( "Confidences add up to " + confidence_sum + " instead of 1.0" );
219 * This replaces (by way of example)
225 * I.e. it removes less specific prefixes.
228 private final static List<Prefix> removeLessSpecificPrefixes( final List<Prefix> l ) {
229 final List<Prefix> cleaned = new ArrayList<>();
230 for( final Prefix o : l ) {
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() ) ) {
247 private static void sortPrefixesAccordingToConfidence( final List<Prefix> l ) {
248 Collections.sort( l, new Comparator<Prefix>() {
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);
258 private int compare( final double a, final double b ) {
259 return a > b ? -1 : a > b ? 1 : 0;
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 ) {
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() );
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 ) );