tooltip displays formatted annotation row description including sequence reference...
[jalview.git] / src / jalview / appletgui / AppletJmol.java
index a48e77e..82a5911 100644 (file)
@@ -29,11 +29,12 @@ import jalview.io.*;
 
 import org.jmol.api.*;
 import org.jmol.adapter.smarter.SmarterJmolAdapter;
+
 import org.jmol.popup.*;
 import jalview.schemes.*;
 
 
-public class AppletJmol extends Frame
+public class AppletJmol extends EmbmenuFrame
     implements  StructureListener, JmolStatusListener,
     KeyListener, ActionListener, ItemListener
 
@@ -66,6 +67,7 @@ public class AppletJmol extends Frame
   TextField inputLine;
   TextArea history;
   SequenceI[] sequence;
+  String [] chains;
   StructureSelectionManager ssm;
   RenderPanel renderPanel;
   AlignmentPanel ap;
@@ -77,11 +79,13 @@ public class AppletJmol extends Frame
 
   public AppletJmol(PDBEntry pdbentry,
                     SequenceI[] seq,
+                    String[] chains,
                     AlignmentPanel ap,
                     String protocol)
   {
     this.ap = ap;
     this.sequence = seq;
+    this.chains = chains;
     this.pdbentry = pdbentry;
 
    String alreadyMapped = StructureSelectionManager
@@ -91,33 +95,10 @@ public class AppletJmol extends Frame
     if (alreadyMapped != null)
     {
        StructureSelectionManager.getStructureSelectionManager()
-            .setMapping(seq, pdbentry.getFile(), protocol);
-        return;
+            .setMapping(seq, chains, pdbentry.getFile(), protocol);
+       //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);
@@ -155,19 +136,62 @@ 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)
     {
+      // import structure data from pdbentry.getFile based on given protocol
       if (protocol.equals(AppletFormatAdapter.PASTE))
+      { 
         loadInline(pdbentry.getFile());
-      else
+      } else
+      if (protocol.equals(AppletFormatAdapter.FILE) || protocol.equals(AppletFormatAdapter.URL)){
           viewer.openFile(pdbentry.getFile());
+      }
+      else
+        {
+        // probably CLASSLOADER based datasource..
+        // Try and get a reader on the datasource, and pass that to Jmol
+        try {
+          FileParse fparser = new jalview.io.FileParse(protocol, pdbentry.getFile());
+          if (!fparser.isValid())
+          {
+            throw new Exception("Invalid datasource. "+fparser.getWarningMessage());
+          }
+          viewer.openReader(pdbentry.getFile(), pdbentry.getId(), fparser.getReader());
+        } catch (Exception e)
+        {
+          // give up!
+          System.err.println("Couldn't access pdbentry id="+pdbentry.getId()+" and file="+pdbentry.getFile()+" using protocol="+protocol);
+          e.printStackTrace();
+        }
+        }
     }
-
-    this.setBounds(400, 400, 400, 400);
-
-    this.setVisible(true);
+    
+    jalview.bin.JalviewLite.addFrame(this, "Jmol", 400,400);
   }
 
   public void loadInline(String string)
@@ -352,6 +376,7 @@ public class AppletJmol extends Frame
   {
     if (evt.getSource() == seqColour)
     {
+      lastCommand = null;
       colourBySequence = seqColour.getState();
       colourBySequence(ap);
     }
@@ -439,14 +464,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());
 
@@ -460,21 +482,32 @@ public class AppletJmol extends Frame
 //End StructureListener
 ////////////////////////////
 
-  FeatureRenderer fr;
-  public void colourBySequence(AlignmentPanel ap)
+  public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
   {
-    if(!colourBySequence)
-      return;
+    if (!pdbfile.equals(pdbentry.getFile()))
+      return null;
+
+    return new Color(viewer.getAtomArgb(atomIndex));
+  }
 
+  String lastCommand;
+  FeatureRenderer fr=null;
+  public void colourBySequence(AlignmentPanel sourceap)
+  {
+    this.ap = sourceap;
+
+    if (!colourBySequence)
+      return;
 
     StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
 
     if (mapping.length < 1)
       return;
 
-    SequenceRenderer sr = ap.seqPanel.seqCanvas.getSequenceRenderer();
+    SequenceRenderer sr = new SequenceRenderer(ap.av);
 
     boolean showFeatures = false;
+
     if (ap.av.showSequenceFeatures)
     {
       showFeatures = true;
@@ -491,16 +524,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])
+        if (mapping[m].getSequence() == sequence[s]
+            && (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;
@@ -510,7 +551,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() + ","
@@ -528,19 +569,23 @@ public class AppletJmol extends Frame
             }
 
             command.append(";color["
-                             + col.getRed() + ","
-                             + col.getGreen() + ","
-                             + col.getBlue() + "]");
-
+                           + col.getRed() + ","
+                           + col.getGreen() + ","
+                           + col.getBlue() + "]");
           }
           break;
         }
       }
     }
 
-    viewer.evalStringQuiet(command.toString());
+    if (lastCommand == null || !lastCommand.equals(command.toString()))
+    {
+      viewer.evalStringQuiet(command.toString());
+    }
+    lastCommand = command.toString();
   }
 
+
   StringBuffer condenseCommand(String command, int pos)
   {
 
@@ -606,14 +651,14 @@ public class AppletJmol extends Frame
       MCview.PDBfile pdb;
       if (loadedInline)
       {
-        pdb = ssm.setMapping(sequence,
+        pdb = ssm.setMapping(sequence,chains,
                                 pdbentry.getFile(),
                                 AppletFormatAdapter.PASTE);
         pdbentry.setFile("INLINE"+pdb.id);
       }
       else
       {
-         pdb = ssm.setMapping(sequence,
+         pdb = ssm.setMapping(sequence,chains,
                               pdbentry.getFile(),
                               AppletFormatAdapter.URL);
       }
@@ -621,8 +666,7 @@ public class AppletJmol extends Frame
       pdbentry.setId(pdb.id);
 
       ssm.addStructureViewerListener(this);
-      if (fr!=null)
-        fr.featuresAdded();
+
       Vector chains = new Vector();
       for (int i = 0; i < pdb.chains.size(); i++)
       {
@@ -713,7 +757,7 @@ public class AppletJmol extends Frame
       picked+=strInfo.substring(strInfo.indexOf(":")+1,
                                strInfo.indexOf("."));
 
-    picked+=".C";
+    picked+=".CA";
 
     if (!atomsPicked.contains(picked))
     {