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;
32 StructureMapping[] mappings;
34 Hashtable mappingData = new Hashtable();
36 public static StructureSelectionManager getStructureSelectionManager()
40 instance = new StructureSelectionManager();
47 * flag controlling whether SeqMappings are relayed from received sequence
48 * mouse over events to other sequences
50 boolean relaySeqMappings = true;
53 * Enable or disable relay of seqMapping events to other sequences. You might
54 * want to do this if there are many sequence mappings and the host computer
59 public void setRelaySeqMappings(boolean relay)
61 relaySeqMappings = relay;
65 * get the state of the relay seqMappings flag.
67 * @return true if sequence mouse overs are being relayed to other mapped
70 public boolean isRelaySeqMappingsEnabled()
72 return relaySeqMappings;
75 Vector listeners = new Vector();
77 public void addStructureViewerListener(Object svl)
79 if (!listeners.contains(svl))
81 listeners.addElement(svl);
85 public String alreadyMappedToFile(String pdbid)
89 for (int i = 0; i < mappings.length; i++)
91 if (mappings[i].getPdbId().equals(pdbid))
93 return mappings[i].pdbfile;
101 * There will be better ways of doing this in the future, for now we'll use
102 * the tried and tested MCview pdb mapping
104 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
105 String[] targetChains, String pdbFile, String protocol)
107 MCview.PDBfile pdb = null;
110 pdb = new MCview.PDBfile(pdbFile, protocol);
111 } catch (Exception ex)
113 ex.printStackTrace();
118 for (int s = 0; s < sequence.length; s++)
120 if (targetChains != null && targetChains[s] != null)
121 targetChain = targetChains[s];
122 else if (sequence[s].getName().indexOf("|") > -1)
124 targetChain = sequence[s].getName().substring(
125 sequence[s].getName().lastIndexOf("|") + 1);
131 AlignSeq maxAlignseq = null;
132 String maxChainId = " ";
133 PDBChain maxChain = null;
135 for (int i = 0; i < pdb.chains.size(); i++)
137 AlignSeq as = new AlignSeq(sequence[s], ((PDBChain) pdb.chains
138 .elementAt(i)).sequence, AlignSeq.PEP);
139 as.calcScoreMatrix();
141 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
143 if (as.maxscore > max
144 || (as.maxscore == max && chain.id.equals(targetChain)))
149 maxChainId = chain.id;
153 final StringBuffer mappingDetails = new StringBuffer();
154 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
155 + maxChain.sequence.getSequenceAsString());
156 mappingDetails.append("\nNo of residues = "
157 + maxChain.residues.size() + "\n\n");
158 PrintStream ps = new PrintStream(System.out)
160 public void print(String x)
162 mappingDetails.append(x);
165 public void println()
167 mappingDetails.append("\n");
171 maxAlignseq.printAlignment(ps);
173 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
174 + " " + maxAlignseq.seq2end);
175 mappingDetails.append("\nSEQ start/end "
176 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
177 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
179 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
181 maxChain.transferRESNUMFeatures(sequence[s], null);
183 int[][] mapping = new int[sequence[s].getEnd() + 2][2];
189 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
190 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
192 resNum = tmp.resNumber;
193 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
194 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
198 } while (index < maxChain.atoms.size());
200 if (mappings == null)
202 mappings = new StructureMapping[1];
206 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
207 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
211 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
212 pdbFile = "INLINE" + pdb.id;
214 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
215 pdbFile, pdb.id, maxChainId, mapping, mappingDetails
217 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
224 public void removeStructureViewerListener(Object svl, String pdbfile)
226 listeners.removeElement(svl);
228 boolean removeMapping = true;
230 StructureListener sl;
231 for (int i = 0; i < listeners.size(); i++)
233 if (listeners.elementAt(i) instanceof StructureListener)
235 sl = (StructureListener) listeners.elementAt(i);
236 if (sl.getPdbFile().equals(pdbfile))
238 removeMapping = false;
244 if (removeMapping && mappings != null)
246 Vector tmp = new Vector();
247 for (int i = 0; i < mappings.length; i++)
249 if (!mappings[i].pdbfile.equals(pdbfile))
251 tmp.addElement(mappings[i]);
255 mappings = new StructureMapping[tmp.size()];
256 tmp.copyInto(mappings);
260 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
262 SearchResults results = null;
263 for (int i = 0; i < listeners.size(); i++)
265 if (listeners.elementAt(i) instanceof SequenceListener)
269 results = new SearchResults();
271 for (int j = 0; j < mappings.length; j++)
273 if (mappings[j].pdbfile.equals(pdbfile)
274 && mappings[j].pdbchain.equals(chain))
276 results.addResult(mappings[j].sequence, mappings[j]
277 .getSeqPos(pdbResNum), mappings[j]
278 .getSeqPos(pdbResNum));
282 if (results.getSize() > 0)
284 ((SequenceListener) listeners.elementAt(i))
285 .highlightSequence(results);
292 Vector seqmappings = null; // should be a simpler list of mapped seuqence
294 * highlight regions associated with a position (indexpos) in seq
295 * @param seq the sequeence that the mouse over occured on
296 * @param indexpos the absolute position being mouseovered in seq (0 to seq.length())
297 * @param index the sequence position (if -1, seq.findPosition is called to resolve the residue number)
299 public void mouseOverSequence(SequenceI seq, int indexpos, int index)
301 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
302 SearchResults results = null;
304 index=seq.findPosition(indexpos);
305 StructureListener sl;
307 for (int i = 0; i < listeners.size(); i++)
309 if (listeners.elementAt(i) instanceof StructureListener)
311 sl = (StructureListener) listeners.elementAt(i);
313 for (int j = 0; j < mappings.length; j++)
315 if (mappings[j].sequence == seq || mappings[j].sequence==seq.getDatasetSequence())
317 atomNo = mappings[j].getAtomNum(index);
321 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
322 mappings[j].pdbchain, mappings[j].pdbfile);
329 if (relaySeqMappings && hasSequenceListeners
330 && listeners.elementAt(i) instanceof SequenceListener)
333 //System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
338 results = new SearchResults();
339 if (index >= seq.getStart() && index <= seq.getEnd())
341 // construct highlighted sequence list
343 if (seqmappings!=null)
345 Enumeration e = seqmappings.elements();
346 while (e.hasMoreElements())
349 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
350 seq, index, results);
353 // hasSequenceListeners = results.getSize() > 0;
354 if (handlingVamsasMo)
356 // maybe have to resolve seq to a dataset seqeunce...
357 // add in additional direct sequence and/or dataset sequence
359 results.addResult(seq, index, index);
363 if (hasSequenceListeners)
365 ((SequenceListener) listeners.elementAt(i))
366 .highlightSequence(results);
369 else if (listeners.elementAt(i) instanceof VamsasListener
370 && !handlingVamsasMo)
373 //System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + " " +
375 // pass the mouse over and absolute position onto the VamsasListener(s)
376 ((VamsasListener) listeners.elementAt(i)).mouseOver(seq, indexpos);
383 * true if a mouse over event from an external (ie Vamsas) source is being
386 boolean handlingVamsasMo = false;
389 * as mouseOverSequence but only route event to SequenceListeners
392 * @param position in an alignment sequence
394 public void mouseOverVamsasSequence(SequenceI sequenceI, int position)
396 handlingVamsasMo = true;
397 long msg = sequenceI.hashCode()*(1+position);
401 mouseOverSequence(sequenceI, position, -1);
403 handlingVamsasMo = false;
406 public Annotation[] colourSequenceFromStructure(SequenceI seq,
410 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
411 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
413 * Annotation [] annotations = new Annotation[seq.getLength()];
415 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
416 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
417 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
419 * for (int j = 0; j < mappings.length; j++) {
421 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid) &&
422 * mappings[j].pdbfile.equals(sl.getPdbFile())) { System.out.println(pdbid+"
423 * "+mappings[j].getPdbId() +" "+mappings[j].pdbfile);
425 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
426 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
428 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
429 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
430 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
431 * mappings[j].pdbfile); }
433 * annotations[index] = new Annotation("X",null,' ',0,col); } return
434 * annotations; } } } }
436 * return annotations;
440 public void structureSelectionChanged()
444 public void sequenceSelectionChanged()
448 public void sequenceColoursChanged(Object source)
450 StructureListener sl;
451 for (int i = 0; i < listeners.size(); i++)
453 if (listeners.elementAt(i) instanceof StructureListener)
455 sl = (StructureListener) listeners.elementAt(i);
456 sl.updateColours(source);
461 public StructureMapping[] getMapping(String pdbfile)
463 Vector tmp = new Vector();
464 for (int i = 0; i < mappings.length; i++)
466 if (mappings[i].pdbfile.equals(pdbfile))
468 tmp.addElement(mappings[i]);
472 StructureMapping[] ret = new StructureMapping[tmp.size()];
473 for (int i = 0; i < tmp.size(); i++)
475 ret[i] = (StructureMapping) tmp.elementAt(i);
481 public String printMapping(String pdbfile)
483 StringBuffer sb = new StringBuffer();
484 for (int i = 0; i < mappings.length; i++)
486 if (mappings[i].pdbfile.equals(pdbfile))
488 sb.append(mappings[i].mappingDetails);
492 return sb.toString();
495 private int[] seqmappingrefs = null; // refcount for seqmappings elements
497 private synchronized void modifySeqMappingList(boolean add,
498 AlignedCodonFrame[] codonFrames)
500 if (!add && (seqmappings == null || seqmappings.size() == 0))
502 if (seqmappings == null)
503 seqmappings = new Vector();
504 if (codonFrames != null && codonFrames.length > 0)
506 for (int cf = 0; cf < codonFrames.length; cf++)
508 if (seqmappings.contains(codonFrames[cf]))
512 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
516 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
518 int pos = seqmappings.indexOf(codonFrames[cf]);
519 int[] nr = new int[seqmappingrefs.length - 1];
522 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
524 if (pos < seqmappingrefs.length - 1)
526 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
527 seqmappingrefs.length - pos - 2);
536 seqmappings.addElement(codonFrames[cf]);
538 int[] nsr = new int[(seqmappingrefs == null) ? 1
539 : seqmappingrefs.length + 1];
540 if (seqmappingrefs != null && seqmappingrefs.length > 0)
541 System.arraycopy(seqmappingrefs, 0, nsr, 0,
542 seqmappingrefs.length);
543 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
544 seqmappingrefs = nsr;
551 public void removeMappings(AlignedCodonFrame[] codonFrames)
553 modifySeqMappingList(false, codonFrames);
556 public void addMappings(AlignedCodonFrame[] codonFrames)
558 modifySeqMappingList(true, codonFrames);