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.datamodel.*;
27 public class StructureSelectionManager
29 static StructureSelectionManager instance;
31 StructureMapping[] mappings;
34 * debug function - write all mappings to stdout
36 public void reportMapping() {
\r
39 System.err.println("reportMapping: No PDB/Sequence mappings.");
\r
41 System.err.println("reportMapping: There are "+mappings.length+" mappings.");
\r
42 for (int m=0;m<mappings.length;m++)
\r
44 System.err.println("mapping "+m+" : "+mappings[m].pdbfile);
\r
48 Hashtable mappingData = new Hashtable();
50 public static StructureSelectionManager getStructureSelectionManager()
54 instance = new StructureSelectionManager();
61 * flag controlling whether SeqMappings are relayed from received sequence
62 * mouse over events to other sequences
64 boolean relaySeqMappings = true;
67 * Enable or disable relay of seqMapping events to other sequences. You might
68 * want to do this if there are many sequence mappings and the host computer
73 public void setRelaySeqMappings(boolean relay)
75 relaySeqMappings = relay;
79 * get the state of the relay seqMappings flag.
81 * @return true if sequence mouse overs are being relayed to other mapped
84 public boolean isRelaySeqMappingsEnabled()
86 return relaySeqMappings;
89 Vector listeners = new Vector();
92 * register a listener for alignment sequence mouseover events
95 public void addStructureViewerListener(Object svl)
97 if (!listeners.contains(svl))
99 listeners.addElement(svl);
103 public String alreadyMappedToFile(String pdbid)
105 if (mappings != null)
107 for (int i = 0; i < mappings.length; i++)
109 if (mappings[i].getPdbId().equals(pdbid))
111 return mappings[i].pdbfile;
119 * create sequence structure mappings between each sequence and the given
120 * pdbFile (retrieved via the given protocol).
123 * - one or more sequences to be mapped to pdbFile
124 * @param targetChains
125 * - optional chain specification for mapping each sequence to pdb
126 * (may be nill, individual elements may be nill)
128 * - structure data resource
130 * - how to resolve data from resource
131 * @return null or the structure data parsed as a pdb file
133 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
134 String[] targetChains, String pdbFile, String protocol)
137 * There will be better ways of doing this in the future, for now we'll use
138 * the tried and tested MCview pdb mapping
140 MCview.PDBfile pdb = null;
143 pdb = new MCview.PDBfile(pdbFile, protocol);
144 } catch (Exception ex)
146 ex.printStackTrace();
151 for (int s = 0; s < sequence.length; s++)
153 if (targetChains != null && targetChains[s] != null)
154 targetChain = targetChains[s];
155 else if (sequence[s].getName().indexOf("|") > -1)
157 targetChain = sequence[s].getName().substring(
158 sequence[s].getName().lastIndexOf("|") + 1);
164 AlignSeq maxAlignseq = null;
165 String maxChainId = " ";
166 PDBChain maxChain = null;
167 boolean first = true;
168 for (int i = 0; i < pdb.chains.size(); i++)
170 // TODO: re http://issues.jalview.org/browse/JAL-583 : this patch may
171 // need to be revoked
172 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
173 if (targetChain.length() > 0 && !targetChain.equals(chain.id))
175 continue; // don't try to map chains don't match.
177 // end of patch for limiting computed mappings
178 // TODO: correctly determine sequence type for mixed na/peptide
180 AlignSeq as = new AlignSeq(sequence[s],
181 ((PDBChain) pdb.chains.elementAt(i)).sequence,
182 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
184 as.calcScoreMatrix();
187 if (first || as.maxscore > max
188 || (as.maxscore == max && chain.id.equals(targetChain)))
194 maxChainId = chain.id;
197 if (maxChain == null)
201 final StringBuffer mappingDetails = new StringBuffer();
202 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
203 + maxChain.sequence.getSequenceAsString());
204 mappingDetails.append("\nNo of residues = "
205 + maxChain.residues.size() + "\n\n");
206 PrintStream ps = new PrintStream(System.out)
208 public void print(String x)
210 mappingDetails.append(x);
213 public void println()
215 mappingDetails.append("\n");
219 maxAlignseq.printAlignment(ps);
221 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
222 + " " + maxAlignseq.seq2end);
223 mappingDetails.append("\nSEQ start/end "
224 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
225 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
227 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
229 maxChain.transferRESNUMFeatures(sequence[s], null);
231 // allocate enough slots to store the mapping from positions in
232 // sequence[s] to the associated chain
233 int[][] mapping = new int[sequence[s].findPosition(sequence[s].getLength()) + 2][2];
239 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
240 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
242 resNum = tmp.resNumber;
243 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
244 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
248 } while (index < maxChain.atoms.size());
250 if (mappings == null)
252 mappings = new StructureMapping[1];
256 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
257 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
261 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
262 pdbFile = "INLINE" + pdb.id;
264 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
265 pdbFile, pdb.id, maxChainId, mapping,
266 mappingDetails.toString());
267 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
274 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
276 listeners.removeElement(svl);
277 if (svl instanceof SequenceListener)
279 for (int i=0;i<listeners.size();i++)
281 if (listeners.elementAt(i) instanceof StructureListener)
283 ((StructureListener)listeners.elementAt(i)).releaseReferences(svl);
288 if (pdbfiles == null)
292 boolean removeMapping = true;
294 Vector pdbs = new Vector();
295 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
297 StructureListener sl;
298 for (int i = 0; i < listeners.size(); i++)
300 if (listeners.elementAt(i) instanceof StructureListener)
302 sl = (StructureListener) listeners.elementAt(i);
303 handlepdbs = sl.getPdbFile();
304 for (int j = 0; j < handlepdbs.length; j++)
306 if (pdbs.contains(handlepdbs[j]))
308 pdbs.removeElement(handlepdbs[j]);
315 if (pdbs.size() > 0 && mappings != null)
317 Vector tmp = new Vector();
318 for (int i = 0; i < mappings.length; i++)
320 if (!pdbs.contains(mappings[i].pdbfile))
322 tmp.addElement(mappings[i]);
326 mappings = new StructureMapping[tmp.size()];
327 tmp.copyInto(mappings);
331 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
333 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
334 SearchResults results = null;
335 SequenceI lastseq = null;
336 int lastipos = -1, indexpos;
337 for (int i = 0; i < listeners.size(); i++)
339 if (listeners.elementAt(i) instanceof SequenceListener)
343 results = new SearchResults();
345 if (mappings != null)
347 for (int j = 0; j < mappings.length; j++)
349 if (mappings[j].pdbfile.equals(pdbfile)
350 && mappings[j].pdbchain.equals(chain))
352 indexpos = mappings[j].getSeqPos(pdbResNum);
353 if (lastipos != indexpos && lastseq != mappings[j].sequence)
355 results.addResult(mappings[j].sequence, indexpos, indexpos);
357 lastseq = mappings[j].sequence;
358 // construct highlighted sequence list
359 if (seqmappings != null)
362 Enumeration e = seqmappings.elements();
363 while (e.hasMoreElements())
366 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
367 mappings[j].sequence, indexpos, results);
377 if (results.getSize() > 0)
379 for (int i = 0; i < listeners.size(); i++)
381 Object li = listeners.elementAt(i);
382 if (li instanceof SequenceListener)
383 ((SequenceListener) li).highlightSequence(results);
388 Vector seqmappings = null; // should be a simpler list of mapped seuqence
391 * highlight regions associated with a position (indexpos) in seq
394 * the sequeence that the mouse over occured on
396 * the absolute position being mouseovered in seq (0 to seq.length())
398 * the sequence position (if -1, seq.findPosition is called to
399 * resolve the residue number)
401 public void mouseOverSequence(SequenceI seq, int indexpos, int index,
404 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
405 SearchResults results = null;
407 index = seq.findPosition(indexpos);
408 StructureListener sl;
410 for (int i = 0; i < listeners.size(); i++)
412 if (listeners.elementAt(i) instanceof StructureListener)
414 sl = (StructureListener) listeners.elementAt(i);
415 if (mappings == null)
419 for (int j = 0; j < mappings.length; j++)
421 if (mappings[j].sequence == seq
422 || mappings[j].sequence == seq.getDatasetSequence())
424 atomNo = mappings[j].getAtomNum(index);
428 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
429 mappings[j].pdbchain, mappings[j].pdbfile);
436 if (relaySeqMappings && hasSequenceListeners
437 && listeners.elementAt(i) instanceof SequenceListener)
440 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
445 results = new SearchResults();
446 if (index >= seq.getStart() && index <= seq.getEnd())
448 // construct highlighted sequence list
450 if (seqmappings != null)
452 Enumeration e = seqmappings.elements();
453 while (e.hasMoreElements())
456 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
457 seq, index, results);
460 // hasSequenceListeners = results.getSize() > 0;
461 if (handlingVamsasMo)
463 // maybe have to resolve seq to a dataset seqeunce...
464 // add in additional direct sequence and/or dataset sequence
466 results.addResult(seq, index, index);
470 if (hasSequenceListeners)
472 ((SequenceListener) listeners.elementAt(i))
473 .highlightSequence(results);
476 else if (listeners.elementAt(i) instanceof VamsasListener
477 && !handlingVamsasMo)
480 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
483 // pass the mouse over and absolute position onto the
485 ((VamsasListener) listeners.elementAt(i)).mouseOver(seq,
493 * true if a mouse over event from an external (ie Vamsas) source is being
496 boolean handlingVamsasMo = false;
501 * as mouseOverSequence but only route event to SequenceListeners
505 * in an alignment sequence
507 public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
510 handlingVamsasMo = true;
511 long msg = sequenceI.hashCode() * (1 + position);
515 mouseOverSequence(sequenceI, position, -1, source);
517 handlingVamsasMo = false;
520 public Annotation[] colourSequenceFromStructure(SequenceI seq,
524 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
525 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
527 * Annotation [] annotations = new Annotation[seq.getLength()];
529 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
530 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
531 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
533 * for (int j = 0; j < mappings.length; j++) {
535 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
536 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
537 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
538 * "+mappings[j].pdbfile);
540 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
541 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
543 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
544 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
545 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
546 * mappings[j].pdbfile); }
548 * annotations[index] = new Annotation("X",null,' ',0,col); } return
549 * annotations; } } } }
551 * return annotations;
555 public void structureSelectionChanged()
559 public void sequenceSelectionChanged()
563 public void sequenceColoursChanged(Object source)
565 StructureListener sl;
566 for (int i = 0; i < listeners.size(); i++)
568 if (listeners.elementAt(i) instanceof StructureListener)
570 sl = (StructureListener) listeners.elementAt(i);
571 sl.updateColours(source);
576 public StructureMapping[] getMapping(String pdbfile)
578 Vector tmp = new Vector();
579 if (mappings != null)
581 for (int i = 0; i < mappings.length; i++)
583 if (mappings[i].pdbfile.equals(pdbfile))
585 tmp.addElement(mappings[i]);
589 StructureMapping[] ret = new StructureMapping[tmp.size()];
590 for (int i = 0; i < tmp.size(); i++)
592 ret[i] = (StructureMapping) tmp.elementAt(i);
598 public String printMapping(String pdbfile)
600 StringBuffer sb = new StringBuffer();
601 for (int i = 0; i < mappings.length; i++)
603 if (mappings[i].pdbfile.equals(pdbfile))
605 sb.append(mappings[i].mappingDetails);
609 return sb.toString();
612 private int[] seqmappingrefs = null; // refcount for seqmappings elements
614 private synchronized void modifySeqMappingList(boolean add,
615 AlignedCodonFrame[] codonFrames)
617 if (!add && (seqmappings == null || seqmappings.size() == 0))
619 if (seqmappings == null)
620 seqmappings = new Vector();
621 if (codonFrames != null && codonFrames.length > 0)
623 for (int cf = 0; cf < codonFrames.length; cf++)
625 if (seqmappings.contains(codonFrames[cf]))
629 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
633 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
635 int pos = seqmappings.indexOf(codonFrames[cf]);
636 int[] nr = new int[seqmappingrefs.length - 1];
639 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
641 if (pos < seqmappingrefs.length - 1)
643 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
644 seqmappingrefs.length - pos - 2);
653 seqmappings.addElement(codonFrames[cf]);
655 int[] nsr = new int[(seqmappingrefs == null) ? 1
656 : seqmappingrefs.length + 1];
657 if (seqmappingrefs != null && seqmappingrefs.length > 0)
658 System.arraycopy(seqmappingrefs, 0, nsr, 0,
659 seqmappingrefs.length);
660 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
661 seqmappingrefs = nsr;
668 public void removeMappings(AlignedCodonFrame[] codonFrames)
670 modifySeqMappingList(false, codonFrames);
673 public void addMappings(AlignedCodonFrame[] codonFrames)
675 modifySeqMappingList(true, codonFrames);
678 Vector sel_listeners = new Vector();
680 public void addSelectionListener(SelectionListener selecter)
682 if (!sel_listeners.contains(selecter))
684 sel_listeners.addElement(selecter);
688 public void removeSelectionListener(SelectionListener toremove)
690 if (sel_listeners.contains(toremove))
692 sel_listeners.removeElement(toremove);
696 public synchronized void sendSelection(
697 jalview.datamodel.SequenceGroup selection,
698 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
700 if (sel_listeners != null && sel_listeners.size() > 0)
702 Enumeration listeners = sel_listeners.elements();
703 while (listeners.hasMoreElements())
705 SelectionListener slis = ((SelectionListener) listeners
709 slis.selection(selection, colsel, source);