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 * There will be better ways of doing this in the future, for now we'll use
101 * the tried and tested MCview pdb mapping
103 synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
104 String[] targetChains, String pdbFile, String protocol)
106 MCview.PDBfile pdb = null;
109 pdb = new MCview.PDBfile(pdbFile, protocol);
110 } catch (Exception ex)
112 ex.printStackTrace();
117 for (int s = 0; s < sequence.length; s++)
119 if (targetChains != null && targetChains[s] != null)
120 targetChain = targetChains[s];
121 else if (sequence[s].getName().indexOf("|") > -1)
123 targetChain = sequence[s].getName().substring(
124 sequence[s].getName().lastIndexOf("|") + 1);
130 AlignSeq maxAlignseq = null;
131 String maxChainId = " ";
132 PDBChain maxChain = null;
134 for (int i = 0; i < pdb.chains.size(); i++)
136 AlignSeq as = new AlignSeq(sequence[s], ((PDBChain) pdb.chains
137 .elementAt(i)).sequence, AlignSeq.PEP);
138 as.calcScoreMatrix();
140 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
142 if (as.maxscore > max
143 || (as.maxscore == max && chain.id.equals(targetChain)))
148 maxChainId = chain.id;
152 final StringBuffer mappingDetails = new StringBuffer();
153 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
154 + maxChain.sequence.getSequenceAsString());
155 mappingDetails.append("\nNo of residues = "
156 + maxChain.residues.size() + "\n\n");
157 PrintStream ps = new PrintStream(System.out)
159 public void print(String x)
161 mappingDetails.append(x);
164 public void println()
166 mappingDetails.append("\n");
170 maxAlignseq.printAlignment(ps);
172 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
173 + " " + maxAlignseq.seq2end);
174 mappingDetails.append("\nSEQ start/end "
175 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
176 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
178 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
180 maxChain.transferRESNUMFeatures(sequence[s], null);
182 int[][] mapping = new int[sequence[s].getEnd() + 2][2];
188 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
189 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
191 resNum = tmp.resNumber;
192 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
193 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
197 } while (index < maxChain.atoms.size());
199 if (mappings == null)
201 mappings = new StructureMapping[1];
205 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
206 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
210 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
211 pdbFile = "INLINE" + pdb.id;
213 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
214 pdbFile, pdb.id, maxChainId, mapping, mappingDetails
216 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
223 public void removeStructureViewerListener(Object svl, String pdbfile)
225 listeners.removeElement(svl);
227 boolean removeMapping = true;
229 StructureListener sl;
230 for (int i = 0; i < listeners.size(); i++)
232 if (listeners.elementAt(i) instanceof StructureListener)
234 sl = (StructureListener) listeners.elementAt(i);
235 if (sl.getPdbFile().equals(pdbfile))
237 removeMapping = false;
243 if (removeMapping && mappings != null)
245 Vector tmp = new Vector();
246 for (int i = 0; i < mappings.length; i++)
248 if (!mappings[i].pdbfile.equals(pdbfile))
250 tmp.addElement(mappings[i]);
254 mappings = new StructureMapping[tmp.size()];
255 tmp.copyInto(mappings);
259 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
261 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
262 SearchResults results = null;
263 for (int i = 0; i < listeners.size(); i++)
265 if (listeners.elementAt(i) instanceof SequenceListener)
269 results = new SearchResults();
272 for (int j = 0; j < mappings.length; j++)
274 if (mappings[j].pdbfile.equals(pdbfile)
275 && mappings[j].pdbchain.equals(chain))
277 indexpos = mappings[j].getSeqPos(pdbResNum);
278 results.addResult(mappings[j].sequence, indexpos, indexpos);
279 // construct highlighted sequence list
280 if (seqmappings != null)
283 Enumeration e = seqmappings.elements();
284 while (e.hasMoreElements())
287 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
288 mappings[j].sequence, indexpos, results);
295 if (results.getSize() > 0)
297 for (int i = 0; i < listeners.size(); i++)
299 Object li = listeners.elementAt(i);
300 if (li instanceof SequenceListener)
301 ((SequenceListener) li).highlightSequence(results);
306 Vector seqmappings = null; // should be a simpler list of mapped seuqence
309 * highlight regions associated with a position (indexpos) in seq
312 * the sequeence that the mouse over occured on
314 * the absolute position being mouseovered in seq (0 to seq.length())
316 * the sequence position (if -1, seq.findPosition is called to
317 * resolve the residue number)
319 public void mouseOverSequence(SequenceI seq, int indexpos, int index)
321 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
322 SearchResults results = null;
324 index = seq.findPosition(indexpos);
325 StructureListener sl;
327 for (int i = 0; i < listeners.size(); i++)
329 if (listeners.elementAt(i) instanceof StructureListener)
331 sl = (StructureListener) listeners.elementAt(i);
333 for (int j = 0; j < mappings.length; j++)
335 if (mappings[j].sequence == seq
336 || mappings[j].sequence == seq.getDatasetSequence())
338 atomNo = mappings[j].getAtomNum(index);
342 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
343 mappings[j].pdbchain, mappings[j].pdbfile);
350 if (relaySeqMappings && hasSequenceListeners
351 && listeners.elementAt(i) instanceof SequenceListener)
354 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
359 results = new SearchResults();
360 if (index >= seq.getStart() && index <= seq.getEnd())
362 // construct highlighted sequence list
364 if (seqmappings != null)
366 Enumeration e = seqmappings.elements();
367 while (e.hasMoreElements())
370 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
371 seq, index, results);
374 // hasSequenceListeners = results.getSize() > 0;
375 if (handlingVamsasMo)
377 // maybe have to resolve seq to a dataset seqeunce...
378 // add in additional direct sequence and/or dataset sequence
380 results.addResult(seq, index, index);
384 if (hasSequenceListeners)
386 ((SequenceListener) listeners.elementAt(i))
387 .highlightSequence(results);
390 else if (listeners.elementAt(i) instanceof VamsasListener
391 && !handlingVamsasMo)
394 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
397 // pass the mouse over and absolute position onto the
399 ((VamsasListener) listeners.elementAt(i))
400 .mouseOver(seq, indexpos);
407 * true if a mouse over event from an external (ie Vamsas) source is being
410 boolean handlingVamsasMo = false;
415 * as mouseOverSequence but only route event to SequenceListeners
419 * in an alignment sequence
421 public void mouseOverVamsasSequence(SequenceI sequenceI, int position)
423 handlingVamsasMo = true;
424 long msg = sequenceI.hashCode() * (1 + position);
428 mouseOverSequence(sequenceI, position, -1);
430 handlingVamsasMo = false;
433 public Annotation[] colourSequenceFromStructure(SequenceI seq,
437 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
438 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
440 * Annotation [] annotations = new Annotation[seq.getLength()];
442 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
443 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
444 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
446 * for (int j = 0; j < mappings.length; j++) {
448 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
449 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
450 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
451 * "+mappings[j].pdbfile);
453 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
454 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
456 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
457 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
458 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
459 * mappings[j].pdbfile); }
461 * annotations[index] = new Annotation("X",null,' ',0,col); } return
462 * annotations; } } } }
464 * return annotations;
468 public void structureSelectionChanged()
472 public void sequenceSelectionChanged()
476 public void sequenceColoursChanged(Object source)
478 StructureListener sl;
479 for (int i = 0; i < listeners.size(); i++)
481 if (listeners.elementAt(i) instanceof StructureListener)
483 sl = (StructureListener) listeners.elementAt(i);
484 sl.updateColours(source);
489 public StructureMapping[] getMapping(String pdbfile)
491 Vector tmp = new Vector();
492 for (int i = 0; i < mappings.length; i++)
494 if (mappings[i].pdbfile.equals(pdbfile))
496 tmp.addElement(mappings[i]);
500 StructureMapping[] ret = new StructureMapping[tmp.size()];
501 for (int i = 0; i < tmp.size(); i++)
503 ret[i] = (StructureMapping) tmp.elementAt(i);
509 public String printMapping(String pdbfile)
511 StringBuffer sb = new StringBuffer();
512 for (int i = 0; i < mappings.length; i++)
514 if (mappings[i].pdbfile.equals(pdbfile))
516 sb.append(mappings[i].mappingDetails);
520 return sb.toString();
523 private int[] seqmappingrefs = null; // refcount for seqmappings elements
525 private synchronized void modifySeqMappingList(boolean add,
526 AlignedCodonFrame[] codonFrames)
528 if (!add && (seqmappings == null || seqmappings.size() == 0))
530 if (seqmappings == null)
531 seqmappings = new Vector();
532 if (codonFrames != null && codonFrames.length > 0)
534 for (int cf = 0; cf < codonFrames.length; cf++)
536 if (seqmappings.contains(codonFrames[cf]))
540 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
544 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
546 int pos = seqmappings.indexOf(codonFrames[cf]);
547 int[] nr = new int[seqmappingrefs.length - 1];
550 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
552 if (pos < seqmappingrefs.length - 1)
554 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
555 seqmappingrefs.length - pos - 2);
564 seqmappings.addElement(codonFrames[cf]);
566 int[] nsr = new int[(seqmappingrefs == null) ? 1
567 : seqmappingrefs.length + 1];
568 if (seqmappingrefs != null && seqmappingrefs.length > 0)
569 System.arraycopy(seqmappingrefs, 0, nsr, 0,
570 seqmappingrefs.length);
571 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
572 seqmappingrefs = nsr;
579 public void removeMappings(AlignedCodonFrame[] codonFrames)
581 modifySeqMappingList(false, codonFrames);
584 public void addMappings(AlignedCodonFrame[] codonFrames)
586 modifySeqMappingList(true, codonFrames);
589 Vector sel_listeners = new Vector();
591 public void addSelectionListener(SelectionListener selecter)
593 if (!sel_listeners.contains(selecter))
595 sel_listeners.addElement(selecter);
599 public void removeSelectionListener(SelectionListener toremove)
601 if (sel_listeners.contains(toremove))
603 sel_listeners.removeElement(toremove);
607 public synchronized void sendSelection(
608 jalview.datamodel.SequenceGroup selection,
609 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
611 if (sel_listeners != null && sel_listeners.size() > 0)
613 Enumeration listeners = sel_listeners.elements();
614 while (listeners.hasMoreElements())
616 SelectionListener slis = ((SelectionListener) listeners
620 slis.selection(selection, colsel, source);