Check exploded pos is not the same as current frame
[jalview.git] / src / jalview / gui / RedundancyPanel.java
index e4ef46e..6f5b09d 100755 (executable)
 /*\r
-* Jalview - A Sequence Alignment Editor and Viewer\r
-* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
-*\r
-* This program is free software; you can redistribute it and/or\r
-* modify it under the terms of the GNU General Public License\r
-* as published by the Free Software Foundation; either version 2\r
-* of the License, or (at your option) any later version.\r
-*\r
-* This program is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with this program; if not, write to the Free Software\r
-* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
-*/\r
-\r
+ * Jalview - A Sequence Alignment Editor and Viewer\r
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+ */\r
 package jalview.gui;\r
 \r
-import jalview.jbgui.*;\r
-import jalview.datamodel.*;\r
-import java.awt.event.*;\r
 import java.util.*;\r
+\r
+import java.awt.event.*;\r
+import javax.swing.*;\r
 import javax.swing.event.*;\r
 \r
+import jalview.commands.*;\r
+import jalview.datamodel.*;\r
+import jalview.jbgui.*;\r
+import jalview.util.*;\r
+\r
+/**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @author $author$\r
+ * @version $Revision$\r
+ */\r
 public class RedundancyPanel\r
-    extends GSliderPanel\r
+    extends GSliderPanel implements Runnable\r
 {\r
   AlignFrame af;\r
   AlignmentPanel ap;\r
   Stack historyList = new Stack(); // simpler than synching with alignFrame.\r
+  float[] redundancy;\r
+  SequenceI[] originalSequences;\r
+  JInternalFrame frame;\r
+  Vector redundantSeqs;\r
 \r
-  public RedundancyPanel(AlignmentPanel ap, AlignFrame af)\r
+  /**\r
+   * Creates a new RedundancyPanel object.\r
+   *\r
+   * @param ap DOCUMENT ME!\r
+   * @param af DOCUMENT ME!\r
+   */\r
+  public RedundancyPanel(final AlignmentPanel ap, AlignFrame af)\r
   {\r
     this.ap = ap;\r
     this.af = af;\r
-    label.setText("Enter the redundancy threshold");\r
+    redundantSeqs = new Vector();\r
 \r
     slider.addChangeListener(new ChangeListener()\r
     {\r
       public void stateChanged(ChangeEvent evt)\r
       {\r
         valueField.setText(slider.getValue() + "");\r
+        sliderValueChanged();\r
       }\r
     });\r
 \r
+    applyButton.setText("Remove");\r
+    allGroupsCheck.setVisible(false);\r
     slider.setMinimum(0);\r
     slider.setMaximum(100);\r
     slider.setValue(100);\r
 \r
+    Thread worker = new Thread(this);\r
+    worker.start();\r
+\r
+    frame = new JInternalFrame();\r
+    frame.setContentPane(this);\r
+    Desktop.addInternalFrame(frame, "Redundancy threshold selection", 400,\r
+                             100, false);\r
+    frame.addInternalFrameListener(new InternalFrameAdapter()\r
+    {\r
+      public void internalFrameClosing(InternalFrameEvent evt)\r
+      {\r
+        ap.idPanel.idCanvas.setHighlighted(null);\r
+      }\r
+    }\r
+    );\r
+\r
   }\r
 \r
-  public void applyButton_actionPerformed(ActionEvent e)\r
+  /**\r
+   * This is a copy of remove redundancy in jalivew.datamodel.Alignment\r
+   * except we dont want to remove redundancy, just calculate once\r
+   * so we can use the slider to dynamically hide redundant sequences\r
+   *\r
+   * @param threshold DOCUMENT ME!\r
+   * @param sel DOCUMENT ME!\r
+   *\r
+   * @return DOCUMENT ME!\r
+   */\r
+  public void run()\r
   {\r
-    float threshold = slider.getValue();\r
-    Vector del;\r
-    HistoryItem it;\r
-    historyList.push(it=new HistoryItem("Remove redundancy",\r
-                                      ap.av.alignment, HistoryItem.HIDE));\r
-    if (historyList.size()==1\r
-        || !af.historyList.contains(historyList.firstElement())) {\r
-      af.addHistoryItem((HistoryItem) historyList.firstElement());\r
-      af.updateEditMenuBar();\r
-    }\r
-    undoButton.setEnabled(true);\r
+    JProgressBar progress = new JProgressBar();\r
+    progress.setIndeterminate(true);\r
+    southPanel.add(progress, java.awt.BorderLayout.SOUTH);\r
+\r
+    label.setText("Calculating....");\r
+\r
+    slider.setVisible(false);\r
+    applyButton.setEnabled(false);\r
+    valueField.setVisible(false);\r
+\r
+    validate();\r
+\r
+    String[] omitHidden = null;\r
+\r
     SequenceGroup sg = ap.av.getSelectionGroup();\r
-    if (sg != null && sg.getSize() >= 1)\r
+    int height;\r
+\r
+    int start, end;\r
+\r
+    if ( (sg != null) && (sg.getSize() >= 1))\r
     {\r
+      originalSequences = sg.getSequencesInOrder(ap.av.alignment);\r
+      start = sg.getStartRes();\r
+      end = sg.getEndRes();\r
+    }\r
+    else\r
+    {\r
+      originalSequences = ap.av.alignment.getSequencesArray();\r
+      start = 0;\r
+      end = ap.av.alignment.getWidth();\r
+    }\r
 \r
-      del = ap.av.alignment.removeRedundancy(threshold, sg.sequences);\r
-      for (int i = 0; i < del.size(); i++)\r
+    height = originalSequences.length;\r
+\r
+    redundancy = new float[height];\r
+    for (int i = 0; i < height; i++)\r
+    {\r
+      redundancy[i] = 0f;\r
+    }\r
+\r
+    if (ap.av.hasHiddenColumns)\r
+    {\r
+      omitHidden = ap.av.getViewAsString(sg != null);\r
+    }\r
+\r
+    // long start = System.currentTimeMillis();\r
+\r
+    float pid;\r
+    String seqi, seqj;\r
+    for (int i = 0; i < height; i++)\r
+    {\r
+\r
+      for (int j = 0; j < i; j++)\r
       {\r
-        if (sg.sequences.contains( (SequenceI) del.elementAt(i)))\r
+        if (i == j)\r
+        {\r
+          continue;\r
+        }\r
+\r
+        if (omitHidden == null)\r
+        {\r
+          seqi = originalSequences[i].getSequenceAsString(start, end);\r
+          seqj = originalSequences[j].getSequenceAsString(start, end);\r
+        }\r
+        else\r
+        {\r
+          seqi = omitHidden[i];\r
+          seqj = omitHidden[j];\r
+        }\r
+\r
+        pid = Comparison.PID(seqi, seqj);\r
+\r
+        if (seqj.length() < seqi.length())\r
+        {\r
+          redundancy[j] = Math.max(pid, redundancy[j]);\r
+        }\r
+        else\r
         {\r
-          sg.deleteSequence( (SequenceI) del.elementAt(i));\r
+          redundancy[i] = Math.max(pid, redundancy[i]);\r
         }\r
+\r
       }\r
+    }\r
+\r
+    progress.setIndeterminate(false);\r
+    progress.setVisible(false);\r
+    progress = null;\r
+\r
+    label.setText("Enter the redundancy threshold");\r
+    slider.setVisible(true);\r
+    applyButton.setEnabled(true);\r
+    valueField.setVisible(true);\r
+\r
+    validate();\r
+    // System.out.println((System.currentTimeMillis()-start));\r
+  }\r
 \r
+  void sliderValueChanged()\r
+  {\r
+    if (redundancy == null)\r
+    {\r
+      return;\r
     }\r
-    else\r
+\r
+    float value = slider.getValue();\r
+\r
+    for (int i = 0; i < redundancy.length; i++)\r
+    {\r
+      if (value > redundancy[i])\r
+      {\r
+        redundantSeqs.remove(originalSequences[i]);\r
+      }\r
+      else if (!redundantSeqs.contains(originalSequences[i]))\r
+      {\r
+        redundantSeqs.add(originalSequences[i]);\r
+      }\r
+\r
+    }\r
+\r
+    ap.idPanel.idCanvas.setHighlighted(redundantSeqs);\r
+  }\r
+\r
+  /**\r
+   * DOCUMENT ME!\r
+   *\r
+   * @param e DOCUMENT ME!\r
+   */\r
+  public void applyButton_actionPerformed(ActionEvent e)\r
+  {\r
+    Vector del = new Vector();\r
+\r
+    undoButton.setEnabled(true);\r
+\r
+    float value = slider.getValue();\r
+    SequenceGroup sg = ap.av.getSelectionGroup();\r
+\r
+    for (int i = 0; i < redundancy.length; i++)\r
     {\r
-      Vector s = new Vector();\r
-      int i = 0;\r
-      while (i < ap.av.alignment.getHeight())\r
+      if (value <= redundancy[i])\r
       {\r
-        s.addElement(ap.av.alignment.getSequenceAt(i));\r
-        i++;\r
+        del.addElement(originalSequences[i]);\r
       }\r
-      del = ap.av.alignment.removeRedundancy(threshold, s);\r
     }\r
 \r
     // This has to be done before the restoreHistoryItem method of alignFrame will\r
     // actually restore these sequences.\r
+    if (del.size() > 0)\r
+    {\r
+      SequenceI[] deleted = new SequenceI[del.size()];\r
 \r
-    if (del.size()>0)\r
-      for (int i=0,j=del.size(); i<j; i++) {\r
-        SequenceI sq = (SequenceI) del.elementAt(i);\r
-        sq.deleteChars(0,sq.getLength());\r
+      int width = 0;\r
+      for (int i = 0; i < del.size(); i++)\r
+      {\r
+        deleted[i] = (SequenceI) del.elementAt(i);\r
+        if (deleted[i].getLength() > width)\r
+        {\r
+          width = deleted[i].getLength();\r
+        }\r
       }\r
 \r
-    ap.av.firePropertyChange("alignment", null,\r
-                             ap.av.getAlignment().getSequences());\r
-    ap.av.resetSeqLimits(ap.seqPanel.seqCanvas.getHeight());\r
-    if (ap.av.getAlignment().getHeight() < 1)\r
-    {\r
-      try\r
+      EditCommand cut = new EditCommand("Remove Redundancy",\r
+                                        EditCommand.CUT, deleted, 0, width,\r
+                                        ap.av.alignment);\r
+\r
+      for (int i = 0; i < del.size(); i++)\r
       {\r
-        af.setClosed(true);\r
+        ap.av.alignment.deleteSequence(deleted[i]);\r
+        PaintRefresher.Refresh(this,\r
+                               ap.av.getSequenceSetId(),\r
+                               true,\r
+                               true);\r
+        if (sg != null)\r
+        {\r
+          sg.deleteSequence(deleted[i], false);\r
+        }\r
       }\r
-      catch (Exception ex)\r
-      {}\r
+\r
+      historyList.push(cut);\r
+\r
+      ap.alignFrame.addHistoryItem(cut);\r
+\r
+      ap.av.firePropertyChange("alignment", null,\r
+                               ap.av.getAlignment().getSequences());\r
     }\r
-    ap.av.updateConservation();\r
-    ap.av.updateConsensus();\r
-    af.updateEditMenuBar();\r
-    ap.repaint();\r
 \r
   }\r
 \r
+  /**\r
+   * DOCUMENT ME!\r
+   *\r
+   * @param e DOCUMENT ME!\r
+   */\r
   public void undoButton_actionPerformed(ActionEvent e)\r
   {\r
-    HistoryItem hi = (HistoryItem) historyList.pop();\r
-    af.restoreHistoryItem(hi);\r
+    CommandI command = (CommandI) historyList.pop();\r
+    command.undoCommand();\r
+\r
+    if (ap.av.historyList.contains(command))\r
+    {\r
+      ap.av.historyList.remove(command);\r
+      af.updateEditMenuBar();\r
+    }\r
+\r
+    ap.paintAlignment(true);\r
+\r
     if (historyList.size() == 0)\r
     {\r
       undoButton.setEnabled(false);\r
-      if (af.historyList.contains(hi))\r
-      {\r
-        af.historyList.remove(hi);\r
-        af.updateEditMenuBar();\r
-      }\r
     }\r
   }\r
+\r
+  /**\r
+   * DOCUMENT ME!\r
+   *\r
+   * @param e DOCUMENT ME!\r
+   */\r
   public void valueField_actionPerformed(ActionEvent e)\r
   {\r
     try\r
@@ -145,4 +331,5 @@ public class RedundancyPanel
       valueField.setText(slider.getValue() + "");\r
     }\r
   }\r
+\r
 }\r