FeatureRenderer mixups fixed
[jalview.git] / src / MCview / AppletPDBCanvas.java
index dd33b0c..47aa94b 100755 (executable)
@@ -28,8 +28,11 @@ import java.awt.event.*;
 import jalview.analysis.*;
 import jalview.datamodel.*;
 
+import jalview.appletgui.*;
+import jalview.structure.*;
+
 public class AppletPDBCanvas
-    extends Panel implements MouseListener, MouseMotionListener
+    extends Panel implements MouseListener, MouseMotionListener, StructureListener
 {
 
   MCMatrix idmat = new MCMatrix(3, 3);
@@ -40,6 +43,7 @@ public class AppletPDBCanvas
   int omy = 0;
   int my = 0;
   public PDBfile pdb;
+  PDBEntry pdbentry;
   int bsize;
   Image img;
   Graphics ig;
@@ -63,8 +67,7 @@ public class AppletPDBCanvas
   int xmid;
   int ymid;
   Font font = new Font("Helvetica", Font.PLAIN, 10);
-  jalview.appletgui.SeqCanvas seqcanvas;
-  public Sequence sequence;
+  public SequenceI [] sequence;
   final StringBuffer mappingDetails = new StringBuffer();
   String appletToolTip = null;
   int toolx, tooly;
@@ -74,33 +77,50 @@ public class AppletPDBCanvas
   Bond highlightBond1, highlightBond2;
   boolean errorLoading = false;
   boolean seqColoursReady = false;
-  jalview.appletgui.FeatureRenderer fr;
+  FeatureRenderer fr;
+  AlignmentPanel ap;
+  StructureSelectionManager ssm;
+
+  public AppletPDBCanvas(PDBEntry pdbentry,
+                         SequenceI[] seq,
+                         AlignmentPanel ap,
+                         String protocol)
 
-  public AppletPDBCanvas(jalview.appletgui.SeqCanvas seqcanvas, Sequence seq)
   {
-    this.seqcanvas = seqcanvas;
+    this.ap = ap;
+    this.pdbentry = pdbentry;
     this.sequence = seq;
 
-    seqcanvas.setPDBCanvas(this);
-    addKeyListener(new KeyAdapter()
+    ssm = StructureSelectionManager.getStructureSelectionManager();
+
+    try{
+      pdb = ssm.setMapping(seq, pdbentry.getFile(), protocol);
+
+      if(protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
+       pdbentry.setFile("INLINE"+pdb.id);
+
+    }catch(Exception ex)
     {
+      ex.printStackTrace();
+      return;
+    }
 
-      public void keyPressed(KeyEvent evt)
-      {
-        doKeyPressed(evt);
-      }
-    });
-  }
+    pdbentry.setId(pdb.id);
+
+    ssm.addStructureViewerListener(this);
+
+    colourBySequence();
 
-  public void setPDBFile(PDBfile pdb)
-  {
     int max = -10;
     int maxchain = -1;
     int pdbstart = 0;
     int pdbend = 0;
     int seqstart = 0;
     int seqend = 0;
-    AlignSeq maxAlignseq = null; ;
+    AlignSeq maxAlignseq = null;
+
+    //JUST DEAL WITH ONE SEQUENCE FOR NOW
+    SequenceI sequence = seq[0];
 
     for (int i = 0; i < pdb.chains.size(); i++)
     {
@@ -158,8 +178,8 @@ public class AppletPDBCanvas
     mainchain.seqstart = seqstart;
     mainchain.seqend = seqend;
     mainchain.isVisible = true;
-    mainchain.makeExactMapping(maxAlignseq, sequence);
-    mainchain.transferRESNUMFeatures(sequence, null);
+  //  mainchain.makeExactMapping(maxAlignseq, sequence);
+  //  mainchain.transferRESNUMFeatures(sequence, null);
     this.pdb = pdb;
     this.prefsize = new Dimension(getSize().width, getSize().height);
 
@@ -184,6 +204,14 @@ public class AppletPDBCanvas
     addMouseMotionListener(this);
     addMouseListener(this);
 
+    addKeyListener(new KeyAdapter()
+    {
+      public void keyPressed(KeyEvent evt)
+      {
+        doKeyPressed(evt);
+      }
+    });
+
     findCentre();
     findWidth();
 
@@ -211,8 +239,8 @@ public class AppletPDBCanvas
         }
       }
     }
-    updateSeqColours();
     seqColoursReady = true;
+    colourBySequence();
     redrawneeded = true;
     repaint();
   }
@@ -455,7 +483,7 @@ public class AppletPDBCanvas
     drawLabels(ig);
   }
 
-  void setColours(jalview.schemes.ColourSchemeI cs)
+  public void setColours(jalview.schemes.ColourSchemeI cs)
   {
     bysequence = false;
     pdb.setColours(cs);
@@ -463,111 +491,80 @@ public class AppletPDBCanvas
     repaint();
   }
 
-  public void updateSeqColours()
-  {
-    if (pdbAction)
-    {
-      return;
-    }
-
-    if (bysequence && pdb != null)
-    {
-      for (int ii = 0; ii < pdb.chains.size(); ii++)
-      {
-        colourBySequence( (PDBChain) pdb.chains.elementAt(ii));
-      }
-    }
 
-    redrawneeded = true;
-    repaint();
-  }
-
-  int findTrueIndex(int pos)
-  {
-    // returns the alignment position for a residue
-    int j = sequence.getStart();
-    int i = 0;
-
-    while ( (i < sequence.getLength()) && (j <= sequence.getEnd()) &&
-           (j <= pos + 1))
-    {
-      if (!jalview.util.Comparison.isGap(sequence.getCharAt(i)))
-      {
-        j++;
-      }
-
-      i++;
-    }
-
-    if (i > 1)
-    {
-      i--;
-    }
-
-    if ( (j == sequence.getEnd()) && (j < pos))
-    {
-      return sequence.getEnd() + 1;
-    }
-    else
-    {
-      return i;
-    }
-  }
 
   // This method has been taken out of PDBChain to allow
   // Applet and Application specific sequence renderers to be used
-  void colourBySequence(PDBChain chain)
+  void colourBySequence()
   {
-    boolean showFeatures = false;
+    SequenceRenderer sr = ap.getSequenceRenderer();
 
-    if (seqcanvas.getViewport().getShowSequenceFeatures())
+    StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
+
+    boolean showFeatures = false;
+    if (ap.av.getShowSequenceFeatures())
     {
       if (fr == null)
       {
-        fr = new jalview.appletgui.FeatureRenderer(seqcanvas.getViewport());
+        fr = new jalview.appletgui.FeatureRenderer(ap.av);
       }
-      fr.transferSettings(seqcanvas.getFeatureRenderer());
+
+      fr.transferSettings(ap.getFeatureRenderer());
+
       showFeatures = true;
     }
 
-    for (int i = 0; i < chain.bonds.size(); i++)
+    PDBChain chain;
+    if (bysequence && pdb != null)
     {
-      Bond tmp = (Bond) chain.bonds.elementAt(i);
-      tmp.startCol = Color.lightGray;
-      tmp.endCol = Color.lightGray;
-      if (chain != mainchain)
-      {
-        continue;
-      }
-
-      if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&
-          (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))
+      for (int ii = 0; ii < pdb.chains.size(); ii++)
       {
+        chain = (PDBChain) pdb.chains.elementAt(ii);
 
-        int index = findTrueIndex(tmp.at1.alignmentMapping);
-        //sequence.findIndex(tmp.at1.alignmentMapping);
-        if (index != -1)
+        for (int i = 0; i < chain.bonds.size(); i++)
         {
-          tmp.startCol = seqcanvas.getSequenceRenderer().
-              getResidueBoxColour(sequence, index);
-
-          if (showFeatures)
+          Bond tmp = (Bond) chain.bonds.elementAt(i);
+          tmp.startCol = Color.lightGray;
+          tmp.endCol = Color.lightGray;
+          if (chain != mainchain)
           {
-            tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);
+            continue;
           }
-        }
-      }
 
-      int index = findTrueIndex(tmp.at2.alignmentMapping);
-      //sequence.findIndex( tmp.at2.alignmentMapping );
-      if (index != -1)
-      {
-        tmp.endCol = seqcanvas.getSequenceRenderer().
-            getResidueBoxColour(sequence, index);
-
-        if (showFeatures)
-        {
-          tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);
+          for (int s = 0; s < sequence.length; s++)
+          {
+            for (int m = 0; m < mapping.length; m++)
+            {
+              if (mapping[m].getSequence() == sequence[s])
+              {
+                int pos = mapping[m].getSeqPos(tmp.at1.resNumber)-1;
+                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);
+                  }
+                }
+                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);
+                  }
+                }
+
+              }
+            }
+          }
         }
       }
     }
@@ -788,7 +785,7 @@ public class AppletPDBCanvas
       chain = (PDBChain) pdb.chains.elementAt(foundchain);
       if (chain == mainchain)
       {
-        highlightSeqcanvas(fatom.alignmentMapping);
+        mouseOverStructure(fatom.resNumber, chain.id);
       }
     }
 
@@ -803,35 +800,13 @@ public class AppletPDBCanvas
     }
     else
     {
-      highlightSeqcanvas( -1);
+      mouseOverStructure(-1, chain!=null?chain.id:null);
       appletToolTip = null;
       redrawneeded = true;
       repaint();
     }
   }
 
-  void highlightSeqcanvas(int pos)
-  {
-    SearchResults searchResults = new SearchResults();
-    if (highlightRes != null)
-    {
-      for (int i = 0; i < highlightRes.size(); i++)
-      {
-        int a = Integer.parseInt(highlightRes.elementAt(
-            i).toString()) + 1;
-
-        searchResults.addResult(sequence, a, a);
-      }
-    }
-
-    if (pos != -1)
-    {
-      searchResults.addResult(sequence, pos + 1, pos + 1);
-    }
-
-    seqcanvas.highlightSearchResults(searchResults);
-  }
-
   public void mouseClicked(MouseEvent e)
   {
   }
@@ -1097,4 +1072,97 @@ public class AppletPDBCanvas
     setupBonds();
   }
 
+
+  //////////////////////////////////
+  ///StructureListener
+  public String getPdbFile()
+  {
+    return "???";
+  }
+
+
+  String lastMessage;
+  public void mouseOverStructure(int pdbResNum, String chain)
+  {
+      if (lastMessage == null || !lastMessage.equals(pdbResNum+chain))
+        ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
+
+      lastMessage = pdbResNum+chain;
+  }
+
+  StringBuffer resetLastRes = new StringBuffer();
+  StringBuffer eval = new StringBuffer();
+
+  public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
+  {
+    if (!seqColoursReady)
+    {
+      return;
+    }
+
+    if (highlightRes != null
+        && highlightRes.contains( (atomIndex - 1) + ""))
+    {
+      return;
+    }
+
+    int index = -1;
+    Bond tmpBond;
+    for (index = 0; index < mainchain.bonds.size(); index++)
+    {
+      tmpBond = (Bond) mainchain.bonds.elementAt(index);
+      if (tmpBond.at1.atomIndex == atomIndex)
+      {
+        if (highlightBond1 != null)
+        {
+          highlightBond1.at2.isSelected = false;
+        }
+
+        if (highlightBond2 != null)
+        {
+          highlightBond2.at1.isSelected = false;
+        }
+
+        highlightBond1 = null;
+        highlightBond2 = null;
+
+        if (index > 0)
+        {
+          highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
+          highlightBond1.at2.isSelected = true;
+        }
+
+        if (index != mainchain.bonds.size())
+        {
+          highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
+          highlightBond2.at1.isSelected = true;
+        }
+
+        break;
+      }
+    }
+
+    redrawneeded = true;
+    repaint();
+  }
+
+
+  public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
+  {
+    return Color.white;
+   // if (!pdbfile.equals(pdbentry.getFile()))
+   //   return null;
+
+    //return new Color(viewer.getAtomArgb(atomIndex));
+  }
+
+  public void updateColours(Object source)
+  {
+    AlignmentPanel ap = (AlignmentPanel) source;
+    colourBySequence();
+    redrawneeded = true;
+    repaint();
+  }
+
+
 }