2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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()
42 System.err.println("reportMapping: No PDB/Sequence mappings.");
46 System.err.println("reportMapping: There are " + mappings.length
48 for (int m = 0; m < mappings.length; m++)
50 System.err.println("mapping " + m + " : " + mappings[m].pdbfile);
55 Hashtable mappingData = new Hashtable();
56 private static StructureSelectionManager nullProvider = null;
57 public static StructureSelectionManager getStructureSelectionManager(
58 StructureSelectionManagerProvider context)
61 if (nullProvider == null)
63 if (instances != null)
66 "Implementation error. Structure selection manager's context is 'null'",
67 new NullPointerException("SSM context is null"));
71 nullProvider = new StructureSelectionManager();
76 if (instances == null)
78 instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider, StructureSelectionManager>();
80 StructureSelectionManager instance = instances.get(context);
83 if (nullProvider!=null)
85 instance = nullProvider;
87 instance = new StructureSelectionManager();
89 instances.put(context, instance);
95 * flag controlling whether SeqMappings are relayed from received sequence
96 * mouse over events to other sequences
98 boolean relaySeqMappings = true;
101 * Enable or disable relay of seqMapping events to other sequences. You might
102 * want to do this if there are many sequence mappings and the host computer
107 public void setRelaySeqMappings(boolean relay)
109 relaySeqMappings = relay;
113 * get the state of the relay seqMappings flag.
115 * @return true if sequence mouse overs are being relayed to other mapped
118 public boolean isRelaySeqMappingsEnabled()
120 return relaySeqMappings;
123 Vector listeners = new Vector();
126 * register a listener for alignment sequence mouseover events
130 public void addStructureViewerListener(Object svl)
132 if (!listeners.contains(svl))
134 listeners.addElement(svl);
138 public String alreadyMappedToFile(String pdbid)
140 if (mappings != null)
142 for (int i = 0; i < mappings.length; i++)
144 if (mappings[i].getPdbId().equals(pdbid))
146 return mappings[i].pdbfile;
154 * create sequence structure mappings between each sequence and the given
155 * pdbFile (retrieved via the given protocol).
158 * - one or more sequences to be mapped to pdbFile
159 * @param targetChains
160 * - optional chain specification for mapping each sequence to pdb
161 * (may be nill, individual elements may be nill)
163 * - structure data resource
165 * - how to resolve data from resource
166 * @return null or the structure data parsed as a pdb file
168 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
169 String[] targetChains, String pdbFile, String protocol)
172 * There will be better ways of doing this in the future, for now we'll use
173 * the tried and tested MCview pdb mapping
175 MCview.PDBfile pdb = null;
178 pdb = new MCview.PDBfile(pdbFile, protocol);
179 } catch (Exception ex)
181 ex.printStackTrace();
186 for (int s = 0; s < sequence.length; s++)
188 boolean infChain = true;
189 if (targetChains != null && targetChains[s] != null)
192 targetChain = targetChains[s];
194 else if (sequence[s].getName().indexOf("|") > -1)
196 targetChain = sequence[s].getName().substring(
197 sequence[s].getName().lastIndexOf("|") + 1);
198 if (targetChain.length() > 1)
200 if (targetChain.trim().length() == 0)
206 // not a valid chain identifier
215 AlignSeq maxAlignseq = null;
216 String maxChainId = " ";
217 PDBChain maxChain = null;
218 boolean first = true;
219 for (int i = 0; i < pdb.chains.size(); i++)
221 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
222 if (targetChain.length() > 0 && !targetChain.equals(chain.id)
225 continue; // don't try to map chains don't match.
227 // TODO: correctly determine sequence type for mixed na/peptide
229 AlignSeq as = new AlignSeq(sequence[s],
230 ((PDBChain) pdb.chains.elementAt(i)).sequence,
231 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
233 as.calcScoreMatrix();
236 if (first || as.maxscore > max
237 || (as.maxscore == max && chain.id.equals(targetChain)))
243 maxChainId = chain.id;
246 if (maxChain == null)
250 final StringBuffer mappingDetails = new StringBuffer();
251 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
252 + maxChain.sequence.getSequenceAsString());
253 mappingDetails.append("\nNo of residues = "
254 + maxChain.residues.size() + "\n\n");
255 PrintStream ps = new PrintStream(System.out)
257 public void print(String x)
259 mappingDetails.append(x);
262 public void println()
264 mappingDetails.append("\n");
268 maxAlignseq.printAlignment(ps);
270 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
271 + " " + maxAlignseq.seq2end);
272 mappingDetails.append("\nSEQ start/end "
273 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
274 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
276 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
278 maxChain.transferRESNUMFeatures(sequence[s], null);
280 // allocate enough slots to store the mapping from positions in
281 // sequence[s] to the associated chain
282 int[][] mapping = new int[sequence[s].findPosition(sequence[s]
283 .getLength()) + 2][2];
289 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
290 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
292 resNum = tmp.resNumber;
293 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
294 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
298 } while (index < maxChain.atoms.size());
300 if (mappings == null)
302 mappings = new StructureMapping[1];
306 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
307 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
311 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
312 pdbFile = "INLINE" + pdb.id;
314 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
315 pdbFile, pdb.id, maxChainId, mapping,
316 mappingDetails.toString());
317 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
324 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
326 listeners.removeElement(svl);
327 if (svl instanceof SequenceListener)
329 for (int i = 0; i < listeners.size(); i++)
331 if (listeners.elementAt(i) instanceof StructureListener)
333 ((StructureListener) listeners.elementAt(i))
334 .releaseReferences(svl);
339 if (pdbfiles == null)
343 boolean removeMapping = true;
345 Vector pdbs = new Vector();
346 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
348 StructureListener sl;
349 for (int i = 0; i < listeners.size(); i++)
351 if (listeners.elementAt(i) instanceof StructureListener)
353 sl = (StructureListener) listeners.elementAt(i);
354 handlepdbs = sl.getPdbFile();
355 for (int j = 0; j < handlepdbs.length; j++)
357 if (pdbs.contains(handlepdbs[j]))
359 pdbs.removeElement(handlepdbs[j]);
366 if (pdbs.size() > 0 && mappings != null)
368 Vector tmp = new Vector();
369 for (int i = 0; i < mappings.length; i++)
371 if (!pdbs.contains(mappings[i].pdbfile))
373 tmp.addElement(mappings[i]);
377 mappings = new StructureMapping[tmp.size()];
378 tmp.copyInto(mappings);
382 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
384 if (listeners == null)
386 // old or prematurely sent event
389 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
390 SearchResults results = null;
391 SequenceI lastseq = null;
392 int lastipos = -1, indexpos;
393 for (int i = 0; i < listeners.size(); i++)
395 if (listeners.elementAt(i) instanceof SequenceListener)
399 results = new SearchResults();
401 if (mappings != null)
403 for (int j = 0; j < mappings.length; j++)
405 if (mappings[j].pdbfile.equals(pdbfile)
406 && mappings[j].pdbchain.equals(chain))
408 indexpos = mappings[j].getSeqPos(pdbResNum);
409 if (lastipos != indexpos && lastseq != mappings[j].sequence)
411 results.addResult(mappings[j].sequence, indexpos, indexpos);
413 lastseq = mappings[j].sequence;
414 // construct highlighted sequence list
415 if (seqmappings != null)
418 Enumeration e = seqmappings.elements();
419 while (e.hasMoreElements())
422 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
423 mappings[j].sequence, indexpos, results);
435 for (int i = 0; i < listeners.size(); i++)
437 Object li = listeners.elementAt(i);
438 if (li instanceof SequenceListener)
439 ((SequenceListener) li).highlightSequence(results);
444 Vector seqmappings = null; // should be a simpler list of mapped seuqence
447 * highlight regions associated with a position (indexpos) in seq
450 * the sequeence that the mouse over occured on
452 * the absolute position being mouseovered in seq (0 to seq.length())
454 * the sequence position (if -1, seq.findPosition is called to
455 * resolve the residue number)
457 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
460 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
461 SearchResults results = null;
463 index = seq.findPosition(indexpos);
464 StructureListener sl;
466 for (int i = 0; i < listeners.size(); i++)
468 Object listener = listeners.elementAt(i);
469 if (listener == source)
473 if (listener instanceof StructureListener)
475 sl = (StructureListener) listener;
476 if (mappings == null)
480 for (int j = 0; j < mappings.length; j++)
482 if (mappings[j].sequence == seq
483 || mappings[j].sequence == seq.getDatasetSequence())
485 atomNo = mappings[j].getAtomNum(index);
489 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
490 mappings[j].pdbchain, mappings[j].pdbfile);
497 if (relaySeqMappings && hasSequenceListeners
498 && listener instanceof SequenceListener)
501 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
506 results = new SearchResults();
507 if (index >= seq.getStart() && index <= seq.getEnd())
509 // construct highlighted sequence list
511 if (seqmappings != null)
513 Enumeration e = seqmappings.elements();
514 while (e.hasMoreElements())
517 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
518 seq, index, results);
521 // hasSequenceListeners = results.getSize() > 0;
522 if (handlingVamsasMo)
524 // maybe have to resolve seq to a dataset seqeunce...
525 // add in additional direct sequence and/or dataset sequence
527 results.addResult(seq, index, index);
531 if (hasSequenceListeners)
533 ((SequenceListener) listener).highlightSequence(results);
536 else if (listener instanceof VamsasListener && !handlingVamsasMo)
539 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
542 // pass the mouse over and absolute position onto the
544 ((VamsasListener) listener).mouseOver(seq, indexpos, source);
546 else if (listener instanceof SecondaryStructureListener)
548 ((SecondaryStructureListener) listener).mouseOverSequence(seq,
556 * true if a mouse over event from an external (ie Vamsas) source is being
559 boolean handlingVamsasMo = false;
564 * as mouseOverSequence but only route event to SequenceListeners
568 * in an alignment sequence
570 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
573 handlingVamsasMo = true;
574 long msg = sequenceI.hashCode() * (1 + position);
578 mouseOverSequence(sequenceI, position, -1, source);
580 handlingVamsasMo = false;
583 public Annotation[] colourSequenceFromStructure(SequenceI seq,
587 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
588 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
590 * Annotation [] annotations = new Annotation[seq.getLength()];
592 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
593 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
594 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
596 * for (int j = 0; j < mappings.length; j++) {
598 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
599 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
600 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
601 * "+mappings[j].pdbfile);
603 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
604 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
606 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
607 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
608 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
609 * mappings[j].pdbfile); }
611 * annotations[index] = new Annotation("X",null,' ',0,col); } return
612 * annotations; } } } }
614 * return annotations;
618 public void structureSelectionChanged()
622 public void sequenceSelectionChanged()
626 public void sequenceColoursChanged(Object source)
628 StructureListener sl;
629 for (int i = 0; i < listeners.size(); i++)
631 if (listeners.elementAt(i) instanceof StructureListener)
633 sl = (StructureListener) listeners.elementAt(i);
634 sl.updateColours(source);
639 public StructureMapping[] getMapping(String pdbfile)
641 Vector tmp = new Vector();
642 if (mappings != null)
644 for (int i = 0; i < mappings.length; i++)
646 if (mappings[i].pdbfile.equals(pdbfile))
648 tmp.addElement(mappings[i]);
652 StructureMapping[] ret = new StructureMapping[tmp.size()];
653 for (int i = 0; i < tmp.size(); i++)
655 ret[i] = (StructureMapping) tmp.elementAt(i);
661 public String printMapping(String pdbfile)
663 StringBuffer sb = new StringBuffer();
664 for (int i = 0; i < mappings.length; i++)
666 if (mappings[i].pdbfile.equals(pdbfile))
668 sb.append(mappings[i].mappingDetails);
672 return sb.toString();
675 private int[] seqmappingrefs = null; // refcount for seqmappings elements
677 private synchronized void modifySeqMappingList(boolean add,
678 AlignedCodonFrame[] codonFrames)
680 if (!add && (seqmappings == null || seqmappings.size() == 0))
682 if (seqmappings == null)
683 seqmappings = new Vector();
684 if (codonFrames != null && codonFrames.length > 0)
686 for (int cf = 0; cf < codonFrames.length; cf++)
688 if (seqmappings.contains(codonFrames[cf]))
692 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
696 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
698 int pos = seqmappings.indexOf(codonFrames[cf]);
699 int[] nr = new int[seqmappingrefs.length - 1];
702 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
704 if (pos < seqmappingrefs.length - 1)
706 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
707 seqmappingrefs.length - pos - 2);
716 seqmappings.addElement(codonFrames[cf]);
718 int[] nsr = new int[(seqmappingrefs == null) ? 1
719 : seqmappingrefs.length + 1];
720 if (seqmappingrefs != null && seqmappingrefs.length > 0)
721 System.arraycopy(seqmappingrefs, 0, nsr, 0,
722 seqmappingrefs.length);
723 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
724 seqmappingrefs = nsr;
731 public void removeMappings(AlignedCodonFrame[] codonFrames)
733 modifySeqMappingList(false, codonFrames);
736 public void addMappings(AlignedCodonFrame[] codonFrames)
738 modifySeqMappingList(true, codonFrames);
741 Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
743 public void addSelectionListener(SelectionListener selecter)
745 if (!sel_listeners.contains(selecter))
747 sel_listeners.addElement(selecter);
751 public void removeSelectionListener(SelectionListener toremove)
753 if (sel_listeners.contains(toremove))
755 sel_listeners.removeElement(toremove);
759 public synchronized void sendSelection(
760 jalview.datamodel.SequenceGroup selection,
761 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
763 if (sel_listeners != null && sel_listeners.size() > 0)
765 Enumeration listeners = sel_listeners.elements();
766 while (listeners.hasMoreElements())
768 SelectionListener slis = ((SelectionListener) listeners
772 slis.selection(selection, colsel, source);
779 Vector<AlignmentViewPanelListener> view_listeners = new Vector<AlignmentViewPanelListener>();
781 public synchronized void sendViewPosition(
782 jalview.api.AlignmentViewPanel source, int startRes, int endRes,
783 int startSeq, int endSeq)
786 if (view_listeners != null && view_listeners.size() > 0)
788 Enumeration<AlignmentViewPanelListener> listeners = view_listeners
790 while (listeners.hasMoreElements())
792 AlignmentViewPanelListener slis = listeners.nextElement();
795 slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
802 public void finalize() throws Throwable
804 if (listeners != null)
809 if (mappingData != null)
814 if (sel_listeners != null)
816 sel_listeners.clear();
817 sel_listeners = null;
819 if (view_listeners != null)
821 view_listeners.clear();
822 view_listeners = null;
825 seqmappingrefs = null;
829 * release all references associated with this manager provider
833 public static void release(StructureSelectionManagerProvider jalviewLite)
835 // synchronized (instances)
837 if (instances == null)
841 StructureSelectionManager mnger = (instances.get(jalviewLite));
844 instances.remove(jalviewLite);
848 } catch (Throwable x)