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;
39 import org.forester.util.UserException;
41 public final class ResultMulti {
43 private final String _separator;
44 private final List<Prefix> _greatest_common_prefixes = new ArrayList<>();
45 private final List<Prefix> _greatest_common_prefixes_up = new ArrayList<>();
46 private final List<Prefix> _greatest_common_prefixes_down = new ArrayList<>();
47 private List<Prefix> _all = null;
48 private List<Prefix> _collapsed = null;
49 private List<Prefix> _cleaned_spec = null;
50 private boolean _has_specifics = false;
51 private List<Prefix> _all_up = null;
52 private List<Prefix> _collapsed_up = null;
53 private List<Prefix> _cleaned_spec_up = null;
54 private boolean _has_specifics_up = false;
55 private List<Prefix> _all_down = null;
56 private List<Prefix> _collapsed_down = null;
57 private List<Prefix> _cleaned_spec_down = null;
58 private boolean _has_specifics_down = false;
59 private String _query_name_prefix = "";
61 ResultMulti( final String separator ) {
62 _separator = separator;
67 _separator = AnalysisMulti.DEFAULT_SEPARATOR;
71 public List<Prefix> getAllMultiHitPrefixesUp() {
75 public List<Prefix> getCollapsedMultiHitPrefixesUp() {
79 public List<Prefix> getSpecificMultiHitPrefixesUp() {
80 return _cleaned_spec_up;
83 public boolean isHasSpecificMultiHitsPrefixesUp() {
84 return _has_specifics_up;
87 public List<Prefix> getAllMultiHitPrefixesDown() {
91 public List<Prefix> getCollapsedMultiHitPrefixesDown() {
92 return _collapsed_down;
95 public List<Prefix> getSpecificMultiHitPrefixesDown() {
96 return _cleaned_spec_down;
99 public boolean isHasSpecificMultiHitsPrefixesDown() {
100 return _has_specifics_down;
103 public List<Prefix> getAllMultiHitPrefixes() {
107 public List<Prefix> getCollapsedMultiHitPrefixes() {
111 public List<Prefix> getSpecificMultiHitPrefixes() {
112 return _cleaned_spec;
115 public boolean isHasSpecificMultiHitsPrefixes() {
116 return _has_specifics;
119 public String getQueryNamePrefix() {
120 return _query_name_prefix;
124 public final String toString() {
125 final StringBuilder sb = new StringBuilder();
126 sb.append( "Query: " );
127 sb.append( getQueryNamePrefix() );
128 sb.append( ForesterUtil.LINE_SEPARATOR );
129 sb.append( "Matching Clade(s):" );
130 sb.append( ForesterUtil.LINE_SEPARATOR );
131 for( final Prefix prefix : _collapsed ) {
133 sb.append( ForesterUtil.LINE_SEPARATOR );
135 if ( _has_specifics ) {
136 sb.append( ForesterUtil.LINE_SEPARATOR );
137 sb.append( "Specific-hit(s):" );
138 sb.append( ForesterUtil.LINE_SEPARATOR );
139 for( final Prefix prefix : _cleaned_spec ) {
141 sb.append( ForesterUtil.LINE_SEPARATOR );
143 sb.append( ForesterUtil.LINE_SEPARATOR );
144 sb.append( "Matching Clade(s) with Specific-hit(s):" );
145 sb.append( ForesterUtil.LINE_SEPARATOR );
146 for( final Prefix prefix : _collapsed ) {
148 sb.append( ForesterUtil.LINE_SEPARATOR );
149 for( final Prefix spec : _cleaned_spec ) {
150 if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
151 sb.append( " " + spec );
152 sb.append( ForesterUtil.LINE_SEPARATOR );
157 if ( !ForesterUtil.isEmpty( _all_down ) ) {
158 sb.append( ForesterUtil.LINE_SEPARATOR );
159 sb.append( "Matching Down-tree Bracketing Clade(s):" );
160 sb.append( ForesterUtil.LINE_SEPARATOR );
161 for( final Prefix prefix : _collapsed_down ) {
163 sb.append( ForesterUtil.LINE_SEPARATOR );
166 if ( !ForesterUtil.isEmpty( _all_up ) ) {
167 sb.append( ForesterUtil.LINE_SEPARATOR );
168 sb.append( "Matching Up-tree Bracketing Clade(s):" );
169 sb.append( ForesterUtil.LINE_SEPARATOR );
170 for( final Prefix prefix : _collapsed_up ) {
172 sb.append( ForesterUtil.LINE_SEPARATOR );
175 return sb.toString();
178 void addGreatestCommonPrefix( final String prefix, final double confidence ) {
179 _greatest_common_prefixes.add( new Prefix( prefix, confidence, _separator ) );
182 void addGreatestCommonPrefixUp( final String prefix_up, final double confidence ) {
183 _greatest_common_prefixes_up.add( new Prefix( prefix_up, confidence, _separator ) );
186 void addGreatestCommonPrefixDown( final String prefix_down, final double confidence ) {
187 _greatest_common_prefixes_down.add( new Prefix( prefix_down, confidence, _separator ) );
190 void setQueryNamePrefix( final String query_name_prefix ) {
191 if ( !ForesterUtil.isEmpty( _query_name_prefix ) ) {
192 throw new IllegalStateException( "illegal attempt to change the query name prefix" );
194 _query_name_prefix = query_name_prefix;
197 final void analyze( final double cutoff_for_specifics ) throws UserException {
199 analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator, cutoff_for_specifics );
200 analyzeGreatestCommonPrefixesUp( _greatest_common_prefixes_up, _separator, cutoff_for_specifics );
201 analyzeGreatestCommonPrefixesDown( _greatest_common_prefixes_down, _separator, cutoff_for_specifics );
204 private final void reset() {
205 _all = new ArrayList<>();
206 _collapsed = new ArrayList<>();
207 _cleaned_spec = new ArrayList<>();
208 _has_specifics = false;
209 _all_up = new ArrayList<>();
210 _collapsed_up = new ArrayList<>();
211 _cleaned_spec_up = new ArrayList<>();
212 _has_specifics_up = false;
213 _all_down = new ArrayList<>();
214 _collapsed_down = new ArrayList<>();
215 _cleaned_spec_down = new ArrayList<>();
216 _has_specifics_down = false;
219 private final void analyzeGreatestCommonPrefixes( final List<Prefix> greatest_common_prefixes,
220 final String separator,
221 final double cutoff )
222 throws UserException {
223 final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes, separator );
224 if ( !ForesterUtil.isEmpty( l ) ) {
225 sortPrefixesAccordingToConfidence( l );
226 _all = removeLessSpecificPrefixes( l );
227 _collapsed = collapse( _all );
228 _has_specifics = false;
230 _cleaned_spec = obtainSpecifics( cutoff, _all, _collapsed );
231 if ( !ForesterUtil.isEmpty( _cleaned_spec ) ) {
232 _has_specifics = true;
238 private final void analyzeGreatestCommonPrefixesUp( final List<Prefix> greatest_common_prefixes_up,
239 final String separator,
240 final double cutoff )
241 throws UserException {
242 final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_up, separator );
243 if ( !ForesterUtil.isEmpty( l ) ) {
244 sortPrefixesAccordingToConfidence( l );
245 _all_up = removeLessSpecificPrefixes( l );
246 _collapsed_up = collapse( _all_up );
247 _has_specifics_up = false;
249 _cleaned_spec_up = obtainSpecifics( cutoff, _all_up, _collapsed_up );
250 if ( !ForesterUtil.isEmpty( _cleaned_spec_up ) ) {
251 _has_specifics_up = true;
257 final void analyzeGreatestCommonPrefixesDown( final List<Prefix> greatest_common_prefixes_down,
258 final String separator,
259 final double cutoff )
260 throws UserException {
261 final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_down, separator );
262 if ( !ForesterUtil.isEmpty( l ) ) {
263 sortPrefixesAccordingToConfidence( l );
264 _all_down = removeLessSpecificPrefixes( l );
265 _collapsed_down = collapse( _all_down );
266 _has_specifics_down = false;
268 _cleaned_spec_down = obtainSpecifics( cutoff, _all_down, _collapsed_down );
269 if ( !ForesterUtil.isEmpty( _cleaned_spec_down ) ) {
270 _has_specifics_down = true;
276 final static List<Prefix> obtainSpecifics( final double cutoff,
277 final List<Prefix> cleaned,
278 final List<Prefix> collapsed ) {
279 final List<Prefix> cleaned_spec = new ArrayList<>();
280 final Set<String> collapsed_set = new HashSet<>();
281 for( final Prefix prefix : collapsed ) {
282 collapsed_set.add( prefix.getPrefix() );
284 final List<Prefix> spec = new ArrayList<>();
285 for( final Prefix prefix : cleaned ) {
286 if ( ( prefix.getConfidence() >= cutoff ) && !collapsed_set.contains( prefix.getPrefix() ) ) {
290 for( final Prefix o : spec ) {
292 for( final Prefix i : spec ) {
293 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) ) ) {
299 cleaned_spec.add( o );
305 private final static List<Prefix> collapse( final List<Prefix> cleaned ) throws UserException {
306 final List<Prefix> collapsed = new ArrayList<>();
307 final Set<String> firsts = new HashSet<>();
308 double confidence_sum = 0;
309 for( final Prefix prefix : cleaned ) {
310 final String f = prefix.getPrefixFirstElement();
311 if ( !firsts.contains( f ) ) {
313 collapsed.add( prefix );
314 confidence_sum += prefix.getConfidence();
317 if ( !ForesterUtil.isEqual( confidence_sum, 1.0, 1E-5 ) ) {
318 throw new UserException( "confidences add up to " + confidence_sum + " instead of 1.0" );
324 * This replaces (by way of example)
330 * I.e. it removes less specific prefixes.
333 private final static List<Prefix> removeLessSpecificPrefixes( final List<Prefix> l ) {
334 final List<Prefix> cleaned = new ArrayList<>();
335 for( final Prefix o : l ) {
337 for( final Prefix i : l ) {
338 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) )
339 && ForesterUtil.isEqual( i.getConfidence(),
340 o.getConfidence() ) ) {
352 private final static void sortPrefixesAccordingToConfidence( final List<Prefix> l ) {
353 Collections.sort( l, new Comparator<Prefix>() {
356 public int compare( final Prefix x, final Prefix y ) {
357 return compare( x.getConfidence(), y.getConfidence() );
360 private int compare( final double a, final double b ) {
361 return a > b ? -1 : a > b ? 1 : 0;
366 private final static List<Prefix> obtainAllPrefixes( final List<Prefix> greatest_common_prefixes,
367 final String separator ) {
368 final SortedMap<String, Double> map = new TreeMap<>();
369 for( final Prefix prefix : greatest_common_prefixes ) {
370 final List<String> prefixes = ForesterUtil.spliIntoPrefixes( prefix.getPrefix(), separator );
371 for( final String p : prefixes ) {
375 for( final String key : map.keySet() ) {
376 for( final Prefix prefix : greatest_common_prefixes ) {
377 if ( prefix.getPrefix().startsWith( key ) ) {
378 map.put( key, map.get( key ) + prefix.getConfidence() );
382 final List<Prefix> l = new ArrayList<>();
383 for( final Entry<String, Double> entry : map.entrySet() ) {
384 l.add( new Prefix( entry.getKey(), entry.getValue(), separator ) );