JAL-2446 merged to spike branch
[jalview.git] / src / MCview / AppletPDBCanvas.java
index 211cff0..39111c3 100644 (file)
@@ -26,6 +26,9 @@ import jalview.appletgui.FeatureRenderer;
 import jalview.appletgui.SequenceRenderer;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
+import jalview.io.DataSourceType;
+import jalview.io.StructureFile;
+import jalview.renderer.seqfeatures.FeatureColourFinder;
 import jalview.structure.AtomSpec;
 import jalview.structure.StructureListener;
 import jalview.structure.StructureMapping;
@@ -67,7 +70,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   int my = 0;
 
-  public PDBfile pdb;
+  public StructureFile pdb;
 
   PDBEntry pdbentry;
 
@@ -127,7 +130,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   PDBChain mainchain;
 
-  Vector highlightRes;
+  Vector<String> highlightRes;
 
   boolean pdbAction = false;
 
@@ -144,7 +147,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
   StructureSelectionManager ssm;
 
   public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
-          String[] chains, AlignmentPanel ap, String protocol)
+          String[] chains, AlignmentPanel ap, DataSourceType protocol)
 
   {
     this.ap = ap;
@@ -158,9 +161,9 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     {
       pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
 
-      if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
+      if (protocol == DataSourceType.PASTE)
       {
-        pdbentry.setFile("INLINE" + pdb.id);
+        pdbentry.setFile("INLINE" + pdb.getId());
       }
 
     } catch (Exception ex)
@@ -169,13 +172,13 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
       return;
     }
 
-    pdbentry.setId(pdb.id);
+    pdbentry.setId(pdb.getId());
 
     ssm.addStructureViewerListener(this);
 
     colourBySequence();
 
-    int max = -10;
+    float max = -10;
     int maxchain = -1;
     int pdbstart = 0;
     int pdbend = 0;
@@ -185,33 +188,34 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     // JUST DEAL WITH ONE SEQUENCE FOR NOW
     SequenceI sequence = seq[0];
 
-    for (int i = 0; i < pdb.chains.size(); i++)
+    for (int i = 0; i < pdb.getChains().size(); i++)
     {
 
-      mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
-              + pdb.chains.elementAt(i).sequence
-                      .getSequenceAsString());
+      mappingDetails
+              .append("\n\nPDB Sequence is :\nSequence = "
+                      + pdb.getChains().elementAt(i).sequence
+                              .getSequenceAsString());
       mappingDetails.append("\nNo of residues = "
-              + pdb.chains.elementAt(i).residues.size()
-              + "\n\n");
+              + pdb.getChains().elementAt(i).residues.size() + "\n\n");
 
       // Now lets compare the sequences to get
       // the start and end points.
       // Align the sequence to the pdb
       // TODO: DNa/Pep switch
       AlignSeq as = new AlignSeq(sequence,
-              pdb.chains.elementAt(i).sequence,
-              pdb.chains.elementAt(i).isNa ? AlignSeq.DNA
-                      : AlignSeq.PEP);
+              pdb.getChains().elementAt(i).sequence, pdb.getChains()
+                      .elementAt(i).isNa ? AlignSeq.DNA : AlignSeq.PEP);
       as.calcScoreMatrix();
       as.traceAlignment();
       PrintStream ps = new PrintStream(System.out)
       {
+        @Override
         public void print(String x)
         {
           mappingDetails.append(x);
         }
 
+        @Override
         public void println()
         {
           mappingDetails.append("\n");
@@ -235,7 +239,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
       mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
     }
 
-    mainchain = pdb.chains.elementAt(maxchain);
+    mainchain = pdb.getChains().elementAt(maxchain);
 
     mainchain.pdbstart = pdbstart;
     mainchain.pdbend = pdbend;
@@ -270,6 +274,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
     addKeyListener(new KeyAdapter()
     {
+      @Override
       public void keyPressed(KeyEvent evt)
       {
         doKeyPressed(evt);
@@ -284,19 +289,19 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     scale = findScale();
   }
 
-  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 (int ii = 0; ii < pdb.getChains().size(); ii++)
     {
-      if (pdb.chains.elementAt(ii).isVisible)
+      if (pdb.getChains().elementAt(ii).isVisible)
       {
-        Vector tmp = pdb.chains.elementAt(ii).bonds;
+        Vector<Bond> tmp = pdb.getChains().elementAt(ii).bonds;
 
         for (int i = 0; i < tmp.size(); i++)
         {
@@ -323,16 +328,14 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     min[1] = (float) 1e30;
     min[2] = (float) 1e30;
 
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (int ii = 0; ii < pdb.getChains().size(); ii++)
     {
-      if (pdb.chains.elementAt(ii).isVisible)
+      if (pdb.getChains().elementAt(ii).isVisible)
       {
-        Vector bonds = pdb.chains.elementAt(ii).bonds;
+        Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
 
-        for (int i = 0; i < bonds.size(); i++)
+        for (Bond tmp : bonds)
         {
-          Bond tmp = (Bond) bonds.elementAt(i);
-
           if (tmp.start[0] >= max[0])
           {
             max[0] = tmp.start[0];
@@ -453,24 +456,19 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     int bsize = 0;
 
     // Find centre coordinate
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (int ii = 0; ii < pdb.getChains().size(); ii++)
     {
-      if (pdb.chains.elementAt(ii).isVisible)
+      if (pdb.getChains().elementAt(ii).isVisible)
       {
-        Vector bonds = pdb.chains.elementAt(ii).bonds;
+        Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
 
         bsize += bonds.size();
 
-        for (int i = 0; i < bonds.size(); i++)
+        for (Bond b : 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 + b.start[0] + b.end[0];
+          ytot = ytot + b.start[1] + b.end[1];
+          ztot = ztot + b.start[2] + b.end[2];
         }
       }
     }
@@ -480,6 +478,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     centre[2] = ztot / (2 * (float) bsize);
   }
 
+  @Override
   public void paint(Graphics g)
   {
 
@@ -579,16 +578,18 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
       showFeatures = true;
     }
 
+    FeatureColourFinder finder = new FeatureColourFinder(fr);
+
     PDBChain chain;
     if (bysequence && pdb != null)
     {
-      for (int ii = 0; ii < pdb.chains.size(); ii++)
+      for (int ii = 0; ii < pdb.getChains().size(); ii++)
       {
-        chain = pdb.chains.elementAt(ii);
+        chain = pdb.getChains().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)
@@ -606,25 +607,16 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
                 if (pos > 0)
                 {
                   pos = sequence[s].findIndex(pos);
-                  tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
-                  if (showFeatures)
-                  {
-                    tmp.startCol = fr.findFeatureColour(tmp.startCol,
-                            sequence[s], pos);
-                  }
+                  tmp.startCol = sr.getResidueColour(sequence[s], pos,
+                          finder);
                 }
                 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
                 if (pos > 0)
                 {
                   pos = sequence[s].findIndex(pos);
-                  tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
-                  if (showFeatures)
-                  {
-                    tmp.endCol = fr.findFeatureColour(tmp.endCol,
-                            sequence[s], pos);
-                  }
+                  tmp.endCol = sr
+                          .getResidueColour(sequence[s], pos, finder);
                 }
-
               }
             }
           }
@@ -644,13 +636,13 @@ public class AppletPDBCanvas extends Panel 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) + (getSize().width / 2));
       ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
@@ -784,6 +776,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     }
   }
 
+  @Override
   public void mousePressed(MouseEvent e)
   {
     pdbAction = true;
@@ -796,23 +789,25 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
       repaint();
       if (foundchain != -1)
       {
-        PDBChain chain = pdb.chains.elementAt(foundchain);
+        PDBChain chain = pdb.getChains().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.removeElement(fatom.alignmentMapping + "");
+              highlightRes.removeElement(atomString);
             }
             else
             {
-              highlightRes.addElement(fatom.alignmentMapping + "");
+              highlightRes.addElement(atomString);
             }
           }
         }
@@ -826,6 +821,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     dragging = false;
   }
 
+  @Override
   public void mouseMoved(MouseEvent e)
   {
     pdbAction = true;
@@ -842,7 +838,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     PDBChain chain = null;
     if (foundchain != -1)
     {
-      chain = pdb.chains.elementAt(foundchain);
+      chain = pdb.getChains().elementAt(foundchain);
       if (chain == mainchain)
       {
         mouseOverStructure(fatom.resNumber, chain.id);
@@ -868,18 +864,22 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     }
   }
 
+  @Override
   public void mouseClicked(MouseEvent e)
   {
   }
 
+  @Override
   public void mouseEntered(MouseEvent e)
   {
   }
 
+  @Override
   public void mouseExited(MouseEvent e)
   {
   }
 
+  @Override
   public void mouseDragged(MouseEvent evt)
   {
     int x = evt.getX();
@@ -901,14 +901,10 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     }
 
     // Alter the bonds
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (PDBChain chain : pdb.getChains())
     {
-      Vector bonds = 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]);
 
@@ -933,6 +929,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     repaint();
   }
 
+  @Override
   public void mouseReleased(MouseEvent evt)
   {
     dragging = false;
@@ -942,18 +939,12 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
   void drawLabels(Graphics g)
   {
 
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (PDBChain chain : pdb.getChains())
     {
-      PDBChain chain = pdb.chains.elementAt(ii);
-
       if (chain.isVisible)
       {
-        Vector bonds = 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);
@@ -961,7 +952,6 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
           if (tmpBond.at2.isSelected)
           {
-
             labelAtom(g, tmpBond, 2);
           }
         }
@@ -1000,19 +990,19 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
     foundchain = -1;
 
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (int ii = 0; ii < pdb.getChains().size(); ii++)
     {
-      PDBChain chain = pdb.chains.elementAt(ii);
+      PDBChain chain = pdb.getChains().elementAt(ii);
       int truex;
       Bond tmpBond = null;
 
       if (chain.isVisible)
       {
-        Vector bonds = pdb.chains.elementAt(ii).bonds;
+        Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
 
         for (int i = 0; i < bonds.size(); i++)
         {
-          tmpBond = (Bond) bonds.elementAt(i);
+          tmpBond = bonds.elementAt(i);
 
           truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
 
@@ -1049,13 +1039,14 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
       if (fatom != null) // )&& chain.ds != null)
       {
-        chain = pdb.chains.elementAt(foundchain);
+        chain = pdb.getChains().elementAt(foundchain);
       }
     }
 
     return fatom;
   }
 
+  @Override
   public void update(Graphics g)
   {
     paint(g);
@@ -1077,7 +1068,7 @@ public class AppletPDBCanvas extends Panel 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)
@@ -1095,13 +1086,13 @@ public class AppletPDBCanvas extends Panel 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;
         }
 
@@ -1115,9 +1106,9 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   public void setAllchainsVisible(boolean b)
   {
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (int ii = 0; ii < pdb.getChains().size(); ii++)
     {
-      PDBChain chain = pdb.chains.elementAt(ii);
+      PDBChain chain = pdb.getChains().elementAt(ii);
       chain.isVisible = b;
     }
     mainchain.isVisible = true;
@@ -1127,10 +1118,10 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   // ////////////////////////////////
   // /StructureListener
-  public String[] getPdbFile()
+  @Override
+  public String[] getStructureFiles()
   {
-    return new String[]
-    { pdbentry.getFile() };
+    return new String[] { pdbentry.getFile() };
   }
 
   String lastMessage;
@@ -1187,7 +1178,7 @@ public class AppletPDBCanvas extends Panel 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.atomIndex == atomIndex)
       {
         if (highlightBond1 != null)
@@ -1205,13 +1196,13 @@ public class AppletPDBCanvas extends Panel 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;
         }
 
@@ -1230,6 +1221,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     // return new Color(viewer.getAtomArgb(atomIndex));
   }
 
+  @Override
   public void updateColours(Object source)
   {
     colourBySequence();
@@ -1244,4 +1236,19 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   }
 
+  @Override
+  public boolean isListeningFor(SequenceI seq)
+  {
+    if (sequence != null)
+    {
+      for (SequenceI s : sequence)
+      {
+        if (s == seq)
+        {
+          return true;
+        }
+      }
+    }
+    return false;
+  }
 }