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;
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
101 * pdbFile (retrieved via the given protocol).
104 * - one or more sequences to be mapped to pdbFile
105 * @param targetChains
106 * - optional chain specification for mapping each sequence to pdb
107 * (may be nill, individual elements may be nill)
109 * - structure data resource
111 * - how to resolve data from resource
112 * @return null or the structure data parsed as a pdb file
114 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
115 String[] targetChains, String pdbFile, String protocol)
118 * There will be better ways of doing this in the future, for now we'll use
119 * the tried and tested MCview pdb mapping
121 MCview.PDBfile pdb = null;
124 pdb = new MCview.PDBfile(pdbFile, protocol);
125 } catch (Exception ex)
127 ex.printStackTrace();
132 for (int s = 0; s < sequence.length; s++)
134 if (targetChains != null && targetChains[s] != null)
135 targetChain = targetChains[s];
136 else if (sequence[s].getName().indexOf("|") > -1)
138 targetChain = sequence[s].getName().substring(
139 sequence[s].getName().lastIndexOf("|") + 1);
145 AlignSeq maxAlignseq = null;
146 String maxChainId = " ";
147 PDBChain maxChain = null;
148 boolean first = true;
149 for (int i = 0; i < pdb.chains.size(); i++)
151 // TODO: re http://issues.jalview.org/browse/JAL-583 : this patch may
152 // need to be revoked
153 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
154 if (targetChain.length() > 0 && !targetChain.equals(chain.id))
156 continue; // don't try to map chains don't match.
158 // end of patch for limiting computed mappings
159 // TODO: correctly determine sequence type for mixed na/peptide
161 AlignSeq as = new AlignSeq(sequence[s],
162 ((PDBChain) pdb.chains.elementAt(i)).sequence,
163 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
165 as.calcScoreMatrix();
168 if (first || as.maxscore > max
169 || (as.maxscore == max && chain.id.equals(targetChain)))
175 maxChainId = chain.id;
178 if (maxChain == null)
182 final StringBuffer mappingDetails = new StringBuffer();
183 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
184 + maxChain.sequence.getSequenceAsString());
185 mappingDetails.append("\nNo of residues = "
186 + maxChain.residues.size() + "\n\n");
187 PrintStream ps = new PrintStream(System.out)
189 public void print(String x)
191 mappingDetails.append(x);
194 public void println()
196 mappingDetails.append("\n");
200 maxAlignseq.printAlignment(ps);
202 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
203 + " " + maxAlignseq.seq2end);
204 mappingDetails.append("\nSEQ start/end "
205 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
206 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
208 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
210 maxChain.transferRESNUMFeatures(sequence[s], null);
212 int[][] mapping = new int[sequence[s].getEnd() + 2][2];
218 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
219 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
221 resNum = tmp.resNumber;
222 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
223 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
227 } while (index < maxChain.atoms.size());
229 if (mappings == null)
231 mappings = new StructureMapping[1];
235 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
236 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
240 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
241 pdbFile = "INLINE" + pdb.id;
243 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
244 pdbFile, pdb.id, maxChainId, mapping,
245 mappingDetails.toString());
246 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
253 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
255 listeners.removeElement(svl);
256 if (pdbfiles == null)
260 boolean removeMapping = true;
262 Vector pdbs = new Vector();
263 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
265 StructureListener sl;
266 for (int i = 0; i < listeners.size(); i++)
268 if (listeners.elementAt(i) instanceof StructureListener)
270 sl = (StructureListener) listeners.elementAt(i);
271 handlepdbs = sl.getPdbFile();
272 for (int j = 0; j < handlepdbs.length; j++)
274 if (pdbs.contains(handlepdbs[j]))
276 pdbs.removeElement(handlepdbs[j]);
283 if (pdbs.size() > 0 && mappings != null)
285 Vector tmp = new Vector();
286 for (int i = 0; i < mappings.length; i++)
288 if (!pdbs.contains(mappings[i].pdbfile))
290 tmp.addElement(mappings[i]);
294 mappings = new StructureMapping[tmp.size()];
295 tmp.copyInto(mappings);
299 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
301 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
302 SearchResults results = null;
303 for (int i = 0; i < listeners.size(); i++)
305 if (listeners.elementAt(i) instanceof SequenceListener)
309 results = new SearchResults();
312 if (mappings != null)
314 for (int j = 0; j < mappings.length; j++)
316 if (mappings[j].pdbfile.equals(pdbfile)
317 && mappings[j].pdbchain.equals(chain))
319 indexpos = mappings[j].getSeqPos(pdbResNum);
320 results.addResult(mappings[j].sequence, indexpos, indexpos);
321 // construct highlighted sequence list
322 if (seqmappings != null)
325 Enumeration e = seqmappings.elements();
326 while (e.hasMoreElements())
329 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
330 mappings[j].sequence, indexpos, results);
338 if (results.getSize() > 0)
340 for (int i = 0; i < listeners.size(); i++)
342 Object li = listeners.elementAt(i);
343 if (li instanceof SequenceListener)
344 ((SequenceListener) li).highlightSequence(results);
349 Vector seqmappings = null; // should be a simpler list of mapped seuqence
352 * highlight regions associated with a position (indexpos) in seq
355 * the sequeence that the mouse over occured on
357 * the absolute position being mouseovered in seq (0 to seq.length())
359 * the sequence position (if -1, seq.findPosition is called to
360 * resolve the residue number)
362 public void mouseOverSequence(SequenceI seq, int indexpos, int index)
364 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
365 SearchResults results = null;
367 index = seq.findPosition(indexpos);
368 StructureListener sl;
370 for (int i = 0; i < listeners.size(); i++)
372 if (listeners.elementAt(i) instanceof StructureListener)
374 sl = (StructureListener) listeners.elementAt(i);
375 if (mappings == null)
379 for (int j = 0; j < mappings.length; j++)
381 if (mappings[j].sequence == seq
382 || mappings[j].sequence == seq.getDatasetSequence())
384 atomNo = mappings[j].getAtomNum(index);
388 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
389 mappings[j].pdbchain, mappings[j].pdbfile);
396 if (relaySeqMappings && hasSequenceListeners
397 && listeners.elementAt(i) instanceof SequenceListener)
400 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
405 results = new SearchResults();
406 if (index >= seq.getStart() && index <= seq.getEnd())
408 // construct highlighted sequence list
410 if (seqmappings != null)
412 Enumeration e = seqmappings.elements();
413 while (e.hasMoreElements())
416 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
417 seq, index, results);
420 // hasSequenceListeners = results.getSize() > 0;
421 if (handlingVamsasMo)
423 // maybe have to resolve seq to a dataset seqeunce...
424 // add in additional direct sequence and/or dataset sequence
426 results.addResult(seq, index, index);
430 if (hasSequenceListeners)
432 ((SequenceListener) listeners.elementAt(i))
433 .highlightSequence(results);
436 else if (listeners.elementAt(i) instanceof VamsasListener
437 && !handlingVamsasMo)
440 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
443 // pass the mouse over and absolute position onto the
445 ((VamsasListener) listeners.elementAt(i))
446 .mouseOver(seq, indexpos);
453 * true if a mouse over event from an external (ie Vamsas) source is being
456 boolean handlingVamsasMo = false;
461 * as mouseOverSequence but only route event to SequenceListeners
465 * in an alignment sequence
467 public void mouseOverVamsasSequence(SequenceI sequenceI, int position)
469 handlingVamsasMo = true;
470 long msg = sequenceI.hashCode() * (1 + position);
474 mouseOverSequence(sequenceI, position, -1);
476 handlingVamsasMo = false;
479 public Annotation[] colourSequenceFromStructure(SequenceI seq,
483 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
484 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
486 * Annotation [] annotations = new Annotation[seq.getLength()];
488 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
489 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
490 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
492 * for (int j = 0; j < mappings.length; j++) {
494 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
495 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
496 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
497 * "+mappings[j].pdbfile);
499 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
500 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
502 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
503 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
504 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
505 * mappings[j].pdbfile); }
507 * annotations[index] = new Annotation("X",null,' ',0,col); } return
508 * annotations; } } } }
510 * return annotations;
514 public void structureSelectionChanged()
518 public void sequenceSelectionChanged()
522 public void sequenceColoursChanged(Object source)
524 StructureListener sl;
525 for (int i = 0; i < listeners.size(); i++)
527 if (listeners.elementAt(i) instanceof StructureListener)
529 sl = (StructureListener) listeners.elementAt(i);
530 sl.updateColours(source);
535 public StructureMapping[] getMapping(String pdbfile)
537 Vector tmp = new Vector();
538 if (mappings != null)
540 for (int i = 0; i < mappings.length; i++)
542 if (mappings[i].pdbfile.equals(pdbfile))
544 tmp.addElement(mappings[i]);
548 StructureMapping[] ret = new StructureMapping[tmp.size()];
549 for (int i = 0; i < tmp.size(); i++)
551 ret[i] = (StructureMapping) tmp.elementAt(i);
557 public String printMapping(String pdbfile)
559 StringBuffer sb = new StringBuffer();
560 for (int i = 0; i < mappings.length; i++)
562 if (mappings[i].pdbfile.equals(pdbfile))
564 sb.append(mappings[i].mappingDetails);
568 return sb.toString();
571 private int[] seqmappingrefs = null; // refcount for seqmappings elements
573 private synchronized void modifySeqMappingList(boolean add,
574 AlignedCodonFrame[] codonFrames)
576 if (!add && (seqmappings == null || seqmappings.size() == 0))
578 if (seqmappings == null)
579 seqmappings = new Vector();
580 if (codonFrames != null && codonFrames.length > 0)
582 for (int cf = 0; cf < codonFrames.length; cf++)
584 if (seqmappings.contains(codonFrames[cf]))
588 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
592 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
594 int pos = seqmappings.indexOf(codonFrames[cf]);
595 int[] nr = new int[seqmappingrefs.length - 1];
598 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
600 if (pos < seqmappingrefs.length - 1)
602 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
603 seqmappingrefs.length - pos - 2);
612 seqmappings.addElement(codonFrames[cf]);
614 int[] nsr = new int[(seqmappingrefs == null) ? 1
615 : seqmappingrefs.length + 1];
616 if (seqmappingrefs != null && seqmappingrefs.length > 0)
617 System.arraycopy(seqmappingrefs, 0, nsr, 0,
618 seqmappingrefs.length);
619 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
620 seqmappingrefs = nsr;
627 public void removeMappings(AlignedCodonFrame[] codonFrames)
629 modifySeqMappingList(false, codonFrames);
632 public void addMappings(AlignedCodonFrame[] codonFrames)
634 modifySeqMappingList(true, codonFrames);
637 Vector sel_listeners = new Vector();
639 public void addSelectionListener(SelectionListener selecter)
641 if (!sel_listeners.contains(selecter))
643 sel_listeners.addElement(selecter);
647 public void removeSelectionListener(SelectionListener toremove)
649 if (sel_listeners.contains(toremove))
651 sel_listeners.removeElement(toremove);
655 public synchronized void sendSelection(
656 jalview.datamodel.SequenceGroup selection,
657 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
659 if (sel_listeners != null && sel_listeners.size() > 0)
661 Enumeration listeners = sel_listeners.elements();
662 while (listeners.hasMoreElements())
664 SelectionListener slis = ((SelectionListener) listeners
668 slis.selection(selection, colsel, source);