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();
57 public static StructureSelectionManager getStructureSelectionManager(
58 StructureSelectionManagerProvider context)
60 if (instances == null)
62 instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider, StructureSelectionManager>();
64 StructureSelectionManager instance = instances.get(context);
67 instances.put(context, instance = new StructureSelectionManager());
73 * flag controlling whether SeqMappings are relayed from received sequence
74 * mouse over events to other sequences
76 boolean relaySeqMappings = true;
79 * Enable or disable relay of seqMapping events to other sequences. You might
80 * want to do this if there are many sequence mappings and the host computer
85 public void setRelaySeqMappings(boolean relay)
87 relaySeqMappings = relay;
91 * get the state of the relay seqMappings flag.
93 * @return true if sequence mouse overs are being relayed to other mapped
96 public boolean isRelaySeqMappingsEnabled()
98 return relaySeqMappings;
101 Vector listeners = new Vector();
104 * register a listener for alignment sequence mouseover events
108 public void addStructureViewerListener(Object svl)
110 if (!listeners.contains(svl))
112 listeners.addElement(svl);
116 public String alreadyMappedToFile(String pdbid)
118 if (mappings != null)
120 for (int i = 0; i < mappings.length; i++)
122 if (mappings[i].getPdbId().equals(pdbid))
124 return mappings[i].pdbfile;
132 * create sequence structure mappings between each sequence and the given
133 * pdbFile (retrieved via the given protocol).
136 * - one or more sequences to be mapped to pdbFile
137 * @param targetChains
138 * - optional chain specification for mapping each sequence to pdb
139 * (may be nill, individual elements may be nill)
141 * - structure data resource
143 * - how to resolve data from resource
144 * @return null or the structure data parsed as a pdb file
146 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
147 String[] targetChains, String pdbFile, String protocol)
150 * There will be better ways of doing this in the future, for now we'll use
151 * the tried and tested MCview pdb mapping
153 MCview.PDBfile pdb = null;
156 pdb = new MCview.PDBfile(pdbFile, protocol);
157 } catch (Exception ex)
159 ex.printStackTrace();
164 for (int s = 0; s < sequence.length; s++)
166 boolean infChain = true;
167 if (targetChains != null && targetChains[s] != null)
170 targetChain = targetChains[s];
172 else if (sequence[s].getName().indexOf("|") > -1)
174 targetChain = sequence[s].getName().substring(
175 sequence[s].getName().lastIndexOf("|") + 1);
176 if (targetChain.length() > 1)
178 if (targetChain.trim().length() == 0)
184 // not a valid chain identifier
193 AlignSeq maxAlignseq = null;
194 String maxChainId = " ";
195 PDBChain maxChain = null;
196 boolean first = true;
197 for (int i = 0; i < pdb.chains.size(); i++)
199 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
200 if (targetChain.length() > 0 && !targetChain.equals(chain.id)
203 continue; // don't try to map chains don't match.
205 // TODO: correctly determine sequence type for mixed na/peptide
207 AlignSeq as = new AlignSeq(sequence[s],
208 ((PDBChain) pdb.chains.elementAt(i)).sequence,
209 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
211 as.calcScoreMatrix();
214 if (first || as.maxscore > max
215 || (as.maxscore == max && chain.id.equals(targetChain)))
221 maxChainId = chain.id;
224 if (maxChain == null)
228 final StringBuffer mappingDetails = new StringBuffer();
229 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
230 + maxChain.sequence.getSequenceAsString());
231 mappingDetails.append("\nNo of residues = "
232 + maxChain.residues.size() + "\n\n");
233 PrintStream ps = new PrintStream(System.out)
235 public void print(String x)
237 mappingDetails.append(x);
240 public void println()
242 mappingDetails.append("\n");
246 maxAlignseq.printAlignment(ps);
248 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
249 + " " + maxAlignseq.seq2end);
250 mappingDetails.append("\nSEQ start/end "
251 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
252 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
254 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
256 maxChain.transferRESNUMFeatures(sequence[s], null);
258 // allocate enough slots to store the mapping from positions in
259 // sequence[s] to the associated chain
260 int[][] mapping = new int[sequence[s].findPosition(sequence[s]
261 .getLength()) + 2][2];
267 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
268 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
270 resNum = tmp.resNumber;
271 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
272 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
276 } while (index < maxChain.atoms.size());
278 if (mappings == null)
280 mappings = new StructureMapping[1];
284 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
285 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
289 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
290 pdbFile = "INLINE" + pdb.id;
292 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
293 pdbFile, pdb.id, maxChainId, mapping,
294 mappingDetails.toString());
295 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
302 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
304 listeners.removeElement(svl);
305 if (svl instanceof SequenceListener)
307 for (int i = 0; i < listeners.size(); i++)
309 if (listeners.elementAt(i) instanceof StructureListener)
311 ((StructureListener) listeners.elementAt(i))
312 .releaseReferences(svl);
317 if (pdbfiles == null)
321 boolean removeMapping = true;
323 Vector pdbs = new Vector();
324 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
326 StructureListener sl;
327 for (int i = 0; i < listeners.size(); i++)
329 if (listeners.elementAt(i) instanceof StructureListener)
331 sl = (StructureListener) listeners.elementAt(i);
332 handlepdbs = sl.getPdbFile();
333 for (int j = 0; j < handlepdbs.length; j++)
335 if (pdbs.contains(handlepdbs[j]))
337 pdbs.removeElement(handlepdbs[j]);
344 if (pdbs.size() > 0 && mappings != null)
346 Vector tmp = new Vector();
347 for (int i = 0; i < mappings.length; i++)
349 if (!pdbs.contains(mappings[i].pdbfile))
351 tmp.addElement(mappings[i]);
355 mappings = new StructureMapping[tmp.size()];
356 tmp.copyInto(mappings);
360 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
362 if (listeners == null)
364 // old or prematurely sent event
367 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
368 SearchResults results = null;
369 SequenceI lastseq = null;
370 int lastipos = -1, indexpos;
371 for (int i = 0; i < listeners.size(); i++)
373 if (listeners.elementAt(i) instanceof SequenceListener)
377 results = new SearchResults();
379 if (mappings != null)
381 for (int j = 0; j < mappings.length; j++)
383 if (mappings[j].pdbfile.equals(pdbfile)
384 && mappings[j].pdbchain.equals(chain))
386 indexpos = mappings[j].getSeqPos(pdbResNum);
387 if (lastipos != indexpos && lastseq != mappings[j].sequence)
389 results.addResult(mappings[j].sequence, indexpos, indexpos);
391 lastseq = mappings[j].sequence;
392 // construct highlighted sequence list
393 if (seqmappings != null)
396 Enumeration e = seqmappings.elements();
397 while (e.hasMoreElements())
400 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
401 mappings[j].sequence, indexpos, results);
413 for (int i = 0; i < listeners.size(); i++)
415 Object li = listeners.elementAt(i);
416 if (li instanceof SequenceListener)
417 ((SequenceListener) li).highlightSequence(results);
422 Vector seqmappings = null; // should be a simpler list of mapped seuqence
425 * highlight regions associated with a position (indexpos) in seq
428 * the sequeence that the mouse over occured on
430 * the absolute position being mouseovered in seq (0 to seq.length())
432 * the sequence position (if -1, seq.findPosition is called to
433 * resolve the residue number)
435 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
438 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
439 SearchResults results = null;
441 index = seq.findPosition(indexpos);
442 StructureListener sl;
444 for (int i = 0; i < listeners.size(); i++)
446 Object listener = listeners.elementAt(i);
447 if (listener == source)
451 if (listener instanceof StructureListener)
453 sl = (StructureListener) listener;
454 if (mappings == null)
458 for (int j = 0; j < mappings.length; j++)
460 if (mappings[j].sequence == seq
461 || mappings[j].sequence == seq.getDatasetSequence())
463 atomNo = mappings[j].getAtomNum(index);
467 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
468 mappings[j].pdbchain, mappings[j].pdbfile);
475 if (relaySeqMappings && hasSequenceListeners
476 && listener instanceof SequenceListener)
479 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
484 results = new SearchResults();
485 if (index >= seq.getStart() && index <= seq.getEnd())
487 // construct highlighted sequence list
489 if (seqmappings != null)
491 Enumeration e = seqmappings.elements();
492 while (e.hasMoreElements())
495 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
496 seq, index, results);
499 // hasSequenceListeners = results.getSize() > 0;
500 if (handlingVamsasMo)
502 // maybe have to resolve seq to a dataset seqeunce...
503 // add in additional direct sequence and/or dataset sequence
505 results.addResult(seq, index, index);
509 if (hasSequenceListeners)
511 ((SequenceListener) listener).highlightSequence(results);
514 else if (listener instanceof VamsasListener && !handlingVamsasMo)
517 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
520 // pass the mouse over and absolute position onto the
522 ((VamsasListener) listener).mouseOver(seq, indexpos, source);
524 else if (listener instanceof SecondaryStructureListener)
526 ((SecondaryStructureListener) listener).mouseOverSequence(seq,
534 * true if a mouse over event from an external (ie Vamsas) source is being
537 boolean handlingVamsasMo = false;
542 * as mouseOverSequence but only route event to SequenceListeners
546 * in an alignment sequence
548 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
551 handlingVamsasMo = true;
552 long msg = sequenceI.hashCode() * (1 + position);
556 mouseOverSequence(sequenceI, position, -1, source);
558 handlingVamsasMo = false;
561 public Annotation[] colourSequenceFromStructure(SequenceI seq,
565 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
566 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
568 * Annotation [] annotations = new Annotation[seq.getLength()];
570 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
571 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
572 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
574 * for (int j = 0; j < mappings.length; j++) {
576 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
577 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
578 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
579 * "+mappings[j].pdbfile);
581 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
582 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
584 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
585 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
586 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
587 * mappings[j].pdbfile); }
589 * annotations[index] = new Annotation("X",null,' ',0,col); } return
590 * annotations; } } } }
592 * return annotations;
596 public void structureSelectionChanged()
600 public void sequenceSelectionChanged()
604 public void sequenceColoursChanged(Object source)
606 StructureListener sl;
607 for (int i = 0; i < listeners.size(); i++)
609 if (listeners.elementAt(i) instanceof StructureListener)
611 sl = (StructureListener) listeners.elementAt(i);
612 sl.updateColours(source);
617 public StructureMapping[] getMapping(String pdbfile)
619 Vector tmp = new Vector();
620 if (mappings != null)
622 for (int i = 0; i < mappings.length; i++)
624 if (mappings[i].pdbfile.equals(pdbfile))
626 tmp.addElement(mappings[i]);
630 StructureMapping[] ret = new StructureMapping[tmp.size()];
631 for (int i = 0; i < tmp.size(); i++)
633 ret[i] = (StructureMapping) tmp.elementAt(i);
639 public String printMapping(String pdbfile)
641 StringBuffer sb = new StringBuffer();
642 for (int i = 0; i < mappings.length; i++)
644 if (mappings[i].pdbfile.equals(pdbfile))
646 sb.append(mappings[i].mappingDetails);
650 return sb.toString();
653 private int[] seqmappingrefs = null; // refcount for seqmappings elements
655 private synchronized void modifySeqMappingList(boolean add,
656 AlignedCodonFrame[] codonFrames)
658 if (!add && (seqmappings == null || seqmappings.size() == 0))
660 if (seqmappings == null)
661 seqmappings = new Vector();
662 if (codonFrames != null && codonFrames.length > 0)
664 for (int cf = 0; cf < codonFrames.length; cf++)
666 if (seqmappings.contains(codonFrames[cf]))
670 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
674 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
676 int pos = seqmappings.indexOf(codonFrames[cf]);
677 int[] nr = new int[seqmappingrefs.length - 1];
680 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
682 if (pos < seqmappingrefs.length - 1)
684 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
685 seqmappingrefs.length - pos - 2);
694 seqmappings.addElement(codonFrames[cf]);
696 int[] nsr = new int[(seqmappingrefs == null) ? 1
697 : seqmappingrefs.length + 1];
698 if (seqmappingrefs != null && seqmappingrefs.length > 0)
699 System.arraycopy(seqmappingrefs, 0, nsr, 0,
700 seqmappingrefs.length);
701 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
702 seqmappingrefs = nsr;
709 public void removeMappings(AlignedCodonFrame[] codonFrames)
711 modifySeqMappingList(false, codonFrames);
714 public void addMappings(AlignedCodonFrame[] codonFrames)
716 modifySeqMappingList(true, codonFrames);
719 Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
721 public void addSelectionListener(SelectionListener selecter)
723 if (!sel_listeners.contains(selecter))
725 sel_listeners.addElement(selecter);
729 public void removeSelectionListener(SelectionListener toremove)
731 if (sel_listeners.contains(toremove))
733 sel_listeners.removeElement(toremove);
737 public synchronized void sendSelection(
738 jalview.datamodel.SequenceGroup selection,
739 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
741 if (sel_listeners != null && sel_listeners.size() > 0)
743 Enumeration listeners = sel_listeners.elements();
744 while (listeners.hasMoreElements())
746 SelectionListener slis = ((SelectionListener) listeners
750 slis.selection(selection, colsel, source);
757 Vector<AlignmentViewPanelListener> view_listeners = new Vector<AlignmentViewPanelListener>();
759 public synchronized void sendViewPosition(
760 jalview.api.AlignmentViewPanel source, int startRes, int endRes,
761 int startSeq, int endSeq)
764 if (view_listeners != null && view_listeners.size() > 0)
766 Enumeration<AlignmentViewPanelListener> listeners = view_listeners
768 while (listeners.hasMoreElements())
770 AlignmentViewPanelListener slis = listeners.nextElement();
773 slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
780 public void finalize() throws Throwable
782 if (listeners != null)
787 if (mappingData != null)
792 if (sel_listeners != null)
794 sel_listeners.clear();
795 sel_listeners = null;
797 if (view_listeners != null)
799 view_listeners.clear();
800 view_listeners = null;
803 seqmappingrefs = null;
807 * release all references associated with this manager provider
811 public static void release(StructureSelectionManagerProvider jalviewLite)
813 // synchronized (instances)
815 if (instances == null)
819 StructureSelectionManager mnger = (instances.get(jalviewLite));
822 instances.remove(jalviewLite);
826 } catch (Throwable x)