Merge remote-tracking branch 'remotes/origin/2_5_1_rna_merge' into develop (JAL-826)
[jalview.git] / src / jalview / appletgui / AppletJmol.java
index fb15c82..f3d7fa7 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
- * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
  * 
  * This file is part of Jalview.
  * 
@@ -33,7 +33,7 @@ import org.jmol.viewer.JmolConstants;
 
 import jalview.schemes.*;
 
-public class AppletJmol extends EmbmenuFrame implements 
+public class AppletJmol extends EmbmenuFrame implements
 // StructureListener,
         KeyListener, ActionListener, ItemListener, SequenceStructureBinding
 
@@ -52,6 +52,8 @@ public class AppletJmol extends EmbmenuFrame implements
 
   CheckboxMenuItem seqColour = new CheckboxMenuItem("By Sequence", true);
 
+  CheckboxMenuItem jmolColour = new CheckboxMenuItem("Using Jmol", false);
+  
   MenuItem chain = new MenuItem("By Chain");
 
   MenuItem charge = new MenuItem("Charge & Cysteine");
@@ -69,6 +71,8 @@ public class AppletJmol extends EmbmenuFrame implements
   MenuItem turn = new MenuItem("Turn Propensity");
 
   MenuItem buried = new MenuItem("Buried Index");
+  
+  MenuItem purinepyrimidine = new MenuItem("Purine/Pyrimidine");
 
   MenuItem user = new MenuItem("User Defined Colours");
 
@@ -83,6 +87,7 @@ public class AppletJmol extends EmbmenuFrame implements
   RenderPanel renderPanel;
 
   AlignmentPanel ap;
+  ArrayList _aps = new ArrayList();
 
   String fileLoadingError;
 
@@ -99,12 +104,40 @@ public class AppletJmol extends EmbmenuFrame implements
    */
   String protocol = null;
 
+  /**
+   * Load a bunch of pdb entries associated with sequences in the alignment and
+   * display them - aligning them if necessary.
+   * 
+   * @param pdbentries
+   *          each pdb file (at least one needed)
+   * @param boundseqs
+   *          each set of sequences for each pdb file (must match number of pdb
+   *          files)
+   * @param boundchains
+   *          the target pdb chain corresponding with each sequence associated
+   *          with each pdb file (may be null at any level)
+   * @param align
+   *          true/false
+   * @param ap
+   *          associated alignment
+   * @param protocol
+   *          how to get pdb data
+   */
+  public AppletJmol(PDBEntry[] pdbentries, SequenceI[][] boundseqs,
+          String[][] boundchains, boolean align, AlignmentPanel ap,
+          String protocol)
+  {
+    throw new Error("Not yet implemented.");
+  }
+
   public AppletJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
           AlignmentPanel ap, String protocol)
   {
     this.ap = ap;
-    jmb = new AppletJmolBinding(this, new PDBEntry[]
-    { pdbentry }, seq, chains, protocol);
+    jmb = new AppletJmolBinding(this, ap.getStructureSelectionManager(), new PDBEntry[]
+    { pdbentry }, new SequenceI[][]
+    { seq }, new String[][]
+    { chains }, protocol);
     jmb.setColourBySequence(true);
     if (pdbentry.getId() == null || pdbentry.getId().length() < 1)
     {
@@ -126,12 +159,12 @@ public class AppletJmol extends EmbmenuFrame implements
     }
 
     String alreadyMapped = StructureSelectionManager
-            .getStructureSelectionManager().alreadyMappedToFile(
+            .getStructureSelectionManager(ap.av.applet).alreadyMappedToFile(
                     pdbentry.getId());
     MCview.PDBfile reader = null;
     if (alreadyMapped != null)
     {
-      reader = StructureSelectionManager.getStructureSelectionManager()
+      reader = StructureSelectionManager.getStructureSelectionManager(ap.av.applet)
               .setMapping(seq, chains, pdbentry.getFile(), protocol);
       // PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
       // FOR NOW, LETS JUST OPEN A NEW WINDOW
@@ -149,14 +182,16 @@ public class AppletJmol extends EmbmenuFrame implements
     hydro.addActionListener(this);
     chain.addActionListener(this);
     seqColour.addItemListener(this);
+    jmolColour.addItemListener(this);
     zappo.addActionListener(this);
     taylor.addActionListener(this);
     helix.addActionListener(this);
     strand.addActionListener(this);
     turn.addActionListener(this);
     buried.addActionListener(this);
+    purinepyrimidine.addActionListener(this);
     user.addActionListener(this);
-
+    
     jmolHelp.addActionListener(this);
 
     coloursMenu.add(seqColour);
@@ -169,18 +204,37 @@ public class AppletJmol extends EmbmenuFrame implements
     coloursMenu.add(strand);
     coloursMenu.add(turn);
     coloursMenu.add(buried);
+    coloursMenu.add(purinepyrimidine);
     coloursMenu.add(user);
-
+    coloursMenu.add(jmolColour);
     helpMenu.add(jmolHelp);
+    this.setLayout(new BorderLayout());
 
     setMenuBar(menuBar);
 
     renderPanel = new RenderPanel();
     embedMenuIfNeeded(renderPanel);
     this.add(renderPanel, BorderLayout.CENTER);
-    jmb.allocateViewer(renderPanel, 
-            "jalviewJmol", ap.av.applet.getDocumentBase(), ap.av.applet
-                    .getCodeBase(), "");
+    scriptWindow = new Panel();
+    scriptWindow.setVisible(false);
+    // this.add(scriptWindow, BorderLayout.SOUTH);
+
+    try
+    {
+      jmb.allocateViewer(renderPanel, true, ap.av.applet.getName()
+              + "_jmol_", ap.av.applet.getDocumentBase(),
+              ap.av.applet.getCodeBase(), "-applet", scriptWindow, null);
+    } catch (Exception e)
+    {
+      System.err
+              .println("Couldn't create a jmol viewer. Args to allocate viewer were:\nDocumentBase="
+                      + ap.av.applet.getDocumentBase()
+                      + "\nCodebase="
+                      + ap.av.applet.getCodeBase());
+      e.printStackTrace();
+      dispose();
+      return;
+    }
     jmb.newJmolPopup(true, "Jmol", true);
 
     this.addWindowListener(new WindowAdapter()
@@ -190,12 +244,18 @@ public class AppletJmol extends EmbmenuFrame implements
         closeViewer();
       }
     });
-
+    if (pdbentry.getProperty() == null)
+    {
+      pdbentry.setProperty(new Hashtable());
+      pdbentry.getProperty().put("protocol", protocol);
+    }
     if (pdbentry.getFile() != null)
     {
       // import structure data from pdbentry.getFile based on given protocol
       if (protocol.equals(AppletFormatAdapter.PASTE))
       {
+        // TODO: JAL-623 : correctly record file contents for matching up later
+        // pdbentry.getProperty().put("pdbfilehash",""+pdbentry.getFile().hashCode());
         loadInline(pdbentry.getFile());
       }
       else if (protocol.equals(AppletFormatAdapter.FILE)
@@ -244,7 +304,8 @@ public class AppletJmol extends EmbmenuFrame implements
             throw new Exception(
                     "Invalid datasource. Could not obtain Reader.");
           }
-          jmb.viewer.openReader(pdbentry.getFile(), pdbentry.getId(), freader);
+          jmb.viewer.openReader(pdbentry.getFile(), pdbentry.getId(),
+                  freader);
         } catch (Exception e)
         {
           // give up!
@@ -259,28 +320,10 @@ public class AppletJmol extends EmbmenuFrame implements
     jalview.bin.JalviewLite.addFrame(this, jmb.getViewerTitle(), 400, 400);
   }
 
-  /**
-   * create a new binding between structures in an existing jmol viewer instance
-   * and an alignpanel with sequences that have existing PDBFile entries. Note,
-   * this does not open a new Jmol window, or modify the display of the
-   * structures in the original jmol window.
-   * 
-   * @param viewer2
-   * @param alignPanel
-   * @param seqs
-   *          - sequences to search for associations
-   */
-  public AppletJmol(JmolViewer viewer2, AlignmentPanel alignPanel,
-          SequenceI[] seqs)
-  {
-
-    // TODO Auto-generated constructor stub
-  }
-
   public void loadInline(String string)
   {
     loadedInline = true;
-    jmb.viewer.openStringInline(string);
+    jmb.loadInline(string);
   }
 
   void setChainMenuItems(Vector chains)
@@ -339,15 +382,25 @@ public class AppletJmol extends EmbmenuFrame implements
       Frame frame = new Frame();
       frame.add(cap);
 
-      jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping",
-              550, 600);
       StringBuffer sb = new StringBuffer();
-      for (int s = 0; s < jmb.pdbentry.length; s++)
+      try
+      {
+        for (int s = 0; s < jmb.pdbentry.length; s++)
+        {
+          sb.append(jmb.printMapping(
+                          jmb.pdbentry[s].getFile()));
+          sb.append("\n");
+        }
+        cap.setText(sb.toString());
+      } catch (OutOfMemoryError ex)
       {
-        sb.append(StructureSelectionManager.getStructureSelectionManager()
-                .printMapping(jmb.pdbentry[s].getFile()));
-        sb.append("\n");
+        frame.dispose();
+        System.err
+                .println("Out of memory when trying to create dialog box with sequence-structure mapping.");
+        return;
       }
+      jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping",
+              550, 600);
     }
     else if (evt.getSource() == charge)
     {
@@ -395,6 +448,10 @@ public class AppletJmol extends EmbmenuFrame implements
       setEnabled(buried);
       jmb.setJalviewColourScheme(new BuriedColourScheme());
     }
+    else if(evt.getSource() == purinepyrimidine)
+    {
+       jmb.setJalviewColourScheme(new PurinePyrimidineColourScheme());
+    }
     else if (evt.getSource() == user)
     {
       setEnabled(user);
@@ -427,23 +484,29 @@ public class AppletJmol extends EmbmenuFrame implements
   }
 
   /**
-   * tick or untick the seqColour menu entry depending upon if it was selected or not.
+   * tick or untick the seqColour menu entry or jmoColour entry depending upon if it was selected
+   * or not.
+   * 
    * @param itm
    */
   private void setEnabled(MenuItem itm)
   {
-    seqColour.setState(itm==seqColour);
-    jmb.setColourBySequence(itm==seqColour);
+    jmolColour.setState(itm == jmolColour);
+    seqColour.setState(itm == seqColour);
+    jmb.setColourBySequence(itm == seqColour);
   }
 
   public void itemStateChanged(ItemEvent evt)
   {
+    if (evt.getSource() == jmolColour)
+    {
+      setEnabled(jmolColour);
+      jmb.setColourBySequence(false);
+    } else
     if (evt.getSource() == seqColour)
     {
       setEnabled(seqColour);
-      jmb
-              .colourBySequence(ap.av.getShowSequenceFeatures(),
-                      ap.av.alignment);
+      jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
     }
     else if (!allChainsSelected)
       centerViewer();
@@ -471,7 +534,7 @@ public class AppletJmol extends EmbmenuFrame implements
   public void updateColours(Object source)
   {
     AlignmentPanel ap = (AlignmentPanel) source;
-    jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap.av.alignment);
+    jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
   }
 
   public void updateTitleAndMenus()
@@ -482,7 +545,7 @@ public class AppletJmol extends EmbmenuFrame implements
       return;
     }
     setChainMenuItems(jmb.chainNames);
-    jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap.av.alignment);
+    jmb.colourBySequence(ap.av.getShowSequenceFeatures(), ap);
 
     setTitle(jmb.getViewerTitle());
   }
@@ -498,22 +561,30 @@ public class AppletJmol extends EmbmenuFrame implements
     }
   }
 
+  Panel splitPane = null;
+
   public void showConsole(boolean showConsole)
   {
-    if (scriptWindow == null)
-    {
-      scriptWindow = new Panel(new BorderLayout());
-      inputLine = new TextField();
-      history = new TextArea(5, 40);
-      scriptWindow.add(history, BorderLayout.CENTER);
-      scriptWindow.add(inputLine, BorderLayout.SOUTH);
-      add(scriptWindow, BorderLayout.SOUTH);
+    if (showConsole)
+    {
+      remove(renderPanel);
+      splitPane = new Panel();
+
+      splitPane.setLayout(new java.awt.GridLayout(2, 1));
+      splitPane.add(renderPanel);
+      splitPane.add(scriptWindow);
+      scriptWindow.setVisible(true);
+      this.add(splitPane, BorderLayout.CENTER);
+      splitPane.setVisible(true);
+      splitPane.validate();
+    }
+    else
+    {
       scriptWindow.setVisible(false);
-      history.setEditable(false);
-      inputLine.addKeyListener(this);
+      remove(splitPane);
+      add(renderPanel, BorderLayout.CENTER);
+      splitPane = null;
     }
-
-    scriptWindow.setVisible(!scriptWindow.isVisible());
     validate();
   }
 
@@ -555,37 +626,39 @@ public class AppletJmol extends EmbmenuFrame implements
       }
     }
   }
-/*
-  @Override
-  public Color getColour(int atomIndex, int pdbResNum, String chain,
-          String pdbId)
-  {
-    return jmb.getColour(atomIndex, pdbResNum, chain, pdbId);
-  }
-
-  @Override
-  public String[] getPdbFile()
-  {
-    return jmb.getPdbFile();
-  }
 
-  @Override
-  public void highlightAtom(int atomIndex, int pdbResNum, String chain,
-          String pdbId)
+  /*
+   * @Override public Color getColour(int atomIndex, int pdbResNum, String
+   * chain, String pdbId) { return jmb.getColour(atomIndex, pdbResNum, chain,
+   * pdbId); }
+   * 
+   * @Override public String[] getPdbFile() { return jmb.getPdbFile(); }
+   * 
+   * @Override public void highlightAtom(int atomIndex, int pdbResNum, String
+   * chain, String pdbId) { jmb.highlightAtom(atomIndex, pdbResNum, chain,
+   * pdbId);
+   * 
+   * }
+   * 
+   * @Override public void mouseOverStructure(int atomIndex, String strInfo) {
+   * jmb.mouseOverStructure(atomIndex, strInfo);
+   * 
+   * }
+   */
+  public void setJalviewColourScheme(UserColourScheme ucs)
   {
-    jmb.highlightAtom(atomIndex, pdbResNum, chain, pdbId);
-
+    jmb.setJalviewColourScheme(ucs);
   }
 
-  @Override
-  public void mouseOverStructure(int atomIndex, String strInfo)
-  {
-    jmb.mouseOverStructure(atomIndex, strInfo);
-
-  }
-*/
-  public void setJalviewColourScheme(UserColourScheme ucs)
+  public AlignmentPanel getAlignmentPanelFor(AlignmentI alignment)
   {
-    jmb.setJalviewColourScheme(ucs);
+    for (int i=0;i<_aps.size();i++)
+    {
+      if (((AlignmentPanel)_aps.get(i)).av.getAlignment()==alignment)
+      {
+        return ((AlignmentPanel)_aps.get(i));
+      }
+    }
+    return ap;
   }
 }