Jalview colours added to Jmol
[jalview.git] / src / jalview / gui / AppJMol.java
index 7b17d68..575c6ec 100644 (file)
@@ -32,6 +32,7 @@ import jalview.gui.*;
 import jalview.structure.*;\r
 import jalview.datamodel.PDBEntry;\r
 import jalview.io.*;\r
+import jalview.schemes.*;\r
 \r
 import org.jmol.api.*;\r
 import org.jmol.adapter.smarter.SmarterJmolAdapter;\r
@@ -49,9 +50,41 @@ public class AppJMol
   PDBEntry pdbentry;\r
   SequenceI[] sequence;\r
   StructureSelectionManager ssm;\r
+  JSplitPane splitPane;\r
   RenderPanel renderPanel;\r
   AlignmentPanel ap;\r
   String fileLoadingError;\r
+  boolean colourBySequence = true;\r
+\r
+  public AppJMol(String file, String id,\r
+                 SequenceI[] seq,\r
+                 AlignmentPanel ap,\r
+                 String loadStatus,\r
+                 Rectangle bounds)\r
+  {\r
+    pdbentry = new PDBEntry();\r
+    pdbentry.setFile(file);\r
+    pdbentry.setId(id);\r
+    this.sequence = seq;\r
+    this.ap = ap;\r
+    this.setBounds(bounds);\r
+\r
+    colourBySequence = false;\r
+    seqColour.setSelected(false);\r
+\r
+    jalview.gui.Desktop.addInternalFrame(this, "Loading File",\r
+                                         bounds.width,bounds.height);\r
+\r
+    initJmol(loadStatus);\r
+\r
+    this.addInternalFrameListener(new InternalFrameAdapter()\r
+    {\r
+      public void internalFrameClosing(InternalFrameEvent internalFrameEvent)\r
+      {\r
+        closeViewer();\r
+      }\r
+    });\r
+  }\r
 \r
   public AppJMol(PDBEntry pdbentry, SequenceI[] seq, AlignmentPanel ap)\r
   {\r
@@ -60,6 +93,7 @@ public class AppJMol
     String alreadyMapped = StructureSelectionManager\r
         .getStructureSelectionManager()\r
         .alreadyMappedToFile(pdbentry.getId());\r
+\r
     if (alreadyMapped != null)\r
     {\r
       int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,\r
@@ -81,14 +115,11 @@ public class AppJMol
     this.pdbentry = pdbentry;\r
     this.sequence = seq;\r
 \r
-    renderPanel = new RenderPanel();\r
-    this.getContentPane().add(renderPanel, java.awt.BorderLayout.CENTER);\r
-\r
     jalview.gui.Desktop.addInternalFrame(this, "Loading File", 400, 400);\r
 \r
     if (pdbentry.getFile() != null)\r
     {\r
-      initJmol();\r
+      initJmol("load \""+pdbentry.getFile()+"\"");\r
     }\r
     else\r
     {\r
@@ -105,8 +136,12 @@ public class AppJMol
     });\r
   }\r
 \r
-  void initJmol()\r
+  void initJmol(String command)\r
   {\r
+    renderPanel = new RenderPanel();\r
+\r
+    this.getContentPane().add(renderPanel, java.awt.BorderLayout.CENTER);\r
+\r
     StringBuffer title = new StringBuffer(sequence[0].getName() + ":" +\r
                                           pdbentry.getId());\r
 \r
@@ -129,16 +164,14 @@ public class AppJMol
     viewer = org.jmol.api.JmolViewer.allocateViewer(renderPanel,\r
         new SmarterJmolAdapter());\r
 \r
+\r
     viewer.setAppletContext("", null, null, "");\r
 \r
     viewer.setJmolStatusListener(this);\r
 \r
-    scriptWindow = new ScriptWindow(viewer);\r
-\r
     jmolpopup = JmolPopup.newJmolPopup(viewer);\r
 \r
-    viewer.openFile(pdbentry.getFile());\r
-\r
+    viewer.evalStringQuiet(command);\r
   }\r
 \r
 \r
@@ -223,7 +256,7 @@ public class AppJMol
       String query = "pdb:" + pdbentry.getId();\r
       pdbentry.setFile(ebi.fetchDataAsFile(query, "default", "raw")\r
                        .getAbsolutePath());\r
-      initJmol();\r
+      initJmol("load "+pdbentry.getFile());\r
     }\r
     catch (Exception ex)\r
     {\r
@@ -334,6 +367,116 @@ public class AppJMol
     }\r
   }\r
 \r
+\r
+  public void seqColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    colourBySequence = seqColour.isSelected();\r
+    colourBySequence(ap);\r
+  }\r
+\r
+  public void chainColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    colourBySequence = false;\r
+    seqColour.setSelected(false);\r
+    viewer.evalStringQuiet("select *;color chain");\r
+  }\r
+\r
+  public void chargeColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    colourBySequence = false;\r
+    seqColour.setSelected(false);\r
+    viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"\r
+                      +"select LYS,ARG;color blue;select CYS;color yellow");\r
+  }\r
+\r
+  public void zappoColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    setJalviewColourScheme(new ZappoColourScheme());\r
+  }\r
+\r
+  public void taylorColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    setJalviewColourScheme(new TaylorColourScheme());\r
+  }\r
+\r
+  public void hydroColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    setJalviewColourScheme(new HydrophobicColourScheme());\r
+  }\r
+\r
+  public void helixColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    setJalviewColourScheme(new HelixColourScheme());\r
+  }\r
+\r
+  public void strandColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    setJalviewColourScheme(new StrandColourScheme());\r
+  }\r
+\r
+  public void turnColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    setJalviewColourScheme(new TurnColourScheme());\r
+  }\r
+\r
+  public void buriedColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    setJalviewColourScheme(new BuriedColourScheme());\r
+  }\r
+\r
+  public void setJalviewColourScheme(ColourSchemeI cs)\r
+  {\r
+    colourBySequence = false;\r
+    seqColour.setSelected(false);\r
+\r
+    if(cs==null)\r
+      return;\r
+\r
+    String res;\r
+    int index;\r
+    Color col;\r
+\r
+    Enumeration en = ResidueProperties.aa3Hash.keys();\r
+    StringBuffer command = new StringBuffer("select *;color white;");\r
+    while(en.hasMoreElements())\r
+    {\r
+      res = en.nextElement().toString();\r
+      index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();\r
+      if(index>20)\r
+        continue;\r
+\r
+      col = cs.findColour(ResidueProperties.aa[index].charAt(0));\r
+\r
+      command.append("select "+res+";color["\r
+                        + col.getRed() + ","\r
+                        + col.getGreen() + ","\r
+                        + col.getBlue() + "];");\r
+    }\r
+\r
+    viewer.evalStringQuiet(command.toString());\r
+  }\r
+\r
+  public void userColour_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    new UserDefinedColours(this, null);\r
+  }\r
+\r
+  public void backGround_actionPerformed(ActionEvent actionEvent)\r
+  {\r
+    java.awt.Color col = JColorChooser.showDialog(this,\r
+                                                  "Select Background Colour",\r
+                                                  null);\r
+\r
+    if (col != null)\r
+    {\r
+      viewer.evalStringQuiet("background ["\r
+                        + col.getRed() + ","\r
+                        + col.getGreen() + ","\r
+                        + col.getBlue() + "];");\r
+    }\r
+  }\r
+\r
+\r
   //////////////////////////////////\r
   ///StructureListener\r
   public String getPdbFile()\r
@@ -416,6 +559,10 @@ public class AppJMol
   FeatureRenderer fr;\r
   public void colourBySequence(AlignmentPanel ap)\r
   {\r
+    if(!colourBySequence)\r
+      return;\r
+\r
+\r
     StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());\r
 \r
     if (mapping.length < 1)\r
@@ -454,6 +601,21 @@ public class AppJMol
 \r
             lastPos = pos;\r
 \r
+            Color col = sr.getResidueBoxColour(sequence[s], r);\r
+\r
+            if (showFeatures)\r
+              col = fr.findFeatureColour(col, sequence[s], r);\r
+\r
+            if (command.toString().endsWith(":" + mapping[m].getChain()+\r
+                                            ";color["\r
+                                            + col.getRed() + ","\r
+                                            + col.getGreen() + ","\r
+                                            + col.getBlue() + "]"))\r
+            {\r
+              command = condenseCommand(command, pos);\r
+              continue;\r
+            }\r
+\r
             command.append(";select " + pos);\r
 \r
             if (!mapping[m].getChain().equals(" "))\r
@@ -461,17 +623,13 @@ public class AppJMol
               command.append(":" + mapping[m].getChain());\r
             }\r
 \r
-            Color col = sr.getResidueBoxColour(sequence[s], r);\r
-\r
-            if (showFeatures)\r
-              col = fr.findFeatureColour(col, sequence[s], r);\r
-\r
-            command.append("; color ["\r
-                           + col.getRed() + ","\r
-                           + col.getGreen() + ","\r
-                           + col.getBlue() + "]");\r
+            command.append(";color["\r
+                             + col.getRed() + ","\r
+                             + col.getGreen() + ","\r
+                             + col.getBlue() + "]");\r
 \r
           }\r
+          break;\r
         }\r
       }\r
     }\r
@@ -479,6 +637,28 @@ public class AppJMol
     viewer.evalStringQuiet(command.toString());\r
   }\r
 \r
+  StringBuffer condenseCommand(StringBuffer command, int pos)\r
+  {\r
+    StringBuffer sb = new StringBuffer(command.substring(0, command.lastIndexOf("select")+7));\r
+\r
+    command.delete(0, sb.length());\r
+\r
+    String start;\r
+\r
+    if (command.indexOf("-") > -1)\r
+    {\r
+      start = command.substring(0,command.indexOf("-"));\r
+    }\r
+    else\r
+    {\r
+      start = command.substring(0, command.indexOf(":"));\r
+    }\r
+\r
+    sb.append(start+"-"+pos+command.substring(command.indexOf(":")));\r
+\r
+    return sb;\r
+  }\r
+\r
   /////////////////////////////////\r
   //JmolStatusListener\r
 \r
@@ -597,25 +777,33 @@ public class AppJMol
 \r
   public void showConsole(boolean showConsole)\r
   {\r
-    final String title = "Jmol Script Window for: "+this.getTitle();\r
     if (scriptWindow == null)\r
-      return;\r
-    if (showConsole)\r
+      scriptWindow = new ScriptWindow(this);\r
+\r
+    if(showConsole)\r
     {\r
-      javax.swing.SwingUtilities.invokeLater(new Runnable()\r
+      if(splitPane==null)\r
       {\r
-        public void run()\r
-        {\r
-          jalview.gui.Desktop.addInternalFrame(scriptWindow,\r
-                                               title,\r
-                                               400,\r
-                                               300);\r
-        }\r
-      });\r
+        splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);\r
+        splitPane.setTopComponent(renderPanel);\r
+        splitPane.setBottomComponent(scriptWindow);\r
+        this.getContentPane().add(splitPane, BorderLayout.CENTER);\r
+      }\r
 \r
+      splitPane.setDividerLocation(getHeight()-200);\r
+      splitPane.validate();\r
     }\r
     else\r
-      scriptWindow.setVisible(false);\r
+    {\r
+      if (splitPane != null)\r
+        splitPane.setVisible(false);\r
+\r
+      splitPane = null;\r
+\r
+      this.getContentPane().add(renderPanel, BorderLayout.CENTER);\r
+    }\r
+\r
+    validate();\r
   }\r
 \r
   public float functionXY(String functionName, int x, int y)\r