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();
57 private static StructureSelectionManager nullProvider = null;
58 public static StructureSelectionManager getStructureSelectionManager(
59 StructureSelectionManagerProvider context)
62 if (nullProvider == null)
64 if (instances != null)
67 "Implementation error. Structure selection manager's context is 'null'",
68 new NullPointerException("SSM context is null"));
72 nullProvider = new StructureSelectionManager();
77 if (instances == null)
79 instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider, StructureSelectionManager>();
81 StructureSelectionManager instance = instances.get(context);
84 if (nullProvider!=null)
86 instance = nullProvider;
88 instance = new StructureSelectionManager();
90 instances.put(context, instance);
96 * flag controlling whether SeqMappings are relayed from received sequence
97 * mouse over events to other sequences
99 boolean relaySeqMappings = true;
102 * Enable or disable relay of seqMapping events to other sequences. You might
103 * want to do this if there are many sequence mappings and the host computer
108 public void setRelaySeqMappings(boolean relay)
110 relaySeqMappings = relay;
114 * get the state of the relay seqMappings flag.
116 * @return true if sequence mouse overs are being relayed to other mapped
119 public boolean isRelaySeqMappingsEnabled()
121 return relaySeqMappings;
124 Vector listeners = new Vector();
127 * register a listener for alignment sequence mouseover events
131 public void addStructureViewerListener(Object svl)
133 if (!listeners.contains(svl))
135 listeners.addElement(svl);
139 public String alreadyMappedToFile(String pdbid)
141 if (mappings != null)
143 for (int i = 0; i < mappings.length; i++)
145 if (mappings[i].getPdbId().equals(pdbid))
147 return mappings[i].pdbfile;
155 * create sequence structure mappings between each sequence and the given
156 * pdbFile (retrieved via the given protocol).
159 * - one or more sequences to be mapped to pdbFile
160 * @param targetChains
161 * - optional chain specification for mapping each sequence to pdb
162 * (may be nill, individual elements may be nill)
164 * - structure data resource
166 * - how to resolve data from resource
167 * @return null or the structure data parsed as a pdb file
169 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
170 String[] targetChains, String pdbFile, String protocol)
173 * There will be better ways of doing this in the future, for now we'll use
174 * the tried and tested MCview pdb mapping
176 MCview.PDBfile pdb = null;
179 pdb = new MCview.PDBfile(pdbFile, protocol);
180 } catch (Exception ex)
182 ex.printStackTrace();
187 for (int s = 0; s < sequence.length; s++)
189 boolean infChain = true;
190 if (targetChains != null && targetChains[s] != null)
193 targetChain = targetChains[s];
195 else if (sequence[s].getName().indexOf("|") > -1)
197 targetChain = sequence[s].getName().substring(
198 sequence[s].getName().lastIndexOf("|") + 1);
199 if (targetChain.length() > 1)
201 if (targetChain.trim().length() == 0)
207 // not a valid chain identifier
216 AlignSeq maxAlignseq = null;
217 String maxChainId = " ";
218 PDBChain maxChain = null;
219 boolean first = true;
220 for (int i = 0; i < pdb.chains.size(); i++)
222 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
223 if (targetChain.length() > 0 && !targetChain.equals(chain.id)
226 continue; // don't try to map chains don't match.
228 // TODO: correctly determine sequence type for mixed na/peptide
230 AlignSeq as = new AlignSeq(sequence[s],
231 ((PDBChain) pdb.chains.elementAt(i)).sequence,
232 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
234 as.calcScoreMatrix();
237 if (first || as.maxscore > max
238 || (as.maxscore == max && chain.id.equals(targetChain)))
244 maxChainId = chain.id;
247 if (maxChain == null)
251 final StringBuffer mappingDetails = new StringBuffer();
252 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
253 + maxChain.sequence.getSequenceAsString());
254 mappingDetails.append("\nNo of residues = "
255 + maxChain.residues.size() + "\n\n");
256 PrintStream ps = new PrintStream(System.out)
258 public void print(String x)
260 mappingDetails.append(x);
263 public void println()
265 mappingDetails.append("\n");
269 maxAlignseq.printAlignment(ps);
271 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
272 + " " + maxAlignseq.seq2end);
273 mappingDetails.append("\nSEQ start/end "
274 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
275 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
277 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
279 maxChain.transferRESNUMFeatures(sequence[s], null);
281 // allocate enough slots to store the mapping from positions in
282 // sequence[s] to the associated chain
283 int[][] mapping = new int[sequence[s].findPosition(sequence[s]
284 .getLength()) + 2][2];
290 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
291 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
293 resNum = tmp.resNumber;
294 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
295 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
299 } while (index < maxChain.atoms.size());
301 if (mappings == null)
303 mappings = new StructureMapping[1];
307 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
308 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
312 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
313 pdbFile = "INLINE" + pdb.id;
315 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
316 pdbFile, pdb.id, maxChainId, mapping,
317 mappingDetails.toString());
318 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
325 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
327 listeners.removeElement(svl);
328 if (svl instanceof SequenceListener)
330 for (int i = 0; i < listeners.size(); i++)
332 if (listeners.elementAt(i) instanceof StructureListener)
334 ((StructureListener) listeners.elementAt(i))
335 .releaseReferences(svl);
340 if (pdbfiles == null)
344 boolean removeMapping = true;
346 Vector pdbs = new Vector();
347 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
349 StructureListener sl;
350 for (int i = 0; i < listeners.size(); i++)
352 if (listeners.elementAt(i) instanceof StructureListener)
354 sl = (StructureListener) listeners.elementAt(i);
355 handlepdbs = sl.getPdbFile();
356 for (int j = 0; j < handlepdbs.length; j++)
358 if (pdbs.contains(handlepdbs[j]))
360 pdbs.removeElement(handlepdbs[j]);
367 if (pdbs.size() > 0 && mappings != null)
369 Vector tmp = new Vector();
370 for (int i = 0; i < mappings.length; i++)
372 if (!pdbs.contains(mappings[i].pdbfile))
374 tmp.addElement(mappings[i]);
378 mappings = new StructureMapping[tmp.size()];
379 tmp.copyInto(mappings);
383 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
385 if (listeners == null)
387 // old or prematurely sent event
390 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
391 SearchResults results = null;
392 SequenceI lastseq = null;
393 int lastipos = -1, indexpos;
394 for (int i = 0; i < listeners.size(); i++)
396 if (listeners.elementAt(i) instanceof SequenceListener)
400 results = new SearchResults();
402 if (mappings != null)
404 for (int j = 0; j < mappings.length; j++)
406 if (mappings[j].pdbfile.equals(pdbfile)
407 && mappings[j].pdbchain.equals(chain))
409 indexpos = mappings[j].getSeqPos(pdbResNum);
410 if (lastipos != indexpos && lastseq != mappings[j].sequence)
412 results.addResult(mappings[j].sequence, indexpos, indexpos);
414 lastseq = mappings[j].sequence;
415 // construct highlighted sequence list
416 if (seqmappings != null)
419 Enumeration e = seqmappings.elements();
420 while (e.hasMoreElements())
423 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
424 mappings[j].sequence, indexpos, results);
436 for (int i = 0; i < listeners.size(); i++)
438 Object li = listeners.elementAt(i);
439 if (li instanceof SequenceListener)
440 ((SequenceListener) li).highlightSequence(results);
445 Vector seqmappings = null; // should be a simpler list of mapped seuqence
448 * highlight regions associated with a position (indexpos) in seq
451 * the sequeence that the mouse over occured on
453 * the absolute position being mouseovered in seq (0 to seq.length())
455 * the sequence position (if -1, seq.findPosition is called to
456 * resolve the residue number)
458 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
461 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
462 SearchResults results = null;
464 index = seq.findPosition(indexpos);
465 StructureListener sl;
467 for (int i = 0; i < listeners.size(); i++)
469 Object listener = listeners.elementAt(i);
470 if (listener == source)
474 if (listener instanceof StructureListener)
476 sl = (StructureListener) listener;
477 if (mappings == null)
481 for (int j = 0; j < mappings.length; j++)
483 if (mappings[j].sequence == seq
484 || mappings[j].sequence == seq.getDatasetSequence())
486 atomNo = mappings[j].getAtomNum(index);
490 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
491 mappings[j].pdbchain, mappings[j].pdbfile);
498 if (relaySeqMappings && hasSequenceListeners
499 && listener instanceof SequenceListener)
502 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
507 results = new SearchResults();
508 if (index >= seq.getStart() && index <= seq.getEnd())
510 // construct highlighted sequence list
512 if (seqmappings != null)
514 Enumeration e = seqmappings.elements();
515 while (e.hasMoreElements())
518 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
519 seq, index, results);
522 // hasSequenceListeners = results.getSize() > 0;
523 if (handlingVamsasMo)
525 // maybe have to resolve seq to a dataset seqeunce...
526 // add in additional direct sequence and/or dataset sequence
528 results.addResult(seq, index, index);
532 if (hasSequenceListeners)
534 ((SequenceListener) listener).highlightSequence(results);
537 else if (listener instanceof VamsasListener && !handlingVamsasMo)
540 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
543 // pass the mouse over and absolute position onto the
545 ((VamsasListener) listener).mouseOver(seq, indexpos, source);
547 else if (listener instanceof SecondaryStructureListener)
549 ((SecondaryStructureListener) listener).mouseOverSequence(seq,
557 * true if a mouse over event from an external (ie Vamsas) source is being
560 boolean handlingVamsasMo = false;
565 * as mouseOverSequence but only route event to SequenceListeners
569 * in an alignment sequence
571 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
574 handlingVamsasMo = true;
575 long msg = sequenceI.hashCode() * (1 + position);
579 mouseOverSequence(sequenceI, position, -1, source);
581 handlingVamsasMo = false;
584 public Annotation[] colourSequenceFromStructure(SequenceI seq,
588 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
589 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
591 * Annotation [] annotations = new Annotation[seq.getLength()];
593 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
594 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
595 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
597 * for (int j = 0; j < mappings.length; j++) {
599 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
600 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
601 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
602 * "+mappings[j].pdbfile);
604 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
605 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
607 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
608 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
609 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
610 * mappings[j].pdbfile); }
612 * annotations[index] = new Annotation("X",null,' ',0,col); } return
613 * annotations; } } } }
615 * return annotations;
619 public void structureSelectionChanged()
623 public void sequenceSelectionChanged()
627 public void sequenceColoursChanged(Object source)
629 StructureListener sl;
630 for (int i = 0; i < listeners.size(); i++)
632 if (listeners.elementAt(i) instanceof StructureListener)
634 sl = (StructureListener) listeners.elementAt(i);
635 sl.updateColours(source);
640 public StructureMapping[] getMapping(String pdbfile)
642 Vector tmp = new Vector();
643 if (mappings != null)
645 for (int i = 0; i < mappings.length; i++)
647 if (mappings[i].pdbfile.equals(pdbfile))
649 tmp.addElement(mappings[i]);
653 StructureMapping[] ret = new StructureMapping[tmp.size()];
654 for (int i = 0; i < tmp.size(); i++)
656 ret[i] = (StructureMapping) tmp.elementAt(i);
662 public String printMapping(String pdbfile)
664 StringBuffer sb = new StringBuffer();
665 for (int i = 0; i < mappings.length; i++)
667 if (mappings[i].pdbfile.equals(pdbfile))
669 sb.append(mappings[i].mappingDetails);
673 return sb.toString();
676 private int[] seqmappingrefs = null; // refcount for seqmappings elements
678 private synchronized void modifySeqMappingList(boolean add,
679 AlignedCodonFrame[] codonFrames)
681 if (!add && (seqmappings == null || seqmappings.size() == 0))
683 if (seqmappings == null)
684 seqmappings = new Vector();
685 if (codonFrames != null && codonFrames.length > 0)
687 for (int cf = 0; cf < codonFrames.length; cf++)
689 if (seqmappings.contains(codonFrames[cf]))
693 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
697 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
699 int pos = seqmappings.indexOf(codonFrames[cf]);
700 int[] nr = new int[seqmappingrefs.length - 1];
703 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
705 if (pos < seqmappingrefs.length - 1)
707 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
708 seqmappingrefs.length - pos - 2);
717 seqmappings.addElement(codonFrames[cf]);
719 int[] nsr = new int[(seqmappingrefs == null) ? 1
720 : seqmappingrefs.length + 1];
721 if (seqmappingrefs != null && seqmappingrefs.length > 0)
722 System.arraycopy(seqmappingrefs, 0, nsr, 0,
723 seqmappingrefs.length);
724 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
725 seqmappingrefs = nsr;
732 public void removeMappings(AlignedCodonFrame[] codonFrames)
734 modifySeqMappingList(false, codonFrames);
737 public void addMappings(AlignedCodonFrame[] codonFrames)
739 modifySeqMappingList(true, codonFrames);
742 Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
744 public void addSelectionListener(SelectionListener selecter)
746 if (!sel_listeners.contains(selecter))
748 sel_listeners.addElement(selecter);
752 public void removeSelectionListener(SelectionListener toremove)
754 if (sel_listeners.contains(toremove))
756 sel_listeners.removeElement(toremove);
760 public synchronized void sendSelection(
761 jalview.datamodel.SequenceGroup selection,
762 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
764 if (sel_listeners != null && sel_listeners.size() > 0)
766 Enumeration listeners = sel_listeners.elements();
767 while (listeners.hasMoreElements())
769 SelectionListener slis = ((SelectionListener) listeners
773 slis.selection(selection, colsel, source);
780 Vector<AlignmentViewPanelListener> view_listeners = new Vector<AlignmentViewPanelListener>();
782 public synchronized void sendViewPosition(
783 jalview.api.AlignmentViewPanel source, int startRes, int endRes,
784 int startSeq, int endSeq)
787 if (view_listeners != null && view_listeners.size() > 0)
789 Enumeration<AlignmentViewPanelListener> listeners = view_listeners
791 while (listeners.hasMoreElements())
793 AlignmentViewPanelListener slis = listeners.nextElement();
796 slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
803 public void finalize() throws Throwable
805 if (listeners != null)
810 if (mappingData != null)
815 if (sel_listeners != null)
817 sel_listeners.clear();
818 sel_listeners = null;
820 if (view_listeners != null)
822 view_listeners.clear();
823 view_listeners = null;
826 seqmappingrefs = null;
830 * release all references associated with this manager provider
834 public static void release(StructureSelectionManagerProvider jalviewLite)
836 // synchronized (instances)
838 if (instances == null)
842 StructureSelectionManager mnger = (instances.get(jalviewLite));
845 instances.remove(jalviewLite);
849 } catch (Throwable x)