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)
62 throw new Error("Implementation error. Structure selection manager's context is 'null'", new NullPointerException("SSM context is null"));
64 if (instances == null)
66 instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider, StructureSelectionManager>();
68 StructureSelectionManager instance = instances.get(context);
71 instances.put(context, instance = new StructureSelectionManager());
77 * flag controlling whether SeqMappings are relayed from received sequence
78 * mouse over events to other sequences
80 boolean relaySeqMappings = true;
83 * Enable or disable relay of seqMapping events to other sequences. You might
84 * want to do this if there are many sequence mappings and the host computer
89 public void setRelaySeqMappings(boolean relay)
91 relaySeqMappings = relay;
95 * get the state of the relay seqMappings flag.
97 * @return true if sequence mouse overs are being relayed to other mapped
100 public boolean isRelaySeqMappingsEnabled()
102 return relaySeqMappings;
105 Vector listeners = new Vector();
108 * register a listener for alignment sequence mouseover events
112 public void addStructureViewerListener(Object svl)
114 if (!listeners.contains(svl))
116 listeners.addElement(svl);
120 public String alreadyMappedToFile(String pdbid)
122 if (mappings != null)
124 for (int i = 0; i < mappings.length; i++)
126 if (mappings[i].getPdbId().equals(pdbid))
128 return mappings[i].pdbfile;
136 * create sequence structure mappings between each sequence and the given
137 * pdbFile (retrieved via the given protocol).
140 * - one or more sequences to be mapped to pdbFile
141 * @param targetChains
142 * - optional chain specification for mapping each sequence to pdb
143 * (may be nill, individual elements may be nill)
145 * - structure data resource
147 * - how to resolve data from resource
148 * @return null or the structure data parsed as a pdb file
150 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
151 String[] targetChains, String pdbFile, String protocol)
154 * There will be better ways of doing this in the future, for now we'll use
155 * the tried and tested MCview pdb mapping
157 MCview.PDBfile pdb = null;
160 pdb = new MCview.PDBfile(pdbFile, protocol);
161 } catch (Exception ex)
163 ex.printStackTrace();
168 for (int s = 0; s < sequence.length; s++)
170 boolean infChain = true;
171 if (targetChains != null && targetChains[s] != null)
174 targetChain = targetChains[s];
176 else if (sequence[s].getName().indexOf("|") > -1)
178 targetChain = sequence[s].getName().substring(
179 sequence[s].getName().lastIndexOf("|") + 1);
180 if (targetChain.length() > 1)
182 if (targetChain.trim().length() == 0)
188 // not a valid chain identifier
197 AlignSeq maxAlignseq = null;
198 String maxChainId = " ";
199 PDBChain maxChain = null;
200 boolean first = true;
201 for (int i = 0; i < pdb.chains.size(); i++)
203 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
204 if (targetChain.length() > 0 && !targetChain.equals(chain.id)
207 continue; // don't try to map chains don't match.
209 // TODO: correctly determine sequence type for mixed na/peptide
211 AlignSeq as = new AlignSeq(sequence[s],
212 ((PDBChain) pdb.chains.elementAt(i)).sequence,
213 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
215 as.calcScoreMatrix();
218 if (first || as.maxscore > max
219 || (as.maxscore == max && chain.id.equals(targetChain)))
225 maxChainId = chain.id;
228 if (maxChain == null)
232 final StringBuffer mappingDetails = new StringBuffer();
233 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
234 + maxChain.sequence.getSequenceAsString());
235 mappingDetails.append("\nNo of residues = "
236 + maxChain.residues.size() + "\n\n");
237 PrintStream ps = new PrintStream(System.out)
239 public void print(String x)
241 mappingDetails.append(x);
244 public void println()
246 mappingDetails.append("\n");
250 maxAlignseq.printAlignment(ps);
252 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
253 + " " + maxAlignseq.seq2end);
254 mappingDetails.append("\nSEQ start/end "
255 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
256 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
258 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
260 maxChain.transferRESNUMFeatures(sequence[s], null);
262 // allocate enough slots to store the mapping from positions in
263 // sequence[s] to the associated chain
264 int[][] mapping = new int[sequence[s].findPosition(sequence[s]
265 .getLength()) + 2][2];
271 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
272 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
274 resNum = tmp.resNumber;
275 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
276 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
280 } while (index < maxChain.atoms.size());
282 if (mappings == null)
284 mappings = new StructureMapping[1];
288 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
289 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
293 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
294 pdbFile = "INLINE" + pdb.id;
296 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
297 pdbFile, pdb.id, maxChainId, mapping,
298 mappingDetails.toString());
299 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
306 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
308 listeners.removeElement(svl);
309 if (svl instanceof SequenceListener)
311 for (int i = 0; i < listeners.size(); i++)
313 if (listeners.elementAt(i) instanceof StructureListener)
315 ((StructureListener) listeners.elementAt(i))
316 .releaseReferences(svl);
321 if (pdbfiles == null)
325 boolean removeMapping = true;
327 Vector pdbs = new Vector();
328 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
330 StructureListener sl;
331 for (int i = 0; i < listeners.size(); i++)
333 if (listeners.elementAt(i) instanceof StructureListener)
335 sl = (StructureListener) listeners.elementAt(i);
336 handlepdbs = sl.getPdbFile();
337 for (int j = 0; j < handlepdbs.length; j++)
339 if (pdbs.contains(handlepdbs[j]))
341 pdbs.removeElement(handlepdbs[j]);
348 if (pdbs.size() > 0 && mappings != null)
350 Vector tmp = new Vector();
351 for (int i = 0; i < mappings.length; i++)
353 if (!pdbs.contains(mappings[i].pdbfile))
355 tmp.addElement(mappings[i]);
359 mappings = new StructureMapping[tmp.size()];
360 tmp.copyInto(mappings);
364 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
366 if (listeners == null)
368 // old or prematurely sent event
371 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
372 SearchResults results = null;
373 SequenceI lastseq = null;
374 int lastipos = -1, indexpos;
375 for (int i = 0; i < listeners.size(); i++)
377 if (listeners.elementAt(i) instanceof SequenceListener)
381 results = new SearchResults();
383 if (mappings != null)
385 for (int j = 0; j < mappings.length; j++)
387 if (mappings[j].pdbfile.equals(pdbfile)
388 && mappings[j].pdbchain.equals(chain))
390 indexpos = mappings[j].getSeqPos(pdbResNum);
391 if (lastipos != indexpos && lastseq != mappings[j].sequence)
393 results.addResult(mappings[j].sequence, indexpos, indexpos);
395 lastseq = mappings[j].sequence;
396 // construct highlighted sequence list
397 if (seqmappings != null)
400 Enumeration e = seqmappings.elements();
401 while (e.hasMoreElements())
404 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
405 mappings[j].sequence, indexpos, results);
417 for (int i = 0; i < listeners.size(); i++)
419 Object li = listeners.elementAt(i);
420 if (li instanceof SequenceListener)
421 ((SequenceListener) li).highlightSequence(results);
426 Vector seqmappings = null; // should be a simpler list of mapped seuqence
429 * highlight regions associated with a position (indexpos) in seq
432 * the sequeence that the mouse over occured on
434 * the absolute position being mouseovered in seq (0 to seq.length())
436 * the sequence position (if -1, seq.findPosition is called to
437 * resolve the residue number)
439 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
442 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
443 SearchResults results = null;
445 index = seq.findPosition(indexpos);
446 StructureListener sl;
448 for (int i = 0; i < listeners.size(); i++)
450 Object listener = listeners.elementAt(i);
451 if (listener == source)
455 if (listener instanceof StructureListener)
457 sl = (StructureListener) listener;
458 if (mappings == null)
462 for (int j = 0; j < mappings.length; j++)
464 if (mappings[j].sequence == seq
465 || mappings[j].sequence == seq.getDatasetSequence())
467 atomNo = mappings[j].getAtomNum(index);
471 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
472 mappings[j].pdbchain, mappings[j].pdbfile);
479 if (relaySeqMappings && hasSequenceListeners
480 && listener instanceof SequenceListener)
483 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
488 results = new SearchResults();
489 if (index >= seq.getStart() && index <= seq.getEnd())
491 // construct highlighted sequence list
493 if (seqmappings != null)
495 Enumeration e = seqmappings.elements();
496 while (e.hasMoreElements())
499 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
500 seq, index, results);
503 // hasSequenceListeners = results.getSize() > 0;
504 if (handlingVamsasMo)
506 // maybe have to resolve seq to a dataset seqeunce...
507 // add in additional direct sequence and/or dataset sequence
509 results.addResult(seq, index, index);
513 if (hasSequenceListeners)
515 ((SequenceListener) listener).highlightSequence(results);
518 else if (listener instanceof VamsasListener && !handlingVamsasMo)
521 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
524 // pass the mouse over and absolute position onto the
526 ((VamsasListener) listener).mouseOver(seq, indexpos, source);
528 else if (listener instanceof SecondaryStructureListener)
530 ((SecondaryStructureListener) listener).mouseOverSequence(seq,
538 * true if a mouse over event from an external (ie Vamsas) source is being
541 boolean handlingVamsasMo = false;
546 * as mouseOverSequence but only route event to SequenceListeners
550 * in an alignment sequence
552 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
555 handlingVamsasMo = true;
556 long msg = sequenceI.hashCode() * (1 + position);
560 mouseOverSequence(sequenceI, position, -1, source);
562 handlingVamsasMo = false;
565 public Annotation[] colourSequenceFromStructure(SequenceI seq,
569 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
570 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
572 * Annotation [] annotations = new Annotation[seq.getLength()];
574 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
575 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
576 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
578 * for (int j = 0; j < mappings.length; j++) {
580 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
581 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
582 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
583 * "+mappings[j].pdbfile);
585 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
586 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
588 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
589 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
590 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
591 * mappings[j].pdbfile); }
593 * annotations[index] = new Annotation("X",null,' ',0,col); } return
594 * annotations; } } } }
596 * return annotations;
600 public void structureSelectionChanged()
604 public void sequenceSelectionChanged()
608 public void sequenceColoursChanged(Object source)
610 StructureListener sl;
611 for (int i = 0; i < listeners.size(); i++)
613 if (listeners.elementAt(i) instanceof StructureListener)
615 sl = (StructureListener) listeners.elementAt(i);
616 sl.updateColours(source);
621 public StructureMapping[] getMapping(String pdbfile)
623 Vector tmp = new Vector();
624 if (mappings != null)
626 for (int i = 0; i < mappings.length; i++)
628 if (mappings[i].pdbfile.equals(pdbfile))
630 tmp.addElement(mappings[i]);
634 StructureMapping[] ret = new StructureMapping[tmp.size()];
635 for (int i = 0; i < tmp.size(); i++)
637 ret[i] = (StructureMapping) tmp.elementAt(i);
643 public String printMapping(String pdbfile)
645 StringBuffer sb = new StringBuffer();
646 for (int i = 0; i < mappings.length; i++)
648 if (mappings[i].pdbfile.equals(pdbfile))
650 sb.append(mappings[i].mappingDetails);
654 return sb.toString();
657 private int[] seqmappingrefs = null; // refcount for seqmappings elements
659 private synchronized void modifySeqMappingList(boolean add,
660 AlignedCodonFrame[] codonFrames)
662 if (!add && (seqmappings == null || seqmappings.size() == 0))
664 if (seqmappings == null)
665 seqmappings = new Vector();
666 if (codonFrames != null && codonFrames.length > 0)
668 for (int cf = 0; cf < codonFrames.length; cf++)
670 if (seqmappings.contains(codonFrames[cf]))
674 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
678 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
680 int pos = seqmappings.indexOf(codonFrames[cf]);
681 int[] nr = new int[seqmappingrefs.length - 1];
684 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
686 if (pos < seqmappingrefs.length - 1)
688 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
689 seqmappingrefs.length - pos - 2);
698 seqmappings.addElement(codonFrames[cf]);
700 int[] nsr = new int[(seqmappingrefs == null) ? 1
701 : seqmappingrefs.length + 1];
702 if (seqmappingrefs != null && seqmappingrefs.length > 0)
703 System.arraycopy(seqmappingrefs, 0, nsr, 0,
704 seqmappingrefs.length);
705 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
706 seqmappingrefs = nsr;
713 public void removeMappings(AlignedCodonFrame[] codonFrames)
715 modifySeqMappingList(false, codonFrames);
718 public void addMappings(AlignedCodonFrame[] codonFrames)
720 modifySeqMappingList(true, codonFrames);
723 Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
725 public void addSelectionListener(SelectionListener selecter)
727 if (!sel_listeners.contains(selecter))
729 sel_listeners.addElement(selecter);
733 public void removeSelectionListener(SelectionListener toremove)
735 if (sel_listeners.contains(toremove))
737 sel_listeners.removeElement(toremove);
741 public synchronized void sendSelection(
742 jalview.datamodel.SequenceGroup selection,
743 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
745 if (sel_listeners != null && sel_listeners.size() > 0)
747 Enumeration listeners = sel_listeners.elements();
748 while (listeners.hasMoreElements())
750 SelectionListener slis = ((SelectionListener) listeners
754 slis.selection(selection, colsel, source);
761 Vector<AlignmentViewPanelListener> view_listeners = new Vector<AlignmentViewPanelListener>();
763 public synchronized void sendViewPosition(
764 jalview.api.AlignmentViewPanel source, int startRes, int endRes,
765 int startSeq, int endSeq)
768 if (view_listeners != null && view_listeners.size() > 0)
770 Enumeration<AlignmentViewPanelListener> listeners = view_listeners
772 while (listeners.hasMoreElements())
774 AlignmentViewPanelListener slis = listeners.nextElement();
777 slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
784 public void finalize() throws Throwable
786 if (listeners != null)
791 if (mappingData != null)
796 if (sel_listeners != null)
798 sel_listeners.clear();
799 sel_listeners = null;
801 if (view_listeners != null)
803 view_listeners.clear();
804 view_listeners = null;
807 seqmappingrefs = null;
811 * release all references associated with this manager provider
815 public static void release(StructureSelectionManagerProvider jalviewLite)
817 // synchronized (instances)
819 if (instances == null)
823 StructureSelectionManager mnger = (instances.get(jalviewLite));
826 instances.remove(jalviewLite);
830 } catch (Throwable x)