JAL-1007 refactored code and patched calculation bug
authorjprocter <jprocter@compbio.dundee.ac.uk>
Sun, 2 Sep 2012 15:49:15 +0000 (16:49 +0100)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Sun, 2 Sep 2012 15:49:15 +0000 (16:49 +0100)
src/jalview/analysis/AlignSeq.java
src/jalview/appletgui/RedundancyPanel.java
src/jalview/gui/RedundancyPanel.java

index 2ce08fc..bb4796f 100755 (executable)
@@ -918,4 +918,85 @@ public class AlignSeq
       }
     }
   }
+
+  /**
+   * compute the PID vector used by the redundancy filter.
+   * @param originalSequences - sequences in alignment that are to filtered
+   * @param omitHidden - null or strings to be analysed (typically, visible portion of each sequence in alignment) 
+   * @param start - first column in window for calculation
+   * @param end - last column in window for calculation
+   * @param ungapped - if true then use ungapped sequence to compute PID
+   * @return vector containing maximum PID for i-th sequence and any sequences longer than that seuqence 
+   */
+  public static float[] computeRedundancyMatrix(SequenceI[] originalSequences,
+          String[] omitHidden, int start, int end, boolean ungapped)
+  {
+    int height=originalSequences.length;
+    float[] redundancy = new float[height];
+    int[] lngth=new int[height];
+    for (int i = 0; i < height; i++)
+    {
+      redundancy[i] = 0f;
+      lngth[i]=-1;
+    }
+
+
+    // long start = System.currentTimeMillis();
+
+    float pid;
+    String seqi, seqj;
+    for (int i = 0; i < height; i++)
+    {
+      
+      for (int j = 0; j < i; j++)
+      {
+        if (i == j)
+        {
+          continue;
+        }
+
+        if (omitHidden == null)
+        {
+          seqi = originalSequences[i].getSequenceAsString(start, end);
+          seqj = originalSequences[j].getSequenceAsString(start, end);
+        }
+        else
+        {
+          seqi = omitHidden[i];
+          seqj = omitHidden[j];
+        }
+        if (lngth[i]==-1)
+        {
+          String ug=AlignSeq.extractGaps(Comparison.GapChars, seqi);
+          lngth[i]=ug.length();
+          if (ungapped)
+          {
+            seqi=ug;
+          }
+        }
+        if (lngth[j]==-1)
+        {
+          String ug=AlignSeq.extractGaps(Comparison.GapChars, seqj);
+          lngth[j]=ug.length();
+          if (ungapped)
+          {
+            seqj=ug;
+          }
+        }
+        pid = Comparison.PID(seqi, seqj);
+
+        // use real sequence length rather than string length 
+        if (lngth[j]<lngth[i])
+        {
+          redundancy[j] = Math.max(pid, redundancy[j]);
+        }
+        else
+        {
+          redundancy[i] = Math.max(pid, redundancy[i]);
+        }
+
+      }
+    }
+    return redundancy;
+  }
 }
index 0b4987c..b420758 100755 (executable)
@@ -22,6 +22,7 @@ import java.util.*;
 import java.awt.*;
 import java.awt.event.*;
 
+import jalview.analysis.AlignSeq;
 import jalview.commands.*;
 import jalview.datamodel.*;
 
@@ -120,55 +121,7 @@ public class RedundancyPanel extends SliderPanel implements Runnable,
 
     height = originalSequences.length;
 
-    redundancy = new float[height];
-    for (int i = 0; i < height; i++)
-    {
-      redundancy[i] = 0f;
-    }
-
-    // if (ap.av.hasHiddenColumns)
-    {
-      // omitHidden = ap.av.getSelectionAsString();
-    }
-
-    // long start = System.currentTimeMillis();
-
-    float pid;
-    String seqi, seqj;
-    for (int i = 0; i < height; i++)
-    {
-      for (int j = 0; j < i; j++)
-      {
-        if (i == j)
-        {
-          continue;
-        }
-
-        if (omitHidden == null)
-        {
-          seqi = originalSequences[i].getSequenceAsString(start, end);
-          seqj = originalSequences[j].getSequenceAsString(start, end);
-        }
-        else
-        {
-          seqi = omitHidden[i];
-          seqj = omitHidden[j];
-        }
-
-        pid = jalview.util.Comparison.PID(seqi, seqj);
-
-        if (seqj.length() < seqi.length())
-        {
-          redundancy[j] = Math.max(pid, redundancy[j]);
-        }
-        else
-        {
-          redundancy[i] = Math.max(pid, redundancy[i]);
-        }
-
-      }
-    }
-
+    redundancy = AlignSeq.computeRedundancyMatrix(originalSequences, omitHidden, start, end, false);
     label.setText("Enter the redundancy threshold");
     slider.setVisible(true);
     applyButton.setEnabled(true);
index 9f117b6..f0a4f91 100755 (executable)
@@ -23,6 +23,7 @@ import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.event.*;
 
+import jalview.analysis.AlignSeq;
 import jalview.commands.*;
 import jalview.datamodel.*;
 import jalview.jbgui.*;
@@ -143,56 +144,11 @@ public class RedundancyPanel extends GSliderPanel implements Runnable
     }
 
     height = originalSequences.length;
-
-    redundancy = new float[height];
-    for (int i = 0; i < height; i++)
-    {
-      redundancy[i] = 0f;
-    }
-
     if (ap.av.hasHiddenColumns())
     {
       omitHidden = ap.av.getViewAsString(sg != null);
     }
-
-    // long start = System.currentTimeMillis();
-
-    float pid;
-    String seqi, seqj;
-    for (int i = 0; i < height; i++)
-    {
-
-      for (int j = 0; j < i; j++)
-      {
-        if (i == j)
-        {
-          continue;
-        }
-
-        if (omitHidden == null)
-        {
-          seqi = originalSequences[i].getSequenceAsString(start, end);
-          seqj = originalSequences[j].getSequenceAsString(start, end);
-        }
-        else
-        {
-          seqi = omitHidden[i];
-          seqj = omitHidden[j];
-        }
-
-        pid = Comparison.PID(seqi, seqj);
-
-        if (seqj.length() < seqi.length())
-        {
-          redundancy[j] = Math.max(pid, redundancy[j]);
-        }
-        else
-        {
-          redundancy[i] = Math.max(pid, redundancy[i]);
-        }
-
-      }
-    }
+    redundancy = AlignSeq.computeRedundancyMatrix(originalSequences, omitHidden, start, end, false);
 
     progress.setIndeterminate(false);
     progress.setVisible(false);