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[maxChain.sequence.getEnd() + 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 (pdbfiles == null)
266 boolean removeMapping = true;
268 Vector pdbs = new Vector();
269 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
271 StructureListener sl;
272 for (int i = 0; i < listeners.size(); i++)
274 if (listeners.elementAt(i) instanceof StructureListener)
276 sl = (StructureListener) listeners.elementAt(i);
277 handlepdbs = sl.getPdbFile();
278 for (int j = 0; j < handlepdbs.length; j++)
280 if (pdbs.contains(handlepdbs[j]))
282 pdbs.removeElement(handlepdbs[j]);
289 if (pdbs.size() > 0 && mappings != null)
291 Vector tmp = new Vector();
292 for (int i = 0; i < mappings.length; i++)
294 if (!pdbs.contains(mappings[i].pdbfile))
296 tmp.addElement(mappings[i]);
300 mappings = new StructureMapping[tmp.size()];
301 tmp.copyInto(mappings);
305 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
307 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
308 SearchResults results = null;
309 SequenceI lastseq = null;
310 int lastipos = -1, indexpos;
311 for (int i = 0; i < listeners.size(); i++)
313 if (listeners.elementAt(i) instanceof SequenceListener)
317 results = new SearchResults();
319 if (mappings != null)
321 for (int j = 0; j < mappings.length; j++)
323 if (mappings[j].pdbfile.equals(pdbfile)
324 && mappings[j].pdbchain.equals(chain))
326 indexpos = mappings[j].getSeqPos(pdbResNum);
327 if (lastipos != indexpos && lastseq != mappings[j].sequence)
329 results.addResult(mappings[j].sequence, indexpos, indexpos);
331 lastseq = mappings[j].sequence;
332 // construct highlighted sequence list
333 if (seqmappings != null)
336 Enumeration e = seqmappings.elements();
337 while (e.hasMoreElements())
340 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
341 mappings[j].sequence, indexpos, results);
351 if (results.getSize() > 0)
353 for (int i = 0; i < listeners.size(); i++)
355 Object li = listeners.elementAt(i);
356 if (li instanceof SequenceListener)
357 ((SequenceListener) li).highlightSequence(results);
362 Vector seqmappings = null; // should be a simpler list of mapped seuqence
365 * highlight regions associated with a position (indexpos) in seq
368 * the sequeence that the mouse over occured on
370 * the absolute position being mouseovered in seq (0 to seq.length())
372 * the sequence position (if -1, seq.findPosition is called to
373 * resolve the residue number)
375 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
378 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
379 SearchResults results = null;
381 index = seq.findPosition(indexpos);
382 StructureListener sl;
384 for (int i = 0; i < listeners.size(); i++)
386 if (listeners.elementAt(i) instanceof StructureListener)
388 sl = (StructureListener) listeners.elementAt(i);
389 if (mappings == null)
393 for (int j = 0; j < mappings.length; j++)
395 if (mappings[j].sequence == seq
396 || mappings[j].sequence == seq.getDatasetSequence())
398 atomNo = mappings[j].getAtomNum(index);
402 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
403 mappings[j].pdbchain, mappings[j].pdbfile);
410 if (relaySeqMappings && hasSequenceListeners
411 && listeners.elementAt(i) instanceof SequenceListener)
414 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
419 results = new SearchResults();
420 if (index >= seq.getStart() && index <= seq.getEnd())
422 // construct highlighted sequence list
424 if (seqmappings != null)
426 Enumeration e = seqmappings.elements();
427 while (e.hasMoreElements())
430 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
431 seq, index, results);
434 // hasSequenceListeners = results.getSize() > 0;
435 if (handlingVamsasMo)
437 // maybe have to resolve seq to a dataset seqeunce...
438 // add in additional direct sequence and/or dataset sequence
440 results.addResult(seq, index, index);
444 if (hasSequenceListeners)
446 ((SequenceListener) listeners.elementAt(i))
447 .highlightSequence(results);
450 else if (listeners.elementAt(i) instanceof VamsasListener
451 && !handlingVamsasMo)
454 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
457 // pass the mouse over and absolute position onto the
459 ((VamsasListener) listeners.elementAt(i)).mouseOver(seq,
467 * true if a mouse over event from an external (ie Vamsas) source is being
470 boolean handlingVamsasMo = false;
475 * as mouseOverSequence but only route event to SequenceListeners
479 * in an alignment sequence
481 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
484 handlingVamsasMo = true;
485 long msg = sequenceI.hashCode() * (1 + position);
489 mouseOverSequence(sequenceI, position, -1, source);
491 handlingVamsasMo = false;
494 public Annotation[] colourSequenceFromStructure(SequenceI seq,
498 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
499 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
501 * Annotation [] annotations = new Annotation[seq.getLength()];
503 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
504 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
505 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
507 * for (int j = 0; j < mappings.length; j++) {
509 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
510 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
511 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
512 * "+mappings[j].pdbfile);
514 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
515 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
517 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
518 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
519 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
520 * mappings[j].pdbfile); }
522 * annotations[index] = new Annotation("X",null,' ',0,col); } return
523 * annotations; } } } }
525 * return annotations;
529 public void structureSelectionChanged()
533 public void sequenceSelectionChanged()
537 public void sequenceColoursChanged(Object source)
539 StructureListener sl;
540 for (int i = 0; i < listeners.size(); i++)
542 if (listeners.elementAt(i) instanceof StructureListener)
544 sl = (StructureListener) listeners.elementAt(i);
545 sl.updateColours(source);
550 public StructureMapping[] getMapping(String pdbfile)
552 Vector tmp = new Vector();
553 if (mappings != null)
555 for (int i = 0; i < mappings.length; i++)
557 if (mappings[i].pdbfile.equals(pdbfile))
559 tmp.addElement(mappings[i]);
563 StructureMapping[] ret = new StructureMapping[tmp.size()];
564 for (int i = 0; i < tmp.size(); i++)
566 ret[i] = (StructureMapping) tmp.elementAt(i);
572 public String printMapping(String pdbfile)
574 StringBuffer sb = new StringBuffer();
575 for (int i = 0; i < mappings.length; i++)
577 if (mappings[i].pdbfile.equals(pdbfile))
579 sb.append(mappings[i].mappingDetails);
583 return sb.toString();
586 private int[] seqmappingrefs = null; // refcount for seqmappings elements
588 private synchronized void modifySeqMappingList(boolean add,
589 AlignedCodonFrame[] codonFrames)
591 if (!add && (seqmappings == null || seqmappings.size() == 0))
593 if (seqmappings == null)
594 seqmappings = new Vector();
595 if (codonFrames != null && codonFrames.length > 0)
597 for (int cf = 0; cf < codonFrames.length; cf++)
599 if (seqmappings.contains(codonFrames[cf]))
603 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
607 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
609 int pos = seqmappings.indexOf(codonFrames[cf]);
610 int[] nr = new int[seqmappingrefs.length - 1];
613 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
615 if (pos < seqmappingrefs.length - 1)
617 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
618 seqmappingrefs.length - pos - 2);
627 seqmappings.addElement(codonFrames[cf]);
629 int[] nsr = new int[(seqmappingrefs == null) ? 1
630 : seqmappingrefs.length + 1];
631 if (seqmappingrefs != null && seqmappingrefs.length > 0)
632 System.arraycopy(seqmappingrefs, 0, nsr, 0,
633 seqmappingrefs.length);
634 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
635 seqmappingrefs = nsr;
642 public void removeMappings(AlignedCodonFrame[] codonFrames)
644 modifySeqMappingList(false, codonFrames);
647 public void addMappings(AlignedCodonFrame[] codonFrames)
649 modifySeqMappingList(true, codonFrames);
652 Vector sel_listeners = new Vector();
654 public void addSelectionListener(SelectionListener selecter)
656 if (!sel_listeners.contains(selecter))
658 sel_listeners.addElement(selecter);
662 public void removeSelectionListener(SelectionListener toremove)
664 if (sel_listeners.contains(toremove))
666 sel_listeners.removeElement(toremove);
670 public synchronized void sendSelection(
671 jalview.datamodel.SequenceGroup selection,
672 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
674 if (sel_listeners != null && sel_listeners.size() > 0)
676 Enumeration listeners = sel_listeners.elements();
677 while (listeners.hasMoreElements())
679 SelectionListener slis = ((SelectionListener) listeners
683 slis.selection(selection, colsel, source);