2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.structure;
27 import jalview.analysis.*;
28 import jalview.api.AlignmentViewPanel;
29 import jalview.api.StructureSelectionManagerProvider;
30 import jalview.datamodel.*;
32 public class StructureSelectionManager
34 static IdentityHashMap<StructureSelectionManagerProvider, StructureSelectionManager> instances;
36 StructureMapping[] mappings;
39 * debug function - write all mappings to stdout
41 public void reportMapping()
45 System.err.println("reportMapping: No PDB/Sequence mappings.");
49 System.err.println("reportMapping: There are " + mappings.length
51 for (int m = 0; m < mappings.length; m++)
53 System.err.println("mapping " + m + " : " + mappings[m].pdbfile);
58 Hashtable mappingData = new Hashtable();
59 private static StructureSelectionManager nullProvider = null;
60 public static StructureSelectionManager getStructureSelectionManager(
61 StructureSelectionManagerProvider context)
64 if (nullProvider == null)
66 if (instances != null)
69 "Implementation error. Structure selection manager's context is 'null'",
70 new NullPointerException("SSM context is null"));
74 nullProvider = new StructureSelectionManager();
79 if (instances == null)
81 instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider, StructureSelectionManager>();
83 StructureSelectionManager instance = instances.get(context);
86 if (nullProvider!=null)
88 instance = nullProvider;
90 instance = new StructureSelectionManager();
92 instances.put(context, instance);
98 * flag controlling whether SeqMappings are relayed from received sequence
99 * mouse over events to other sequences
101 boolean relaySeqMappings = true;
104 * Enable or disable relay of seqMapping events to other sequences. You might
105 * want to do this if there are many sequence mappings and the host computer
110 public void setRelaySeqMappings(boolean relay)
112 relaySeqMappings = relay;
116 * get the state of the relay seqMappings flag.
118 * @return true if sequence mouse overs are being relayed to other mapped
121 public boolean isRelaySeqMappingsEnabled()
123 return relaySeqMappings;
126 Vector listeners = new Vector();
129 * register a listener for alignment sequence mouseover events
133 public void addStructureViewerListener(Object svl)
135 if (!listeners.contains(svl))
137 listeners.addElement(svl);
141 public String alreadyMappedToFile(String pdbid)
143 if (mappings != null)
145 for (int i = 0; i < mappings.length; i++)
147 if (mappings[i].getPdbId().equals(pdbid))
149 return mappings[i].pdbfile;
157 * create sequence structure mappings between each sequence and the given
158 * pdbFile (retrieved via the given protocol).
161 * - one or more sequences to be mapped to pdbFile
162 * @param targetChains
163 * - optional chain specification for mapping each sequence to pdb
164 * (may be nill, individual elements may be nill)
166 * - structure data resource
168 * - how to resolve data from resource
169 * @return null or the structure data parsed as a pdb file
171 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
172 String[] targetChains, String pdbFile, String protocol)
175 * There will be better ways of doing this in the future, for now we'll use
176 * the tried and tested MCview pdb mapping
178 MCview.PDBfile pdb = null;
181 pdb = new MCview.PDBfile(pdbFile, protocol);
182 } catch (Exception ex)
184 ex.printStackTrace();
189 for (int s = 0; s < sequence.length; s++)
191 boolean infChain = true;
192 if (targetChains != null && targetChains[s] != null)
195 targetChain = targetChains[s];
197 else if (sequence[s].getName().indexOf("|") > -1)
199 targetChain = sequence[s].getName().substring(
200 sequence[s].getName().lastIndexOf("|") + 1);
201 if (targetChain.length() > 1)
203 if (targetChain.trim().length() == 0)
209 // not a valid chain identifier
218 AlignSeq maxAlignseq = null;
219 String maxChainId = " ";
220 PDBChain maxChain = null;
221 boolean first = true;
222 for (int i = 0; i < pdb.chains.size(); i++)
224 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
225 if (targetChain.length() > 0 && !targetChain.equals(chain.id)
228 continue; // don't try to map chains don't match.
230 // TODO: correctly determine sequence type for mixed na/peptide
232 AlignSeq as = new AlignSeq(sequence[s],
233 ((PDBChain) pdb.chains.elementAt(i)).sequence,
234 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
236 as.calcScoreMatrix();
239 if (first || as.maxscore > max
240 || (as.maxscore == max && chain.id.equals(targetChain)))
246 maxChainId = chain.id;
249 if (maxChain == null)
253 final StringBuffer mappingDetails = new StringBuffer();
254 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
255 + maxChain.sequence.getSequenceAsString());
256 mappingDetails.append("\nNo of residues = "
257 + maxChain.residues.size() + "\n\n");
258 PrintStream ps = new PrintStream(System.out)
260 public void print(String x)
262 mappingDetails.append(x);
265 public void println()
267 mappingDetails.append("\n");
271 maxAlignseq.printAlignment(ps);
273 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
274 + " " + maxAlignseq.seq2end);
275 mappingDetails.append("\nSEQ start/end "
276 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
277 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
279 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
281 maxChain.transferRESNUMFeatures(sequence[s], null);
283 // allocate enough slots to store the mapping from positions in
284 // sequence[s] to the associated chain
285 int[][] mapping = new int[sequence[s].findPosition(sequence[s]
286 .getLength()) + 2][2];
292 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
293 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
295 resNum = tmp.resNumber;
296 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
297 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
301 } while (index < maxChain.atoms.size());
303 if (mappings == null)
305 mappings = new StructureMapping[1];
309 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
310 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
314 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
315 pdbFile = "INLINE" + pdb.id;
317 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
318 pdbFile, pdb.id, maxChainId, mapping,
319 mappingDetails.toString());
320 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
327 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
329 listeners.removeElement(svl);
330 if (svl instanceof SequenceListener)
332 for (int i = 0; i < listeners.size(); i++)
334 if (listeners.elementAt(i) instanceof StructureListener)
336 ((StructureListener) listeners.elementAt(i))
337 .releaseReferences(svl);
342 if (pdbfiles == null)
346 boolean removeMapping = true;
348 Vector pdbs = new Vector();
349 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
351 StructureListener sl;
352 for (int i = 0; i < listeners.size(); i++)
354 if (listeners.elementAt(i) instanceof StructureListener)
356 sl = (StructureListener) listeners.elementAt(i);
357 handlepdbs = sl.getPdbFile();
358 for (int j = 0; j < handlepdbs.length; j++)
360 if (pdbs.contains(handlepdbs[j]))
362 pdbs.removeElement(handlepdbs[j]);
369 if (pdbs.size() > 0 && mappings != null)
371 Vector tmp = new Vector();
372 for (int i = 0; i < mappings.length; i++)
374 if (!pdbs.contains(mappings[i].pdbfile))
376 tmp.addElement(mappings[i]);
380 mappings = new StructureMapping[tmp.size()];
381 tmp.copyInto(mappings);
385 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
387 if (listeners == null)
389 // old or prematurely sent event
392 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
393 SearchResults results = null;
394 SequenceI lastseq = null;
395 int lastipos = -1, indexpos;
396 for (int i = 0; i < listeners.size(); i++)
398 if (listeners.elementAt(i) instanceof SequenceListener)
402 results = new SearchResults();
404 if (mappings != null)
406 for (int j = 0; j < mappings.length; j++)
408 if (mappings[j].pdbfile.equals(pdbfile)
409 && mappings[j].pdbchain.equals(chain))
411 indexpos = mappings[j].getSeqPos(pdbResNum);
412 if (lastipos != indexpos && lastseq != mappings[j].sequence)
414 results.addResult(mappings[j].sequence, indexpos, indexpos);
416 lastseq = mappings[j].sequence;
417 // construct highlighted sequence list
418 if (seqmappings != null)
421 Enumeration e = seqmappings.elements();
422 while (e.hasMoreElements())
425 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
426 mappings[j].sequence, indexpos, results);
438 for (int i = 0; i < listeners.size(); i++)
440 Object li = listeners.elementAt(i);
441 if (li instanceof SequenceListener)
442 ((SequenceListener) li).highlightSequence(results);
447 Vector seqmappings = null; // should be a simpler list of mapped seuqence
450 * highlight regions associated with a position (indexpos) in seq
453 * the sequeence that the mouse over occured on
455 * the absolute position being mouseovered in seq (0 to seq.length())
457 * the sequence position (if -1, seq.findPosition is called to
458 * resolve the residue number)
460 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
463 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
464 SearchResults results = null;
466 index = seq.findPosition(indexpos);
467 StructureListener sl;
469 for (int i = 0; i < listeners.size(); i++)
471 Object listener = listeners.elementAt(i);
472 if (listener == source)
476 if (listener instanceof StructureListener)
478 sl = (StructureListener) listener;
479 if (mappings == null)
483 for (int j = 0; j < mappings.length; j++)
485 if (mappings[j].sequence == seq
486 || mappings[j].sequence == seq.getDatasetSequence())
488 atomNo = mappings[j].getAtomNum(index);
492 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
493 mappings[j].pdbchain, mappings[j].pdbfile);
500 if (relaySeqMappings && hasSequenceListeners
501 && listener instanceof SequenceListener)
504 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
509 results = new SearchResults();
510 if (index >= seq.getStart() && index <= seq.getEnd())
512 // construct highlighted sequence list
514 if (seqmappings != null)
516 Enumeration e = seqmappings.elements();
517 while (e.hasMoreElements())
520 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
521 seq, index, results);
524 // hasSequenceListeners = results.getSize() > 0;
525 if (handlingVamsasMo)
527 // maybe have to resolve seq to a dataset seqeunce...
528 // add in additional direct sequence and/or dataset sequence
530 results.addResult(seq, index, index);
534 if (hasSequenceListeners)
536 ((SequenceListener) listener).highlightSequence(results);
539 else if (listener instanceof VamsasListener && !handlingVamsasMo)
542 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
545 // pass the mouse over and absolute position onto the
547 ((VamsasListener) listener).mouseOver(seq, indexpos, source);
549 else if (listener instanceof SecondaryStructureListener)
551 ((SecondaryStructureListener) listener).mouseOverSequence(seq,
559 * true if a mouse over event from an external (ie Vamsas) source is being
562 boolean handlingVamsasMo = false;
567 * as mouseOverSequence but only route event to SequenceListeners
571 * in an alignment sequence
573 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
576 handlingVamsasMo = true;
577 long msg = sequenceI.hashCode() * (1 + position);
581 mouseOverSequence(sequenceI, position, -1, source);
583 handlingVamsasMo = false;
586 public Annotation[] colourSequenceFromStructure(SequenceI seq,
590 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
591 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
593 * Annotation [] annotations = new Annotation[seq.getLength()];
595 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
596 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
597 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
599 * for (int j = 0; j < mappings.length; j++) {
601 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
602 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
603 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
604 * "+mappings[j].pdbfile);
606 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
607 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
609 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
610 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
611 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
612 * mappings[j].pdbfile); }
614 * annotations[index] = new Annotation("X",null,' ',0,col); } return
615 * annotations; } } } }
617 * return annotations;
621 public void structureSelectionChanged()
625 public void sequenceSelectionChanged()
629 public void sequenceColoursChanged(Object source)
631 StructureListener sl;
632 for (int i = 0; i < listeners.size(); i++)
634 if (listeners.elementAt(i) instanceof StructureListener)
636 sl = (StructureListener) listeners.elementAt(i);
637 sl.updateColours(source);
642 public StructureMapping[] getMapping(String pdbfile)
644 Vector tmp = new Vector();
645 if (mappings != null)
647 for (int i = 0; i < mappings.length; i++)
649 if (mappings[i].pdbfile.equals(pdbfile))
651 tmp.addElement(mappings[i]);
655 StructureMapping[] ret = new StructureMapping[tmp.size()];
656 for (int i = 0; i < tmp.size(); i++)
658 ret[i] = (StructureMapping) tmp.elementAt(i);
664 public String printMapping(String pdbfile)
666 StringBuffer sb = new StringBuffer();
667 for (int i = 0; i < mappings.length; i++)
669 if (mappings[i].pdbfile.equals(pdbfile))
671 sb.append(mappings[i].mappingDetails);
675 return sb.toString();
678 private int[] seqmappingrefs = null; // refcount for seqmappings elements
680 private synchronized void modifySeqMappingList(boolean add,
681 AlignedCodonFrame[] codonFrames)
683 if (!add && (seqmappings == null || seqmappings.size() == 0))
685 if (seqmappings == null)
686 seqmappings = new Vector();
687 if (codonFrames != null && codonFrames.length > 0)
689 for (int cf = 0; cf < codonFrames.length; cf++)
691 if (seqmappings.contains(codonFrames[cf]))
695 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
699 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
701 int pos = seqmappings.indexOf(codonFrames[cf]);
702 int[] nr = new int[seqmappingrefs.length - 1];
705 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
707 if (pos < seqmappingrefs.length - 1)
709 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
710 seqmappingrefs.length - pos - 2);
719 seqmappings.addElement(codonFrames[cf]);
721 int[] nsr = new int[(seqmappingrefs == null) ? 1
722 : seqmappingrefs.length + 1];
723 if (seqmappingrefs != null && seqmappingrefs.length > 0)
724 System.arraycopy(seqmappingrefs, 0, nsr, 0,
725 seqmappingrefs.length);
726 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
727 seqmappingrefs = nsr;
734 public void removeMappings(AlignedCodonFrame[] codonFrames)
736 modifySeqMappingList(false, codonFrames);
739 public void addMappings(AlignedCodonFrame[] codonFrames)
741 modifySeqMappingList(true, codonFrames);
744 Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
746 public void addSelectionListener(SelectionListener selecter)
748 if (!sel_listeners.contains(selecter))
750 sel_listeners.addElement(selecter);
754 public void removeSelectionListener(SelectionListener toremove)
756 if (sel_listeners.contains(toremove))
758 sel_listeners.removeElement(toremove);
762 public synchronized void sendSelection(
763 jalview.datamodel.SequenceGroup selection,
764 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
766 if (sel_listeners != null && sel_listeners.size() > 0)
768 Enumeration listeners = sel_listeners.elements();
769 while (listeners.hasMoreElements())
771 SelectionListener slis = ((SelectionListener) listeners
775 slis.selection(selection, colsel, source);
782 Vector<AlignmentViewPanelListener> view_listeners = new Vector<AlignmentViewPanelListener>();
784 public synchronized void sendViewPosition(
785 jalview.api.AlignmentViewPanel source, int startRes, int endRes,
786 int startSeq, int endSeq)
789 if (view_listeners != null && view_listeners.size() > 0)
791 Enumeration<AlignmentViewPanelListener> listeners = view_listeners
793 while (listeners.hasMoreElements())
795 AlignmentViewPanelListener slis = listeners.nextElement();
798 slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
805 public void finalize() throws Throwable
807 if (listeners != null)
812 if (mappingData != null)
817 if (sel_listeners != null)
819 sel_listeners.clear();
820 sel_listeners = null;
822 if (view_listeners != null)
824 view_listeners.clear();
825 view_listeners = null;
828 seqmappingrefs = null;
832 * release all references associated with this manager provider
836 public static void release(StructureSelectionManagerProvider jalviewLite)
838 // synchronized (instances)
840 if (instances == null)
844 StructureSelectionManager mnger = (instances.get(jalviewLite));
847 instances.remove(jalviewLite);
851 } catch (Throwable x)