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;
60 ResultMulti( final String separator ) {
61 _separator = separator;
66 _separator = AnalysisMulti.DEFAULT_SEPARATOR;
70 public List<Prefix> getAllMultiHitPrefixesUp() {
74 public List<Prefix> getCollapsedMultiHitPrefixesUp() {
78 public List<Prefix> getSpecificMultiHitPrefixesUp() {
79 return _cleaned_spec_up;
82 public boolean isHasSpecificMultiHitsPrefixesUp() {
83 return _has_specifics_up;
86 public List<Prefix> getAllMultiHitPrefixesDown() {
90 public List<Prefix> getCollapsedMultiHitPrefixesDown() {
91 return _collapsed_down;
94 public List<Prefix> getSpecificMultiHitPrefixesDown() {
95 return _cleaned_spec_down;
98 public boolean isHasSpecificMultiHitsPrefixesDown() {
99 return _has_specifics_down;
102 public List<Prefix> getAllMultiHitPrefixes() {
106 public List<Prefix> getCollapsedMultiHitPrefixes() {
110 public List<Prefix> getSpecificMultiHitPrefixes() {
111 return _cleaned_spec;
114 public boolean isHasSpecificMultiHitsPrefixes() {
115 return _has_specifics;
119 public final String toString() {
120 final StringBuilder sb = new StringBuilder();
121 sb.append( "Matching Clade(s):" );
122 sb.append( ForesterUtil.LINE_SEPARATOR );
123 for( final Prefix prefix : _collapsed ) {
125 sb.append( ForesterUtil.LINE_SEPARATOR );
127 if ( _has_specifics ) {
128 sb.append( ForesterUtil.LINE_SEPARATOR );
129 sb.append( "Specific-hit(s):" );
130 sb.append( ForesterUtil.LINE_SEPARATOR );
131 for( final Prefix prefix : _cleaned_spec ) {
133 sb.append( ForesterUtil.LINE_SEPARATOR );
135 sb.append( ForesterUtil.LINE_SEPARATOR );
136 sb.append( "Matching Clade(s) with Specific-hit(s):" );
137 sb.append( ForesterUtil.LINE_SEPARATOR );
138 for( final Prefix prefix : _collapsed ) {
140 sb.append( ForesterUtil.LINE_SEPARATOR );
141 for( final Prefix spec : _cleaned_spec ) {
142 if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
143 sb.append( " " + spec );
144 sb.append( ForesterUtil.LINE_SEPARATOR );
149 if ( !ForesterUtil.isEmpty( _all_down ) ) {
150 sb.append( ForesterUtil.LINE_SEPARATOR );
151 sb.append( "Matching Down-tree Bracketing Clade(s):" );
152 sb.append( ForesterUtil.LINE_SEPARATOR );
153 for( final Prefix prefix : _collapsed_down ) {
155 sb.append( ForesterUtil.LINE_SEPARATOR );
158 if ( !ForesterUtil.isEmpty( _all_up ) ) {
159 sb.append( ForesterUtil.LINE_SEPARATOR );
160 sb.append( "Matching Up-tree Bracketing Clade(s):" );
161 sb.append( ForesterUtil.LINE_SEPARATOR );
162 for( final Prefix prefix : _collapsed_up ) {
164 sb.append( ForesterUtil.LINE_SEPARATOR );
167 return sb.toString();
170 void addGreatestCommonPrefix( final String prefix, final double confidence ) {
171 _greatest_common_prefixes.add( new Prefix( prefix, confidence, _separator ) );
174 void addGreatestCommonPrefixUp( final String prefix_up, final double confidence ) {
175 _greatest_common_prefixes_up.add( new Prefix( prefix_up, confidence, _separator ) );
178 void addGreatestCommonPrefixDown( final String prefix_down, final double confidence ) {
179 _greatest_common_prefixes_down.add( new Prefix( prefix_down, confidence, _separator ) );
182 final void analyze( final double cutoff_for_specifics ) throws UserException {
184 analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator, cutoff_for_specifics );
185 analyzeGreatestCommonPrefixesUp( _greatest_common_prefixes_up, _separator, cutoff_for_specifics );
186 analyzeGreatestCommonPrefixesDown( _greatest_common_prefixes_down, _separator, cutoff_for_specifics );
189 private final void reset() {
190 _all = new ArrayList<>();
191 _collapsed = new ArrayList<>();
192 _cleaned_spec = new ArrayList<>();
193 _has_specifics = false;
194 _all_up = new ArrayList<>();
195 _collapsed_up = new ArrayList<>();
196 _cleaned_spec_up = new ArrayList<>();
197 _has_specifics_up = false;
198 _all_down = new ArrayList<>();
199 _collapsed_down = new ArrayList<>();
200 _cleaned_spec_down = new ArrayList<>();
201 _has_specifics_down = false;
204 private final void analyzeGreatestCommonPrefixes( final List<Prefix> greatest_common_prefixes,
205 final String separator,
206 final double cutoff )
207 throws UserException {
208 final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes, separator );
209 if ( !ForesterUtil.isEmpty( l ) ) {
210 sortPrefixesAccordingToConfidence( l );
211 _all = removeLessSpecificPrefixes( l );
212 _collapsed = collapse( _all );
213 _has_specifics = false;
215 _cleaned_spec = obtainSpecifics( cutoff, _all, _collapsed );
216 if ( !ForesterUtil.isEmpty( _cleaned_spec ) ) {
217 _has_specifics = true;
223 private final void analyzeGreatestCommonPrefixesUp( final List<Prefix> greatest_common_prefixes_up,
224 final String separator,
225 final double cutoff )
226 throws UserException {
227 final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_up, separator );
228 if ( !ForesterUtil.isEmpty( l ) ) {
229 sortPrefixesAccordingToConfidence( l );
230 _all_up = removeLessSpecificPrefixes( l );
231 _collapsed_up = collapse( _all_up );
232 _has_specifics_up = false;
234 _cleaned_spec_up = obtainSpecifics( cutoff, _all_up, _collapsed_up );
235 if ( !ForesterUtil.isEmpty( _cleaned_spec_up ) ) {
236 _has_specifics_up = true;
242 final void analyzeGreatestCommonPrefixesDown( final List<Prefix> greatest_common_prefixes_down,
243 final String separator,
244 final double cutoff )
245 throws UserException {
246 final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_down, separator );
247 if ( !ForesterUtil.isEmpty( l ) ) {
248 sortPrefixesAccordingToConfidence( l );
249 _all_down = removeLessSpecificPrefixes( l );
250 _collapsed_down = collapse( _all_down );
251 _has_specifics_down = false;
253 _cleaned_spec_down = obtainSpecifics( cutoff, _all_down, _collapsed_down );
254 if ( !ForesterUtil.isEmpty( _cleaned_spec_down ) ) {
255 _has_specifics_down = true;
261 final static List<Prefix> obtainSpecifics( final double cutoff,
262 final List<Prefix> cleaned,
263 final List<Prefix> collapsed ) {
264 final List<Prefix> cleaned_spec = new ArrayList<>();
265 final Set<String> collapsed_set = new HashSet<>();
266 for( final Prefix prefix : collapsed ) {
267 collapsed_set.add( prefix.getPrefix() );
269 final List<Prefix> spec = new ArrayList<>();
270 for( final Prefix prefix : cleaned ) {
271 if ( ( prefix.getConfidence() >= cutoff ) && !collapsed_set.contains( prefix.getPrefix() ) ) {
275 for( final Prefix o : spec ) {
277 for( final Prefix i : spec ) {
278 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) ) ) {
284 cleaned_spec.add( o );
290 private final static List<Prefix> collapse( final List<Prefix> cleaned ) throws UserException {
291 final List<Prefix> collapsed = new ArrayList<>();
292 final Set<String> firsts = new HashSet<>();
293 double confidence_sum = 0;
294 for( final Prefix prefix : cleaned ) {
295 final String f = prefix.getPrefixFirstElement();
296 if ( !firsts.contains( f ) ) {
298 collapsed.add( prefix );
299 confidence_sum += prefix.getConfidence();
302 if ( !ForesterUtil.isEqual( confidence_sum, 1.0, 1E-5 ) ) {
303 throw new UserException( "confidences add up to " + confidence_sum + " instead of 1.0" );
309 * This replaces (by way of example)
315 * I.e. it removes less specific prefixes.
318 private final static List<Prefix> removeLessSpecificPrefixes( final List<Prefix> l ) {
319 final List<Prefix> cleaned = new ArrayList<>();
320 for( final Prefix o : l ) {
322 for( final Prefix i : l ) {
323 if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) )
324 && ForesterUtil.isEqual( i.getConfidence(),
325 o.getConfidence() ) ) {
337 private final static void sortPrefixesAccordingToConfidence( final List<Prefix> l ) {
338 Collections.sort( l, new Comparator<Prefix>() {
341 public int compare( final Prefix x, final Prefix y ) {
342 return compare( x.getConfidence(), y.getConfidence() );
345 private int compare( final double a, final double b ) {
346 return a > b ? -1 : a > b ? 1 : 0;
351 private final static List<Prefix> obtainAllPrefixes( final List<Prefix> greatest_common_prefixes,
352 final String separator ) {
353 final SortedMap<String, Double> map = new TreeMap<>();
354 for( final Prefix prefix : greatest_common_prefixes ) {
355 final List<String> prefixes = ForesterUtil.spliIntoPrefixes( prefix.getPrefix(), separator );
356 for( final String p : prefixes ) {
360 for( final String key : map.keySet() ) {
361 for( final Prefix prefix : greatest_common_prefixes ) {
362 if ( prefix.getPrefix().startsWith( key ) ) {
363 map.put( key, map.get( key ) + prefix.getConfidence() );
367 final List<Prefix> l = new ArrayList<>();
368 for( final Entry<String, Double> entry : map.entrySet() ) {
369 l.add( new Prefix( entry.getKey(), entry.getValue(), separator ) );