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.api.AlignmentViewPanel;
26 import jalview.api.StructureSelectionManagerProvider;
27 import jalview.datamodel.*;
29 public class StructureSelectionManager
31 static IdentityHashMap<StructureSelectionManagerProvider,StructureSelectionManager> instances;
33 StructureMapping[] mappings;
36 * debug function - write all mappings to stdout
38 public void reportMapping() {
\r
41 System.err.println("reportMapping: No PDB/Sequence mappings.");
\r
43 System.err.println("reportMapping: There are "+mappings.length+" mappings.");
\r
44 for (int m=0;m<mappings.length;m++)
\r
46 System.err.println("mapping "+m+" : "+mappings[m].pdbfile);
\r
50 Hashtable mappingData = new Hashtable();
52 public static StructureSelectionManager getStructureSelectionManager(StructureSelectionManagerProvider context)
54 if (instances == null)
56 instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider,StructureSelectionManager>();
58 StructureSelectionManager instance=instances.get(context);
61 instances.put(context, instance=new StructureSelectionManager());
67 * flag controlling whether SeqMappings are relayed from received sequence
68 * mouse over events to other sequences
70 boolean relaySeqMappings = true;
73 * Enable or disable relay of seqMapping events to other sequences. You might
74 * want to do this if there are many sequence mappings and the host computer
79 public void setRelaySeqMappings(boolean relay)
81 relaySeqMappings = relay;
85 * get the state of the relay seqMappings flag.
87 * @return true if sequence mouse overs are being relayed to other mapped
90 public boolean isRelaySeqMappingsEnabled()
92 return relaySeqMappings;
95 Vector listeners = new Vector();
98 * register a listener for alignment sequence mouseover events
101 public void addStructureViewerListener(Object svl)
103 if (!listeners.contains(svl))
105 listeners.addElement(svl);
109 public String alreadyMappedToFile(String pdbid)
111 if (mappings != null)
113 for (int i = 0; i < mappings.length; i++)
115 if (mappings[i].getPdbId().equals(pdbid))
117 return mappings[i].pdbfile;
125 * create sequence structure mappings between each sequence and the given
126 * pdbFile (retrieved via the given protocol).
129 * - one or more sequences to be mapped to pdbFile
130 * @param targetChains
131 * - optional chain specification for mapping each sequence to pdb
132 * (may be nill, individual elements may be nill)
134 * - structure data resource
136 * - how to resolve data from resource
137 * @return null or the structure data parsed as a pdb file
139 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
140 String[] targetChains, String pdbFile, String protocol)
143 * There will be better ways of doing this in the future, for now we'll use
144 * the tried and tested MCview pdb mapping
146 MCview.PDBfile pdb = null;
149 pdb = new MCview.PDBfile(pdbFile, protocol);
150 } catch (Exception ex)
152 ex.printStackTrace();
157 for (int s = 0; s < sequence.length; s++)
159 boolean infChain = true;
160 if (targetChains != null && targetChains[s] != null)
163 targetChain = targetChains[s];
165 else if (sequence[s].getName().indexOf("|") > -1)
167 targetChain = sequence[s].getName().substring(
168 sequence[s].getName().lastIndexOf("|") + 1);
169 if (targetChain.length() > 1)
171 if (targetChain.trim().length() == 0)
177 // not a valid chain identifier
186 AlignSeq maxAlignseq = null;
187 String maxChainId = " ";
188 PDBChain maxChain = null;
189 boolean first = true;
190 for (int i = 0; i < pdb.chains.size(); i++)
192 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
193 if (targetChain.length() > 0 && !targetChain.equals(chain.id) && !infChain)
195 continue; // don't try to map chains don't match.
197 // TODO: correctly determine sequence type for mixed na/peptide
199 AlignSeq as = new AlignSeq(sequence[s],
200 ((PDBChain) pdb.chains.elementAt(i)).sequence,
201 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
203 as.calcScoreMatrix();
206 if (first || as.maxscore > max
207 || (as.maxscore == max && chain.id.equals(targetChain)))
213 maxChainId = chain.id;
216 if (maxChain == null)
220 final StringBuffer mappingDetails = new StringBuffer();
221 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
222 + maxChain.sequence.getSequenceAsString());
223 mappingDetails.append("\nNo of residues = "
224 + maxChain.residues.size() + "\n\n");
225 PrintStream ps = new PrintStream(System.out)
227 public void print(String x)
229 mappingDetails.append(x);
232 public void println()
234 mappingDetails.append("\n");
238 maxAlignseq.printAlignment(ps);
240 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
241 + " " + maxAlignseq.seq2end);
242 mappingDetails.append("\nSEQ start/end "
243 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
244 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
246 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
248 maxChain.transferRESNUMFeatures(sequence[s], null);
250 // allocate enough slots to store the mapping from positions in
251 // sequence[s] to the associated chain
252 int[][] mapping = new int[sequence[s].findPosition(sequence[s].getLength()) + 2][2];
258 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
259 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
261 resNum = tmp.resNumber;
262 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
263 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
267 } while (index < maxChain.atoms.size());
269 if (mappings == null)
271 mappings = new StructureMapping[1];
275 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
276 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
280 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
281 pdbFile = "INLINE" + pdb.id;
283 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
284 pdbFile, pdb.id, maxChainId, mapping,
285 mappingDetails.toString());
286 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
293 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
295 listeners.removeElement(svl);
296 if (svl instanceof SequenceListener)
298 for (int i=0;i<listeners.size();i++)
300 if (listeners.elementAt(i) instanceof StructureListener)
302 ((StructureListener)listeners.elementAt(i)).releaseReferences(svl);
307 if (pdbfiles == null)
311 boolean removeMapping = true;
313 Vector pdbs = new Vector();
314 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
316 StructureListener sl;
317 for (int i = 0; i < listeners.size(); i++)
319 if (listeners.elementAt(i) instanceof StructureListener)
321 sl = (StructureListener) listeners.elementAt(i);
322 handlepdbs = sl.getPdbFile();
323 for (int j = 0; j < handlepdbs.length; j++)
325 if (pdbs.contains(handlepdbs[j]))
327 pdbs.removeElement(handlepdbs[j]);
334 if (pdbs.size() > 0 && mappings != null)
336 Vector tmp = new Vector();
337 for (int i = 0; i < mappings.length; i++)
339 if (!pdbs.contains(mappings[i].pdbfile))
341 tmp.addElement(mappings[i]);
345 mappings = new StructureMapping[tmp.size()];
346 tmp.copyInto(mappings);
350 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
352 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
353 SearchResults results = null;
354 SequenceI lastseq = null;
355 int lastipos = -1, indexpos;
356 for (int i = 0; i < listeners.size(); i++)
358 if (listeners.elementAt(i) instanceof SequenceListener)
362 results = new SearchResults();
364 if (mappings != null)
366 for (int j = 0; j < mappings.length; j++)
368 if (mappings[j].pdbfile.equals(pdbfile)
369 && mappings[j].pdbchain.equals(chain))
371 indexpos = mappings[j].getSeqPos(pdbResNum);
372 if (lastipos != indexpos && lastseq != mappings[j].sequence)
374 results.addResult(mappings[j].sequence, indexpos, indexpos);
376 lastseq = mappings[j].sequence;
377 // construct highlighted sequence list
378 if (seqmappings != null)
381 Enumeration e = seqmappings.elements();
382 while (e.hasMoreElements())
385 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
386 mappings[j].sequence, indexpos, results);
398 for (int i = 0; i < listeners.size(); i++)
400 Object li = listeners.elementAt(i);
401 if (li instanceof SequenceListener)
402 ((SequenceListener) li).highlightSequence(results);
407 Vector seqmappings = null; // should be a simpler list of mapped seuqence
410 * highlight regions associated with a position (indexpos) in seq
413 * the sequeence that the mouse over occured on
415 * the absolute position being mouseovered in seq (0 to seq.length())
417 * the sequence position (if -1, seq.findPosition is called to
418 * resolve the residue number)
420 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
423 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
424 SearchResults results = null;
426 index = seq.findPosition(indexpos);
427 StructureListener sl;
429 for (int i = 0; i < listeners.size(); i++)
431 if (listeners.elementAt(i) instanceof StructureListener)
433 sl = (StructureListener) listeners.elementAt(i);
434 if (mappings == null)
438 for (int j = 0; j < mappings.length; j++)
440 if (mappings[j].sequence == seq
441 || mappings[j].sequence == seq.getDatasetSequence())
443 atomNo = mappings[j].getAtomNum(index);
447 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
448 mappings[j].pdbchain, mappings[j].pdbfile);
455 if (relaySeqMappings && hasSequenceListeners
456 && listeners.elementAt(i) instanceof SequenceListener)
459 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
464 results = new SearchResults();
465 if (index >= seq.getStart() && index <= seq.getEnd())
467 // construct highlighted sequence list
469 if (seqmappings != null)
471 Enumeration e = seqmappings.elements();
472 while (e.hasMoreElements())
475 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
476 seq, index, results);
479 // hasSequenceListeners = results.getSize() > 0;
480 if (handlingVamsasMo)
482 // maybe have to resolve seq to a dataset seqeunce...
483 // add in additional direct sequence and/or dataset sequence
485 results.addResult(seq, index, index);
489 if (hasSequenceListeners)
491 ((SequenceListener) listeners.elementAt(i))
492 .highlightSequence(results);
495 else if (listeners.elementAt(i) instanceof VamsasListener
496 && !handlingVamsasMo)
499 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
502 // pass the mouse over and absolute position onto the
504 ((VamsasListener) listeners.elementAt(i)).mouseOver(seq,
512 * true if a mouse over event from an external (ie Vamsas) source is being
515 boolean handlingVamsasMo = false;
520 * as mouseOverSequence but only route event to SequenceListeners
524 * in an alignment sequence
526 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
529 handlingVamsasMo = true;
530 long msg = sequenceI.hashCode() * (1 + position);
534 mouseOverSequence(sequenceI, position, -1, source);
536 handlingVamsasMo = false;
539 public Annotation[] colourSequenceFromStructure(SequenceI seq,
543 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
544 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
546 * Annotation [] annotations = new Annotation[seq.getLength()];
548 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
549 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
550 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
552 * for (int j = 0; j < mappings.length; j++) {
554 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
555 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
556 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
557 * "+mappings[j].pdbfile);
559 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
560 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
562 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
563 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
564 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
565 * mappings[j].pdbfile); }
567 * annotations[index] = new Annotation("X",null,' ',0,col); } return
568 * annotations; } } } }
570 * return annotations;
574 public void structureSelectionChanged()
578 public void sequenceSelectionChanged()
582 public void sequenceColoursChanged(Object source)
584 StructureListener sl;
585 for (int i = 0; i < listeners.size(); i++)
587 if (listeners.elementAt(i) instanceof StructureListener)
589 sl = (StructureListener) listeners.elementAt(i);
590 sl.updateColours(source);
595 public StructureMapping[] getMapping(String pdbfile)
597 Vector tmp = new Vector();
598 if (mappings != null)
600 for (int i = 0; i < mappings.length; i++)
602 if (mappings[i].pdbfile.equals(pdbfile))
604 tmp.addElement(mappings[i]);
608 StructureMapping[] ret = new StructureMapping[tmp.size()];
609 for (int i = 0; i < tmp.size(); i++)
611 ret[i] = (StructureMapping) tmp.elementAt(i);
617 public String printMapping(String pdbfile)
619 StringBuffer sb = new StringBuffer();
620 for (int i = 0; i < mappings.length; i++)
622 if (mappings[i].pdbfile.equals(pdbfile))
624 sb.append(mappings[i].mappingDetails);
628 return sb.toString();
631 private int[] seqmappingrefs = null; // refcount for seqmappings elements
633 private synchronized void modifySeqMappingList(boolean add,
634 AlignedCodonFrame[] codonFrames)
636 if (!add && (seqmappings == null || seqmappings.size() == 0))
638 if (seqmappings == null)
639 seqmappings = new Vector();
640 if (codonFrames != null && codonFrames.length > 0)
642 for (int cf = 0; cf < codonFrames.length; cf++)
644 if (seqmappings.contains(codonFrames[cf]))
648 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
652 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
654 int pos = seqmappings.indexOf(codonFrames[cf]);
655 int[] nr = new int[seqmappingrefs.length - 1];
658 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
660 if (pos < seqmappingrefs.length - 1)
662 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
663 seqmappingrefs.length - pos - 2);
672 seqmappings.addElement(codonFrames[cf]);
674 int[] nsr = new int[(seqmappingrefs == null) ? 1
675 : seqmappingrefs.length + 1];
676 if (seqmappingrefs != null && seqmappingrefs.length > 0)
677 System.arraycopy(seqmappingrefs, 0, nsr, 0,
678 seqmappingrefs.length);
679 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
680 seqmappingrefs = nsr;
687 public void removeMappings(AlignedCodonFrame[] codonFrames)
689 modifySeqMappingList(false, codonFrames);
692 public void addMappings(AlignedCodonFrame[] codonFrames)
694 modifySeqMappingList(true, codonFrames);
697 Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
699 public void addSelectionListener(SelectionListener selecter)
701 if (!sel_listeners.contains(selecter))
703 sel_listeners.addElement(selecter);
707 public void removeSelectionListener(SelectionListener toremove)
709 if (sel_listeners.contains(toremove))
711 sel_listeners.removeElement(toremove);
715 public synchronized void sendSelection(
716 jalview.datamodel.SequenceGroup selection,
717 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
719 if (sel_listeners != null && sel_listeners.size() > 0)
721 Enumeration listeners = sel_listeners.elements();
722 while (listeners.hasMoreElements())
724 SelectionListener slis = ((SelectionListener) listeners
728 slis.selection(selection, colsel, source);
735 Vector<AlignmentViewPanelListener> view_listeners=new Vector<AlignmentViewPanelListener>();
736 public synchronized void sendViewPosition(jalview.api.AlignmentViewPanel source, int startRes,
737 int endRes, int startSeq, int endSeq)
740 if (view_listeners != null && view_listeners.size() > 0)
742 Enumeration<AlignmentViewPanelListener> listeners = view_listeners.elements();
743 while (listeners.hasMoreElements())
745 AlignmentViewPanelListener slis = listeners
749 slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
757 public void finalize() throws Throwable {
758 if (listeners!=null) {
762 if (mappingData!=null)
767 if (sel_listeners!=null)
769 sel_listeners.clear();
772 if (view_listeners!=null)
774 view_listeners.clear();
782 * release all references associated with this manager provider
785 public static void release(StructureSelectionManagerProvider jalviewLite)
787 StructureSelectionManager mnger=(instances.get(jalviewLite));
790 instances.remove(jalviewLite);
793 } catch (Throwable x){};