--- /dev/null
+/*\r
+ * Jalview - A Sequence Alignment Editor and Viewer\r
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
+ */\r
+\r
+package jalview.structure;\r
+\r
+import jalview.datamodel.*;\r
+\r
+public class StructureMapping\r
+{\r
+ String mappingDetails;\r
+ SequenceI sequence;\r
+ String pdbfile;\r
+ String pdbid;\r
+ String pdbchain;\r
+\r
+ //Mapping index 0 is resNum, index 1 is atomNo\r
+ int[][] mapping;\r
+\r
+ public StructureMapping(SequenceI seq,\r
+ String pdbfile,\r
+ String pdbid,\r
+ String chain,\r
+ int[][] mapping,\r
+ String mappingDetails)\r
+ {\r
+ sequence = seq;\r
+ this.pdbfile = pdbfile;\r
+ this.pdbid = pdbid;\r
+ this.pdbchain = chain;\r
+ this.mapping = mapping;\r
+ this.mappingDetails = mappingDetails;\r
+ }\r
+\r
+ public SequenceI getSequence()\r
+ {\r
+ return sequence;\r
+ }\r
+\r
+ public String getChain()\r
+ {\r
+ return pdbchain;\r
+ }\r
+\r
+ public String getPdbId()\r
+ {\r
+ return pdbid;\r
+ }\r
+\r
+ public int getAtomNum(int seqpos)\r
+ {\r
+ if (mapping.length > seqpos)\r
+ {\r
+ return mapping[seqpos][1];\r
+ }\r
+ else\r
+ {\r
+ return 0;\r
+ }\r
+ }\r
+\r
+ public int getPDBResNum(int seqpos)\r
+ {\r
+ if (mapping.length > seqpos)\r
+ {\r
+ return mapping[seqpos][0];\r
+ }\r
+ else\r
+ {\r
+ return 0;\r
+ }\r
+ }\r
+\r
+ public int getSeqPos(int pdbResNum)\r
+ {\r
+ for (int i = 0; i < mapping.length; i++)\r
+ {\r
+ if (mapping[i][0] == pdbResNum)\r
+ {\r
+ return i;\r
+ }\r
+ }\r
+ return -1;\r
+ }\r
+}\r
--- /dev/null
+/*\r
+ * Jalview - A Sequence Alignment Editor and Viewer\r
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation; either version 2\r
+ * of the License, or (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
+ */\r
+package jalview.structure;\r
+\r
+import java.io.*;\r
+import java.util.*;\r
+\r
+import MCview.*;\r
+import jalview.analysis.*;\r
+import jalview.datamodel.*;\r
+\r
+public class StructureSelectionManager\r
+{\r
+ static StructureSelectionManager instance;\r
+ StructureMapping[] mappings;\r
+ Hashtable mappingData = new Hashtable();\r
+\r
+ public static StructureSelectionManager getStructureSelectionManager()\r
+ {\r
+ if (instance == null)\r
+ {\r
+ instance = new StructureSelectionManager();\r
+ }\r
+\r
+ return instance;\r
+ }\r
+\r
+ Vector listeners = new Vector();\r
+ public void addStructureViewerListener(Object svl)\r
+ {\r
+ if (!listeners.contains(svl))\r
+ {\r
+ listeners.addElement(svl);\r
+ }\r
+ }\r
+\r
+ public String alreadyMappedToFile(String pdbid)\r
+ {\r
+ if (mappings != null)\r
+ {\r
+ for (int i = 0; i < mappings.length; i++)\r
+ {\r
+ if (mappings[i].getPdbId().equals(pdbid))\r
+ {\r
+ return mappings[i].pdbfile;\r
+ }\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /*\r
+ There will be better ways of doing this in the future, for now we'll use\r
+ the tried and tested MCview pdb mapping\r
+ */\r
+ public MCview.PDBfile setMapping(SequenceI[] sequence,\r
+ String pdbFile,\r
+ String protocol)\r
+ {\r
+ MCview.PDBfile pdb = null;\r
+ try\r
+ {\r
+ pdb = new MCview.PDBfile(pdbFile, protocol);\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ return null;\r
+ }\r
+\r
+ for (int s = 0; s < sequence.length; s++)\r
+ {\r
+ String targetChain = "";\r
+\r
+ if (sequence[s].getName().indexOf("|") > -1)\r
+ {\r
+ targetChain = sequence[s].getName().substring(\r
+ sequence[s].getName().lastIndexOf("|") + 1);\r
+ }\r
+\r
+ int max = -10;\r
+ AlignSeq maxAlignseq = null;\r
+ String maxChainId = " ";\r
+ PDBChain maxChain = null;\r
+\r
+ for (int i = 0; i < pdb.chains.size(); i++)\r
+ {\r
+ AlignSeq as = new AlignSeq(sequence[s],\r
+ ( (PDBChain) pdb.chains.elementAt(i)).\r
+ sequence,\r
+ AlignSeq.PEP);\r
+ as.calcScoreMatrix();\r
+ as.traceAlignment();\r
+ PDBChain chain = ( (PDBChain) pdb.chains.elementAt(i));\r
+\r
+ if (as.maxscore > max\r
+ || (as.maxscore == max && chain.id.equals(targetChain)))\r
+ {\r
+ maxChain = chain;\r
+ max = as.maxscore;\r
+ maxAlignseq = as;\r
+ maxChainId = chain.id;\r
+ }\r
+ }\r
+\r
+ final StringBuffer mappingDetails = new StringBuffer();\r
+ mappingDetails.append("\n\nPDB Sequence is :\nSequence = " +\r
+ maxChain.sequence.getSequenceAsString());\r
+ mappingDetails.append("\nNo of residues = " +\r
+ maxChain.residues.\r
+ size() +\r
+ "\n\n");\r
+ PrintStream ps = new PrintStream(System.out)\r
+ {\r
+ public void print(String x)\r
+ {\r
+ mappingDetails.append(x);\r
+ }\r
+\r
+ public void println()\r
+ {\r
+ mappingDetails.append("\n");\r
+ }\r
+ };\r
+\r
+ maxAlignseq.printAlignment(ps);\r
+\r
+ mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start + " " +\r
+ maxAlignseq.seq2end);\r
+ mappingDetails.append("\nSEQ start/end "\r
+ + (maxAlignseq.seq1start + sequence[s].getStart() - 1) +\r
+ " "\r
+ + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));\r
+\r
+ maxChain.makeExactMapping(maxAlignseq, sequence[s]);\r
+\r
+ // maxChain.transferRESNUMFeatures(sequence[s], null);\r
+\r
+ int[][] mapping = new int[sequence[s].getEnd() + 2][2];\r
+ int resNum = -10000;\r
+ int index = 0;\r
+\r
+\r
+ do\r
+ {\r
+ Atom tmp = (Atom) maxChain.atoms.elementAt(index);\r
+ if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)\r
+ {\r
+ resNum = tmp.resNumber;\r
+ mapping[tmp.alignmentMapping+1][0] = tmp.resNumber;\r
+ mapping[tmp.alignmentMapping+1][1] = tmp.atomIndex;\r
+ }\r
+\r
+ index++;\r
+ }\r
+ while(index < maxChain.atoms.size());\r
+\r
+ if (mappings == null)\r
+ {\r
+ mappings = new StructureMapping[1];\r
+ }\r
+ else\r
+ {\r
+ StructureMapping[] tmp = new StructureMapping[mappings.length + 1];\r
+ System.arraycopy(mappings, 0, tmp, 0, mappings.length);\r
+ mappings = tmp;\r
+ }\r
+\r
+ if(protocol.equals(jalview.io.AppletFormatAdapter.PASTE))\r
+ pdbFile = "INLINE"+pdb.id;\r
+\r
+ mappings[mappings.length - 1]\r
+ = new StructureMapping(sequence[s], pdbFile, pdb.id, maxChainId,\r
+ mapping, mappingDetails.toString());\r
+ }\r
+ /////////\r
+\r
+ return pdb;\r
+ }\r
+\r
+ public void removeStructureViewerListener(Object svl, String pdbfile)\r
+ {\r
+ listeners.removeElement(svl);\r
+\r
+ boolean removeMapping = true;\r
+\r
+ StructureListener sl;\r
+ for (int i = 0; i < listeners.size(); i++)\r
+ {\r
+ if (listeners.elementAt(i) instanceof StructureListener)\r
+ {\r
+ sl = (StructureListener) listeners.elementAt(i);\r
+ if (sl.getPdbFile().equals(pdbfile))\r
+ {\r
+ removeMapping = false;\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ if (removeMapping && mappings!=null)\r
+ {\r
+ Vector tmp = new Vector();\r
+ for (int i = 0; i < mappings.length; i++)\r
+ {\r
+ if (!mappings[i].pdbfile.equals(pdbfile))\r
+ {\r
+ tmp.addElement(mappings[i]);\r
+ }\r
+ }\r
+\r
+ mappings = new StructureMapping[tmp.size()];\r
+ tmp.copyInto(mappings);\r
+ }\r
+ }\r
+\r
+ public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)\r
+ {\r
+ SequenceListener sl;\r
+ for (int i = 0; i < listeners.size(); i++)\r
+ {\r
+ if (listeners.elementAt(i) instanceof SequenceListener)\r
+ {\r
+ sl = (SequenceListener) listeners.elementAt(i);\r
+\r
+ for (int j = 0; j < mappings.length; j++)\r
+ {\r
+ if (mappings[j].pdbfile.equals(pdbfile) &&\r
+ mappings[j].pdbchain.equals(chain))\r
+ {\r
+ sl.highlightSequence(mappings[j].sequence,\r
+ mappings[j].getSeqPos(pdbResNum));\r
+ }\r
+ }\r
+\r
+ sl.highlightSequence(null, pdbResNum);\r
+ }\r
+ }\r
+ }\r
+\r
+ public void mouseOverSequence(SequenceI seq, int index)\r
+ {\r
+ StructureListener sl;\r
+ int atomNo = 0;\r
+ for (int i = 0; i < listeners.size(); i++)\r
+ {\r
+ if (listeners.elementAt(i) instanceof StructureListener)\r
+ {\r
+ sl = (StructureListener) listeners.elementAt(i);\r
+\r
+ for (int j = 0; j < mappings.length; j++)\r
+ {\r
+ if (mappings[j].sequence == seq)\r
+ {\r
+ atomNo = mappings[j].getAtomNum(index);\r
+\r
+ if (atomNo > 0)\r
+ {\r
+ sl.highlightAtom(atomNo,\r
+ mappings[j].getPDBResNum(index),\r
+ mappings[j].pdbchain,\r
+ mappings[j].pdbfile);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ public void structureSelectionChanged()\r
+ {\r
+ StructureListener svl;\r
+ for (int i = 0; i < listeners.size(); i++)\r
+ {\r
+ svl = (StructureListener) listeners.elementAt(i);\r
+ }\r
+ }\r
+\r
+ public void sequenceSelectionChanged()\r
+ {\r
+ StructureListener svl;\r
+ for (int i = 0; i < listeners.size(); i++)\r
+ {\r
+ svl = (StructureListener) listeners.elementAt(i);\r
+ }\r
+ }\r
+\r
+ public void sequenceColoursChanged(Object source)\r
+ {\r
+ StructureListener sl;\r
+ for (int i = 0; i < listeners.size(); i++)\r
+ {\r
+ if (listeners.elementAt(i) instanceof StructureListener)\r
+ {\r
+ sl = (StructureListener) listeners.elementAt(i);\r
+ sl.updateColours(source);\r
+ }\r
+ }\r
+ }\r
+\r
+ public StructureMapping[] getMapping(String pdbfile)\r
+ {\r
+ Vector tmp = new Vector();\r
+ for (int i = 0; i < mappings.length; i++)\r
+ {\r
+ if (mappings[i].pdbfile.equals(pdbfile))\r
+ {\r
+ tmp.addElement(mappings[i]);\r
+ }\r
+ }\r
+\r
+ StructureMapping[] ret = new StructureMapping[tmp.size()];\r
+ for (int i = 0; i < tmp.size(); i++)\r
+ {\r
+ ret[i] = (StructureMapping) tmp.elementAt(i);\r
+ }\r
+\r
+ return ret;\r
+ }\r
+\r
+ public String printMapping(String pdbfile)\r
+ {\r
+ StringBuffer sb = new StringBuffer();\r
+ for (int i = 0; i < mappings.length; i++)\r
+ {\r
+ if (mappings[i].pdbfile.equals(pdbfile))\r
+ {\r
+ sb.append(mappings[i].mappingDetails);\r
+ }\r
+ }\r
+\r
+ return sb.toString();\r
+ }\r
+}\r