JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / MCview / PDBCanvas.java
index f6e6427..fdd673b 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
- * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package MCview;
 
-import java.io.*;
-import java.util.*;
-
+import jalview.analysis.AlignSeq;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SequenceI;
+import jalview.gui.AlignmentPanel;
+import jalview.gui.FeatureRenderer;
+import jalview.gui.SequenceRenderer;
+import jalview.structure.AtomSpec;
+import jalview.structure.StructureListener;
+import jalview.structure.StructureMapping;
+import jalview.structure.StructureSelectionManager;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Event;
+import java.awt.Font;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
-import java.awt.*;
-import java.awt.event.*;
-import javax.swing.*;
-
-import jalview.analysis.*;
-import jalview.datamodel.*;
-import jalview.gui.*;
-import jalview.structure.*;
+import java.awt.Image;
+import java.awt.RenderingHints;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.io.PrintStream;
+import java.util.List;
+import java.util.Vector;
+
+import javax.swing.JPanel;
+import javax.swing.ToolTipManager;
 
 public class PDBCanvas extends JPanel implements MouseListener,
         MouseMotionListener, StructureListener
@@ -104,7 +123,7 @@ public class PDBCanvas extends JPanel implements MouseListener,
 
   PDBChain mainchain;
 
-  Vector highlightRes;
+  Vector<String> highlightRes;
 
   boolean pdbAction = false;
 
@@ -134,7 +153,9 @@ public class PDBCanvas extends JPanel implements MouseListener,
       pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
 
       if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
+      {
         pdbentry.setFile("INLINE" + pdb.id);
+      }
 
     } catch (Exception ex)
     {
@@ -167,17 +188,15 @@ public class PDBCanvas extends JPanel implements MouseListener,
     {
 
       mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
-              + ((PDBChain) pdb.chains.elementAt(i)).sequence
-                      .getSequenceAsString());
+              + pdb.chains.elementAt(i).sequence.getSequenceAsString());
       mappingDetails.append("\nNo of residues = "
-              + ((PDBChain) pdb.chains.elementAt(i)).residues.size()
-              + "\n\n");
+              + pdb.chains.elementAt(i).residues.size() + "\n\n");
 
       // Now lets compare the sequences to get
       // the start and end points.
       // Align the sequence to the pdb
       AlignSeq as = new AlignSeq(sequence,
-              ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");
+              pdb.chains.elementAt(i).sequence, "pep");
       as.calcScoreMatrix();
       as.traceAlignment();
       PrintStream ps = new PrintStream(System.out)
@@ -210,7 +229,7 @@ public class PDBCanvas extends JPanel implements MouseListener,
       mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
     }
 
-    mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
+    mainchain = pdb.chains.elementAt(maxchain);
 
     mainchain.pdbstart = pdbstart;
     mainchain.pdbend = pdbend;
@@ -244,23 +263,21 @@ public class PDBCanvas extends JPanel implements MouseListener,
     ToolTipManager.sharedInstance().setDismissDelay(10000);
   }
 
-  Vector visiblebonds;
+  Vector<Bond> visiblebonds;
 
   void setupBonds()
   {
     seqColoursReady = false;
     // Sort the bonds by z coord
-    visiblebonds = new Vector();
+    visiblebonds = new Vector<Bond>();
 
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (PDBChain chain : pdb.chains)
     {
-      if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
+      if (chain.isVisible)
       {
-        Vector tmp = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
-
-        for (int i = 0; i < tmp.size(); i++)
+        for (Bond bond : chain.bonds)
         {
-          visiblebonds.addElement(tmp.elementAt(i));
+          visiblebonds.addElement(bond);
         }
       }
     }
@@ -284,16 +301,12 @@ public class PDBCanvas extends JPanel implements MouseListener,
     min[1] = (float) 1e30;
     min[2] = (float) 1e30;
 
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (PDBChain chain : pdb.chains)
     {
-      if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
+      if (chain.isVisible)
       {
-        Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
-
-        for (int i = 0; i < bonds.size(); i++)
+        for (Bond tmp : chain.bonds)
         {
-          Bond tmp = (Bond) bonds.elementAt(i);
-
           if (tmp.start[0] >= max[0])
           {
             max[0] = tmp.start[0];
@@ -362,9 +375,9 @@ public class PDBCanvas extends JPanel implements MouseListener,
      * System.out.println("zmax " + max[2] + " min " + min[2]);
      */
 
-    width[0] = (float) Math.abs(max[0] - min[0]);
-    width[1] = (float) Math.abs(max[1] - min[1]);
-    width[2] = (float) Math.abs(max[2] - min[2]);
+    width[0] = Math.abs(max[0] - min[0]);
+    width[1] = Math.abs(max[1] - min[1]);
+    width[2] = Math.abs(max[2] - min[2]);
 
     maxwidth = width[0];
 
@@ -419,24 +432,17 @@ public class PDBCanvas extends JPanel implements MouseListener,
     int bsize = 0;
 
     // Find centre coordinate
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (PDBChain chain : pdb.chains)
     {
-      if (((PDBChain) pdb.chains.elementAt(ii)).isVisible)
+      if (chain.isVisible)
       {
-        Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
+        bsize += chain.bonds.size();
 
-        bsize += bonds.size();
-
-        for (int i = 0; i < bonds.size(); i++)
+        for (Bond bond : chain.bonds)
         {
-          xtot = xtot + ((Bond) bonds.elementAt(i)).start[0]
-                  + ((Bond) bonds.elementAt(i)).end[0];
-
-          ytot = ytot + ((Bond) bonds.elementAt(i)).start[1]
-                  + ((Bond) bonds.elementAt(i)).end[1];
-
-          ztot = ztot + ((Bond) bonds.elementAt(i)).start[2]
-                  + ((Bond) bonds.elementAt(i)).end[2];
+          xtot = xtot + bond.start[0] + bond.end[0];
+          ytot = ytot + bond.start[1] + bond.end[1];
+          ztot = ztot + bond.start[2] + bond.end[2];
         }
       }
     }
@@ -537,11 +543,11 @@ public class PDBCanvas extends JPanel implements MouseListener,
     {
       for (int ii = 0; ii < pdb.chains.size(); ii++)
       {
-        chain = (PDBChain) pdb.chains.elementAt(ii);
+        chain = pdb.chains.elementAt(ii);
 
         for (int i = 0; i < chain.bonds.size(); i++)
         {
-          Bond tmp = (Bond) chain.bonds.elementAt(i);
+          Bond tmp = chain.bonds.elementAt(i);
           tmp.startCol = Color.lightGray;
           tmp.endCol = Color.lightGray;
           if (chain != mainchain)
@@ -597,13 +603,13 @@ public class PDBCanvas extends JPanel implements MouseListener,
         zsort = new Zsort();
       }
 
-      zsort.Zsort(visiblebonds);
+      zsort.sort(visiblebonds);
     }
 
     Bond tmpBond = null;
     for (int i = 0; i < visiblebonds.size(); i++)
     {
-      tmpBond = (Bond) visiblebonds.elementAt(i);
+      tmpBond = visiblebonds.elementAt(i);
 
       xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
       ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
@@ -751,23 +757,25 @@ public class PDBCanvas extends JPanel implements MouseListener,
       repaint();
       if (foundchain != -1)
       {
-        PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
+        PDBChain chain = pdb.chains.elementAt(foundchain);
         if (chain == mainchain)
         {
           if (fatom.alignmentMapping != -1)
           {
             if (highlightRes == null)
             {
-              highlightRes = new Vector();
+              highlightRes = new Vector<String>();
             }
 
-            if (highlightRes.contains(fatom.alignmentMapping + ""))
+            final String atomString = Integer
+                    .toString(fatom.alignmentMapping);
+            if (highlightRes.contains(atomString))
             {
-              highlightRes.remove(fatom.alignmentMapping + "");
+              highlightRes.remove(atomString);
             }
             else
             {
-              highlightRes.add(fatom.alignmentMapping + "");
+              highlightRes.add(atomString);
             }
           }
         }
@@ -797,7 +805,7 @@ public class PDBCanvas extends JPanel implements MouseListener,
     PDBChain chain = null;
     if (foundchain != -1)
     {
-      chain = (PDBChain) pdb.chains.elementAt(foundchain);
+      chain = pdb.chains.elementAt(foundchain);
       if (chain == mainchain)
       {
         mouseOverStructure(fatom.resNumber, chain.id);
@@ -840,23 +848,19 @@ public class PDBCanvas extends JPanel implements MouseListener,
 
     if ((evt.getModifiers() & Event.META_MASK) != 0)
     {
-      objmat.rotatez((float) ((mx - omx)));
+      objmat.rotatez(((mx - omx)));
     }
     else
     {
-      objmat.rotatex((float) ((my - omy)));
-      objmat.rotatey((float) ((omx - mx)));
+      objmat.rotatex(((my - omy)));
+      objmat.rotatey(((omx - mx)));
     }
 
     // Alter the bonds
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (PDBChain chain : pdb.chains)
     {
-      Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
-
-      for (int i = 0; i < bonds.size(); i++)
+      for (Bond tmpBond : chain.bonds)
       {
-        Bond tmpBond = (Bond) bonds.elementAt(i);
-
         // Translate the bond so the centre is 0,0,0
         tmpBond.translate(-centre[0], -centre[1], -centre[2]);
 
@@ -890,18 +894,12 @@ public class PDBCanvas extends JPanel implements MouseListener,
   void drawLabels(Graphics g)
   {
 
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (PDBChain chain : pdb.chains)
     {
-      PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
-
       if (chain.isVisible)
       {
-        Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
-
-        for (int i = 0; i < bonds.size(); i++)
+        for (Bond tmpBond : chain.bonds)
         {
-          Bond tmpBond = (Bond) bonds.elementAt(i);
-
           if (tmpBond.at1.isSelected)
           {
             labelAtom(g, tmpBond, 1);
@@ -909,7 +907,6 @@ public class PDBCanvas extends JPanel implements MouseListener,
 
           if (tmpBond.at2.isSelected)
           {
-
             labelAtom(g, tmpBond, 2);
           }
         }
@@ -948,17 +945,15 @@ public class PDBCanvas extends JPanel implements MouseListener,
 
     for (int ii = 0; ii < pdb.chains.size(); ii++)
     {
-      PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
+      PDBChain chain = pdb.chains.elementAt(ii);
       int truex;
       Bond tmpBond = null;
 
       if (chain.isVisible)
       {
-        Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;
-
-        for (int i = 0; i < bonds.size(); i++)
+        for (Bond bond : chain.bonds)
         {
-          tmpBond = (Bond) bonds.elementAt(i);
+          tmpBond = bond;
 
           truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
 
@@ -994,8 +989,8 @@ public class PDBCanvas extends JPanel implements MouseListener,
       }
 
       if (fatom != null) // )&& chain.ds != null)
-      {
-        chain = (PDBChain) pdb.chains.elementAt(foundchain);
+      { // dead code? value of chain is either overwritten or discarded
+        chain = pdb.chains.elementAt(foundchain);
       }
     }
 
@@ -1020,7 +1015,7 @@ public class PDBCanvas extends JPanel implements MouseListener,
     Bond tmpBond;
     for (index = 0; index < mainchain.bonds.size(); index++)
     {
-      tmpBond = (Bond) mainchain.bonds.elementAt(index);
+      tmpBond = mainchain.bonds.elementAt(index);
       if (tmpBond.at1.alignmentMapping == ii - 1)
       {
         if (highlightBond1 != null)
@@ -1038,13 +1033,13 @@ public class PDBCanvas extends JPanel implements MouseListener,
 
         if (index > 0)
         {
-          highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
+          highlightBond1 = mainchain.bonds.elementAt(index - 1);
           highlightBond1.at2.isSelected = true;
         }
 
         if (index != mainchain.bonds.size())
         {
-          highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
+          highlightBond2 = mainchain.bonds.elementAt(index);
           highlightBond2.at1.isSelected = true;
         }
 
@@ -1060,7 +1055,7 @@ public class PDBCanvas extends JPanel implements MouseListener,
   {
     for (int ii = 0; ii < pdb.chains.size(); ii++)
     {
-      PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
+      PDBChain chain = pdb.chains.elementAt(ii);
       chain.isVisible = b;
     }
     mainchain.isVisible = true;
@@ -1072,8 +1067,7 @@ public class PDBCanvas extends JPanel implements MouseListener,
   // /StructureListener
   public String[] getPdbFile()
   {
-    return new String[]
-    { pdbentry.getFile() };
+    return new String[] { pdbentry.getFile() };
   }
 
   String lastMessage;
@@ -1081,7 +1075,9 @@ public class PDBCanvas extends JPanel implements MouseListener,
   public void mouseOverStructure(int pdbResNum, String chain)
   {
     if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
+    {
       ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
+    }
 
     lastMessage = pdbResNum + chain;
   }
@@ -1090,24 +1086,47 @@ public class PDBCanvas extends JPanel implements MouseListener,
 
   StringBuffer eval = new StringBuffer();
 
-  public void highlightAtom(int atomIndex, int pdbResNum, String chain,
-          String pdbfile)
+  /**
+   * Highlight the specified atoms in the structure.
+   * 
+   * @param atoms
+   */
+  @Override
+  public void highlightAtoms(List<AtomSpec> atoms)
   {
     if (!seqColoursReady)
     {
       return;
     }
 
-    if (highlightRes != null && highlightRes.contains((atomIndex - 1) + ""))
+    for (AtomSpec atom : atoms)
     {
-      return;
+      int atomIndex = atom.getAtomIndex();
+      if (highlightRes != null
+              && highlightRes.contains((atomIndex - 1) + ""))
+      {
+        continue;
+      }
+
+      highlightAtom(atomIndex);
     }
 
+    redrawneeded = true;
+    repaint();
+  }
+
+  /**
+   * Highlight the atom at the specified index.
+   * 
+   * @param atomIndex
+   */
+  protected void highlightAtom(int atomIndex)
+  {
     int index = -1;
     Bond tmpBond;
     for (index = 0; index < mainchain.bonds.size(); index++)
     {
-      tmpBond = (Bond) mainchain.bonds.elementAt(index);
+      tmpBond = mainchain.bonds.elementAt(index);
       if (tmpBond.at1.atomIndex == atomIndex)
       {
         if (highlightBond1 != null)
@@ -1125,22 +1144,19 @@ public class PDBCanvas extends JPanel implements MouseListener,
 
         if (index > 0)
         {
-          highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
+          highlightBond1 = mainchain.bonds.elementAt(index - 1);
           highlightBond1.at2.isSelected = true;
         }
 
         if (index != mainchain.bonds.size())
         {
-          highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
+          highlightBond2 = mainchain.bonds.elementAt(index);
           highlightBond2.at1.isSelected = true;
         }
 
         break;
       }
     }
-
-    redrawneeded = true;
-    repaint();
   }
 
   public Color getColour(int atomIndex, int pdbResNum, String chain,
@@ -1167,4 +1183,19 @@ public class PDBCanvas extends JPanel implements MouseListener,
 
   }
 
+  @Override
+  public boolean isListeningFor(SequenceI seq)
+  {
+    if (sequence != null)
+    {
+      for (SequenceI s : sequence)
+      {
+        if (s == seq)
+        {
+          return true;
+        }
+      }
+    }
+    return false;
+  }
 }