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());
191 maxChain.transferResidueAnnotation(mappings[mappings.length-1]);
198 public void removeStructureViewerListener(Object svl, String pdbfile)
200 listeners.removeElement(svl);
202 boolean removeMapping = true;
204 StructureListener sl;
205 for (int i = 0; i < listeners.size(); i++)
207 if (listeners.elementAt(i) instanceof StructureListener)
209 sl = (StructureListener) listeners.elementAt(i);
210 if (sl.getPdbFile().equals(pdbfile))
212 removeMapping = false;
218 if (removeMapping && mappings!=null)
220 Vector tmp = new Vector();
221 for (int i = 0; i < mappings.length; i++)
223 if (!mappings[i].pdbfile.equals(pdbfile))
225 tmp.addElement(mappings[i]);
229 mappings = new StructureMapping[tmp.size()];
230 tmp.copyInto(mappings);
234 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
237 for (int i = 0; i < listeners.size(); i++)
239 if (listeners.elementAt(i) instanceof SequenceListener)
241 sl = (SequenceListener) listeners.elementAt(i);
243 for (int j = 0; j < mappings.length; j++)
246 if (mappings[j].pdbfile.equals(pdbfile) &&
247 mappings[j].pdbchain.equals(chain))
249 sl.highlightSequence(mappings[j].sequence,
250 mappings[j].getSeqPos(pdbResNum));
254 sl.highlightSequence(null, pdbResNum);
259 public void mouseOverSequence(SequenceI seq, int index)
261 StructureListener sl;
263 for (int i = 0; i < listeners.size(); i++)
265 if (listeners.elementAt(i) instanceof StructureListener)
267 sl = (StructureListener) listeners.elementAt(i);
269 for (int j = 0; j < mappings.length; j++)
271 if (mappings[j].sequence == seq)
273 atomNo = mappings[j].getAtomNum(index);
277 sl.highlightAtom(atomNo,
278 mappings[j].getPDBResNum(index),
279 mappings[j].pdbchain,
280 mappings[j].pdbfile);
288 public Annotation[] colourSequenceFromStructure(SequenceI seq, String pdbid)
291 //THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
292 //UNTIL THE COLOUR BY ANNOTATION IS REWORKED
293 /* Annotation [] annotations = new Annotation[seq.getLength()];
295 StructureListener sl;
297 for (int i = 0; i < listeners.size(); i++)
299 if (listeners.elementAt(i) instanceof StructureListener)
301 sl = (StructureListener) listeners.elementAt(i);
303 for (int j = 0; j < mappings.length; j++)
306 if (mappings[j].sequence == seq
307 && mappings[j].getPdbId().equals(pdbid)
308 && mappings[j].pdbfile.equals(sl.getPdbFile()))
310 System.out.println(pdbid+" "+mappings[j].getPdbId()
311 +" "+mappings[j].pdbfile);
314 for(int index=0; index<seq.getLength(); index++)
316 if(jalview.util.Comparison.isGap(seq.getCharAt(index)))
319 atomNo = mappings[j].getAtomNum(seq.findPosition(index));
320 col = java.awt.Color.white;
323 col = sl.getColour(atomNo,
324 mappings[j].getPDBResNum(index),
325 mappings[j].pdbchain,
326 mappings[j].pdbfile);
329 annotations[index] = new Annotation("X",null,' ',0,col);
337 return annotations;*/
341 public void structureSelectionChanged()
344 public void sequenceSelectionChanged()
347 public void sequenceColoursChanged(Object source)
349 StructureListener sl;
350 for (int i = 0; i < listeners.size(); i++)
352 if (listeners.elementAt(i) instanceof StructureListener)
354 sl = (StructureListener) listeners.elementAt(i);
355 sl.updateColours(source);
360 public StructureMapping[] getMapping(String pdbfile)
362 Vector tmp = new Vector();
363 for (int i = 0; i < mappings.length; i++)
365 if (mappings[i].pdbfile.equals(pdbfile))
367 tmp.addElement(mappings[i]);
371 StructureMapping[] ret = new StructureMapping[tmp.size()];
372 for (int i = 0; i < tmp.size(); i++)
374 ret[i] = (StructureMapping) tmp.elementAt(i);
380 public String printMapping(String pdbfile)
382 StringBuffer sb = new StringBuffer();
383 for (int i = 0; i < mappings.length; i++)
385 if (mappings[i].pdbfile.equals(pdbfile))
387 sb.append(mappings[i].mappingDetails);
391 return sb.toString();