2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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() {
\r
41 System.err.println("reportMapping: No PDB/Sequence mappings.");
\r
43 System.err.println("reportMapping: There are "+mappings.length+" mappings.");
\r
44 for (int m=0;m<mappings.length;m++)
\r
46 System.err.println("mapping "+m+" : "+mappings[m].pdbfile);
\r
50 Hashtable mappingData = new Hashtable();
52 public static StructureSelectionManager getStructureSelectionManager(StructureSelectionManagerProvider context)
54 if (instances == null)
56 instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider,StructureSelectionManager>();
58 StructureSelectionManager instance=instances.get(context);
61 instances.put(context, instance=new StructureSelectionManager());
67 * flag controlling whether SeqMappings are relayed from received sequence
68 * mouse over events to other sequences
70 boolean relaySeqMappings = true;
73 * Enable or disable relay of seqMapping events to other sequences. You might
74 * want to do this if there are many sequence mappings and the host computer
79 public void setRelaySeqMappings(boolean relay)
81 relaySeqMappings = relay;
85 * get the state of the relay seqMappings flag.
87 * @return true if sequence mouse overs are being relayed to other mapped
90 public boolean isRelaySeqMappingsEnabled()
92 return relaySeqMappings;
95 Vector listeners = new Vector();
98 * register a listener for alignment sequence mouseover events
101 public void addStructureViewerListener(Object svl)
103 if (!listeners.contains(svl))
105 listeners.addElement(svl);
109 public String alreadyMappedToFile(String pdbid)
111 if (mappings != null)
113 for (int i = 0; i < mappings.length; i++)
115 if (mappings[i].getPdbId().equals(pdbid))
117 return mappings[i].pdbfile;
125 * create sequence structure mappings between each sequence and the given
126 * pdbFile (retrieved via the given protocol).
129 * - one or more sequences to be mapped to pdbFile
130 * @param targetChains
131 * - optional chain specification for mapping each sequence to pdb
132 * (may be nill, individual elements may be nill)
134 * - structure data resource
136 * - how to resolve data from resource
137 * @return null or the structure data parsed as a pdb file
139 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
140 String[] targetChains, String pdbFile, String protocol)
143 * There will be better ways of doing this in the future, for now we'll use
144 * the tried and tested MCview pdb mapping
146 MCview.PDBfile pdb = null;
149 pdb = new MCview.PDBfile(pdbFile, protocol);
150 } catch (Exception ex)
152 ex.printStackTrace();
157 for (int s = 0; s < sequence.length; s++)
159 if (targetChains != null && targetChains[s] != null)
160 targetChain = targetChains[s];
161 else if (sequence[s].getName().indexOf("|") > -1)
163 targetChain = sequence[s].getName().substring(
164 sequence[s].getName().lastIndexOf("|") + 1);
170 AlignSeq maxAlignseq = null;
171 String maxChainId = " ";
172 PDBChain maxChain = null;
173 boolean first = true;
174 for (int i = 0; i < pdb.chains.size(); i++)
176 // TODO: re http://issues.jalview.org/browse/JAL-583 : this patch may
177 // need to be revoked
178 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
179 if (targetChain.length() > 0 && !targetChain.equals(chain.id))
181 continue; // don't try to map chains don't match.
183 // end of patch for limiting computed mappings
184 // TODO: correctly determine sequence type for mixed na/peptide
186 AlignSeq as = new AlignSeq(sequence[s],
187 ((PDBChain) pdb.chains.elementAt(i)).sequence,
188 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
190 as.calcScoreMatrix();
193 if (first || as.maxscore > max
194 || (as.maxscore == max && chain.id.equals(targetChain)))
200 maxChainId = chain.id;
203 if (maxChain == null)
207 final StringBuffer mappingDetails = new StringBuffer();
208 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
209 + maxChain.sequence.getSequenceAsString());
210 mappingDetails.append("\nNo of residues = "
211 + maxChain.residues.size() + "\n\n");
212 PrintStream ps = new PrintStream(System.out)
214 public void print(String x)
216 mappingDetails.append(x);
219 public void println()
221 mappingDetails.append("\n");
225 maxAlignseq.printAlignment(ps);
227 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
228 + " " + maxAlignseq.seq2end);
229 mappingDetails.append("\nSEQ start/end "
230 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
231 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
233 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
235 maxChain.transferRESNUMFeatures(sequence[s], null);
237 // allocate enough slots to store the mapping from positions in
238 // sequence[s] to the associated chain
239 int[][] mapping = new int[sequence[s].findPosition(sequence[s].getLength()) + 2][2];
245 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
246 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
248 resNum = tmp.resNumber;
249 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
250 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
254 } while (index < maxChain.atoms.size());
256 if (mappings == null)
258 mappings = new StructureMapping[1];
262 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
263 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
267 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
268 pdbFile = "INLINE" + pdb.id;
270 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
271 pdbFile, pdb.id, maxChainId, mapping,
272 mappingDetails.toString());
273 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
280 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
282 listeners.removeElement(svl);
283 if (svl instanceof SequenceListener)
285 for (int i=0;i<listeners.size();i++)
287 if (listeners.elementAt(i) instanceof StructureListener)
289 ((StructureListener)listeners.elementAt(i)).releaseReferences(svl);
294 if (pdbfiles == null)
298 boolean removeMapping = true;
300 Vector pdbs = new Vector();
301 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
303 StructureListener sl;
304 for (int i = 0; i < listeners.size(); i++)
306 if (listeners.elementAt(i) instanceof StructureListener)
308 sl = (StructureListener) listeners.elementAt(i);
309 handlepdbs = sl.getPdbFile();
310 for (int j = 0; j < handlepdbs.length; j++)
312 if (pdbs.contains(handlepdbs[j]))
314 pdbs.removeElement(handlepdbs[j]);
321 if (pdbs.size() > 0 && mappings != null)
323 Vector tmp = new Vector();
324 for (int i = 0; i < mappings.length; i++)
326 if (!pdbs.contains(mappings[i].pdbfile))
328 tmp.addElement(mappings[i]);
332 mappings = new StructureMapping[tmp.size()];
333 tmp.copyInto(mappings);
337 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
339 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
340 SearchResults results = null;
341 SequenceI lastseq = null;
342 int lastipos = -1, indexpos;
343 for (int i = 0; i < listeners.size(); i++)
345 if (listeners.elementAt(i) instanceof SequenceListener)
349 results = new SearchResults();
351 if (mappings != null)
353 for (int j = 0; j < mappings.length; j++)
355 if (mappings[j].pdbfile.equals(pdbfile)
356 && mappings[j].pdbchain.equals(chain))
358 indexpos = mappings[j].getSeqPos(pdbResNum);
359 if (lastipos != indexpos && lastseq != mappings[j].sequence)
361 results.addResult(mappings[j].sequence, indexpos, indexpos);
363 lastseq = mappings[j].sequence;
364 // construct highlighted sequence list
365 if (seqmappings != null)
368 Enumeration e = seqmappings.elements();
369 while (e.hasMoreElements())
372 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
373 mappings[j].sequence, indexpos, results);
385 for (int i = 0; i < listeners.size(); i++)
387 Object li = listeners.elementAt(i);
388 if (li instanceof SequenceListener)
389 ((SequenceListener) li).highlightSequence(results);
394 Vector seqmappings = null; // should be a simpler list of mapped seuqence
397 * highlight regions associated with a position (indexpos) in seq
400 * the sequeence that the mouse over occured on
402 * the absolute position being mouseovered in seq (0 to seq.length())
404 * the sequence position (if -1, seq.findPosition is called to
405 * resolve the residue number)
407 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
410 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
411 SearchResults results = null;
413 index = seq.findPosition(indexpos);
414 StructureListener sl;
416 for (int i = 0; i < listeners.size(); i++)
418 if (listeners.elementAt(i) instanceof StructureListener)
420 sl = (StructureListener) listeners.elementAt(i);
421 if (mappings == null)
425 for (int j = 0; j < mappings.length; j++)
427 if (mappings[j].sequence == seq
428 || mappings[j].sequence == seq.getDatasetSequence())
430 atomNo = mappings[j].getAtomNum(index);
434 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
435 mappings[j].pdbchain, mappings[j].pdbfile);
442 if (relaySeqMappings && hasSequenceListeners
443 && listeners.elementAt(i) instanceof SequenceListener)
446 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
451 results = new SearchResults();
452 if (index >= seq.getStart() && index <= seq.getEnd())
454 // construct highlighted sequence list
456 if (seqmappings != null)
458 Enumeration e = seqmappings.elements();
459 while (e.hasMoreElements())
462 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
463 seq, index, results);
466 // hasSequenceListeners = results.getSize() > 0;
467 if (handlingVamsasMo)
469 // maybe have to resolve seq to a dataset seqeunce...
470 // add in additional direct sequence and/or dataset sequence
472 results.addResult(seq, index, index);
476 if (hasSequenceListeners)
478 ((SequenceListener) listeners.elementAt(i))
479 .highlightSequence(results);
482 else if (listeners.elementAt(i) instanceof VamsasListener
483 && !handlingVamsasMo)
486 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
489 // pass the mouse over and absolute position onto the
491 ((VamsasListener) listeners.elementAt(i)).mouseOver(seq,
499 * true if a mouse over event from an external (ie Vamsas) source is being
502 boolean handlingVamsasMo = false;
507 * as mouseOverSequence but only route event to SequenceListeners
511 * in an alignment sequence
513 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
516 handlingVamsasMo = true;
517 long msg = sequenceI.hashCode() * (1 + position);
521 mouseOverSequence(sequenceI, position, -1, source);
523 handlingVamsasMo = false;
526 public Annotation[] colourSequenceFromStructure(SequenceI seq,
530 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
531 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
533 * Annotation [] annotations = new Annotation[seq.getLength()];
535 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
536 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
537 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
539 * for (int j = 0; j < mappings.length; j++) {
541 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
542 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
543 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
544 * "+mappings[j].pdbfile);
546 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
547 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
549 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
550 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
551 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
552 * mappings[j].pdbfile); }
554 * annotations[index] = new Annotation("X",null,' ',0,col); } return
555 * annotations; } } } }
557 * return annotations;
561 public void structureSelectionChanged()
565 public void sequenceSelectionChanged()
569 public void sequenceColoursChanged(Object source)
571 StructureListener sl;
572 for (int i = 0; i < listeners.size(); i++)
574 if (listeners.elementAt(i) instanceof StructureListener)
576 sl = (StructureListener) listeners.elementAt(i);
577 sl.updateColours(source);
582 public StructureMapping[] getMapping(String pdbfile)
584 Vector tmp = new Vector();
585 if (mappings != null)
587 for (int i = 0; i < mappings.length; i++)
589 if (mappings[i].pdbfile.equals(pdbfile))
591 tmp.addElement(mappings[i]);
595 StructureMapping[] ret = new StructureMapping[tmp.size()];
596 for (int i = 0; i < tmp.size(); i++)
598 ret[i] = (StructureMapping) tmp.elementAt(i);
604 public String printMapping(String pdbfile)
606 StringBuffer sb = new StringBuffer();
607 for (int i = 0; i < mappings.length; i++)
609 if (mappings[i].pdbfile.equals(pdbfile))
611 sb.append(mappings[i].mappingDetails);
615 return sb.toString();
618 private int[] seqmappingrefs = null; // refcount for seqmappings elements
620 private synchronized void modifySeqMappingList(boolean add,
621 AlignedCodonFrame[] codonFrames)
623 if (!add && (seqmappings == null || seqmappings.size() == 0))
625 if (seqmappings == null)
626 seqmappings = new Vector();
627 if (codonFrames != null && codonFrames.length > 0)
629 for (int cf = 0; cf < codonFrames.length; cf++)
631 if (seqmappings.contains(codonFrames[cf]))
635 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
639 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
641 int pos = seqmappings.indexOf(codonFrames[cf]);
642 int[] nr = new int[seqmappingrefs.length - 1];
645 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
647 if (pos < seqmappingrefs.length - 1)
649 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
650 seqmappingrefs.length - pos - 2);
659 seqmappings.addElement(codonFrames[cf]);
661 int[] nsr = new int[(seqmappingrefs == null) ? 1
662 : seqmappingrefs.length + 1];
663 if (seqmappingrefs != null && seqmappingrefs.length > 0)
664 System.arraycopy(seqmappingrefs, 0, nsr, 0,
665 seqmappingrefs.length);
666 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
667 seqmappingrefs = nsr;
674 public void removeMappings(AlignedCodonFrame[] codonFrames)
676 modifySeqMappingList(false, codonFrames);
679 public void addMappings(AlignedCodonFrame[] codonFrames)
681 modifySeqMappingList(true, codonFrames);
684 Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
686 public void addSelectionListener(SelectionListener selecter)
688 if (!sel_listeners.contains(selecter))
690 sel_listeners.addElement(selecter);
694 public void removeSelectionListener(SelectionListener toremove)
696 if (sel_listeners.contains(toremove))
698 sel_listeners.removeElement(toremove);
702 public synchronized void sendSelection(
703 jalview.datamodel.SequenceGroup selection,
704 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
706 if (sel_listeners != null && sel_listeners.size() > 0)
708 Enumeration listeners = sel_listeners.elements();
709 while (listeners.hasMoreElements())
711 SelectionListener slis = ((SelectionListener) listeners
715 slis.selection(selection, colsel, source);
722 Vector<AlignmentViewPanelListener> view_listeners=new Vector<AlignmentViewPanelListener>();
723 public synchronized void sendViewPosition(jalview.api.AlignmentViewPanel source, int startRes,
724 int endRes, int startSeq, int endSeq)
727 if (view_listeners != null && view_listeners.size() > 0)
729 Enumeration<AlignmentViewPanelListener> listeners = view_listeners.elements();
730 while (listeners.hasMoreElements())
732 AlignmentViewPanelListener slis = listeners
736 slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
744 public void finalize() throws Throwable {
745 if (listeners!=null) {
749 if (mappingData!=null)
754 if (sel_listeners!=null)
756 sel_listeners.clear();
759 if (view_listeners!=null)
761 view_listeners.clear();
769 * release all references associated with this manager provider
772 public static void release(StructureSelectionManagerProvider jalviewLite)
774 StructureSelectionManager mnger=(instances.get(jalviewLite));
777 instances.remove(jalviewLite);
780 } catch (Throwable x){};