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();
60 private static StructureSelectionManager nullProvider = null;
62 public static StructureSelectionManager getStructureSelectionManager(
63 StructureSelectionManagerProvider context)
67 if (nullProvider == null)
69 if (instances != null)
72 "Implementation error. Structure selection manager's context is 'null'",
73 new NullPointerException("SSM context is null"));
77 nullProvider = new StructureSelectionManager();
82 if (instances == null)
84 instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider, StructureSelectionManager>();
86 StructureSelectionManager instance = instances.get(context);
89 if (nullProvider != null)
91 instance = nullProvider;
95 instance = new StructureSelectionManager();
97 instances.put(context, instance);
103 * flag controlling whether SeqMappings are relayed from received sequence
104 * mouse over events to other sequences
106 boolean relaySeqMappings = true;
109 * Enable or disable relay of seqMapping events to other sequences. You might
110 * want to do this if there are many sequence mappings and the host computer
115 public void setRelaySeqMappings(boolean relay)
117 relaySeqMappings = relay;
121 * get the state of the relay seqMappings flag.
123 * @return true if sequence mouse overs are being relayed to other mapped
126 public boolean isRelaySeqMappingsEnabled()
128 return relaySeqMappings;
131 Vector listeners = new Vector();
134 * register a listener for alignment sequence mouseover events
138 public void addStructureViewerListener(Object svl)
140 if (!listeners.contains(svl))
142 listeners.addElement(svl);
146 public String alreadyMappedToFile(String pdbid)
148 if (mappings != null)
150 for (int i = 0; i < mappings.length; i++)
152 if (mappings[i].getPdbId().equals(pdbid))
154 return mappings[i].pdbfile;
162 * create sequence structure mappings between each sequence and the given
163 * pdbFile (retrieved via the given protocol).
166 * - one or more sequences to be mapped to pdbFile
167 * @param targetChains
168 * - optional chain specification for mapping each sequence to pdb
169 * (may be nill, individual elements may be nill)
171 * - structure data resource
173 * - how to resolve data from resource
174 * @return null or the structure data parsed as a pdb file
176 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
177 String[] targetChains, String pdbFile, String protocol)
180 * There will be better ways of doing this in the future, for now we'll use
181 * the tried and tested MCview pdb mapping
183 MCview.PDBfile pdb = null;
186 pdb = new MCview.PDBfile(pdbFile, protocol);
187 } catch (Exception ex)
189 ex.printStackTrace();
194 for (int s = 0; s < sequence.length; s++)
196 boolean infChain = true;
197 if (targetChains != null && targetChains[s] != null)
200 targetChain = targetChains[s];
202 else if (sequence[s].getName().indexOf("|") > -1)
204 targetChain = sequence[s].getName().substring(
205 sequence[s].getName().lastIndexOf("|") + 1);
206 if (targetChain.length() > 1)
208 if (targetChain.trim().length() == 0)
214 // not a valid chain identifier
223 AlignSeq maxAlignseq = null;
224 String maxChainId = " ";
225 PDBChain maxChain = null;
226 boolean first = true;
227 for (int i = 0; i < pdb.chains.size(); i++)
229 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
230 if (targetChain.length() > 0 && !targetChain.equals(chain.id)
233 continue; // don't try to map chains don't match.
235 // TODO: correctly determine sequence type for mixed na/peptide
237 AlignSeq as = new AlignSeq(sequence[s],
238 ((PDBChain) pdb.chains.elementAt(i)).sequence,
239 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
241 as.calcScoreMatrix();
244 if (first || as.maxscore > max
245 || (as.maxscore == max && chain.id.equals(targetChain)))
251 maxChainId = chain.id;
254 if (maxChain == null)
258 final StringBuffer mappingDetails = new StringBuffer();
259 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
260 + maxChain.sequence.getSequenceAsString());
261 mappingDetails.append("\nNo of residues = "
262 + maxChain.residues.size() + "\n\n");
263 PrintStream ps = new PrintStream(System.out)
265 public void print(String x)
267 mappingDetails.append(x);
270 public void println()
272 mappingDetails.append("\n");
276 maxAlignseq.printAlignment(ps);
278 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
279 + " " + maxAlignseq.seq2end);
280 mappingDetails.append("\nSEQ start/end "
281 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
282 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
284 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
286 maxChain.transferRESNUMFeatures(sequence[s], null);
288 // allocate enough slots to store the mapping from positions in
289 // sequence[s] to the associated chain
290 int[][] mapping = new int[sequence[s].findPosition(sequence[s]
291 .getLength()) + 2][2];
297 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
298 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
300 resNum = tmp.resNumber;
301 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
302 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
306 } while (index < maxChain.atoms.size());
308 if (mappings == null)
310 mappings = new StructureMapping[1];
314 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
315 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
319 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
320 pdbFile = "INLINE" + pdb.id;
322 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
323 pdbFile, pdb.id, maxChainId, mapping,
324 mappingDetails.toString());
325 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
332 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
334 listeners.removeElement(svl);
335 if (svl instanceof SequenceListener)
337 for (int i = 0; i < listeners.size(); i++)
339 if (listeners.elementAt(i) instanceof StructureListener)
341 ((StructureListener) listeners.elementAt(i))
342 .releaseReferences(svl);
347 if (pdbfiles == null)
351 boolean removeMapping = true;
353 Vector pdbs = new Vector();
354 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
356 StructureListener sl;
357 for (int i = 0; i < listeners.size(); i++)
359 if (listeners.elementAt(i) instanceof StructureListener)
361 sl = (StructureListener) listeners.elementAt(i);
362 handlepdbs = sl.getPdbFile();
363 for (int j = 0; j < handlepdbs.length; j++)
365 if (pdbs.contains(handlepdbs[j]))
367 pdbs.removeElement(handlepdbs[j]);
374 if (pdbs.size() > 0 && mappings != null)
376 Vector tmp = new Vector();
377 for (int i = 0; i < mappings.length; i++)
379 if (!pdbs.contains(mappings[i].pdbfile))
381 tmp.addElement(mappings[i]);
385 mappings = new StructureMapping[tmp.size()];
386 tmp.copyInto(mappings);
390 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
392 if (listeners == null)
394 // old or prematurely sent event
397 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
398 SearchResults results = null;
399 SequenceI lastseq = null;
400 int lastipos = -1, indexpos;
401 for (int i = 0; i < listeners.size(); i++)
403 if (listeners.elementAt(i) instanceof SequenceListener)
407 results = new SearchResults();
409 if (mappings != null)
411 for (int j = 0; j < mappings.length; j++)
413 if (mappings[j].pdbfile.equals(pdbfile)
414 && mappings[j].pdbchain.equals(chain))
416 indexpos = mappings[j].getSeqPos(pdbResNum);
417 if (lastipos != indexpos && lastseq != mappings[j].sequence)
419 results.addResult(mappings[j].sequence, indexpos, indexpos);
421 lastseq = mappings[j].sequence;
422 // construct highlighted sequence list
423 if (seqmappings != null)
426 Enumeration e = seqmappings.elements();
427 while (e.hasMoreElements())
430 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
431 mappings[j].sequence, indexpos, results);
443 for (int i = 0; i < listeners.size(); i++)
445 Object li = listeners.elementAt(i);
446 if (li instanceof SequenceListener)
447 ((SequenceListener) li).highlightSequence(results);
452 Vector seqmappings = null; // should be a simpler list of mapped seuqence
455 * highlight regions associated with a position (indexpos) in seq
458 * the sequeence that the mouse over occured on
460 * the absolute position being mouseovered in seq (0 to seq.length())
462 * the sequence position (if -1, seq.findPosition is called to
463 * resolve the residue number)
465 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
468 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
469 SearchResults results = null;
471 index = seq.findPosition(indexpos);
472 StructureListener sl;
474 for (int i = 0; i < listeners.size(); i++)
476 Object listener = listeners.elementAt(i);
477 if (listener == source)
481 if (listener instanceof StructureListener)
483 sl = (StructureListener) listener;
484 if (mappings == null)
488 for (int j = 0; j < mappings.length; j++)
490 if (mappings[j].sequence == seq
491 || mappings[j].sequence == seq.getDatasetSequence())
493 atomNo = mappings[j].getAtomNum(index);
497 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
498 mappings[j].pdbchain, mappings[j].pdbfile);
505 if (relaySeqMappings && hasSequenceListeners
506 && listener instanceof SequenceListener)
509 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
514 results = new SearchResults();
515 if (index >= seq.getStart() && index <= seq.getEnd())
517 // construct highlighted sequence list
519 if (seqmappings != null)
521 Enumeration e = seqmappings.elements();
522 while (e.hasMoreElements())
525 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
526 seq, index, results);
529 // hasSequenceListeners = results.getSize() > 0;
530 if (handlingVamsasMo)
532 // maybe have to resolve seq to a dataset seqeunce...
533 // add in additional direct sequence and/or dataset sequence
535 results.addResult(seq, index, index);
539 if (hasSequenceListeners)
541 ((SequenceListener) listener).highlightSequence(results);
544 else if (listener instanceof VamsasListener && !handlingVamsasMo)
547 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
550 // pass the mouse over and absolute position onto the
552 ((VamsasListener) listener).mouseOver(seq, indexpos, source);
554 else if (listener instanceof SecondaryStructureListener)
556 ((SecondaryStructureListener) listener).mouseOverSequence(seq,
564 * true if a mouse over event from an external (ie Vamsas) source is being
567 boolean handlingVamsasMo = false;
572 * as mouseOverSequence but only route event to SequenceListeners
576 * in an alignment sequence
578 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
581 handlingVamsasMo = true;
582 long msg = sequenceI.hashCode() * (1 + position);
586 mouseOverSequence(sequenceI, position, -1, source);
588 handlingVamsasMo = false;
591 public Annotation[] colourSequenceFromStructure(SequenceI seq,
595 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
596 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
598 * Annotation [] annotations = new Annotation[seq.getLength()];
600 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
601 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
602 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
604 * for (int j = 0; j < mappings.length; j++) {
606 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
607 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
608 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
609 * "+mappings[j].pdbfile);
611 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
612 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
614 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
615 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
616 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
617 * mappings[j].pdbfile); }
619 * annotations[index] = new Annotation("X",null,' ',0,col); } return
620 * annotations; } } } }
622 * return annotations;
626 public void structureSelectionChanged()
630 public void sequenceSelectionChanged()
634 public void sequenceColoursChanged(Object source)
636 StructureListener sl;
637 for (int i = 0; i < listeners.size(); i++)
639 if (listeners.elementAt(i) instanceof StructureListener)
641 sl = (StructureListener) listeners.elementAt(i);
642 sl.updateColours(source);
647 public StructureMapping[] getMapping(String pdbfile)
649 Vector tmp = new Vector();
650 if (mappings != null)
652 for (int i = 0; i < mappings.length; i++)
654 if (mappings[i].pdbfile.equals(pdbfile))
656 tmp.addElement(mappings[i]);
660 StructureMapping[] ret = new StructureMapping[tmp.size()];
661 for (int i = 0; i < tmp.size(); i++)
663 ret[i] = (StructureMapping) tmp.elementAt(i);
669 public String printMapping(String pdbfile)
671 StringBuffer sb = new StringBuffer();
672 for (int i = 0; i < mappings.length; i++)
674 if (mappings[i].pdbfile.equals(pdbfile))
676 sb.append(mappings[i].mappingDetails);
680 return sb.toString();
683 private int[] seqmappingrefs = null; // refcount for seqmappings elements
685 private synchronized void modifySeqMappingList(boolean add,
686 AlignedCodonFrame[] codonFrames)
688 if (!add && (seqmappings == null || seqmappings.size() == 0))
690 if (seqmappings == null)
691 seqmappings = new Vector();
692 if (codonFrames != null && codonFrames.length > 0)
694 for (int cf = 0; cf < codonFrames.length; cf++)
696 if (seqmappings.contains(codonFrames[cf]))
700 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
704 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
706 int pos = seqmappings.indexOf(codonFrames[cf]);
707 int[] nr = new int[seqmappingrefs.length - 1];
710 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
712 if (pos < seqmappingrefs.length - 1)
714 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
715 seqmappingrefs.length - pos - 2);
724 seqmappings.addElement(codonFrames[cf]);
726 int[] nsr = new int[(seqmappingrefs == null) ? 1
727 : seqmappingrefs.length + 1];
728 if (seqmappingrefs != null && seqmappingrefs.length > 0)
729 System.arraycopy(seqmappingrefs, 0, nsr, 0,
730 seqmappingrefs.length);
731 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
732 seqmappingrefs = nsr;
739 public void removeMappings(AlignedCodonFrame[] codonFrames)
741 modifySeqMappingList(false, codonFrames);
744 public void addMappings(AlignedCodonFrame[] codonFrames)
746 modifySeqMappingList(true, codonFrames);
749 Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
751 public void addSelectionListener(SelectionListener selecter)
753 if (!sel_listeners.contains(selecter))
755 sel_listeners.addElement(selecter);
759 public void removeSelectionListener(SelectionListener toremove)
761 if (sel_listeners.contains(toremove))
763 sel_listeners.removeElement(toremove);
767 public synchronized void sendSelection(
768 jalview.datamodel.SequenceGroup selection,
769 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
771 if (sel_listeners != null && sel_listeners.size() > 0)
773 Enumeration listeners = sel_listeners.elements();
774 while (listeners.hasMoreElements())
776 SelectionListener slis = ((SelectionListener) listeners
780 slis.selection(selection, colsel, source);
787 Vector<AlignmentViewPanelListener> view_listeners = new Vector<AlignmentViewPanelListener>();
789 public synchronized void sendViewPosition(
790 jalview.api.AlignmentViewPanel source, int startRes, int endRes,
791 int startSeq, int endSeq)
794 if (view_listeners != null && view_listeners.size() > 0)
796 Enumeration<AlignmentViewPanelListener> listeners = view_listeners
798 while (listeners.hasMoreElements())
800 AlignmentViewPanelListener slis = listeners.nextElement();
803 slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
810 public void finalize() throws Throwable
812 if (listeners != null)
817 if (mappingData != null)
822 if (sel_listeners != null)
824 sel_listeners.clear();
825 sel_listeners = null;
827 if (view_listeners != null)
829 view_listeners.clear();
830 view_listeners = null;
833 seqmappingrefs = null;
837 * release all references associated with this manager provider
841 public static void release(StructureSelectionManagerProvider jalviewLite)
843 // synchronized (instances)
845 if (instances == null)
849 StructureSelectionManager mnger = (instances.get(jalviewLite));
852 instances.remove(jalviewLite);
856 } catch (Throwable x)