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 ) );
71 void setGreatestCommonPrefixUp( final String greatest_common_prefix_up ) {
72 _greatest_common_prefix_up = greatest_common_prefix_up;
75 void setGreatestCommonPrefixDown( final String greatest_common_prefix_down ) {
76 _greatest_common_prefix_down = greatest_common_prefix_down;
79 void setGreatestCommonCladeSubtreeConfidence( final String greatest_common_clade_confidence ) {
80 _greatest_common_clade_subtree_confidence = greatest_common_clade_confidence;
83 void setGreatestCommonCladeUpSubtreeConfidence( final String greatest_common_clade_confidence_up ) {
84 _greatest_common_clade_subtree_confidence_up = greatest_common_clade_confidence_up;
87 void setGreatestCommonCladeDownSubtreeConfidence( final String greatest_common_clade_confidence_down ) {
88 _greatest_common_clade_subtree_confidence_down = greatest_common_clade_confidence_down;
91 // public String getGreatestCommonPrefix() {
92 // return _greatest_common_prefix;
94 public String getGreatestCommonPrefixUp() {
95 return _greatest_common_prefix_up;
98 public String getGreatestCommonPrefixDown() {
99 return _greatest_common_prefix_down;
102 public String getGreatestCommonCladeSubtreeConfidence() {
103 return _greatest_common_clade_subtree_confidence;
106 public String getGreatestCommonCladeUpSubtreeConfidence() {
107 return _greatest_common_clade_subtree_confidence_up;
110 public String getGreatestCommonCladeDownSubtreeConfidence() {
111 return _greatest_common_clade_subtree_confidence_down;
114 public List<String> getWarnings() {
118 void setLeastEncompassingCladeSize( final int lec_ext_nodes ) {
119 _lec_ext_nodes = lec_ext_nodes;
122 void setTreeSize( final int p_ext_nodes ) {
123 _p_ext_nodes = p_ext_nodes;
126 public int getLeastEncompassingCladeSize() {
127 return _lec_ext_nodes;
130 public int getTreeSize() {
134 public void analyzeGreatestCommonPrefixes( ) {
135 analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator );
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 ) {
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() );
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 ) );
161 Collections.sort( l, new Comparator<Prefix>() {
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);
171 private int compare( final double a, final double b ) {
172 return a > b ? -1 : a > b ? 1 : 0;
175 System.out.println();
176 for( final Prefix prefix : l ) {
177 // System.out.println( prefix );
179 final List<Prefix> cleaned = new ArrayList<>();
180 for( final Prefix o : l ) {
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() ) ) {
194 System.out.println();
195 for( final Prefix prefix : cleaned ) {
196 System.out.println( prefix );
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 ) ) {
205 collapsed.add( prefix );
206 confidence_sum += prefix.getConfidence();
209 if ( !ForesterUtil.isEqual( confidence_sum, 1.0 ) ) {
210 throw new IllegalArgumentException( "Confidences add up to " + confidence_sum + " instead of 1.0" );
212 System.out.println();
213 for( final Prefix prefix : collapsed ) {
214 System.out.println( prefix );