incorrect colour-by-sequence when gaps are present in alignment sequence and pdb...
[jalview.git] / src / jalview / appletgui / AppletJmol.java
index 538d177..1202c68 100644 (file)
@@ -34,7 +34,7 @@ import org.jmol.popup.*;
 import jalview.schemes.*;
 
 
-public class AppletJmol extends Frame
+public class AppletJmol extends EmbmenuFrame
     implements  StructureListener, JmolStatusListener,
     KeyListener, ActionListener, ItemListener
 
@@ -99,29 +99,6 @@ public class AppletJmol extends Frame
        //PROMPT USER HERE TO ADD TO NEW OR EXISTING VIEW?
        //FOR NOW, LETS JUST OPEN A NEW WINDOW
     }
-
-    renderPanel = new RenderPanel();
-
-    this.add(renderPanel, BorderLayout.CENTER);
-    viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter());
-
-    viewer.setAppletContext("jalview",
-                       ap.av.applet.getDocumentBase(),
-                            ap.av.applet.getCodeBase(),
-                            null);
-
-    viewer.setJmolStatusListener(this);
-
-    jmolpopup = JmolPopup.newJmolPopup(viewer);
-
-    this.addWindowListener(new WindowAdapter()
-        {
-          public void windowClosing(WindowEvent evt)
-          {
-            closeViewer();
-          }
-        });
-
     MenuBar menuBar = new MenuBar();
     menuBar.add(fileMenu);
     fileMenu.add(mappingMenuItem);
@@ -159,7 +136,30 @@ public class AppletJmol extends Frame
 
     helpMenu.add(jmolHelp);
 
-    this.setMenuBar(menuBar);
+    setMenuBar(menuBar);
+
+    renderPanel = new RenderPanel();
+    embedMenuIfNeeded(renderPanel);
+    this.add(renderPanel, BorderLayout.CENTER);
+    viewer = JmolViewer.allocateViewer(renderPanel, new SmarterJmolAdapter());
+
+    viewer.setAppletContext("jalview",
+                       ap.av.applet.getDocumentBase(),
+                            ap.av.applet.getCodeBase(),
+                            null);
+
+    viewer.setJmolStatusListener(this);
+
+    jmolpopup = JmolPopup.newJmolPopup(viewer);
+
+    this.addWindowListener(new WindowAdapter()
+        {
+          public void windowClosing(WindowEvent evt)
+          {
+            closeViewer();
+          }
+        });
+
 
     if(pdbentry.getFile()!=null)
     {
@@ -354,6 +354,7 @@ public class AppletJmol extends Frame
   {
     if (evt.getSource() == seqColour)
     {
+      lastCommand = null;
       colourBySequence = seqColour.getState();
       colourBySequence(ap);
     }
@@ -441,14 +442,11 @@ public class AppletJmol extends Frame
       resetLastRes.append(":" + chain);
     }
 
-    eval.append(";color gold;wireframe 100");
+    eval.append(";wireframe 100;"+eval.toString()+".CA;");
 
-    Color col = new Color(viewer.getAtomArgb(atomIndex));
+    resetLastRes.append(";wireframe 0;"+resetLastRes.toString()+".CA;spacefill 0;");
 
-    resetLastRes.append(";color["
-                        + col.getRed() + ","
-                        + col.getGreen() + ","
-                        + col.getBlue() + "];wireframe 0");
+    eval.append("spacefill 200;select none");
 
     viewer.evalStringQuiet(eval.toString());
 
@@ -470,13 +468,14 @@ public class AppletJmol extends Frame
     return new Color(viewer.getAtomArgb(atomIndex));
   }
 
-  FeatureRenderer fr;
   String lastCommand;
-  public void colourBySequence(AlignmentPanel ap)
+  FeatureRenderer fr=null;
+  public void colourBySequence(AlignmentPanel sourceap)
   {
-    if(!colourBySequence)
-      return;
+    this.ap = sourceap;
 
+    if (!colourBySequence)
+      return;
 
     StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
 
@@ -486,6 +485,7 @@ public class AppletJmol extends Frame
     SequenceRenderer sr = new SequenceRenderer(ap.av);
 
     boolean showFeatures = false;
+
     if (ap.av.showSequenceFeatures)
     {
       showFeatures = true;
@@ -502,17 +502,24 @@ public class AppletJmol extends Frame
     int lastPos = -1;
     for (int s = 0; s < sequence.length; s++)
     {
-      for (int m = 0; m < mapping.length; m++)
+      for (int sp,m = 0; m < mapping.length; m++)
       {
         if (mapping[m].getSequence() == sequence[s]
-            && ap.av.alignment.findIndex(sequence[s])>-1)
+            && (sp=ap.av.alignment.findIndex(sequence[s])) > -1)
         {
-          for (int r = 0; r < sequence[s].getLength(); r++)
+          SequenceI asp = ap.av.alignment.getSequenceAt(sp);
+          for (int r = 0; r < asp.getLength(); r++)
           {
+            // no mapping to gaps in sequence
+            if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
+            {
+              continue;
+            }
             int pos = mapping[m].getPDBResNum(
-                sequence[s].findPosition(r));
+                    asp.findPosition(r));
+
 
-            if (pos < 1 || pos==lastPos)
+            if (pos < 1 || pos == lastPos)
               continue;
 
             lastPos = pos;
@@ -522,7 +529,7 @@ public class AppletJmol extends Frame
             if (showFeatures)
               col = fr.findFeatureColour(col, sequence[s], r);
 
-            if (command.toString().endsWith(":" + mapping[m].getChain()+
+            if (command.toString().endsWith(":" + mapping[m].getChain() +
                                             ";color["
                                             + col.getRed() + ","
                                             + col.getGreen() + ","
@@ -540,22 +547,23 @@ public class AppletJmol extends Frame
             }
 
             command.append(";color["
-                             + col.getRed() + ","
-                             + col.getGreen() + ","
-                             + col.getBlue() + "]");
-
+                           + col.getRed() + ","
+                           + col.getGreen() + ","
+                           + col.getBlue() + "]");
           }
           break;
         }
       }
     }
 
-    if (lastCommand != null && !lastCommand.equals(command.toString()))
+    if (lastCommand == null || !lastCommand.equals(command.toString()))
+    {
       viewer.evalStringQuiet(command.toString());
-
+    }
     lastCommand = command.toString();
   }
 
+
   StringBuffer condenseCommand(String command, int pos)
   {
 
@@ -727,7 +735,7 @@ public class AppletJmol extends Frame
       picked+=strInfo.substring(strInfo.indexOf(":")+1,
                                strInfo.indexOf("."));
 
-    picked+=".C";
+    picked+=".CA";
 
     if (!atomsPicked.contains(picked))
     {