2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
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.datamodel.*;
27 public class StructureSelectionManager
29 static StructureSelectionManager instance;
31 StructureMapping[] mappings;
33 Hashtable mappingData = new Hashtable();
35 public static StructureSelectionManager getStructureSelectionManager()
39 instance = new StructureSelectionManager();
46 * flag controlling whether SeqMappings are relayed from received sequence
47 * mouse over events to other sequences
49 boolean relaySeqMappings = true;
52 * Enable or disable relay of seqMapping events to other sequences. You might
53 * want to do this if there are many sequence mappings and the host computer
58 public void setRelaySeqMappings(boolean relay)
60 relaySeqMappings = relay;
64 * get the state of the relay seqMappings flag.
66 * @return true if sequence mouse overs are being relayed to other mapped
69 public boolean isRelaySeqMappingsEnabled()
71 return relaySeqMappings;
74 Vector listeners = new Vector();
76 public void addStructureViewerListener(Object svl)
78 if (!listeners.contains(svl))
80 listeners.addElement(svl);
84 public String alreadyMappedToFile(String pdbid)
88 for (int i = 0; i < mappings.length; i++)
90 if (mappings[i].getPdbId().equals(pdbid))
92 return mappings[i].pdbfile;
100 * create sequence structure mappings between each sequence and the given pdbFile (retrieved via the given protocol).
102 * @param sequence - one or more sequences to be mapped to pdbFile
103 * @param targetChains - optional chain specification for mapping each sequence to pdb (may be nill, individual elements may be nill)
104 * @param pdbFile - structure data resource
105 * @param protocol - how to resolve data from resource
106 * @return null or the structure data parsed as a pdb file
108 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
109 String[] targetChains, String pdbFile, String protocol)
112 * There will be better ways of doing this in the future, for now we'll use
113 * the tried and tested MCview pdb mapping
115 MCview.PDBfile pdb = null;
118 pdb = new MCview.PDBfile(pdbFile, protocol);
119 } catch (Exception ex)
121 ex.printStackTrace();
126 for (int s = 0; s < sequence.length; s++)
128 if (targetChains != null && targetChains[s] != null)
129 targetChain = targetChains[s];
130 else if (sequence[s].getName().indexOf("|") > -1)
132 targetChain = sequence[s].getName().substring(
133 sequence[s].getName().lastIndexOf("|") + 1);
139 AlignSeq maxAlignseq = null;
140 String maxChainId = " ";
141 PDBChain maxChain = null;
143 for (int i = 0; i < pdb.chains.size(); i++)
145 // TODO: re http://issues.jalview.org/browse/JAL-583 : this patch may need to be revoked
146 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
147 if (targetChain.length()>0 && !targetChain.equals(chain.id))
149 continue; // don't try to map chains don't match.
151 // end of patch for limiting computed mappings
152 // TODO: correctly determine sequence type for mixed na/peptide
154 AlignSeq as = new AlignSeq(sequence[s], ((PDBChain) pdb.chains
155 .elementAt(i)).sequence, ((PDBChain) pdb.chains
156 .elementAt(i)).isNa ? AlignSeq.DNA : AlignSeq.PEP);
157 as.calcScoreMatrix();
160 if (first || as.maxscore > max
161 || (as.maxscore == max && chain.id.equals(targetChain)))
167 maxChainId = chain.id;
174 final StringBuffer mappingDetails = new StringBuffer();
175 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
176 + maxChain.sequence.getSequenceAsString());
177 mappingDetails.append("\nNo of residues = "
178 + maxChain.residues.size() + "\n\n");
179 PrintStream ps = new PrintStream(System.out)
181 public void print(String x)
183 mappingDetails.append(x);
186 public void println()
188 mappingDetails.append("\n");
192 maxAlignseq.printAlignment(ps);
194 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
195 + " " + maxAlignseq.seq2end);
196 mappingDetails.append("\nSEQ start/end "
197 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
198 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
200 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
202 maxChain.transferRESNUMFeatures(sequence[s], null);
204 int[][] mapping = new int[sequence[s].getEnd() + 2][2];
210 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
211 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
213 resNum = tmp.resNumber;
214 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
215 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
219 } while (index < maxChain.atoms.size());
221 if (mappings == null)
223 mappings = new StructureMapping[1];
227 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
228 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
232 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
233 pdbFile = "INLINE" + pdb.id;
235 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
236 pdbFile, pdb.id, maxChainId, mapping, mappingDetails
238 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
245 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
247 listeners.removeElement(svl);
252 boolean removeMapping = true;
254 Vector pdbs = new Vector();
255 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
257 StructureListener sl;
258 for (int i = 0; i < listeners.size(); i++)
260 if (listeners.elementAt(i) instanceof StructureListener)
262 sl = (StructureListener) listeners.elementAt(i);
263 handlepdbs = sl.getPdbFile();
264 for (int j = 0; j < handlepdbs.length; j++)
266 if (pdbs.contains(handlepdbs[j]))
268 pdbs.removeElement(handlepdbs[j]);
275 if (pdbs.size()>0 && mappings != null)
277 Vector tmp = new Vector();
278 for (int i = 0; i < mappings.length; i++)
280 if (!pdbs.contains(mappings[i].pdbfile))
282 tmp.addElement(mappings[i]);
286 mappings = new StructureMapping[tmp.size()];
287 tmp.copyInto(mappings);
291 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
293 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
294 SearchResults results = null;
295 for (int i = 0; i < listeners.size(); i++)
297 if (listeners.elementAt(i) instanceof SequenceListener)
301 results = new SearchResults();
304 for (int j = 0; j < mappings.length; j++)
306 if (mappings[j].pdbfile.equals(pdbfile)
307 && mappings[j].pdbchain.equals(chain))
309 indexpos = mappings[j].getSeqPos(pdbResNum);
310 results.addResult(mappings[j].sequence, indexpos, indexpos);
311 // construct highlighted sequence list
312 if (seqmappings != null)
315 Enumeration e = seqmappings.elements();
316 while (e.hasMoreElements())
319 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
320 mappings[j].sequence, indexpos, results);
327 if (results.getSize() > 0)
329 for (int i = 0; i < listeners.size(); i++)
331 Object li = listeners.elementAt(i);
332 if (li instanceof SequenceListener)
333 ((SequenceListener) li).highlightSequence(results);
338 Vector seqmappings = null; // should be a simpler list of mapped seuqence
341 * highlight regions associated with a position (indexpos) in seq
344 * the sequeence that the mouse over occured on
346 * the absolute position being mouseovered in seq (0 to seq.length())
348 * the sequence position (if -1, seq.findPosition is called to
349 * resolve the residue number)
351 public void mouseOverSequence(SequenceI seq, int indexpos, int index)
353 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
354 SearchResults results = null;
356 index = seq.findPosition(indexpos);
357 StructureListener sl;
359 for (int i = 0; i < listeners.size(); i++)
361 if (listeners.elementAt(i) instanceof StructureListener)
363 sl = (StructureListener) listeners.elementAt(i);
368 for (int j = 0; j < mappings.length; j++)
370 if (mappings[j].sequence == seq
371 || mappings[j].sequence == seq.getDatasetSequence())
373 atomNo = mappings[j].getAtomNum(index);
377 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
378 mappings[j].pdbchain, mappings[j].pdbfile);
385 if (relaySeqMappings && hasSequenceListeners
386 && listeners.elementAt(i) instanceof SequenceListener)
389 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
394 results = new SearchResults();
395 if (index >= seq.getStart() && index <= seq.getEnd())
397 // construct highlighted sequence list
399 if (seqmappings != null)
401 Enumeration e = seqmappings.elements();
402 while (e.hasMoreElements())
405 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
406 seq, index, results);
409 // hasSequenceListeners = results.getSize() > 0;
410 if (handlingVamsasMo)
412 // maybe have to resolve seq to a dataset seqeunce...
413 // add in additional direct sequence and/or dataset sequence
415 results.addResult(seq, index, index);
419 if (hasSequenceListeners)
421 ((SequenceListener) listeners.elementAt(i))
422 .highlightSequence(results);
425 else if (listeners.elementAt(i) instanceof VamsasListener
426 && !handlingVamsasMo)
429 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
432 // pass the mouse over and absolute position onto the
434 ((VamsasListener) listeners.elementAt(i))
435 .mouseOver(seq, indexpos);
442 * true if a mouse over event from an external (ie Vamsas) source is being
445 boolean handlingVamsasMo = false;
450 * as mouseOverSequence but only route event to SequenceListeners
454 * in an alignment sequence
456 public void mouseOverVamsasSequence(SequenceI sequenceI, int position)
458 handlingVamsasMo = true;
459 long msg = sequenceI.hashCode() * (1 + position);
463 mouseOverSequence(sequenceI, position, -1);
465 handlingVamsasMo = false;
468 public Annotation[] colourSequenceFromStructure(SequenceI seq,
472 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
473 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
475 * Annotation [] annotations = new Annotation[seq.getLength()];
477 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
478 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
479 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
481 * for (int j = 0; j < mappings.length; j++) {
483 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
484 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
485 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
486 * "+mappings[j].pdbfile);
488 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
489 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
491 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
492 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
493 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
494 * mappings[j].pdbfile); }
496 * annotations[index] = new Annotation("X",null,' ',0,col); } return
497 * annotations; } } } }
499 * return annotations;
503 public void structureSelectionChanged()
507 public void sequenceSelectionChanged()
511 public void sequenceColoursChanged(Object source)
513 StructureListener sl;
514 for (int i = 0; i < listeners.size(); i++)
516 if (listeners.elementAt(i) instanceof StructureListener)
518 sl = (StructureListener) listeners.elementAt(i);
519 sl.updateColours(source);
524 public StructureMapping[] getMapping(String pdbfile)
526 Vector tmp = new Vector();
527 for (int i = 0; i < mappings.length; i++)
529 if (mappings[i].pdbfile.equals(pdbfile))
531 tmp.addElement(mappings[i]);
535 StructureMapping[] ret = new StructureMapping[tmp.size()];
536 for (int i = 0; i < tmp.size(); i++)
538 ret[i] = (StructureMapping) tmp.elementAt(i);
544 public String printMapping(String pdbfile)
546 StringBuffer sb = new StringBuffer();
547 for (int i = 0; i < mappings.length; i++)
549 if (mappings[i].pdbfile.equals(pdbfile))
551 sb.append(mappings[i].mappingDetails);
555 return sb.toString();
558 private int[] seqmappingrefs = null; // refcount for seqmappings elements
560 private synchronized void modifySeqMappingList(boolean add,
561 AlignedCodonFrame[] codonFrames)
563 if (!add && (seqmappings == null || seqmappings.size() == 0))
565 if (seqmappings == null)
566 seqmappings = new Vector();
567 if (codonFrames != null && codonFrames.length > 0)
569 for (int cf = 0; cf < codonFrames.length; cf++)
571 if (seqmappings.contains(codonFrames[cf]))
575 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
579 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
581 int pos = seqmappings.indexOf(codonFrames[cf]);
582 int[] nr = new int[seqmappingrefs.length - 1];
585 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
587 if (pos < seqmappingrefs.length - 1)
589 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
590 seqmappingrefs.length - pos - 2);
599 seqmappings.addElement(codonFrames[cf]);
601 int[] nsr = new int[(seqmappingrefs == null) ? 1
602 : seqmappingrefs.length + 1];
603 if (seqmappingrefs != null && seqmappingrefs.length > 0)
604 System.arraycopy(seqmappingrefs, 0, nsr, 0,
605 seqmappingrefs.length);
606 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
607 seqmappingrefs = nsr;
614 public void removeMappings(AlignedCodonFrame[] codonFrames)
616 modifySeqMappingList(false, codonFrames);
619 public void addMappings(AlignedCodonFrame[] codonFrames)
621 modifySeqMappingList(true, codonFrames);
624 Vector sel_listeners = new Vector();
626 public void addSelectionListener(SelectionListener selecter)
628 if (!sel_listeners.contains(selecter))
630 sel_listeners.addElement(selecter);
634 public void removeSelectionListener(SelectionListener toremove)
636 if (sel_listeners.contains(toremove))
638 sel_listeners.removeElement(toremove);
642 public synchronized void sendSelection(
643 jalview.datamodel.SequenceGroup selection,
644 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
646 if (sel_listeners != null && sel_listeners.size() > 0)
648 Enumeration listeners = sel_listeners.elements();
649 while (listeners.hasMoreElements())
651 SelectionListener slis = ((SelectionListener) listeners
655 slis.selection(selection, colsel, source);