baecac02a00f843533d50f02e348f50644a133bd
[jalview.git] / src / MCview / PDBCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package MCview;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.datamodel.PDBEntry;
25 import jalview.datamodel.SequenceI;
26 import jalview.gui.AlignmentPanel;
27 import jalview.gui.FeatureRenderer;
28 import jalview.gui.SequenceRenderer;
29 import jalview.io.DataSourceType;
30 import jalview.io.StructureFile;
31 import jalview.structure.AtomSpec;
32 import jalview.structure.StructureListener;
33 import jalview.structure.StructureMapping;
34 import jalview.structure.StructureSelectionManager;
35
36 import java.awt.Color;
37 import java.awt.Dimension;
38 import java.awt.Event;
39 import java.awt.Font;
40 import java.awt.Graphics;
41 import java.awt.Graphics2D;
42 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
43 import java.awt.Image;
44 import java.awt.RenderingHints;
45 import java.awt.event.KeyAdapter;
46 import java.awt.event.KeyEvent;
47 import java.awt.event.MouseEvent;
48 import java.awt.event.MouseListener;
49 import java.awt.event.MouseMotionListener;
50 import java.io.PrintStream;
51 import java.util.List;
52 import java.util.Vector;
53
54 import javax.swing.JPanel;
55 import javax.swing.ToolTipManager;
56
57 public class PDBCanvas extends JPanel implements MouseListener,
58         MouseMotionListener, StructureListener
59 {
60   boolean redrawneeded = true;
61
62   int omx = 0;
63
64   int mx = 0;
65
66   int omy = 0;
67
68   int my = 0;
69
70   public StructureFile pdb;
71
72   PDBEntry pdbentry;
73
74   int bsize;
75
76   Image img;
77
78   Graphics ig;
79
80   Dimension prefsize;
81
82   float[] centre = new float[3];
83
84   float[] width = new float[3];
85
86   float maxwidth;
87
88   float scale;
89
90   String inStr;
91
92   String inType;
93
94   boolean bysequence = true;
95
96   boolean depthcue = true;
97
98   boolean wire = false;
99
100   boolean bymolecule = false;
101
102   boolean zbuffer = true;
103
104   boolean dragging;
105
106   int xstart;
107
108   int xend;
109
110   int ystart;
111
112   int yend;
113
114   int xmid;
115
116   int ymid;
117
118   Font font = new Font("Helvetica", Font.PLAIN, 10);
119
120   jalview.gui.SeqCanvas seqcanvas;
121
122   public SequenceI[] sequence;
123
124   final StringBuffer mappingDetails = new StringBuffer();
125
126   PDBChain mainchain;
127
128   Vector<String> highlightRes;
129
130   boolean pdbAction = false;
131
132   boolean seqColoursReady = false;
133
134   jalview.renderer.seqfeatures.FeatureRenderer fr;
135
136   Color backgroundColour = Color.black;
137
138   AlignmentPanel ap;
139
140   StructureSelectionManager ssm;
141
142   String errorMessage;
143
144   void init(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
145           AlignmentPanel ap, DataSourceType protocol)
146   {
147     this.ap = ap;
148     this.pdbentry = pdbentry;
149     this.sequence = seq;
150
151     ssm = ap.av.getStructureSelectionManager();
152
153     try
154     {
155       pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol,
156               ap.alignFrame);
157
158       if (protocol.equals(jalview.io.DataSourceType.PASTE))
159       {
160         pdbentry.setFile("INLINE" + pdb.getId());
161       }
162
163     } catch (Exception ex)
164     {
165       ex.printStackTrace();
166       return;
167     }
168
169     if (pdb == null)
170     {
171       errorMessage = "Error loading file: " + pdbentry.getId();
172       return;
173     }
174     pdbentry.setId(pdb.getId());
175
176     ssm.addStructureViewerListener(this);
177
178     colourBySequence();
179
180     int max = -10;
181     int maxchain = -1;
182     int pdbstart = 0;
183     int pdbend = 0;
184     int seqstart = 0;
185     int seqend = 0;
186
187     // JUST DEAL WITH ONE SEQUENCE FOR NOW
188     SequenceI sequence = seq[0];
189
190     for (int i = 0; i < pdb.getChains().size(); i++)
191     {
192
193       mappingDetails
194               .append("\n\nPDB Sequence is :\nSequence = "
195                       + pdb.getChains().elementAt(i).sequence
196                               .getSequenceAsString());
197       mappingDetails.append("\nNo of residues = "
198               + pdb.getChains().elementAt(i).residues.size() + "\n\n");
199
200       // Now lets compare the sequences to get
201       // the start and end points.
202       // Align the sequence to the pdb
203       AlignSeq as = new AlignSeq(sequence,
204               pdb.getChains().elementAt(i).sequence, "pep");
205       as.calcScoreMatrix();
206       as.traceAlignment();
207       PrintStream ps = new PrintStream(System.out)
208       {
209         @Override
210         public void print(String x)
211         {
212           mappingDetails.append(x);
213         }
214
215         @Override
216         public void println()
217         {
218           mappingDetails.append("\n");
219         }
220       };
221
222       as.printAlignment(ps);
223
224       if (as.maxscore > max)
225       {
226         max = as.maxscore;
227         maxchain = i;
228
229         pdbstart = as.seq2start;
230         pdbend = as.seq2end;
231         seqstart = as.seq1start + sequence.getStart() - 1;
232         seqend = as.seq1end + sequence.getEnd() - 1;
233       }
234
235       mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
236       mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
237     }
238
239     mainchain = pdb.getChains().elementAt(maxchain);
240
241     mainchain.pdbstart = pdbstart;
242     mainchain.pdbend = pdbend;
243     mainchain.seqstart = seqstart;
244     mainchain.seqend = seqend;
245     mainchain.isVisible = true;
246
247     this.pdb = pdb;
248     this.prefsize = new Dimension(getSize().width, getSize().height);
249
250     addMouseMotionListener(this);
251     addMouseListener(this);
252
253     addKeyListener(new KeyAdapter()
254     {
255       @Override
256       public void keyPressed(KeyEvent evt)
257       {
258         keyPressed(evt);
259       }
260     });
261
262     findCentre();
263     findWidth();
264
265     setupBonds();
266
267     scale = findScale();
268
269     ToolTipManager.sharedInstance().registerComponent(this);
270     ToolTipManager.sharedInstance().setInitialDelay(0);
271     ToolTipManager.sharedInstance().setDismissDelay(10000);
272   }
273
274   Vector<Bond> visiblebonds;
275
276   void setupBonds()
277   {
278     seqColoursReady = false;
279     // Sort the bonds by z coord
280     visiblebonds = new Vector<Bond>();
281
282     for (PDBChain chain : pdb.getChains())
283     {
284       if (chain.isVisible)
285       {
286         for (Bond bond : chain.bonds)
287         {
288           visiblebonds.addElement(bond);
289         }
290       }
291     }
292
293     updateSeqColours();
294     seqColoursReady = true;
295     redrawneeded = true;
296     repaint();
297   }
298
299   public void findWidth()
300   {
301     float[] max = new float[3];
302     float[] min = new float[3];
303
304     max[0] = (float) -1e30;
305     max[1] = (float) -1e30;
306     max[2] = (float) -1e30;
307
308     min[0] = (float) 1e30;
309     min[1] = (float) 1e30;
310     min[2] = (float) 1e30;
311
312     for (PDBChain chain : pdb.getChains())
313     {
314       if (chain.isVisible)
315       {
316         for (Bond tmp : chain.bonds)
317         {
318           if (tmp.start[0] >= max[0])
319           {
320             max[0] = tmp.start[0];
321           }
322
323           if (tmp.start[1] >= max[1])
324           {
325             max[1] = tmp.start[1];
326           }
327
328           if (tmp.start[2] >= max[2])
329           {
330             max[2] = tmp.start[2];
331           }
332
333           if (tmp.start[0] <= min[0])
334           {
335             min[0] = tmp.start[0];
336           }
337
338           if (tmp.start[1] <= min[1])
339           {
340             min[1] = tmp.start[1];
341           }
342
343           if (tmp.start[2] <= min[2])
344           {
345             min[2] = tmp.start[2];
346           }
347
348           if (tmp.end[0] >= max[0])
349           {
350             max[0] = tmp.end[0];
351           }
352
353           if (tmp.end[1] >= max[1])
354           {
355             max[1] = tmp.end[1];
356           }
357
358           if (tmp.end[2] >= max[2])
359           {
360             max[2] = tmp.end[2];
361           }
362
363           if (tmp.end[0] <= min[0])
364           {
365             min[0] = tmp.end[0];
366           }
367
368           if (tmp.end[1] <= min[1])
369           {
370             min[1] = tmp.end[1];
371           }
372
373           if (tmp.end[2] <= min[2])
374           {
375             min[2] = tmp.end[2];
376           }
377         }
378       }
379     }
380     /*
381      * System.out.println("xmax " + max[0] + " min " + min[0]);
382      * System.out.println("ymax " + max[1] + " min " + min[1]);
383      * System.out.println("zmax " + max[2] + " min " + min[2]);
384      */
385
386     width[0] = Math.abs(max[0] - min[0]);
387     width[1] = Math.abs(max[1] - min[1]);
388     width[2] = Math.abs(max[2] - min[2]);
389
390     maxwidth = width[0];
391
392     if (width[1] > width[0])
393     {
394       maxwidth = width[1];
395     }
396
397     if (width[2] > width[1])
398     {
399       maxwidth = width[2];
400     }
401
402     // System.out.println("Maxwidth = " + maxwidth);
403   }
404
405   public float findScale()
406   {
407     int dim;
408     int width;
409     int height;
410
411     if (getWidth() != 0)
412     {
413       width = getWidth();
414       height = getHeight();
415     }
416     else
417     {
418       width = prefsize.width;
419       height = prefsize.height;
420     }
421
422     if (width < height)
423     {
424       dim = width;
425     }
426     else
427     {
428       dim = height;
429     }
430
431     return (float) (dim / (1.5d * maxwidth));
432   }
433
434   public void findCentre()
435   {
436     float xtot = 0;
437     float ytot = 0;
438     float ztot = 0;
439
440     int bsize = 0;
441
442     // Find centre coordinate
443     for (PDBChain chain : pdb.getChains())
444     {
445       if (chain.isVisible)
446       {
447         bsize += chain.bonds.size();
448
449         for (Bond bond : chain.bonds)
450         {
451           xtot = xtot + bond.start[0] + bond.end[0];
452           ytot = ytot + bond.start[1] + bond.end[1];
453           ztot = ztot + bond.start[2] + bond.end[2];
454         }
455       }
456     }
457
458     centre[0] = xtot / (2 * (float) bsize);
459     centre[1] = ytot / (2 * (float) bsize);
460     centre[2] = ztot / (2 * (float) bsize);
461   }
462
463   @Override
464   public void paintComponent(Graphics g)
465   {
466     super.paintComponent(g);
467
468     if (!seqColoursReady || errorMessage != null)
469     {
470       g.setColor(Color.black);
471       g.setFont(new Font("Verdana", Font.BOLD, 14));
472       g.drawString(errorMessage == null ? "Retrieving PDB data...."
473               : errorMessage, 20, getHeight() / 2);
474       return;
475     }
476
477     // Only create the image at the beginning -
478     // this saves much memory usage
479     if ((img == null) || (prefsize.width != getWidth())
480             || (prefsize.height != getHeight()))
481
482     {
483       prefsize.width = getWidth();
484       prefsize.height = getHeight();
485
486       scale = findScale();
487       img = createImage(prefsize.width, prefsize.height);
488       ig = img.getGraphics();
489       Graphics2D ig2 = (Graphics2D) ig;
490
491       ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
492               RenderingHints.VALUE_ANTIALIAS_ON);
493
494       redrawneeded = true;
495     }
496
497     if (redrawneeded)
498     {
499       drawAll(ig, prefsize.width, prefsize.height);
500       redrawneeded = false;
501     }
502
503     g.drawImage(img, 0, 0, this);
504
505     pdbAction = false;
506   }
507
508   public void drawAll(Graphics g, int width, int height)
509   {
510     g.setColor(backgroundColour);
511     g.fillRect(0, 0, width, height);
512     drawScene(g);
513     drawLabels(g);
514   }
515
516   public void updateSeqColours()
517   {
518     if (pdbAction)
519     {
520       return;
521     }
522
523     colourBySequence();
524
525     redrawneeded = true;
526     repaint();
527   }
528
529   // This method has been taken out of PDBChain to allow
530   // Applet and Application specific sequence renderers to be used
531   void colourBySequence()
532   {
533     SequenceRenderer sr = new SequenceRenderer(ap.av);
534
535     StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
536
537     boolean showFeatures = false;
538     if (ap.av.isShowSequenceFeatures())
539     {
540       if (fr == null)
541       {
542         fr = new FeatureRenderer(ap);
543       }
544
545       fr.transferSettings(ap.alignFrame.getFeatureRenderer());
546
547       showFeatures = true;
548     }
549
550     PDBChain chain;
551     if (bysequence && pdb != null)
552     {
553       for (int ii = 0; ii < pdb.getChains().size(); ii++)
554       {
555         chain = pdb.getChains().elementAt(ii);
556
557         for (int i = 0; i < chain.bonds.size(); i++)
558         {
559           Bond tmp = chain.bonds.elementAt(i);
560           tmp.startCol = Color.lightGray;
561           tmp.endCol = Color.lightGray;
562           if (chain != mainchain)
563           {
564             continue;
565           }
566
567           for (int s = 0; s < sequence.length; s++)
568           {
569             for (int m = 0; m < mapping.length; m++)
570             {
571               if (mapping[m].getSequence() == sequence[s])
572               {
573                 int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
574                 if (pos > 0)
575                 {
576                   pos = sequence[s].findIndex(pos);
577                   tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
578                   if (showFeatures)
579                   {
580                     tmp.startCol = fr.findFeatureColour(tmp.startCol,
581                             sequence[s], pos);
582                   }
583                 }
584                 pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
585                 if (pos > 0)
586                 {
587                   pos = sequence[s].findIndex(pos);
588                   tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
589                   if (showFeatures)
590                   {
591                     tmp.endCol = fr.findFeatureColour(tmp.endCol,
592                             sequence[s], pos);
593                   }
594                 }
595
596               }
597             }
598           }
599         }
600       }
601     }
602   }
603
604   Zsort zsort;
605
606   public void drawScene(Graphics g)
607   {
608     if (zbuffer)
609     {
610       if (zsort == null)
611       {
612         zsort = new Zsort();
613       }
614
615       zsort.sort(visiblebonds);
616     }
617
618     Bond tmpBond = null;
619     for (int i = 0; i < visiblebonds.size(); i++)
620     {
621       tmpBond = visiblebonds.elementAt(i);
622
623       xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
624       ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
625
626       xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
627       yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getHeight() / 2));
628
629       xmid = (xend + xstart) / 2;
630       ymid = (yend + ystart) / 2;
631       if (depthcue && !bymolecule)
632       {
633         if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
634         {
635
636           g.setColor(tmpBond.startCol.darker().darker());
637           drawLine(g, xstart, ystart, xmid, ymid);
638           g.setColor(tmpBond.endCol.darker().darker());
639           drawLine(g, xmid, ymid, xend, yend);
640
641         }
642         else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
643         {
644           g.setColor(tmpBond.startCol.darker());
645           drawLine(g, xstart, ystart, xmid, ymid);
646
647           g.setColor(tmpBond.endCol.darker());
648           drawLine(g, xmid, ymid, xend, yend);
649         }
650         else
651         {
652           g.setColor(tmpBond.startCol);
653           drawLine(g, xstart, ystart, xmid, ymid);
654
655           g.setColor(tmpBond.endCol);
656           drawLine(g, xmid, ymid, xend, yend);
657         }
658       }
659       else if (depthcue && bymolecule)
660       {
661         if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
662         {
663           g.setColor(Color.green.darker().darker());
664           drawLine(g, xstart, ystart, xend, yend);
665         }
666         else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
667         {
668           g.setColor(Color.green.darker());
669           drawLine(g, xstart, ystart, xend, yend);
670         }
671         else
672         {
673           g.setColor(Color.green);
674           drawLine(g, xstart, ystart, xend, yend);
675         }
676       }
677       else if (!depthcue && !bymolecule)
678       {
679         g.setColor(tmpBond.startCol);
680         drawLine(g, xstart, ystart, xmid, ymid);
681         g.setColor(tmpBond.endCol);
682         drawLine(g, xmid, ymid, xend, yend);
683       }
684       else
685       {
686         drawLine(g, xstart, ystart, xend, yend);
687       }
688
689       if (highlightBond1 != null && highlightBond1 == tmpBond)
690       {
691         g.setColor(tmpBond.endCol.brighter().brighter().brighter()
692                 .brighter());
693         drawLine(g, xmid, ymid, xend, yend);
694       }
695
696       if (highlightBond2 != null && highlightBond2 == tmpBond)
697       {
698         g.setColor(tmpBond.startCol.brighter().brighter().brighter()
699                 .brighter());
700         drawLine(g, xstart, ystart, xmid, ymid);
701       }
702
703     }
704
705   }
706
707   public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
708   {
709     if (!wire)
710     {
711       if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
712       {
713         g.drawLine(x1, y1, x2, y2);
714         g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
715         g.drawLine(x1, y1 - 1, x2, y2 - 1);
716       }
717       else
718       {
719         g.setColor(g.getColor().brighter());
720         g.drawLine(x1, y1, x2, y2);
721         g.drawLine(x1 + 1, y1, x2 + 1, y2);
722         g.drawLine(x1 - 1, y1, x2 - 1, y2);
723       }
724     }
725     else
726     {
727       g.drawLine(x1, y1, x2, y2);
728     }
729   }
730
731   public Dimension minimumsize()
732   {
733     return prefsize;
734   }
735
736   public Dimension preferredsize()
737   {
738     return prefsize;
739   }
740
741   public void keyPressed(KeyEvent evt)
742   {
743     if (evt.getKeyCode() == KeyEvent.VK_UP)
744     {
745       scale = (float) (scale * 1.1);
746       redrawneeded = true;
747       repaint();
748     }
749     else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
750     {
751       scale = (float) (scale * 0.9);
752       redrawneeded = true;
753       repaint();
754     }
755   }
756
757   @Override
758   public void mousePressed(MouseEvent e)
759   {
760     pdbAction = true;
761     Atom fatom = findAtom(e.getX(), e.getY());
762     if (fatom != null)
763     {
764       fatom.isSelected = !fatom.isSelected;
765
766       redrawneeded = true;
767       repaint();
768       if (foundchain != -1)
769       {
770         PDBChain chain = pdb.getChains().elementAt(foundchain);
771         if (chain == mainchain)
772         {
773           if (fatom.alignmentMapping != -1)
774           {
775             if (highlightRes == null)
776             {
777               highlightRes = new Vector<String>();
778             }
779
780             final String atomString = Integer
781                     .toString(fatom.alignmentMapping);
782             if (highlightRes.contains(atomString))
783             {
784               highlightRes.remove(atomString);
785             }
786             else
787             {
788               highlightRes.add(atomString);
789             }
790           }
791         }
792       }
793
794     }
795     mx = e.getX();
796     my = e.getY();
797     omx = mx;
798     omy = my;
799     dragging = false;
800   }
801
802   @Override
803   public void mouseMoved(MouseEvent e)
804   {
805     pdbAction = true;
806     if (highlightBond1 != null)
807     {
808       highlightBond1.at2.isSelected = false;
809       highlightBond2.at1.isSelected = false;
810       highlightBond1 = null;
811       highlightBond2 = null;
812     }
813
814     Atom fatom = findAtom(e.getX(), e.getY());
815
816     PDBChain chain = null;
817     if (foundchain != -1)
818     {
819       chain = pdb.getChains().elementAt(foundchain);
820       if (chain == mainchain)
821       {
822         mouseOverStructure(fatom.resNumber, chain.id);
823       }
824     }
825
826     if (fatom != null)
827     {
828       this.setToolTipText(chain.id + ":" + fatom.resNumber + " "
829               + fatom.resName);
830     }
831     else
832     {
833       mouseOverStructure(-1, chain != null ? chain.id : null);
834       this.setToolTipText(null);
835     }
836   }
837
838   @Override
839   public void mouseClicked(MouseEvent e)
840   {
841   }
842
843   @Override
844   public void mouseEntered(MouseEvent e)
845   {
846   }
847
848   @Override
849   public void mouseExited(MouseEvent e)
850   {
851   }
852
853   @Override
854   public void mouseDragged(MouseEvent evt)
855   {
856     int x = evt.getX();
857     int y = evt.getY();
858     mx = x;
859     my = y;
860
861     MCMatrix objmat = new MCMatrix(3, 3);
862     objmat.setIdentity();
863
864     if ((evt.getModifiers() & Event.META_MASK) != 0)
865     {
866       objmat.rotatez(((mx - omx)));
867     }
868     else
869     {
870       objmat.rotatex(((my - omy)));
871       objmat.rotatey(((omx - mx)));
872     }
873
874     // Alter the bonds
875     for (PDBChain chain : pdb.getChains())
876     {
877       for (Bond tmpBond : chain.bonds)
878       {
879         // Translate the bond so the centre is 0,0,0
880         tmpBond.translate(-centre[0], -centre[1], -centre[2]);
881
882         // Now apply the rotation matrix
883         tmpBond.start = objmat.vectorMultiply(tmpBond.start);
884         tmpBond.end = objmat.vectorMultiply(tmpBond.end);
885
886         // Now translate back again
887         tmpBond.translate(centre[0], centre[1], centre[2]);
888       }
889     }
890
891     objmat = null;
892
893     omx = mx;
894     omy = my;
895
896     dragging = true;
897
898     redrawneeded = true;
899
900     repaint();
901   }
902
903   @Override
904   public void mouseReleased(MouseEvent evt)
905   {
906     dragging = false;
907     return;
908   }
909
910   void drawLabels(Graphics g)
911   {
912
913     for (PDBChain chain : pdb.getChains())
914     {
915       if (chain.isVisible)
916       {
917         for (Bond tmpBond : chain.bonds)
918         {
919           if (tmpBond.at1.isSelected)
920           {
921             labelAtom(g, tmpBond, 1);
922           }
923
924           if (tmpBond.at2.isSelected)
925           {
926             labelAtom(g, tmpBond, 2);
927           }
928         }
929       }
930     }
931   }
932
933   public void labelAtom(Graphics g, Bond b, int n)
934   {
935     g.setFont(font);
936     g.setColor(Color.red);
937     if (n == 1)
938     {
939       int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getWidth() / 2));
940       int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getHeight() / 2));
941
942       g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
943     }
944
945     if (n == 2)
946     {
947       int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getWidth() / 2));
948       int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getHeight() / 2));
949
950       g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
951     }
952   }
953
954   int foundchain = -1;
955
956   public Atom findAtom(int x, int y)
957   {
958     Atom fatom = null;
959
960     foundchain = -1;
961
962     for (int ii = 0; ii < pdb.getChains().size(); ii++)
963     {
964       PDBChain chain = pdb.getChains().elementAt(ii);
965       int truex;
966       Bond tmpBond = null;
967
968       if (chain.isVisible)
969       {
970         for (Bond bond : chain.bonds)
971         {
972           tmpBond = bond;
973
974           truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2));
975
976           if (Math.abs(truex - x) <= 2)
977           {
978             int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getHeight() / 2));
979
980             if (Math.abs(truey - y) <= 2)
981             {
982               fatom = tmpBond.at1;
983               foundchain = ii;
984               break;
985             }
986           }
987         }
988
989         // Still here? Maybe its the last bond
990
991         truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getWidth() / 2));
992
993         if (Math.abs(truex - x) <= 2)
994         {
995           int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getHeight() / 2));
996
997           if (Math.abs(truey - y) <= 2)
998           {
999             fatom = tmpBond.at2;
1000             foundchain = ii;
1001             break;
1002           }
1003         }
1004
1005       }
1006
1007       if (fatom != null) // )&& chain.ds != null)
1008       { // dead code? value of chain is either overwritten or discarded
1009         chain = pdb.getChains().elementAt(foundchain);
1010       }
1011     }
1012
1013     return fatom;
1014   }
1015
1016   Bond highlightBond1, highlightBond2;
1017
1018   public void highlightRes(int ii)
1019   {
1020     if (!seqColoursReady)
1021     {
1022       return;
1023     }
1024
1025     if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
1026     {
1027       return;
1028     }
1029
1030     int index = -1;
1031     Bond tmpBond;
1032     for (index = 0; index < mainchain.bonds.size(); index++)
1033     {
1034       tmpBond = mainchain.bonds.elementAt(index);
1035       if (tmpBond.at1.alignmentMapping == ii - 1)
1036       {
1037         if (highlightBond1 != null)
1038         {
1039           highlightBond1.at2.isSelected = false;
1040         }
1041
1042         if (highlightBond2 != null)
1043         {
1044           highlightBond2.at1.isSelected = false;
1045         }
1046
1047         highlightBond1 = null;
1048         highlightBond2 = null;
1049
1050         if (index > 0)
1051         {
1052           highlightBond1 = mainchain.bonds.elementAt(index - 1);
1053           highlightBond1.at2.isSelected = true;
1054         }
1055
1056         if (index != mainchain.bonds.size())
1057         {
1058           highlightBond2 = mainchain.bonds.elementAt(index);
1059           highlightBond2.at1.isSelected = true;
1060         }
1061
1062         break;
1063       }
1064     }
1065
1066     redrawneeded = true;
1067     repaint();
1068   }
1069
1070   public void setAllchainsVisible(boolean b)
1071   {
1072     for (int ii = 0; ii < pdb.getChains().size(); ii++)
1073     {
1074       PDBChain chain = pdb.getChains().elementAt(ii);
1075       chain.isVisible = b;
1076     }
1077     mainchain.isVisible = true;
1078     findCentre();
1079     setupBonds();
1080   }
1081
1082   // ////////////////////////////////
1083   // /StructureListener
1084   @Override
1085   public String[] getPdbFile()
1086   {
1087     return new String[] { pdbentry.getFile() };
1088   }
1089
1090   String lastMessage;
1091
1092   public void mouseOverStructure(int pdbResNum, String chain)
1093   {
1094     if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
1095     {
1096       ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1097     }
1098
1099     lastMessage = pdbResNum + chain;
1100   }
1101
1102   StringBuffer resetLastRes = new StringBuffer();
1103
1104   StringBuffer eval = new StringBuffer();
1105
1106   /**
1107    * Highlight the specified atoms in the structure.
1108    * 
1109    * @param atoms
1110    */
1111   @Override
1112   public void highlightAtoms(List<AtomSpec> atoms)
1113   {
1114     if (!seqColoursReady)
1115     {
1116       return;
1117     }
1118
1119     for (AtomSpec atom : atoms)
1120     {
1121       int atomIndex = atom.getAtomIndex();
1122       if (highlightRes != null
1123               && highlightRes.contains((atomIndex - 1) + ""))
1124       {
1125         continue;
1126       }
1127
1128       highlightAtom(atomIndex);
1129     }
1130
1131     redrawneeded = true;
1132     repaint();
1133   }
1134
1135   /**
1136    * Highlight the atom at the specified index.
1137    * 
1138    * @param atomIndex
1139    */
1140   protected void highlightAtom(int atomIndex)
1141   {
1142     int index = -1;
1143     Bond tmpBond;
1144     for (index = 0; index < mainchain.bonds.size(); index++)
1145     {
1146       tmpBond = mainchain.bonds.elementAt(index);
1147       if (tmpBond.at1.atomIndex == atomIndex)
1148       {
1149         if (highlightBond1 != null)
1150         {
1151           highlightBond1.at2.isSelected = false;
1152         }
1153
1154         if (highlightBond2 != null)
1155         {
1156           highlightBond2.at1.isSelected = false;
1157         }
1158
1159         highlightBond1 = null;
1160         highlightBond2 = null;
1161
1162         if (index > 0)
1163         {
1164           highlightBond1 = mainchain.bonds.elementAt(index - 1);
1165           highlightBond1.at2.isSelected = true;
1166         }
1167
1168         if (index != mainchain.bonds.size())
1169         {
1170           highlightBond2 = mainchain.bonds.elementAt(index);
1171           highlightBond2.at1.isSelected = true;
1172         }
1173
1174         break;
1175       }
1176     }
1177   }
1178
1179   public Color getColour(int atomIndex, int pdbResNum, String chain,
1180           String pdbfile)
1181   {
1182     return Color.white;
1183     // if (!pdbfile.equals(pdbentry.getFile()))
1184     // return null;
1185
1186     // return new Color(viewer.getAtomArgb(atomIndex));
1187   }
1188
1189   @Override
1190   public void updateColours(Object source)
1191   {
1192     colourBySequence();
1193     redrawneeded = true;
1194     repaint();
1195   }
1196
1197   @Override
1198   public void releaseReferences(Object svl)
1199   {
1200     // TODO Auto-generated method stub
1201
1202   }
1203
1204   @Override
1205   public boolean isListeningFor(SequenceI seq)
1206   {
1207     if (sequence != null)
1208     {
1209       for (SequenceI s : sequence)
1210       {
1211         if (s == seq)
1212         {
1213           return true;
1214         }
1215       }
1216     }
1217     return false;
1218   }
1219 }