removed diamond operator
[jalview.git] / forester / java / src / org / forester / clade_analysis / ResultMulti.java
index d3bfba3..3f55364 100644 (file)
@@ -36,27 +36,35 @@ import java.util.SortedMap;
 import java.util.TreeMap;
 
 import org.forester.util.ForesterUtil;
+import org.forester.util.UserException;
 
 public final class ResultMulti {
 
-    private final String       _separator;
-    private final List<Prefix> _greatest_common_prefixes      = new ArrayList<Prefix>();
-    private final List<Prefix> _greatest_common_prefixes_up   = new ArrayList<Prefix>();
-    private final List<Prefix> _greatest_common_prefixes_down = new ArrayList<Prefix>();
-    private List<Prefix>       _all                           = null;
-    private List<Prefix>       _collapsed                     = null;
-    private List<Prefix>       _cleaned_spec                  = null;
-    private boolean            _has_specifics                 = false;
-    private List<Prefix>       _all_up                        = null;
-    private List<Prefix>       _collapsed_up                  = null;
-    private List<Prefix>       _cleaned_spec_up               = null;
-    private boolean            _has_specifics_up              = false;
-    private List<Prefix>       _all_down                      = null;
-    private List<Prefix>       _collapsed_down                = null;
-    private List<Prefix>       _cleaned_spec_down             = null;
-    private boolean            _has_specifics_down            = false;
+    private final static double MIN_DIFF                       = 1E-5;
+    private final String        _separator;
+    private final List<Prefix>  _greatest_common_prefixes      = new ArrayList<Prefix>();
+    private final List<Prefix>  _greatest_common_prefixes_up   = new ArrayList<Prefix>();
+    private final List<Prefix>  _greatest_common_prefixes_down = new ArrayList<Prefix>();
+    private List<Prefix>        _all                           = null;
+    private List<Prefix>        _collapsed                     = null;
+    private List<Prefix>        _cleaned_spec                  = null;
+    private boolean             _has_specifics                 = false;
+    private List<Prefix>        _all_up                        = null;
+    private List<Prefix>        _collapsed_up                  = null;
+    private List<Prefix>        _cleaned_spec_up               = null;
+    private boolean             _has_specifics_up              = false;
+    private List<Prefix>        _all_down                      = null;
+    private List<Prefix>        _collapsed_down                = null;
+    private List<Prefix>        _cleaned_spec_down             = null;
+    private boolean             _has_specifics_down            = false;
+    private int                 _matches                       = 0;
+    private int                 _ref_tree_ext_nodes            = 0;
+    private String              _query_name_prefix             = "";
 
     ResultMulti( final String separator ) {
+        if ( ForesterUtil.isEmpty( separator ) ) {
+            throw new IllegalArgumentException( "separator must not be null or empty" );
+        }
         _separator = separator;
         reset();
     }
@@ -114,17 +122,25 @@ public final class ResultMulti {
         return _has_specifics;
     }
 
+    public String getQueryNamePrefix() {
+        return _query_name_prefix;
+    }
+
+    public int getNumberOfMatches() {
+        return _matches;
+    }
+
+    public int getReferenceTreeNumberOfExternalNodes() {
+        return _ref_tree_ext_nodes;
+    }
+
     @Override
     public final String toString() {
         final StringBuilder sb = new StringBuilder();
-        sb.append( "Cleaned:" );
+        sb.append( "Query: " );
+        sb.append( getQueryNamePrefix() );
         sb.append( ForesterUtil.LINE_SEPARATOR );
-        for( final Prefix prefix : _all ) {
-            sb.append( prefix );
-            sb.append( ForesterUtil.LINE_SEPARATOR );
-        }
-        sb.append( ForesterUtil.LINE_SEPARATOR );
-        sb.append( "Collapsed:" );
+        sb.append( "Matching Clade(s):" );
         sb.append( ForesterUtil.LINE_SEPARATOR );
         for( final Prefix prefix : _collapsed ) {
             sb.append( prefix );
@@ -132,14 +148,14 @@ public final class ResultMulti {
         }
         if ( _has_specifics ) {
             sb.append( ForesterUtil.LINE_SEPARATOR );
-            sb.append( "Specifics:" );
+            sb.append( "Specific-hit(s):" );
             sb.append( ForesterUtil.LINE_SEPARATOR );
             for( final Prefix prefix : _cleaned_spec ) {
                 sb.append( prefix );
                 sb.append( ForesterUtil.LINE_SEPARATOR );
             }
             sb.append( ForesterUtil.LINE_SEPARATOR );
-            sb.append( "Collapsed With Specifics:" );
+            sb.append( "Matching Clade(s) with Specific-hit(s):" );
             sb.append( ForesterUtil.LINE_SEPARATOR );
             for( final Prefix prefix : _collapsed ) {
                 sb.append( prefix );
@@ -154,80 +170,25 @@ public final class ResultMulti {
         }
         if ( !ForesterUtil.isEmpty( _all_down ) ) {
             sb.append( ForesterUtil.LINE_SEPARATOR );
-            sb.append( "Cleaned Down:" );
-            sb.append( ForesterUtil.LINE_SEPARATOR );
-            for( final Prefix prefix : _all_down ) {
-                sb.append( prefix );
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-            }
-            sb.append( ForesterUtil.LINE_SEPARATOR );
-            sb.append( "Collapsed Down:" );
+            sb.append( "Matching Down-tree Bracketing Clade(s):" );
             sb.append( ForesterUtil.LINE_SEPARATOR );
             for( final Prefix prefix : _collapsed_down ) {
                 sb.append( prefix );
                 sb.append( ForesterUtil.LINE_SEPARATOR );
             }
-            if ( _has_specifics_down ) {
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-                sb.append( "Specifics Down:" );
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-                for( final Prefix prefix : _cleaned_spec_down ) {
-                    sb.append( prefix );
-                    sb.append( ForesterUtil.LINE_SEPARATOR );
-                }
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-                sb.append( "Collapsed With Specifics Down:" );
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-                for( final Prefix prefix : _collapsed_down ) {
-                    sb.append( prefix );
-                    sb.append( ForesterUtil.LINE_SEPARATOR );
-                    for( final Prefix spec : _cleaned_spec_down ) {
-                        if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
-                            sb.append( "    " + spec );
-                            sb.append( ForesterUtil.LINE_SEPARATOR );
-                        }
-                    }
-                }
-            }
         }
         if ( !ForesterUtil.isEmpty( _all_up ) ) {
             sb.append( ForesterUtil.LINE_SEPARATOR );
-            sb.append( "Cleaned Up:" );
-            sb.append( ForesterUtil.LINE_SEPARATOR );
-            for( final Prefix prefix : _all_up ) {
-                sb.append( prefix );
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-            }
-            sb.append( ForesterUtil.LINE_SEPARATOR );
-            sb.append( "Collapsed Up:" );
+            sb.append( "Matching Up-tree Bracketing Clade(s):" );
             sb.append( ForesterUtil.LINE_SEPARATOR );
             for( final Prefix prefix : _collapsed_up ) {
                 sb.append( prefix );
                 sb.append( ForesterUtil.LINE_SEPARATOR );
             }
-            if ( _has_specifics ) {
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-                sb.append( "Specifics Up:" );
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-                for( final Prefix prefix : _cleaned_spec_up ) {
-                    sb.append( prefix );
-                    sb.append( ForesterUtil.LINE_SEPARATOR );
-                }
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-                sb.append( "Collapsed With Specifics Up:" );
-                sb.append( ForesterUtil.LINE_SEPARATOR );
-                for( final Prefix prefix : _collapsed_up ) {
-                    sb.append( prefix );
-                    sb.append( ForesterUtil.LINE_SEPARATOR );
-                    for( final Prefix spec : _cleaned_spec_up ) {
-                        if ( spec.getPrefix().startsWith( prefix.getPrefix() ) ) {
-                            sb.append( "    " + spec );
-                            sb.append( ForesterUtil.LINE_SEPARATOR );
-                        }
-                    }
-                }
-            }
         }
+        sb.append( ForesterUtil.LINE_SEPARATOR );
+        sb.append( "Total Number of Matches: " + getNumberOfMatches() + "/" + getReferenceTreeNumberOfExternalNodes() );
+        sb.append( ForesterUtil.LINE_SEPARATOR );
         return sb.toString();
     }
 
@@ -243,7 +204,28 @@ public final class ResultMulti {
         _greatest_common_prefixes_down.add( new Prefix( prefix_down, confidence, _separator ) );
     }
 
-    final void analyze( final double cutoff_for_specifics ) {
+    void setQueryNamePrefix( final String query_name_prefix ) {
+        if ( !ForesterUtil.isEmpty( _query_name_prefix ) ) {
+            throw new IllegalStateException( "illegal attempt to change the query name prefix" );
+        }
+        _query_name_prefix = query_name_prefix;
+    }
+
+    void setTotalNumberOfMatches( final int matches ) {
+        if ( _matches > 0 ) {
+            throw new IllegalStateException( "illegal attempt to change the number of matches" );
+        }
+        _matches = matches;
+    }
+
+    public void setReferenceTreeNumberOfExternalNodes( final int ext_nodes ) {
+        if ( _ref_tree_ext_nodes > 0 ) {
+            throw new IllegalStateException( "illegal attempt to change the number of external nodes" );
+        }
+        _ref_tree_ext_nodes = ext_nodes;
+    }
+
+    final void analyze( final double cutoff_for_specifics ) throws UserException {
         reset();
         analyzeGreatestCommonPrefixes( _greatest_common_prefixes, _separator, cutoff_for_specifics );
         analyzeGreatestCommonPrefixesUp( _greatest_common_prefixes_up, _separator, cutoff_for_specifics );
@@ -267,15 +249,16 @@ public final class ResultMulti {
 
     private final void analyzeGreatestCommonPrefixes( final List<Prefix> greatest_common_prefixes,
                                                       final String separator,
-                                                      final double cutoff ) {
+                                                      final double cutoff )
+            throws UserException {
         final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes, separator );
         if ( !ForesterUtil.isEmpty( l ) ) {
             sortPrefixesAccordingToConfidence( l );
-            _all = removeLessSpecificPrefixes( l );
+            _all = removeLessSpecificPrefixes( l, separator );
             _collapsed = collapse( _all );
             _has_specifics = false;
             if ( cutoff >= 0 ) {
-                _cleaned_spec = obtainSpecifics( cutoff, _all, _collapsed );
+                _cleaned_spec = obtainSpecifics( cutoff, _all, _collapsed, separator );
                 if ( !ForesterUtil.isEmpty( _cleaned_spec ) ) {
                     _has_specifics = true;
                 }
@@ -285,15 +268,16 @@ public final class ResultMulti {
 
     private final void analyzeGreatestCommonPrefixesUp( final List<Prefix> greatest_common_prefixes_up,
                                                         final String separator,
-                                                        final double cutoff ) {
+                                                        final double cutoff )
+            throws UserException {
         final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_up, separator );
         if ( !ForesterUtil.isEmpty( l ) ) {
             sortPrefixesAccordingToConfidence( l );
-            _all_up = removeLessSpecificPrefixes( l );
+            _all_up = removeLessSpecificPrefixes( l, separator );
             _collapsed_up = collapse( _all_up );
             _has_specifics_up = false;
             if ( cutoff >= 0 ) {
-                _cleaned_spec_up = obtainSpecifics( cutoff, _all_up, _collapsed_up );
+                _cleaned_spec_up = obtainSpecifics( cutoff, _all_up, _collapsed_up, separator );
                 if ( !ForesterUtil.isEmpty( _cleaned_spec_up ) ) {
                     _has_specifics_up = true;
                 }
@@ -303,15 +287,16 @@ public final class ResultMulti {
 
     final void analyzeGreatestCommonPrefixesDown( final List<Prefix> greatest_common_prefixes_down,
                                                   final String separator,
-                                                  final double cutoff ) {
+                                                  final double cutoff )
+            throws UserException {
         final List<Prefix> l = obtainAllPrefixes( greatest_common_prefixes_down, separator );
         if ( !ForesterUtil.isEmpty( l ) ) {
             sortPrefixesAccordingToConfidence( l );
-            _all_down = removeLessSpecificPrefixes( l );
+            _all_down = removeLessSpecificPrefixes( l, separator );
             _collapsed_down = collapse( _all_down );
             _has_specifics_down = false;
             if ( cutoff >= 0 ) {
-                _cleaned_spec_down = obtainSpecifics( cutoff, _all_down, _collapsed_down );
+                _cleaned_spec_down = obtainSpecifics( cutoff, _all_down, _collapsed_down, separator );
                 if ( !ForesterUtil.isEmpty( _cleaned_spec_down ) ) {
                     _has_specifics_down = true;
                 }
@@ -321,13 +306,14 @@ public final class ResultMulti {
 
     final static List<Prefix> obtainSpecifics( final double cutoff,
                                                final List<Prefix> cleaned,
-                                               final List<Prefix> collapsed ) {
-        final List<Prefix> cleaned_spec = new ArrayList<>();
-        final Set<String> collapsed_set = new HashSet<>();
+                                               final List<Prefix> collapsed,
+                                               final String separator ) {
+        final List<Prefix> cleaned_spec = new ArrayList<Prefix>();
+        final Set<String> collapsed_set = new HashSet<String>();
         for( final Prefix prefix : collapsed ) {
             collapsed_set.add( prefix.getPrefix() );
         }
-        final List<Prefix> spec = new ArrayList<>();
+        final List<Prefix> spec = new ArrayList<Prefix>();
         for( final Prefix prefix : cleaned ) {
             if ( ( prefix.getConfidence() >= cutoff ) && !collapsed_set.contains( prefix.getPrefix() ) ) {
                 spec.add( prefix );
@@ -336,10 +322,15 @@ public final class ResultMulti {
         for( final Prefix o : spec ) {
             boolean ok = true;
             for( final Prefix i : spec ) {
-                if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) ) ) {
+                if ( ( !o.getPrefix().equals( i.getPrefix() ) )
+                        && ( ForesterUtil.isContainsPrefix( i.getPrefix(), o.getPrefix(), separator ) ) ) {
                     ok = false;
                     break;
                 }
+                /* if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) ) ) {
+                    ok = false;
+                    break;
+                }*/
             }
             if ( ok ) {
                 cleaned_spec.add( o );
@@ -348,9 +339,9 @@ public final class ResultMulti {
         return cleaned_spec;
     }
 
-    private final static List<Prefix> collapse( final List<Prefix> cleaned ) {
-        final List<Prefix> collapsed = new ArrayList<>();
-        final Set<String> firsts = new HashSet<>();
+    private final static List<Prefix> collapse( final List<Prefix> cleaned ) throws UserException {
+        final List<Prefix> collapsed = new ArrayList<Prefix>();
+        final Set<String> firsts = new HashSet<String>();
         double confidence_sum = 0;
         for( final Prefix prefix : cleaned ) {
             final String f = prefix.getPrefixFirstElement();
@@ -360,8 +351,8 @@ public final class ResultMulti {
                 confidence_sum += prefix.getConfidence();
             }
         }
-        if ( !ForesterUtil.isEqual( confidence_sum, 1.0, 1E-5 ) ) {
-            throw new IllegalArgumentException( "Confidences add up to " + confidence_sum + " instead of 1.0" );
+        if ( !ForesterUtil.isEqual( confidence_sum, 1.0, MIN_DIFF ) ) {
+            throw new UserException( "confidences add up to " + confidence_sum + " instead of 1.0" );
         }
         return collapsed;
     }
@@ -376,12 +367,19 @@ public final class ResultMulti {
      * I.e. it removes less specific prefixes.
      *
      */
-    private final static List<Prefix> removeLessSpecificPrefixes( final List<Prefix> l ) {
-        final List<Prefix> cleaned = new ArrayList<>();
+    private final static List<Prefix> removeLessSpecificPrefixes( final List<Prefix> l, final String separator ) {
+        final List<Prefix> cleaned = new ArrayList<Prefix>();
         for( final Prefix o : l ) {
             boolean ok = true;
             for( final Prefix i : l ) {
-                if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) )
+                /*if ( ( !o.getPrefix().equals( i.getPrefix() ) ) && ( i.getPrefix().startsWith( o.getPrefix() ) )
+                        && ForesterUtil.isEqual( i.getConfidence(),
+                                                 o.getConfidence() ) ) {
+                    ok = false;
+                    break;
+                }*/
+                if ( ( !o.getPrefix().equals( i.getPrefix() ) )
+                        && ( ForesterUtil.isContainsPrefix( i.getPrefix(), o.getPrefix(), separator ) )
                         && ForesterUtil.isEqual( i.getConfidence(),
                                                  o.getConfidence() ) ) {
                     ok = false;
@@ -411,7 +409,7 @@ public final class ResultMulti {
 
     private final static List<Prefix> obtainAllPrefixes( final List<Prefix> greatest_common_prefixes,
                                                          final String separator ) {
-        final SortedMap<String, Double> map = new TreeMap<>();
+        final SortedMap<String, Double> map = new TreeMap<String, Double>();
         for( final Prefix prefix : greatest_common_prefixes ) {
             final List<String> prefixes = ForesterUtil.spliIntoPrefixes( prefix.getPrefix(), separator );
             for( final String p : prefixes ) {
@@ -420,12 +418,12 @@ public final class ResultMulti {
         }
         for( final String key : map.keySet() ) {
             for( final Prefix prefix : greatest_common_prefixes ) {
-                if ( prefix.getPrefix().startsWith( key ) ) {
+                if ( ForesterUtil.isContainsPrefix( prefix.getPrefix(), key, separator ) ) {
                     map.put( key, map.get( key ) + prefix.getConfidence() );
                 }
             }
         }
-        final List<Prefix> l = new ArrayList<>();
+        final List<Prefix> l = new ArrayList<Prefix>();
         for( final Entry<String, Double> entry : map.entrySet() ) {
             l.add( new Prefix( entry.getKey(), entry.getValue(), separator ) );
         }