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
19 package jalview.structure;
25 import jalview.analysis.*;
26 import jalview.datamodel.*;
28 public class StructureSelectionManager
30 static StructureSelectionManager instance;
31 StructureMapping[] mappings;
32 Hashtable mappingData = new Hashtable();
34 public static StructureSelectionManager getStructureSelectionManager()
38 instance = new StructureSelectionManager();
44 Vector listeners = new Vector();
45 public void addStructureViewerListener(Object svl)
47 if (!listeners.contains(svl))
49 listeners.addElement(svl);
53 public String alreadyMappedToFile(String pdbid)
57 for (int i = 0; i < mappings.length; i++)
59 if (mappings[i].getPdbId().equals(pdbid))
61 return mappings[i].pdbfile;
69 There will be better ways of doing this in the future, for now we'll use
70 the tried and tested MCview pdb mapping
72 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
76 MCview.PDBfile pdb = null;
79 pdb = new MCview.PDBfile(pdbFile, protocol);
87 for (int s = 0; s < sequence.length; s++)
89 String targetChain = "";
91 if (sequence[s].getName().indexOf("|") > -1)
93 targetChain = sequence[s].getName().substring(
94 sequence[s].getName().lastIndexOf("|") + 1);
98 AlignSeq maxAlignseq = null;
99 String maxChainId = " ";
100 PDBChain maxChain = null;
102 for (int i = 0; i < pdb.chains.size(); i++)
104 AlignSeq as = new AlignSeq(sequence[s],
105 ( (PDBChain) pdb.chains.elementAt(i)).
108 as.calcScoreMatrix();
110 PDBChain chain = ( (PDBChain) pdb.chains.elementAt(i));
112 if (as.maxscore > max
113 || (as.maxscore == max && chain.id.equals(targetChain)))
118 maxChainId = chain.id;
122 final StringBuffer mappingDetails = new StringBuffer();
123 mappingDetails.append("\n\nPDB Sequence is :\nSequence = " +
124 maxChain.sequence.getSequenceAsString());
125 mappingDetails.append("\nNo of residues = " +
129 PrintStream ps = new PrintStream(System.out)
131 public void print(String x)
133 mappingDetails.append(x);
136 public void println()
138 mappingDetails.append("\n");
142 maxAlignseq.printAlignment(ps);
144 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start + " " +
145 maxAlignseq.seq2end);
146 mappingDetails.append("\nSEQ start/end "
147 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) +
149 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
151 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
153 maxChain.transferRESNUMFeatures(sequence[s], null);
155 int[][] mapping = new int[sequence[s].getEnd() + 2][2];
162 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
163 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
165 resNum = tmp.resNumber;
166 mapping[tmp.alignmentMapping+1][0] = tmp.resNumber;
167 mapping[tmp.alignmentMapping+1][1] = tmp.atomIndex;
172 while(index < maxChain.atoms.size());
174 if (mappings == null)
176 mappings = new StructureMapping[1];
180 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
181 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
185 if(protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
186 pdbFile = "INLINE"+pdb.id;
188 mappings[mappings.length - 1]
189 = new StructureMapping(sequence[s], pdbFile, pdb.id, maxChainId,
190 mapping, mappingDetails.toString());
197 public void removeStructureViewerListener(Object svl, String pdbfile)
199 listeners.removeElement(svl);
201 boolean removeMapping = true;
203 StructureListener sl;
204 for (int i = 0; i < listeners.size(); i++)
206 if (listeners.elementAt(i) instanceof StructureListener)
208 sl = (StructureListener) listeners.elementAt(i);
209 if (sl.getPdbFile().equals(pdbfile))
211 removeMapping = false;
217 if (removeMapping && mappings!=null)
219 Vector tmp = new Vector();
220 for (int i = 0; i < mappings.length; i++)
222 if (!mappings[i].pdbfile.equals(pdbfile))
224 tmp.addElement(mappings[i]);
228 mappings = new StructureMapping[tmp.size()];
229 tmp.copyInto(mappings);
233 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
236 for (int i = 0; i < listeners.size(); i++)
238 if (listeners.elementAt(i) instanceof SequenceListener)
240 sl = (SequenceListener) listeners.elementAt(i);
242 for (int j = 0; j < mappings.length; j++)
244 if (mappings[j].pdbfile.equals(pdbfile) &&
245 mappings[j].pdbchain.equals(chain))
247 sl.highlightSequence(mappings[j].sequence,
248 mappings[j].getSeqPos(pdbResNum));
252 sl.highlightSequence(null, pdbResNum);
257 public void mouseOverSequence(SequenceI seq, int index)
259 StructureListener sl;
261 for (int i = 0; i < listeners.size(); i++)
263 if (listeners.elementAt(i) instanceof StructureListener)
265 sl = (StructureListener) listeners.elementAt(i);
267 for (int j = 0; j < mappings.length; j++)
269 if (mappings[j].sequence == seq)
271 atomNo = mappings[j].getAtomNum(index);
275 sl.highlightAtom(atomNo,
276 mappings[j].getPDBResNum(index),
277 mappings[j].pdbchain,
278 mappings[j].pdbfile);
286 public Annotation[] colourSequenceFromStructure(SequenceI seq, String pdbid)
288 Annotation [] annotations = new Annotation[seq.getLength()];
290 StructureListener sl;
292 for (int i = 0; i < listeners.size(); i++)
294 if (listeners.elementAt(i) instanceof StructureListener)
296 sl = (StructureListener) listeners.elementAt(i);
298 for (int j = 0; j < mappings.length; j++)
301 if (mappings[j].sequence == seq
302 && mappings[j].getPdbId().equals(pdbid)
303 && mappings[j].pdbfile.equals(sl.getPdbFile()))
305 System.out.println(pdbid+" "+mappings[j].getPdbId()
306 +" "+mappings[j].pdbfile);
309 for(int index=0; index<seq.getLength(); index++)
311 if(jalview.util.Comparison.isGap(seq.getCharAt(index)))
314 atomNo = mappings[j].getAtomNum(seq.findPosition(index));
315 col = java.awt.Color.white;
318 col = sl.getColour(atomNo,
319 mappings[j].getPDBResNum(index),
320 mappings[j].pdbchain,
321 mappings[j].pdbfile);
323 // System.out.println(atomNo+" "+mappings[j].getPDBResNum(index)
324 // +" "+index+" "+col);
327 annotations[index] = new Annotation("X",null,' ',0,col);
339 public void structureSelectionChanged()
341 StructureListener svl;
342 for (int i = 0; i < listeners.size(); i++)
344 svl = (StructureListener) listeners.elementAt(i);
348 public void sequenceSelectionChanged()
350 StructureListener svl;
351 for (int i = 0; i < listeners.size(); i++)
353 svl = (StructureListener) listeners.elementAt(i);
357 public void sequenceColoursChanged(Object source)
359 StructureListener sl;
360 for (int i = 0; i < listeners.size(); i++)
362 if (listeners.elementAt(i) instanceof StructureListener)
364 sl = (StructureListener) listeners.elementAt(i);
365 sl.updateColours(source);
370 public StructureMapping[] getMapping(String pdbfile)
372 Vector tmp = new Vector();
373 for (int i = 0; i < mappings.length; i++)
375 if (mappings[i].pdbfile.equals(pdbfile))
377 tmp.addElement(mappings[i]);
381 StructureMapping[] ret = new StructureMapping[tmp.size()];
382 for (int i = 0; i < tmp.size(); i++)
384 ret[i] = (StructureMapping) tmp.elementAt(i);
390 public String printMapping(String pdbfile)
392 StringBuffer sb = new StringBuffer();
393 for (int i = 0; i < mappings.length; i++)
395 if (mappings[i].pdbfile.equals(pdbfile))
397 sb.append(mappings[i].mappingDetails);
401 return sb.toString();