2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.structure;
24 import jalview.analysis.*;
25 import jalview.datamodel.*;
27 public class StructureSelectionManager
29 static StructureSelectionManager instance;
31 StructureMapping[] mappings;
33 Hashtable mappingData = new Hashtable();
35 public static StructureSelectionManager getStructureSelectionManager()
39 instance = new StructureSelectionManager();
46 * flag controlling whether SeqMappings are relayed from received sequence
47 * mouse over events to other sequences
49 boolean relaySeqMappings = true;
52 * Enable or disable relay of seqMapping events to other sequences. You might
53 * want to do this if there are many sequence mappings and the host computer
58 public void setRelaySeqMappings(boolean relay)
60 relaySeqMappings = relay;
64 * get the state of the relay seqMappings flag.
66 * @return true if sequence mouse overs are being relayed to other mapped
69 public boolean isRelaySeqMappingsEnabled()
71 return relaySeqMappings;
74 Vector listeners = new Vector();
77 * register a listener for alignment sequence mouseover events
80 public void addStructureViewerListener(Object svl)
82 if (!listeners.contains(svl))
84 listeners.addElement(svl);
88 public String alreadyMappedToFile(String pdbid)
92 for (int i = 0; i < mappings.length; i++)
94 if (mappings[i].getPdbId().equals(pdbid))
96 return mappings[i].pdbfile;
104 * create sequence structure mappings between each sequence and the given
105 * pdbFile (retrieved via the given protocol).
108 * - one or more sequences to be mapped to pdbFile
109 * @param targetChains
110 * - optional chain specification for mapping each sequence to pdb
111 * (may be nill, individual elements may be nill)
113 * - structure data resource
115 * - how to resolve data from resource
116 * @return null or the structure data parsed as a pdb file
118 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
119 String[] targetChains, String pdbFile, String protocol)
122 * There will be better ways of doing this in the future, for now we'll use
123 * the tried and tested MCview pdb mapping
125 MCview.PDBfile pdb = null;
128 pdb = new MCview.PDBfile(pdbFile, protocol);
129 } catch (Exception ex)
131 ex.printStackTrace();
136 for (int s = 0; s < sequence.length; s++)
138 if (targetChains != null && targetChains[s] != null)
139 targetChain = targetChains[s];
140 else if (sequence[s].getName().indexOf("|") > -1)
142 targetChain = sequence[s].getName().substring(
143 sequence[s].getName().lastIndexOf("|") + 1);
149 AlignSeq maxAlignseq = null;
150 String maxChainId = " ";
151 PDBChain maxChain = null;
152 boolean first = true;
153 for (int i = 0; i < pdb.chains.size(); i++)
155 // TODO: re http://issues.jalview.org/browse/JAL-583 : this patch may
156 // need to be revoked
157 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
158 if (targetChain.length() > 0 && !targetChain.equals(chain.id))
160 continue; // don't try to map chains don't match.
162 // end of patch for limiting computed mappings
163 // TODO: correctly determine sequence type for mixed na/peptide
165 AlignSeq as = new AlignSeq(sequence[s],
166 ((PDBChain) pdb.chains.elementAt(i)).sequence,
167 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
169 as.calcScoreMatrix();
172 if (first || as.maxscore > max
173 || (as.maxscore == max && chain.id.equals(targetChain)))
179 maxChainId = chain.id;
182 if (maxChain == null)
186 final StringBuffer mappingDetails = new StringBuffer();
187 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
188 + maxChain.sequence.getSequenceAsString());
189 mappingDetails.append("\nNo of residues = "
190 + maxChain.residues.size() + "\n\n");
191 PrintStream ps = new PrintStream(System.out)
193 public void print(String x)
195 mappingDetails.append(x);
198 public void println()
200 mappingDetails.append("\n");
204 maxAlignseq.printAlignment(ps);
206 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
207 + " " + maxAlignseq.seq2end);
208 mappingDetails.append("\nSEQ start/end "
209 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
210 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
212 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
214 maxChain.transferRESNUMFeatures(sequence[s], null);
216 // allocate enough slots to store the mapping from positions in
217 // sequence[s] to the associated chain
218 int[][] mapping = new int[sequence[s].findPosition(sequence[s].getLength()) + 2][2];
224 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
225 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
227 resNum = tmp.resNumber;
228 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
229 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
233 } while (index < maxChain.atoms.size());
235 if (mappings == null)
237 mappings = new StructureMapping[1];
241 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
242 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
246 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
247 pdbFile = "INLINE" + pdb.id;
249 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
250 pdbFile, pdb.id, maxChainId, mapping,
251 mappingDetails.toString());
252 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
259 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
261 listeners.removeElement(svl);
262 if (svl instanceof SequenceListener)
264 for (int i=0;i<listeners.size();i++)
266 if (listeners.elementAt(i) instanceof StructureListener)
268 ((StructureListener)listeners.elementAt(i)).releaseReferences(svl);
273 if (pdbfiles == null)
277 boolean removeMapping = true;
279 Vector pdbs = new Vector();
280 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
282 StructureListener sl;
283 for (int i = 0; i < listeners.size(); i++)
285 if (listeners.elementAt(i) instanceof StructureListener)
287 sl = (StructureListener) listeners.elementAt(i);
288 handlepdbs = sl.getPdbFile();
289 for (int j = 0; j < handlepdbs.length; j++)
291 if (pdbs.contains(handlepdbs[j]))
293 pdbs.removeElement(handlepdbs[j]);
300 if (pdbs.size() > 0 && mappings != null)
302 Vector tmp = new Vector();
303 for (int i = 0; i < mappings.length; i++)
305 if (!pdbs.contains(mappings[i].pdbfile))
307 tmp.addElement(mappings[i]);
311 mappings = new StructureMapping[tmp.size()];
312 tmp.copyInto(mappings);
316 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
318 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
319 SearchResults results = null;
320 SequenceI lastseq = null;
321 int lastipos = -1, indexpos;
322 for (int i = 0; i < listeners.size(); i++)
324 if (listeners.elementAt(i) instanceof SequenceListener)
328 results = new SearchResults();
330 if (mappings != null)
332 for (int j = 0; j < mappings.length; j++)
334 if (mappings[j].pdbfile.equals(pdbfile)
335 && mappings[j].pdbchain.equals(chain))
337 indexpos = mappings[j].getSeqPos(pdbResNum);
338 if (lastipos != indexpos && lastseq != mappings[j].sequence)
340 results.addResult(mappings[j].sequence, indexpos, indexpos);
342 lastseq = mappings[j].sequence;
343 // construct highlighted sequence list
344 if (seqmappings != null)
347 Enumeration e = seqmappings.elements();
348 while (e.hasMoreElements())
351 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
352 mappings[j].sequence, indexpos, results);
362 if (results.getSize() > 0)
364 for (int i = 0; i < listeners.size(); i++)
366 Object li = listeners.elementAt(i);
367 if (li instanceof SequenceListener)
368 ((SequenceListener) li).highlightSequence(results);
373 Vector seqmappings = null; // should be a simpler list of mapped seuqence
376 * highlight regions associated with a position (indexpos) in seq
379 * the sequeence that the mouse over occured on
381 * the absolute position being mouseovered in seq (0 to seq.length())
383 * the sequence position (if -1, seq.findPosition is called to
384 * resolve the residue number)
386 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
389 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
390 SearchResults results = null;
392 index = seq.findPosition(indexpos);
393 StructureListener sl;
395 for (int i = 0; i < listeners.size(); i++)
397 if (listeners.elementAt(i) instanceof StructureListener)
399 sl = (StructureListener) listeners.elementAt(i);
400 if (mappings == null)
404 for (int j = 0; j < mappings.length; j++)
406 if (mappings[j].sequence == seq
407 || mappings[j].sequence == seq.getDatasetSequence())
409 atomNo = mappings[j].getAtomNum(index);
413 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
414 mappings[j].pdbchain, mappings[j].pdbfile);
421 if (relaySeqMappings && hasSequenceListeners
422 && listeners.elementAt(i) instanceof SequenceListener)
425 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
430 results = new SearchResults();
431 if (index >= seq.getStart() && index <= seq.getEnd())
433 // construct highlighted sequence list
435 if (seqmappings != null)
437 Enumeration e = seqmappings.elements();
438 while (e.hasMoreElements())
441 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
442 seq, index, results);
445 // hasSequenceListeners = results.getSize() > 0;
446 if (handlingVamsasMo)
448 // maybe have to resolve seq to a dataset seqeunce...
449 // add in additional direct sequence and/or dataset sequence
451 results.addResult(seq, index, index);
455 if (hasSequenceListeners)
457 ((SequenceListener) listeners.elementAt(i))
458 .highlightSequence(results);
461 else if (listeners.elementAt(i) instanceof VamsasListener
462 && !handlingVamsasMo)
465 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
468 // pass the mouse over and absolute position onto the
470 ((VamsasListener) listeners.elementAt(i)).mouseOver(seq,
478 * true if a mouse over event from an external (ie Vamsas) source is being
481 boolean handlingVamsasMo = false;
486 * as mouseOverSequence but only route event to SequenceListeners
490 * in an alignment sequence
492 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
495 handlingVamsasMo = true;
496 long msg = sequenceI.hashCode() * (1 + position);
500 mouseOverSequence(sequenceI, position, -1, source);
502 handlingVamsasMo = false;
505 public Annotation[] colourSequenceFromStructure(SequenceI seq,
509 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
510 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
512 * Annotation [] annotations = new Annotation[seq.getLength()];
514 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
515 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
516 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
518 * for (int j = 0; j < mappings.length; j++) {
520 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
521 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
522 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
523 * "+mappings[j].pdbfile);
525 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
526 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
528 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
529 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
530 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
531 * mappings[j].pdbfile); }
533 * annotations[index] = new Annotation("X",null,' ',0,col); } return
534 * annotations; } } } }
536 * return annotations;
540 public void structureSelectionChanged()
544 public void sequenceSelectionChanged()
548 public void sequenceColoursChanged(Object source)
550 StructureListener sl;
551 for (int i = 0; i < listeners.size(); i++)
553 if (listeners.elementAt(i) instanceof StructureListener)
555 sl = (StructureListener) listeners.elementAt(i);
556 sl.updateColours(source);
561 public StructureMapping[] getMapping(String pdbfile)
563 Vector tmp = new Vector();
564 if (mappings != null)
566 for (int i = 0; i < mappings.length; i++)
568 if (mappings[i].pdbfile.equals(pdbfile))
570 tmp.addElement(mappings[i]);
574 StructureMapping[] ret = new StructureMapping[tmp.size()];
575 for (int i = 0; i < tmp.size(); i++)
577 ret[i] = (StructureMapping) tmp.elementAt(i);
583 public String printMapping(String pdbfile)
585 StringBuffer sb = new StringBuffer();
586 for (int i = 0; i < mappings.length; i++)
588 if (mappings[i].pdbfile.equals(pdbfile))
590 sb.append(mappings[i].mappingDetails);
594 return sb.toString();
597 private int[] seqmappingrefs = null; // refcount for seqmappings elements
599 private synchronized void modifySeqMappingList(boolean add,
600 AlignedCodonFrame[] codonFrames)
602 if (!add && (seqmappings == null || seqmappings.size() == 0))
604 if (seqmappings == null)
605 seqmappings = new Vector();
606 if (codonFrames != null && codonFrames.length > 0)
608 for (int cf = 0; cf < codonFrames.length; cf++)
610 if (seqmappings.contains(codonFrames[cf]))
614 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
618 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
620 int pos = seqmappings.indexOf(codonFrames[cf]);
621 int[] nr = new int[seqmappingrefs.length - 1];
624 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
626 if (pos < seqmappingrefs.length - 1)
628 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
629 seqmappingrefs.length - pos - 2);
638 seqmappings.addElement(codonFrames[cf]);
640 int[] nsr = new int[(seqmappingrefs == null) ? 1
641 : seqmappingrefs.length + 1];
642 if (seqmappingrefs != null && seqmappingrefs.length > 0)
643 System.arraycopy(seqmappingrefs, 0, nsr, 0,
644 seqmappingrefs.length);
645 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
646 seqmappingrefs = nsr;
653 public void removeMappings(AlignedCodonFrame[] codonFrames)
655 modifySeqMappingList(false, codonFrames);
658 public void addMappings(AlignedCodonFrame[] codonFrames)
660 modifySeqMappingList(true, codonFrames);
663 Vector sel_listeners = new Vector();
665 public void addSelectionListener(SelectionListener selecter)
667 if (!sel_listeners.contains(selecter))
669 sel_listeners.addElement(selecter);
673 public void removeSelectionListener(SelectionListener toremove)
675 if (sel_listeners.contains(toremove))
677 sel_listeners.removeElement(toremove);
681 public synchronized void sendSelection(
682 jalview.datamodel.SequenceGroup selection,
683 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
685 if (sel_listeners != null && sel_listeners.size() > 0)
687 Enumeration listeners = sel_listeners.elements();
688 while (listeners.hasMoreElements())
690 SelectionListener slis = ((SelectionListener) listeners
694 slis.selection(selection, colsel, source);