2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
26 import java.awt.event.*;
28 import jalview.analysis.*;
29 import jalview.datamodel.*;
31 import jalview.appletgui.*;
32 import jalview.structure.*;
34 public class AppletPDBCanvas
35 extends Panel implements MouseListener, MouseMotionListener, StructureListener
38 MCMatrix idmat = new MCMatrix(3, 3);
39 MCMatrix objmat = new MCMatrix(3, 3);
40 boolean redrawneeded = true;
51 float[] centre = new float[3];
52 float[] width = new float[3];
57 boolean bysequence = true;
58 boolean depthcue = true;
60 boolean bymolecule = false;
61 boolean zbuffer = true;
69 Font font = new Font("Helvetica", Font.PLAIN, 10);
70 public SequenceI [] sequence;
71 final StringBuffer mappingDetails = new StringBuffer();
72 String appletToolTip = null;
76 boolean pdbAction = false;
77 Bond highlightBond1, highlightBond2;
78 boolean errorLoading = false;
79 boolean seqColoursReady = false;
82 StructureSelectionManager ssm;
84 public AppletPDBCanvas(PDBEntry pdbentry,
91 this.pdbentry = pdbentry;
94 ssm = StructureSelectionManager.getStructureSelectionManager();
97 pdb = ssm.setMapping(seq, pdbentry.getFile(), protocol);
99 if(protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
100 pdbentry.setFile("INLINE"+pdb.id);
102 // fr.featuresAdded();
105 ex.printStackTrace();
109 pdbentry.setId(pdb.id);
111 ssm.addStructureViewerListener(this);
113 colourBySequence(ap.getSequenceRenderer(),
114 ap.av.getShowSequenceFeatures() ?
123 AlignSeq maxAlignseq = null;
125 //JUST DEAL WITH ONE SEQUENCE FOR NOW
126 SequenceI sequence = seq[0];
128 for (int i = 0; i < pdb.chains.size(); i++)
131 mappingDetails.append("\n\nPDB Sequence is :\nSequence = " +
132 ( (PDBChain) pdb.chains.elementAt(i)).sequence.
133 getSequenceAsString());
134 mappingDetails.append("\nNo of residues = " +
135 ( (PDBChain) pdb.chains.elementAt(i)).residues.size() +
138 // Now lets compare the sequences to get
139 // the start and end points.
140 // Align the sequence to the pdb
141 AlignSeq as = new AlignSeq(sequence,
142 ( (PDBChain) pdb.chains.elementAt(i)).sequence,
144 as.calcScoreMatrix();
146 PrintStream ps = new PrintStream(System.out)
148 public void print(String x)
150 mappingDetails.append(x);
153 public void println()
155 mappingDetails.append("\n");
159 as.printAlignment(ps);
161 if (as.maxscore > max)
166 pdbstart = as.seq2start;
168 seqstart = as.seq1start + sequence.getStart() - 1;
169 seqend = as.seq1end + sequence.getEnd() - 1;
173 mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
174 mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
177 mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
179 mainchain.pdbstart = pdbstart;
180 mainchain.pdbend = pdbend;
181 mainchain.seqstart = seqstart;
182 mainchain.seqend = seqend;
183 mainchain.isVisible = true;
184 // mainchain.makeExactMapping(maxAlignseq, sequence);
185 // mainchain.transferRESNUMFeatures(sequence, null);
187 this.prefsize = new Dimension(getSize().width, getSize().height);
189 //Initialize the matrices to identity
190 for (int i = 0; i < 3; i++)
192 for (int j = 0; j < 3; j++)
196 idmat.addElement(i, j, 0);
197 objmat.addElement(i, j, 0);
201 idmat.addElement(i, j, 1);
202 objmat.addElement(i, j, 1);
207 addMouseMotionListener(this);
208 addMouseListener(this);
210 addKeyListener(new KeyAdapter()
212 public void keyPressed(KeyEvent evt)
229 seqColoursReady = false;
230 // Sort the bonds by z coord
231 visiblebonds = new Vector();
233 for (int ii = 0; ii < pdb.chains.size(); ii++)
235 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
237 Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
239 for (int i = 0; i < tmp.size(); i++)
241 visiblebonds.addElement(tmp.elementAt(i));
245 seqColoursReady = true;
246 colourBySequence(ap.getSequenceRenderer(),
247 ap.av.getShowSequenceFeatures() ?
253 public void findWidth()
255 float[] max = new float[3];
256 float[] min = new float[3];
258 max[0] = (float) - 1e30;
259 max[1] = (float) - 1e30;
260 max[2] = (float) - 1e30;
262 min[0] = (float) 1e30;
263 min[1] = (float) 1e30;
264 min[2] = (float) 1e30;
266 for (int ii = 0; ii < pdb.chains.size(); ii++)
268 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
270 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
272 for (int i = 0; i < bonds.size(); i++)
274 Bond tmp = (Bond) bonds.elementAt(i);
276 if (tmp.start[0] >= max[0])
278 max[0] = tmp.start[0];
281 if (tmp.start[1] >= max[1])
283 max[1] = tmp.start[1];
286 if (tmp.start[2] >= max[2])
288 max[2] = tmp.start[2];
291 if (tmp.start[0] <= min[0])
293 min[0] = tmp.start[0];
296 if (tmp.start[1] <= min[1])
298 min[1] = tmp.start[1];
301 if (tmp.start[2] <= min[2])
303 min[2] = tmp.start[2];
306 if (tmp.end[0] >= max[0])
311 if (tmp.end[1] >= max[1])
316 if (tmp.end[2] >= max[2])
321 if (tmp.end[0] <= min[0])
326 if (tmp.end[1] <= min[1])
331 if (tmp.end[2] <= min[2])
339 width[0] = (float) Math.abs(max[0] - min[0]);
340 width[1] = (float) Math.abs(max[1] - min[1]);
341 width[2] = (float) Math.abs(max[2] - min[2]);
345 if (width[1] > width[0])
350 if (width[2] > width[1])
355 // System.out.println("Maxwidth = " + maxwidth);
358 public float findScale()
364 if (getSize().width != 0)
366 width = getSize().width;
367 height = getSize().height;
371 width = prefsize.width;
372 height = prefsize.height;
384 return (float) (dim / (1.5d * maxwidth));
387 public void findCentre()
395 //Find centre coordinate
396 for (int ii = 0; ii < pdb.chains.size(); ii++)
398 if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
400 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
402 bsize += bonds.size();
404 for (int i = 0; i < bonds.size(); i++)
406 xtot = xtot + ( (Bond) bonds.elementAt(i)).start[0] +
407 ( (Bond) bonds.elementAt(i)).end[0];
409 ytot = ytot + ( (Bond) bonds.elementAt(i)).start[1] +
410 ( (Bond) bonds.elementAt(i)).end[1];
412 ztot = ztot + ( (Bond) bonds.elementAt(i)).start[2] +
413 ( (Bond) bonds.elementAt(i)).end[2];
418 centre[0] = xtot / (2 * (float) bsize);
419 centre[1] = ytot / (2 * (float) bsize);
420 centre[2] = ztot / (2 * (float) bsize);
423 public void paint(Graphics g)
428 g.setColor(Color.white);
429 g.fillRect(0, 0, getSize().width, getSize().height);
430 g.setColor(Color.black);
431 g.setFont(new Font("Verdana", Font.BOLD, 14));
432 g.drawString("Error loading PDB data!!", 50, getSize().height / 2);
436 if (!seqColoursReady)
438 g.setColor(Color.black);
439 g.setFont(new Font("Verdana", Font.BOLD, 14));
440 g.drawString("Fetching PDB data...", 50, getSize().height / 2);
444 //Only create the image at the beginning -
445 //this saves much memory usage
446 if ( (img == null) || (prefsize.width != getSize().width) ||
447 (prefsize.height != getSize().height))
452 prefsize.width = getSize().width;
453 prefsize.height = getSize().height;
456 img = createImage(prefsize.width, prefsize.height);
457 ig = img.getGraphics();
463 ex.printStackTrace();
469 drawAll(ig, prefsize.width, prefsize.height);
470 redrawneeded = false;
472 if (appletToolTip != null)
474 ig.setColor(Color.red);
475 ig.drawString(appletToolTip, toolx, tooly);
478 g.drawImage(img, 0, 0, this);
483 public void drawAll(Graphics g, int width, int height)
485 ig.setColor(Color.black);
486 ig.fillRect(0, 0, width, height);
491 public void setColours(jalview.schemes.ColourSchemeI cs)
501 // This method has been taken out of PDBChain to allow
502 // Applet and Application specific sequence renderers to be used
503 void colourBySequence(SequenceRenderer sr, FeatureRenderer seqcanvasfr)
505 StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
507 boolean showFeatures = false;
508 if (seqcanvasfr!=null)
512 fr = new jalview.appletgui.FeatureRenderer(ap.av);
515 fr.transferSettings(seqcanvasfr);
521 if (bysequence && pdb != null)
523 for (int ii = 0; ii < pdb.chains.size(); ii++)
525 chain = (PDBChain) pdb.chains.elementAt(ii);
527 for (int i = 0; i < chain.bonds.size(); i++)
529 Bond tmp = (Bond) chain.bonds.elementAt(i);
530 tmp.startCol = Color.lightGray;
531 tmp.endCol = Color.lightGray;
532 if (chain != mainchain)
537 for (int s = 0; s < sequence.length; s++)
539 for (int m = 0; m < mapping.length; m++)
541 if (mapping[m].getSequence() == sequence[s])
543 int pos = mapping[m].getSeqPos(tmp.at1.resNumber)-1;
546 pos = sequence[s].findIndex(pos);
547 tmp.startCol = sr.getResidueBoxColour(sequence[s], pos);
550 tmp.startCol = fr.findFeatureColour(tmp.startCol,
555 pos = mapping[m].getSeqPos(tmp.at2.resNumber)-1;
558 pos = sequence[s].findIndex(pos);
559 tmp.endCol = sr.getResidueBoxColour(sequence[s], pos);
562 tmp.endCol = fr.findFeatureColour(tmp.endCol,
577 public void drawScene(Graphics g)
586 zsort.Zsort(visiblebonds);
590 for (int i = 0; i < visiblebonds.size(); i++)
592 tmpBond = (Bond) visiblebonds.elementAt(i);
594 xstart = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
595 (getSize().width / 2));
596 ystart = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
597 (getSize().height / 2));
599 xend = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
600 (getSize().width / 2));
601 yend = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
602 (getSize().height / 2));
604 xmid = (xend + xstart) / 2;
605 ymid = (yend + ystart) / 2;
607 if (depthcue && !bymolecule)
609 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
611 g.setColor(tmpBond.startCol.darker().darker());
612 drawLine(g, xstart, ystart, xmid, ymid);
614 g.setColor(tmpBond.endCol.darker().darker());
615 drawLine(g, xmid, ymid, xend, yend);
617 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
619 g.setColor(tmpBond.startCol.darker());
620 drawLine(g, xstart, ystart, xmid, ymid);
622 g.setColor(tmpBond.endCol.darker());
623 drawLine(g, xmid, ymid, xend, yend);
627 g.setColor(tmpBond.startCol);
628 drawLine(g, xstart, ystart, xmid, ymid);
630 g.setColor(tmpBond.endCol);
631 drawLine(g, xmid, ymid, xend, yend);
635 else if (depthcue && bymolecule)
637 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6)))
639 g.setColor(Color.green.darker().darker());
640 drawLine(g, xstart, ystart, xend, yend);
642 else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6)))
644 g.setColor(Color.green.darker());
645 drawLine(g, xstart, ystart, xend, yend);
649 g.setColor(Color.green);
650 drawLine(g, xstart, ystart, xend, yend);
653 else if (!depthcue && !bymolecule)
655 g.setColor(tmpBond.startCol);
656 drawLine(g, xstart, ystart, xmid, ymid);
657 g.setColor(tmpBond.endCol);
658 drawLine(g, xmid, ymid, xend, yend);
662 drawLine(g, xstart, ystart, xend, yend);
665 if (highlightBond1 != null && highlightBond1 == tmpBond)
667 g.setColor(Color.white);
668 drawLine(g, xmid, ymid, xend, yend);
671 if (highlightBond2 != null && highlightBond2 == tmpBond)
673 g.setColor(Color.white);
674 drawLine(g, xstart, ystart, xmid, ymid);
680 public void drawLine(Graphics g, int x1, int y1, int x2, int y2)
684 if ( ( (float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
686 g.drawLine(x1, y1, x2, y2);
687 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
688 g.drawLine(x1, y1 - 1, x2, y2 - 1);
692 g.setColor(g.getColor().brighter());
693 g.drawLine(x1, y1, x2, y2);
694 g.drawLine(x1 + 1, y1, x2 + 1, y2);
695 g.drawLine(x1 - 1, y1, x2 - 1, y2);
700 g.drawLine(x1, y1, x2, y2);
704 public Dimension minimumsize()
709 public Dimension preferredsize()
714 public void doKeyPressed(KeyEvent evt)
716 if (evt.getKeyCode() == KeyEvent.VK_UP)
718 scale = (float) (scale * 1.1);
722 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
724 scale = (float) (scale * 0.9);
730 public void mousePressed(MouseEvent e)
733 Atom fatom = findAtom(e.getX(), e.getY());
736 fatom.isSelected = !fatom.isSelected;
740 if (foundchain != -1)
742 PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
743 if (chain == mainchain)
745 if (fatom.alignmentMapping != -1)
747 if (highlightRes == null)
749 highlightRes = new Vector();
752 if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
754 highlightRes.removeElement(fatom.alignmentMapping + "");
758 highlightRes.addElement(fatom.alignmentMapping + "");
772 public void mouseMoved(MouseEvent e)
775 if (highlightBond1 != null)
777 highlightBond1.at2.isSelected = false;
778 highlightBond2.at1.isSelected = false;
779 highlightBond1 = null;
780 highlightBond2 = null;
783 Atom fatom = findAtom(e.getX(), e.getY());
785 PDBChain chain = null;
786 if (foundchain != -1)
788 chain = (PDBChain) pdb.chains.elementAt(foundchain);
789 if (chain == mainchain)
791 mouseOverStructure(fatom.resNumber, chain.id);
800 appletToolTip = chain.id + ":" + fatom.resNumber + " " + fatom.resName;
806 mouseOverStructure(-1, chain!=null?chain.id:null);
807 appletToolTip = null;
813 public void mouseClicked(MouseEvent e)
817 public void mouseEntered(MouseEvent e)
821 public void mouseExited(MouseEvent e)
825 public void mouseDragged(MouseEvent evt)
832 MCMatrix objmat = new MCMatrix(3, 3);
833 objmat.setIdentity();
835 if ( (evt.getModifiers() & Event.META_MASK) != 0)
837 objmat.rotatez( (float) ( (mx - omx)));
841 objmat.rotatex( (float) ( (my - omy)));
842 objmat.rotatey( (float) ( (omx - mx)));
846 for (int ii = 0; ii < pdb.chains.size(); ii++)
848 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
850 for (int i = 0; i < bonds.size(); i++)
852 Bond tmpBond = (Bond) bonds.elementAt(i);
854 //Translate the bond so the centre is 0,0,0
855 tmpBond.translate( -centre[0], -centre[1], -centre[2]);
857 //Now apply the rotation matrix
858 tmpBond.start = objmat.vectorMultiply(tmpBond.start);
859 tmpBond.end = objmat.vectorMultiply(tmpBond.end);
861 //Now translate back again
862 tmpBond.translate(centre[0], centre[1], centre[2]);
878 public void mouseReleased(MouseEvent evt)
884 void drawLabels(Graphics g)
887 for (int ii = 0; ii < pdb.chains.size(); ii++)
889 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
893 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
895 for (int i = 0; i < bonds.size(); i++)
897 Bond tmpBond = (Bond) bonds.elementAt(i);
899 if (tmpBond.at1.isSelected)
901 labelAtom(g, tmpBond, 1);
904 if (tmpBond.at2.isSelected)
907 labelAtom(g, tmpBond, 2);
914 public void labelAtom(Graphics g, Bond b, int n)
920 int xstart = (int) ( ( (b.start[0] - centre[0]) * scale) +
921 (getSize().width / 2));
922 int ystart = (int) ( ( (b.start[1] - centre[1]) * scale) +
923 (getSize().height / 2));
925 g.setColor(Color.red);
926 g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
931 int xstart = (int) ( ( (b.end[0] - centre[0]) * scale) +
932 (getSize().width / 2));
933 int ystart = (int) ( ( (b.end[1] - centre[1]) * scale) +
934 (getSize().height / 2));
936 g.setColor(Color.red);
937 g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
942 public Atom findAtom(int x, int y)
948 for (int ii = 0; ii < pdb.chains.size(); ii++)
950 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
956 Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
958 for (int i = 0; i < bonds.size(); i++)
960 tmpBond = (Bond) bonds.elementAt(i);
962 truex = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
963 (getSize().width / 2));
965 if (Math.abs(truex - x) <= 2)
967 int truey = (int) ( ( (tmpBond.start[1] - centre[1]) * scale) +
968 (getSize().height / 2));
970 if (Math.abs(truey - y) <= 2)
979 // Still here? Maybe its the last bond
981 truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
982 (getSize().width / 2));
984 if (Math.abs(truex - x) <= 2)
986 int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
987 (getSize().height / 2));
989 if (Math.abs(truey - y) <= 2)
999 if (fatom != null) //)&& chain.ds != null)
1001 chain = (PDBChain) pdb.chains.elementAt(foundchain);
1008 public void update(Graphics g)
1013 public void highlightRes(int ii)
1015 if (!seqColoursReady)
1020 if (highlightRes != null
1021 && highlightRes.contains( (ii - 1) + ""))
1028 for (index = 0; index < mainchain.bonds.size(); index++)
1030 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1031 if (tmpBond.at1.alignmentMapping == ii - 1)
1033 if (highlightBond1 != null)
1035 highlightBond1.at2.isSelected = false;
1038 if (highlightBond2 != null)
1040 highlightBond2.at1.isSelected = false;
1043 highlightBond1 = null;
1044 highlightBond2 = null;
1048 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1049 highlightBond1.at2.isSelected = true;
1052 if (index != mainchain.bonds.size())
1054 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1055 highlightBond2.at1.isSelected = true;
1062 redrawneeded = true;
1066 public void setAllchainsVisible(boolean b)
1068 for (int ii = 0; ii < pdb.chains.size(); ii++)
1070 PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
1071 chain.isVisible = b;
1073 mainchain.isVisible = true;
1079 //////////////////////////////////
1080 ///StructureListener
1081 public String getPdbFile()
1088 public void mouseOverStructure(int pdbResNum, String chain)
1090 if (lastMessage == null || !lastMessage.equals(pdbResNum+chain))
1091 ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
1093 lastMessage = pdbResNum+chain;
1096 StringBuffer resetLastRes = new StringBuffer();
1097 StringBuffer eval = new StringBuffer();
1099 public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
1101 if (!seqColoursReady)
1106 if (highlightRes != null
1107 && highlightRes.contains( (atomIndex - 1) + ""))
1114 for (index = 0; index < mainchain.bonds.size(); index++)
1116 tmpBond = (Bond) mainchain.bonds.elementAt(index);
1117 if (tmpBond.at1.atomIndex == atomIndex)
1119 if (highlightBond1 != null)
1121 highlightBond1.at2.isSelected = false;
1124 if (highlightBond2 != null)
1126 highlightBond2.at1.isSelected = false;
1129 highlightBond1 = null;
1130 highlightBond2 = null;
1134 highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
1135 highlightBond1.at2.isSelected = true;
1138 if (index != mainchain.bonds.size())
1140 highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
1141 highlightBond2.at1.isSelected = true;
1148 redrawneeded = true;
1153 public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
1156 // if (!pdbfile.equals(pdbentry.getFile()))
1159 //return new Color(viewer.getAtomArgb(atomIndex));
1162 public void updateColours(Object source)
1164 AlignmentPanel ap = (AlignmentPanel) source;
1165 colourBySequence(ap.getSequenceRenderer(),
1166 ap.av.getShowSequenceFeatures() ?
1168 redrawneeded = true;