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