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 int[][] mapping = new int[sequence[s].getEnd() + 2][2];
222 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
223 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
225 resNum = tmp.resNumber;
226 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
227 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
231 } while (index < maxChain.atoms.size());
233 if (mappings == null)
235 mappings = new StructureMapping[1];
239 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
240 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
244 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
245 pdbFile = "INLINE" + pdb.id;
247 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
248 pdbFile, pdb.id, maxChainId, mapping,
249 mappingDetails.toString());
250 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
257 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
259 listeners.removeElement(svl);
260 if (pdbfiles == null)
264 boolean removeMapping = true;
266 Vector pdbs = new Vector();
267 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
269 StructureListener sl;
270 for (int i = 0; i < listeners.size(); i++)
272 if (listeners.elementAt(i) instanceof StructureListener)
274 sl = (StructureListener) listeners.elementAt(i);
275 handlepdbs = sl.getPdbFile();
276 for (int j = 0; j < handlepdbs.length; j++)
278 if (pdbs.contains(handlepdbs[j]))
280 pdbs.removeElement(handlepdbs[j]);
287 if (pdbs.size() > 0 && mappings != null)
289 Vector tmp = new Vector();
290 for (int i = 0; i < mappings.length; i++)
292 if (!pdbs.contains(mappings[i].pdbfile))
294 tmp.addElement(mappings[i]);
298 mappings = new StructureMapping[tmp.size()];
299 tmp.copyInto(mappings);
303 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
305 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
306 SearchResults results = null;
307 for (int i = 0; i < listeners.size(); i++)
309 if (listeners.elementAt(i) instanceof SequenceListener)
313 results = new SearchResults();
316 if (mappings != null)
318 for (int j = 0; j < mappings.length; j++)
320 if (mappings[j].pdbfile.equals(pdbfile)
321 && mappings[j].pdbchain.equals(chain))
323 indexpos = mappings[j].getSeqPos(pdbResNum);
324 results.addResult(mappings[j].sequence, indexpos, indexpos);
325 // construct highlighted sequence list
326 if (seqmappings != null)
329 Enumeration e = seqmappings.elements();
330 while (e.hasMoreElements())
333 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
334 mappings[j].sequence, indexpos, results);
342 if (results.getSize() > 0)
344 for (int i = 0; i < listeners.size(); i++)
346 Object li = listeners.elementAt(i);
347 if (li instanceof SequenceListener)
348 ((SequenceListener) li).highlightSequence(results);
353 Vector seqmappings = null; // should be a simpler list of mapped seuqence
356 * highlight regions associated with a position (indexpos) in seq
359 * the sequeence that the mouse over occured on
361 * the absolute position being mouseovered in seq (0 to seq.length())
363 * the sequence position (if -1, seq.findPosition is called to
364 * resolve the residue number)
366 public void mouseOverSequence(SequenceI seq, int indexpos, int index, VamsasSource source)
368 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
369 SearchResults results = null;
371 index = seq.findPosition(indexpos);
372 StructureListener sl;
374 for (int i = 0; i < listeners.size(); i++)
376 if (listeners.elementAt(i) instanceof StructureListener)
378 sl = (StructureListener) listeners.elementAt(i);
379 if (mappings == null)
383 for (int j = 0; j < mappings.length; j++)
385 if (mappings[j].sequence == seq
386 || mappings[j].sequence == seq.getDatasetSequence())
388 atomNo = mappings[j].getAtomNum(index);
392 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
393 mappings[j].pdbchain, mappings[j].pdbfile);
400 if (relaySeqMappings && hasSequenceListeners
401 && listeners.elementAt(i) instanceof SequenceListener)
404 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
409 results = new SearchResults();
410 if (index >= seq.getStart() && index <= seq.getEnd())
412 // construct highlighted sequence list
414 if (seqmappings != null)
416 Enumeration e = seqmappings.elements();
417 while (e.hasMoreElements())
420 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
421 seq, index, results);
424 // hasSequenceListeners = results.getSize() > 0;
425 if (handlingVamsasMo)
427 // maybe have to resolve seq to a dataset seqeunce...
428 // add in additional direct sequence and/or dataset sequence
430 results.addResult(seq, index, index);
434 if (hasSequenceListeners)
436 ((SequenceListener) listeners.elementAt(i))
437 .highlightSequence(results);
440 else if (listeners.elementAt(i) instanceof VamsasListener
441 && !handlingVamsasMo)
444 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
447 // pass the mouse over and absolute position onto the
449 ((VamsasListener) listeners.elementAt(i))
450 .mouseOver(seq, indexpos, source);
457 * true if a mouse over event from an external (ie Vamsas) source is being
460 boolean handlingVamsasMo = false;
465 * as mouseOverSequence but only route event to SequenceListeners
469 * in an alignment sequence
471 public void mouseOverVamsasSequence(SequenceI sequenceI, int position, VamsasSource source)
473 handlingVamsasMo = true;
474 long msg = sequenceI.hashCode() * (1 + position);
478 mouseOverSequence(sequenceI, position, -1, source);
480 handlingVamsasMo = false;
483 public Annotation[] colourSequenceFromStructure(SequenceI seq,
487 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
488 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
490 * Annotation [] annotations = new Annotation[seq.getLength()];
492 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
493 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
494 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
496 * for (int j = 0; j < mappings.length; j++) {
498 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
499 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
500 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
501 * "+mappings[j].pdbfile);
503 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
504 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
506 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
507 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
508 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
509 * mappings[j].pdbfile); }
511 * annotations[index] = new Annotation("X",null,' ',0,col); } return
512 * annotations; } } } }
514 * return annotations;
518 public void structureSelectionChanged()
522 public void sequenceSelectionChanged()
526 public void sequenceColoursChanged(Object source)
528 StructureListener sl;
529 for (int i = 0; i < listeners.size(); i++)
531 if (listeners.elementAt(i) instanceof StructureListener)
533 sl = (StructureListener) listeners.elementAt(i);
534 sl.updateColours(source);
539 public StructureMapping[] getMapping(String pdbfile)
541 Vector tmp = new Vector();
542 if (mappings != null)
544 for (int i = 0; i < mappings.length; i++)
546 if (mappings[i].pdbfile.equals(pdbfile))
548 tmp.addElement(mappings[i]);
552 StructureMapping[] ret = new StructureMapping[tmp.size()];
553 for (int i = 0; i < tmp.size(); i++)
555 ret[i] = (StructureMapping) tmp.elementAt(i);
561 public String printMapping(String pdbfile)
563 StringBuffer sb = new StringBuffer();
564 for (int i = 0; i < mappings.length; i++)
566 if (mappings[i].pdbfile.equals(pdbfile))
568 sb.append(mappings[i].mappingDetails);
572 return sb.toString();
575 private int[] seqmappingrefs = null; // refcount for seqmappings elements
577 private synchronized void modifySeqMappingList(boolean add,
578 AlignedCodonFrame[] codonFrames)
580 if (!add && (seqmappings == null || seqmappings.size() == 0))
582 if (seqmappings == null)
583 seqmappings = new Vector();
584 if (codonFrames != null && codonFrames.length > 0)
586 for (int cf = 0; cf < codonFrames.length; cf++)
588 if (seqmappings.contains(codonFrames[cf]))
592 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
596 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
598 int pos = seqmappings.indexOf(codonFrames[cf]);
599 int[] nr = new int[seqmappingrefs.length - 1];
602 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
604 if (pos < seqmappingrefs.length - 1)
606 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
607 seqmappingrefs.length - pos - 2);
616 seqmappings.addElement(codonFrames[cf]);
618 int[] nsr = new int[(seqmappingrefs == null) ? 1
619 : seqmappingrefs.length + 1];
620 if (seqmappingrefs != null && seqmappingrefs.length > 0)
621 System.arraycopy(seqmappingrefs, 0, nsr, 0,
622 seqmappingrefs.length);
623 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
624 seqmappingrefs = nsr;
631 public void removeMappings(AlignedCodonFrame[] codonFrames)
633 modifySeqMappingList(false, codonFrames);
636 public void addMappings(AlignedCodonFrame[] codonFrames)
638 modifySeqMappingList(true, codonFrames);
641 Vector sel_listeners = new Vector();
643 public void addSelectionListener(SelectionListener selecter)
645 if (!sel_listeners.contains(selecter))
647 sel_listeners.addElement(selecter);
651 public void removeSelectionListener(SelectionListener toremove)
653 if (sel_listeners.contains(toremove))
655 sel_listeners.removeElement(toremove);
659 public synchronized void sendSelection(
660 jalview.datamodel.SequenceGroup selection,
661 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
663 if (sel_listeners != null && sel_listeners.size() > 0)
665 Enumeration listeners = sel_listeners.elements();
666 while (listeners.hasMoreElements())
668 SelectionListener slis = ((SelectionListener) listeners
672 slis.selection(selection, colsel, source);