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(
74 String[] targetChains,
78 MCview.PDBfile pdb = null;
81 pdb = new MCview.PDBfile(pdbFile, protocol);
90 for (int s = 0; s < sequence.length; s++)
92 if(targetChains!=null && targetChains[s]!=null)
93 targetChain = targetChains[s];
94 else if (sequence[s].getName().indexOf("|") > -1)
96 targetChain = sequence[s].getName().substring(
97 sequence[s].getName().lastIndexOf("|") + 1);
104 AlignSeq maxAlignseq = null;
105 String maxChainId = " ";
106 PDBChain maxChain = null;
108 for (int i = 0; i < pdb.chains.size(); i++)
110 AlignSeq as = new AlignSeq(sequence[s],
111 ( (PDBChain) pdb.chains.elementAt(i)).
114 as.calcScoreMatrix();
116 PDBChain chain = ( (PDBChain) pdb.chains.elementAt(i));
118 if (as.maxscore > max
119 || (as.maxscore == max && chain.id.equals(targetChain)))
124 maxChainId = chain.id;
128 final StringBuffer mappingDetails = new StringBuffer();
129 mappingDetails.append("\n\nPDB Sequence is :\nSequence = " +
130 maxChain.sequence.getSequenceAsString());
131 mappingDetails.append("\nNo of residues = " +
135 PrintStream ps = new PrintStream(System.out)
137 public void print(String x)
139 mappingDetails.append(x);
142 public void println()
144 mappingDetails.append("\n");
148 maxAlignseq.printAlignment(ps);
150 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start + " " +
151 maxAlignseq.seq2end);
152 mappingDetails.append("\nSEQ start/end "
153 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) +
155 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
157 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
159 maxChain.transferRESNUMFeatures(sequence[s], null);
161 int[][] mapping = new int[sequence[s].getEnd() + 2][2];
168 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
169 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
171 resNum = tmp.resNumber;
172 mapping[tmp.alignmentMapping+1][0] = tmp.resNumber;
173 mapping[tmp.alignmentMapping+1][1] = tmp.atomIndex;
178 while(index < maxChain.atoms.size());
180 if (mappings == null)
182 mappings = new StructureMapping[1];
186 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
187 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
191 if(protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
192 pdbFile = "INLINE"+pdb.id;
194 mappings[mappings.length - 1]
195 = new StructureMapping(sequence[s], pdbFile, pdb.id, maxChainId,
196 mapping, mappingDetails.toString());
197 maxChain.transferResidueAnnotation(mappings[mappings.length-1]);
204 public void removeStructureViewerListener(Object svl, String pdbfile)
206 listeners.removeElement(svl);
208 boolean removeMapping = true;
210 StructureListener sl;
211 for (int i = 0; i < listeners.size(); i++)
213 if (listeners.elementAt(i) instanceof StructureListener)
215 sl = (StructureListener) listeners.elementAt(i);
216 if (sl.getPdbFile().equals(pdbfile))
218 removeMapping = false;
224 if (removeMapping && mappings!=null)
226 Vector tmp = new Vector();
227 for (int i = 0; i < mappings.length; i++)
229 if (!mappings[i].pdbfile.equals(pdbfile))
231 tmp.addElement(mappings[i]);
235 mappings = new StructureMapping[tmp.size()];
236 tmp.copyInto(mappings);
240 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
243 for (int i = 0; i < listeners.size(); i++)
245 if (listeners.elementAt(i) instanceof SequenceListener)
247 sl = (SequenceListener) listeners.elementAt(i);
249 for (int j = 0; j < mappings.length; j++)
251 if (mappings[j].pdbfile.equals(pdbfile) &&
252 mappings[j].pdbchain.equals(chain))
254 sl.highlightSequence(mappings[j].sequence,
255 mappings[j].getSeqPos(pdbResNum));
262 public void mouseOverSequence(SequenceI seq, int index)
264 StructureListener sl;
266 for (int i = 0; i < listeners.size(); i++)
268 if (listeners.elementAt(i) instanceof StructureListener)
270 sl = (StructureListener) listeners.elementAt(i);
272 for (int j = 0; j < mappings.length; j++)
274 if (mappings[j].sequence == seq)
276 atomNo = mappings[j].getAtomNum(index);
280 sl.highlightAtom(atomNo,
281 mappings[j].getPDBResNum(index),
282 mappings[j].pdbchain,
283 mappings[j].pdbfile);
291 public Annotation[] colourSequenceFromStructure(SequenceI seq, String pdbid)
294 //THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
295 //UNTIL THE COLOUR BY ANNOTATION IS REWORKED
296 /* Annotation [] annotations = new Annotation[seq.getLength()];
298 StructureListener sl;
300 for (int i = 0; i < listeners.size(); i++)
302 if (listeners.elementAt(i) instanceof StructureListener)
304 sl = (StructureListener) listeners.elementAt(i);
306 for (int j = 0; j < mappings.length; j++)
309 if (mappings[j].sequence == seq
310 && mappings[j].getPdbId().equals(pdbid)
311 && mappings[j].pdbfile.equals(sl.getPdbFile()))
313 System.out.println(pdbid+" "+mappings[j].getPdbId()
314 +" "+mappings[j].pdbfile);
317 for(int index=0; index<seq.getLength(); index++)
319 if(jalview.util.Comparison.isGap(seq.getCharAt(index)))
322 atomNo = mappings[j].getAtomNum(seq.findPosition(index));
323 col = java.awt.Color.white;
326 col = sl.getColour(atomNo,
327 mappings[j].getPDBResNum(index),
328 mappings[j].pdbchain,
329 mappings[j].pdbfile);
332 annotations[index] = new Annotation("X",null,' ',0,col);
340 return annotations;*/
344 public void structureSelectionChanged()
347 public void sequenceSelectionChanged()
350 public void sequenceColoursChanged(Object source)
352 StructureListener sl;
353 for (int i = 0; i < listeners.size(); i++)
355 if (listeners.elementAt(i) instanceof StructureListener)
357 sl = (StructureListener) listeners.elementAt(i);
358 sl.updateColours(source);
363 public StructureMapping[] getMapping(String pdbfile)
365 Vector tmp = new Vector();
366 for (int i = 0; i < mappings.length; i++)
368 if (mappings[i].pdbfile.equals(pdbfile))
370 tmp.addElement(mappings[i]);
374 StructureMapping[] ret = new StructureMapping[tmp.size()];
375 for (int i = 0; i < tmp.size(); i++)
377 ret[i] = (StructureMapping) tmp.elementAt(i);
383 public String printMapping(String pdbfile)
385 StringBuffer sb = new StringBuffer();
386 for (int i = 0; i < mappings.length; i++)
388 if (mappings[i].pdbfile.equals(pdbfile))
390 sb.append(mappings[i].mappingDetails);
394 return sb.toString();