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 ResultMulti {
42 private final String _separator;
43 private final List<Prefix> _greatest_common_prefixes = new ArrayList<>();
44 private final List<Prefix> _greatest_common_prefixes_up = new ArrayList<>();
45 private final List<Prefix> _greatest_common_prefixes_down = new ArrayList<>();
46 private List<Prefix> _all = null;
47 private List<Prefix> _collapsed = null;
48 private List<Prefix> _cleaned_spec = null;
49 private boolean _has_specifics = false;
50 private List<Prefix> _all_up = null;
51 private List<Prefix> _collapsed_up = null;
52 private List<Prefix> _cleaned_spec_up = null;
53 private boolean _has_specifics_up = false;
54 private List<Prefix> _all_down = null;
55 private List<Prefix> _collapsed_down = null;
56 private List<Prefix> _cleaned_spec_down = null;
57 private boolean _has_specifics_down = false;
59 ResultMulti( final String separator ) {
60 _separator = separator;
65 _separator = AnalysisMulti.DEFAULT_SEPARATOR;
69 public List<Prefix> getAllMultiHitPrefixesUp() {
73 public List<Prefix> getCollapsedMultiHitPrefixesUp() {
77 public List<Prefix> getSpecificMultiHitPrefixesUp() {
78 return _cleaned_spec_up;
81 public boolean isHasSpecificMultiHitsPrefixesUp() {
82 return _has_specifics_up;
85 public List<Prefix> getAllMultiHitPrefixesDown() {
89 public List<Prefix> getCollapsedMultiHitPrefixesDown() {
90 return _collapsed_down;
93 public List<Prefix> getSpecificMultiHitPrefixesDown() {
94 return _cleaned_spec_down;
97 public boolean isHasSpecificMultiHitsPrefixesDown() {
98 return _has_specifics_down;
101 public List<Prefix> getAllMultiHitPrefixes() {
105 public List<Prefix> getCollapsedMultiHitPrefixes() {
109 public List<Prefix> getSpecificMultiHitPrefixes() {
110 return _cleaned_spec;
113 public boolean isHasSpecificMultiHitsPrefixes() {
114 return _has_specifics;
118 public final String toString() {
119 final StringBuilder sb = new StringBuilder();
120 sb.append( "Matching Clade(s):" );
121 sb.append( ForesterUtil.LINE_SEPARATOR );
122 for( final Prefix prefix : _collapsed ) {
124 sb.append( ForesterUtil.LINE_SEPARATOR );
126 if ( _has_specifics ) {
127 sb.append( ForesterUtil.LINE_SEPARATOR );
128 sb.append( "Specific-hit(s):" );
129 sb.append( ForesterUtil.LINE_SEPARATOR );
130 for( final Prefix prefix : _cleaned_spec ) {
132 sb.append( ForesterUtil.LINE_SEPARATOR );
134 sb.append( ForesterUtil.LINE_SEPARATOR );
135 sb.append( "Matching Clade(s) with Specific-hit(s):" );
136 sb.append( ForesterUtil.LINE_SEPARATOR );
137 for( final Prefix prefix : _collapsed ) {
139 sb.append( ForesterUtil.LINE_SEPARATOR );
140 for( final Prefix spec : _cleaned_spec ) {
141 if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
142 sb.append( " " + spec );
143 sb.append( ForesterUtil.LINE_SEPARATOR );
148 if ( !ForesterUtil.isEmpty( _all_down ) ) {
149 sb.append( ForesterUtil.LINE_SEPARATOR );
150 sb.append( "Matching Down-tree Bracketing Clade(s):" );
151 sb.append( ForesterUtil.LINE_SEPARATOR );
152 for( final Prefix prefix : _collapsed_down ) {
154 sb.append( ForesterUtil.LINE_SEPARATOR );
157 if ( !ForesterUtil.isEmpty( _all_up ) ) {
158 sb.append( ForesterUtil.LINE_SEPARATOR );
159 sb.append( "Matching Up-tree Bracketing Clade(s):" );
160 sb.append( ForesterUtil.LINE_SEPARATOR );
161 for( final Prefix prefix : _collapsed_up ) {
163 sb.append( ForesterUtil.LINE_SEPARATOR );
166 return sb.toString();
169 void addGreatestCommonPrefix( final String prefix, final double confidence ) {
170 _greatest_common_prefixes.add( new Prefix( prefix, confidence, _separator ) );
173 void addGreatestCommonPrefixUp( final String prefix_up, final double confidence ) {
174 _greatest_common_prefixes_up.add( new Prefix( prefix_up, confidence, _separator ) );
177 void addGreatestCommonPrefixDown( final String prefix_down, final double confidence ) {
178 _greatest_common_prefixes_down.add( new Prefix( prefix_down, confidence, _separator ) );
181 final void analyze( final double cutoff_for_specifics ) {
183 analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator, cutoff_for_specifics );
184 analyzeGreatestCommonPrefixesUp( _greatest_common_prefixes_up, _separator, cutoff_for_specifics );
185 analyzeGreatestCommonPrefixesDown( _greatest_common_prefixes_down, _separator, cutoff_for_specifics );
188 private final void reset() {
189 _all = new ArrayList<>();
190 _collapsed = new ArrayList<>();
191 _cleaned_spec = new ArrayList<>();
192 _has_specifics = false;
193 _all_up = new ArrayList<>();
194 _collapsed_up = new ArrayList<>();
195 _cleaned_spec_up = new ArrayList<>();
196 _has_specifics_up = false;
197 _all_down = new ArrayList<>();
198 _collapsed_down = new ArrayList<>();
199 _cleaned_spec_down = new ArrayList<>();
200 _has_specifics_down = false;
203 private final void analyzeGreatestCommonPrefixes( final List<Prefix> greatest_common_prefixes,
204 final String separator,
205 final double cutoff ) {
206 final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes, separator );
207 if ( !ForesterUtil.isEmpty( l ) ) {
208 sortPrefixesAccordingToConfidence( l );
209 _all = removeLessSpecificPrefixes( l );
210 _collapsed = collapse( _all );
211 _has_specifics = false;
213 _cleaned_spec = obtainSpecifics( cutoff, _all, _collapsed );
214 if ( !ForesterUtil.isEmpty( _cleaned_spec ) ) {
215 _has_specifics = true;
221 private final void analyzeGreatestCommonPrefixesUp( final List<Prefix> greatest_common_prefixes_up,
222 final String separator,
223 final double cutoff ) {
224 final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_up, separator );
225 if ( !ForesterUtil.isEmpty( l ) ) {
226 sortPrefixesAccordingToConfidence( l );
227 _all_up = removeLessSpecificPrefixes( l );
228 _collapsed_up = collapse( _all_up );
229 _has_specifics_up = false;
231 _cleaned_spec_up = obtainSpecifics( cutoff, _all_up, _collapsed_up );
232 if ( !ForesterUtil.isEmpty( _cleaned_spec_up ) ) {
233 _has_specifics_up = true;
239 final void analyzeGreatestCommonPrefixesDown( final List<Prefix> greatest_common_prefixes_down,
240 final String separator,
241 final double cutoff ) {
242 final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_down, separator );
243 if ( !ForesterUtil.isEmpty( l ) ) {
244 sortPrefixesAccordingToConfidence( l );
245 _all_down = removeLessSpecificPrefixes( l );
246 _collapsed_down = collapse( _all_down );
247 _has_specifics_down = false;
249 _cleaned_spec_down = obtainSpecifics( cutoff, _all_down, _collapsed_down );
250 if ( !ForesterUtil.isEmpty( _cleaned_spec_down ) ) {
251 _has_specifics_down = true;
257 final static List<Prefix> obtainSpecifics( final double cutoff,
258 final List<Prefix> cleaned,
259 final List<Prefix> collapsed ) {
260 final List<Prefix> cleaned_spec = new ArrayList<>();
261 final Set<String> collapsed_set = new HashSet<>();
262 for( final Prefix prefix : collapsed ) {
263 collapsed_set.add( prefix.getPrefix() );
265 final List<Prefix> spec = new ArrayList<>();
266 for( final Prefix prefix : cleaned ) {
267 if ( ( prefix.getConfidence() >= cutoff ) && !collapsed_set.contains( prefix.getPrefix() ) ) {
271 for( final Prefix o : spec ) {
273 for( final Prefix i : spec ) {
274 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) ) ) {
280 cleaned_spec.add( o );
286 private final static List<Prefix> collapse( final List<Prefix> cleaned ) {
287 final List<Prefix> collapsed = new ArrayList<>();
288 final Set<String> firsts = new HashSet<>();
289 double confidence_sum = 0;
290 for( final Prefix prefix : cleaned ) {
291 final String f = prefix.getPrefixFirstElement();
292 if ( !firsts.contains( f ) ) {
294 collapsed.add( prefix );
295 confidence_sum += prefix.getConfidence();
298 if ( !ForesterUtil.isEqual( confidence_sum, 1.0, 1E-5 ) ) {
299 throw new IllegalArgumentException( "Confidences add up to " + confidence_sum + " instead of 1.0" );
305 * This replaces (by way of example)
311 * I.e. it removes less specific prefixes.
314 private final static List<Prefix> removeLessSpecificPrefixes( final List<Prefix> l ) {
315 final List<Prefix> cleaned = new ArrayList<>();
316 for( final Prefix o : l ) {
318 for( final Prefix i : l ) {
319 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) )
320 && ForesterUtil.isEqual( i.getConfidence(),
321 o.getConfidence() ) ) {
333 private final static void sortPrefixesAccordingToConfidence( final List<Prefix> l ) {
334 Collections.sort( l, new Comparator<Prefix>() {
337 public int compare( final Prefix x, final Prefix y ) {
338 return compare( x.getConfidence(), y.getConfidence() );
341 private int compare( final double a, final double b ) {
342 return a > b ? -1 : a > b ? 1 : 0;
347 private final static List<Prefix> obtainAllPrefixes( final List<Prefix> greatest_common_prefixes,
348 final String separator ) {
349 final SortedMap<String, Double> map = new TreeMap<>();
350 for( final Prefix prefix : greatest_common_prefixes ) {
351 final List<String> prefixes = ForesterUtil.spliIntoPrefixes( prefix.getPrefix(), separator );
352 for( final String p : prefixes ) {
356 for( final String key : map.keySet() ) {
357 for( final Prefix prefix : greatest_common_prefixes ) {
358 if ( prefix.getPrefix().startsWith( key ) ) {
359 map.put( key, map.get( key ) + prefix.getConfidence() );
363 final List<Prefix> l = new ArrayList<>();
364 for( final Entry<String, Double> entry : map.entrySet() ) {
365 l.add( new Prefix( entry.getKey(), entry.getValue(), separator ) );