2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.structure;
25 import jalview.analysis.*;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.api.StructureSelectionManagerProvider;
28 import jalview.datamodel.*;
30 public class StructureSelectionManager
32 static IdentityHashMap<StructureSelectionManagerProvider, StructureSelectionManager> instances;
34 StructureMapping[] mappings;
37 * debug function - write all mappings to stdout
39 public void reportMapping()
43 System.err.println("reportMapping: No PDB/Sequence mappings.");
47 System.err.println("reportMapping: There are " + mappings.length
49 for (int m = 0; m < mappings.length; m++)
51 System.err.println("mapping " + m + " : " + mappings[m].pdbfile);
56 Hashtable mappingData = new Hashtable();
58 public static StructureSelectionManager getStructureSelectionManager(
59 StructureSelectionManagerProvider context)
63 throw new Error("Implementation error. Structure selection manager's context is 'null'", new NullPointerException("SSM context is null"));
65 if (instances == null)
67 instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider, StructureSelectionManager>();
69 StructureSelectionManager instance = instances.get(context);
72 instances.put(context, instance = new StructureSelectionManager());
78 * flag controlling whether SeqMappings are relayed from received sequence
79 * mouse over events to other sequences
81 boolean relaySeqMappings = true;
84 * Enable or disable relay of seqMapping events to other sequences. You might
85 * want to do this if there are many sequence mappings and the host computer
90 public void setRelaySeqMappings(boolean relay)
92 relaySeqMappings = relay;
96 * get the state of the relay seqMappings flag.
98 * @return true if sequence mouse overs are being relayed to other mapped
101 public boolean isRelaySeqMappingsEnabled()
103 return relaySeqMappings;
106 Vector listeners = new Vector();
109 * register a listener for alignment sequence mouseover events
113 public void addStructureViewerListener(Object svl)
115 if (!listeners.contains(svl))
117 listeners.addElement(svl);
121 public String alreadyMappedToFile(String pdbid)
123 if (mappings != null)
125 for (int i = 0; i < mappings.length; i++)
127 if (mappings[i].getPdbId().equals(pdbid))
129 return mappings[i].pdbfile;
137 * create sequence structure mappings between each sequence and the given
138 * pdbFile (retrieved via the given protocol).
141 * - one or more sequences to be mapped to pdbFile
142 * @param targetChains
143 * - optional chain specification for mapping each sequence to pdb
144 * (may be nill, individual elements may be nill)
146 * - structure data resource
148 * - how to resolve data from resource
149 * @return null or the structure data parsed as a pdb file
151 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
152 String[] targetChains, String pdbFile, String protocol)
155 * There will be better ways of doing this in the future, for now we'll use
156 * the tried and tested MCview pdb mapping
158 MCview.PDBfile pdb = null;
161 pdb = new MCview.PDBfile(pdbFile, protocol);
162 } catch (Exception ex)
164 ex.printStackTrace();
169 for (int s = 0; s < sequence.length; s++)
171 boolean infChain = true;
172 if (targetChains != null && targetChains[s] != null)
175 targetChain = targetChains[s];
177 else if (sequence[s].getName().indexOf("|") > -1)
179 targetChain = sequence[s].getName().substring(
180 sequence[s].getName().lastIndexOf("|") + 1);
181 if (targetChain.length() > 1)
183 if (targetChain.trim().length() == 0)
189 // not a valid chain identifier
198 AlignSeq maxAlignseq = null;
199 String maxChainId = " ";
200 PDBChain maxChain = null;
201 boolean first = true;
202 for (int i = 0; i < pdb.chains.size(); i++)
204 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
205 if (targetChain.length() > 0 && !targetChain.equals(chain.id)
208 continue; // don't try to map chains don't match.
210 // TODO: correctly determine sequence type for mixed na/peptide
212 AlignSeq as = new AlignSeq(sequence[s],
213 ((PDBChain) pdb.chains.elementAt(i)).sequence,
214 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
216 as.calcScoreMatrix();
219 if (first || as.maxscore > max
220 || (as.maxscore == max && chain.id.equals(targetChain)))
226 maxChainId = chain.id;
229 if (maxChain == null)
233 final StringBuffer mappingDetails = new StringBuffer();
234 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
235 + maxChain.sequence.getSequenceAsString());
236 mappingDetails.append("\nNo of residues = "
237 + maxChain.residues.size() + "\n\n");
238 PrintStream ps = new PrintStream(System.out)
240 public void print(String x)
242 mappingDetails.append(x);
245 public void println()
247 mappingDetails.append("\n");
251 maxAlignseq.printAlignment(ps);
253 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
254 + " " + maxAlignseq.seq2end);
255 mappingDetails.append("\nSEQ start/end "
256 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
257 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
259 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
261 maxChain.transferRESNUMFeatures(sequence[s], null);
263 // allocate enough slots to store the mapping from positions in
264 // sequence[s] to the associated chain
265 int[][] mapping = new int[sequence[s].findPosition(sequence[s]
266 .getLength()) + 2][2];
272 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
273 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
275 resNum = tmp.resNumber;
276 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
277 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
281 } while (index < maxChain.atoms.size());
283 if (mappings == null)
285 mappings = new StructureMapping[1];
289 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
290 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
294 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
295 pdbFile = "INLINE" + pdb.id;
297 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
298 pdbFile, pdb.id, maxChainId, mapping,
299 mappingDetails.toString());
300 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
307 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
309 listeners.removeElement(svl);
310 if (svl instanceof SequenceListener)
312 for (int i = 0; i < listeners.size(); i++)
314 if (listeners.elementAt(i) instanceof StructureListener)
316 ((StructureListener) listeners.elementAt(i))
317 .releaseReferences(svl);
322 if (pdbfiles == null)
326 boolean removeMapping = true;
328 Vector pdbs = new Vector();
329 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
331 StructureListener sl;
332 for (int i = 0; i < listeners.size(); i++)
334 if (listeners.elementAt(i) instanceof StructureListener)
336 sl = (StructureListener) listeners.elementAt(i);
337 handlepdbs = sl.getPdbFile();
338 for (int j = 0; j < handlepdbs.length; j++)
340 if (pdbs.contains(handlepdbs[j]))
342 pdbs.removeElement(handlepdbs[j]);
349 if (pdbs.size() > 0 && mappings != null)
351 Vector tmp = new Vector();
352 for (int i = 0; i < mappings.length; i++)
354 if (!pdbs.contains(mappings[i].pdbfile))
356 tmp.addElement(mappings[i]);
360 mappings = new StructureMapping[tmp.size()];
361 tmp.copyInto(mappings);
365 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
367 if (listeners == null)
369 // old or prematurely sent event
372 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
373 SearchResults results = null;
374 SequenceI lastseq = null;
375 int lastipos = -1, indexpos;
376 for (int i = 0; i < listeners.size(); i++)
378 if (listeners.elementAt(i) instanceof SequenceListener)
382 results = new SearchResults();
384 if (mappings != null)
386 for (int j = 0; j < mappings.length; j++)
388 if (mappings[j].pdbfile.equals(pdbfile)
389 && mappings[j].pdbchain.equals(chain))
391 indexpos = mappings[j].getSeqPos(pdbResNum);
392 if (lastipos != indexpos && lastseq != mappings[j].sequence)
394 results.addResult(mappings[j].sequence, indexpos, indexpos);
396 lastseq = mappings[j].sequence;
397 // construct highlighted sequence list
398 if (seqmappings != null)
401 Enumeration e = seqmappings.elements();
402 while (e.hasMoreElements())
405 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
406 mappings[j].sequence, indexpos, results);
418 for (int i = 0; i < listeners.size(); i++)
420 Object li = listeners.elementAt(i);
421 if (li instanceof SequenceListener)
422 ((SequenceListener) li).highlightSequence(results);
427 Vector seqmappings = null; // should be a simpler list of mapped seuqence
430 * highlight regions associated with a position (indexpos) in seq
433 * the sequeence that the mouse over occured on
435 * the absolute position being mouseovered in seq (0 to seq.length())
437 * the sequence position (if -1, seq.findPosition is called to
438 * resolve the residue number)
440 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
443 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
444 SearchResults results = null;
446 index = seq.findPosition(indexpos);
447 StructureListener sl;
449 for (int i = 0; i < listeners.size(); i++)
451 Object listener = listeners.elementAt(i);
452 if (listener == source)
456 if (listener instanceof StructureListener)
458 sl = (StructureListener) listener;
459 if (mappings == null)
463 for (int j = 0; j < mappings.length; j++)
465 if (mappings[j].sequence == seq
466 || mappings[j].sequence == seq.getDatasetSequence())
468 atomNo = mappings[j].getAtomNum(index);
472 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
473 mappings[j].pdbchain, mappings[j].pdbfile);
480 if (relaySeqMappings && hasSequenceListeners
481 && listener instanceof SequenceListener)
484 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
489 results = new SearchResults();
490 if (index >= seq.getStart() && index <= seq.getEnd())
492 // construct highlighted sequence list
494 if (seqmappings != null)
496 Enumeration e = seqmappings.elements();
497 while (e.hasMoreElements())
500 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
501 seq, index, results);
504 // hasSequenceListeners = results.getSize() > 0;
505 if (handlingVamsasMo)
507 // maybe have to resolve seq to a dataset seqeunce...
508 // add in additional direct sequence and/or dataset sequence
510 results.addResult(seq, index, index);
514 if (hasSequenceListeners)
516 ((SequenceListener) listener).highlightSequence(results);
519 else if (listener instanceof VamsasListener && !handlingVamsasMo)
522 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
525 // pass the mouse over and absolute position onto the
527 ((VamsasListener) listener).mouseOver(seq, indexpos, source);
529 else if (listener instanceof SecondaryStructureListener)
531 ((SecondaryStructureListener) listener).mouseOverSequence(seq,
539 * true if a mouse over event from an external (ie Vamsas) source is being
542 boolean handlingVamsasMo = false;
547 * as mouseOverSequence but only route event to SequenceListeners
551 * in an alignment sequence
553 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
556 handlingVamsasMo = true;
557 long msg = sequenceI.hashCode() * (1 + position);
561 mouseOverSequence(sequenceI, position, -1, source);
563 handlingVamsasMo = false;
566 public Annotation[] colourSequenceFromStructure(SequenceI seq,
570 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
571 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
573 * Annotation [] annotations = new Annotation[seq.getLength()];
575 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
576 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
577 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
579 * for (int j = 0; j < mappings.length; j++) {
581 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
582 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
583 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
584 * "+mappings[j].pdbfile);
586 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
587 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
589 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
590 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
591 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
592 * mappings[j].pdbfile); }
594 * annotations[index] = new Annotation("X",null,' ',0,col); } return
595 * annotations; } } } }
597 * return annotations;
601 public void structureSelectionChanged()
605 public void sequenceSelectionChanged()
609 public void sequenceColoursChanged(Object source)
611 StructureListener sl;
612 for (int i = 0; i < listeners.size(); i++)
614 if (listeners.elementAt(i) instanceof StructureListener)
616 sl = (StructureListener) listeners.elementAt(i);
617 sl.updateColours(source);
622 public StructureMapping[] getMapping(String pdbfile)
624 Vector tmp = new Vector();
625 if (mappings != null)
627 for (int i = 0; i < mappings.length; i++)
629 if (mappings[i].pdbfile.equals(pdbfile))
631 tmp.addElement(mappings[i]);
635 StructureMapping[] ret = new StructureMapping[tmp.size()];
636 for (int i = 0; i < tmp.size(); i++)
638 ret[i] = (StructureMapping) tmp.elementAt(i);
644 public String printMapping(String pdbfile)
646 StringBuffer sb = new StringBuffer();
647 for (int i = 0; i < mappings.length; i++)
649 if (mappings[i].pdbfile.equals(pdbfile))
651 sb.append(mappings[i].mappingDetails);
655 return sb.toString();
658 private int[] seqmappingrefs = null; // refcount for seqmappings elements
660 private synchronized void modifySeqMappingList(boolean add,
661 AlignedCodonFrame[] codonFrames)
663 if (!add && (seqmappings == null || seqmappings.size() == 0))
665 if (seqmappings == null)
666 seqmappings = new Vector();
667 if (codonFrames != null && codonFrames.length > 0)
669 for (int cf = 0; cf < codonFrames.length; cf++)
671 if (seqmappings.contains(codonFrames[cf]))
675 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
679 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
681 int pos = seqmappings.indexOf(codonFrames[cf]);
682 int[] nr = new int[seqmappingrefs.length - 1];
685 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
687 if (pos < seqmappingrefs.length - 1)
689 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
690 seqmappingrefs.length - pos - 2);
699 seqmappings.addElement(codonFrames[cf]);
701 int[] nsr = new int[(seqmappingrefs == null) ? 1
702 : seqmappingrefs.length + 1];
703 if (seqmappingrefs != null && seqmappingrefs.length > 0)
704 System.arraycopy(seqmappingrefs, 0, nsr, 0,
705 seqmappingrefs.length);
706 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
707 seqmappingrefs = nsr;
714 public void removeMappings(AlignedCodonFrame[] codonFrames)
716 modifySeqMappingList(false, codonFrames);
719 public void addMappings(AlignedCodonFrame[] codonFrames)
721 modifySeqMappingList(true, codonFrames);
724 Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
726 public void addSelectionListener(SelectionListener selecter)
728 if (!sel_listeners.contains(selecter))
730 sel_listeners.addElement(selecter);
734 public void removeSelectionListener(SelectionListener toremove)
736 if (sel_listeners.contains(toremove))
738 sel_listeners.removeElement(toremove);
742 public synchronized void sendSelection(
743 jalview.datamodel.SequenceGroup selection,
744 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
746 if (sel_listeners != null && sel_listeners.size() > 0)
748 Enumeration listeners = sel_listeners.elements();
749 while (listeners.hasMoreElements())
751 SelectionListener slis = ((SelectionListener) listeners
755 slis.selection(selection, colsel, source);
762 Vector<AlignmentViewPanelListener> view_listeners = new Vector<AlignmentViewPanelListener>();
764 public synchronized void sendViewPosition(
765 jalview.api.AlignmentViewPanel source, int startRes, int endRes,
766 int startSeq, int endSeq)
769 if (view_listeners != null && view_listeners.size() > 0)
771 Enumeration<AlignmentViewPanelListener> listeners = view_listeners
773 while (listeners.hasMoreElements())
775 AlignmentViewPanelListener slis = listeners.nextElement();
778 slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
785 public void finalize() throws Throwable
787 if (listeners != null)
792 if (mappingData != null)
797 if (sel_listeners != null)
799 sel_listeners.clear();
800 sel_listeners = null;
802 if (view_listeners != null)
804 view_listeners.clear();
805 view_listeners = null;
808 seqmappingrefs = null;
812 * release all references associated with this manager provider
816 public static void release(StructureSelectionManagerProvider jalviewLite)
818 // synchronized (instances)
820 if (instances == null)
824 StructureSelectionManager mnger = (instances.get(jalviewLite));
827 instances.remove(jalviewLite);
831 } catch (Throwable x)