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++)
137 // TODO: correctly determine sequence type for mixed na/peptide structures
138 AlignSeq as = new AlignSeq(sequence[s], ((PDBChain) pdb.chains
139 .elementAt(i)).sequence, ((PDBChain)pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA : AlignSeq.PEP);
140 as.calcScoreMatrix();
142 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
144 if (as.maxscore > max
145 || (as.maxscore == max && chain.id.equals(targetChain)))
150 maxChainId = chain.id;
154 final StringBuffer mappingDetails = new StringBuffer();
155 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
156 + maxChain.sequence.getSequenceAsString());
157 mappingDetails.append("\nNo of residues = "
158 + maxChain.residues.size() + "\n\n");
159 PrintStream ps = new PrintStream(System.out)
161 public void print(String x)
163 mappingDetails.append(x);
166 public void println()
168 mappingDetails.append("\n");
172 maxAlignseq.printAlignment(ps);
174 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
175 + " " + maxAlignseq.seq2end);
176 mappingDetails.append("\nSEQ start/end "
177 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
178 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
180 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
182 maxChain.transferRESNUMFeatures(sequence[s], null);
184 int[][] mapping = new int[sequence[s].getEnd() + 2][2];
190 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
191 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
193 resNum = tmp.resNumber;
194 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
195 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
199 } while (index < maxChain.atoms.size());
201 if (mappings == null)
203 mappings = new StructureMapping[1];
207 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
208 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
212 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
213 pdbFile = "INLINE" + pdb.id;
215 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
216 pdbFile, pdb.id, maxChainId, mapping, mappingDetails
218 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
225 public void removeStructureViewerListener(Object svl, String pdbfile)
227 listeners.removeElement(svl);
229 boolean removeMapping = true;
231 StructureListener sl;
232 for (int i = 0; i < listeners.size(); i++)
234 if (listeners.elementAt(i) instanceof StructureListener)
236 sl = (StructureListener) listeners.elementAt(i);
237 if (sl.getPdbFile().equals(pdbfile))
239 removeMapping = false;
245 if (removeMapping && mappings != null)
247 Vector tmp = new Vector();
248 for (int i = 0; i < mappings.length; i++)
250 if (!mappings[i].pdbfile.equals(pdbfile))
252 tmp.addElement(mappings[i]);
256 mappings = new StructureMapping[tmp.size()];
257 tmp.copyInto(mappings);
261 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
263 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
264 SearchResults results = null;
265 for (int i = 0; i < listeners.size(); i++)
267 if (listeners.elementAt(i) instanceof SequenceListener)
271 results = new SearchResults();
274 for (int j = 0; j < mappings.length; j++)
276 if (mappings[j].pdbfile.equals(pdbfile)
277 && mappings[j].pdbchain.equals(chain))
279 indexpos = mappings[j].getSeqPos(pdbResNum);
280 results.addResult(mappings[j].sequence, indexpos, indexpos);
281 // construct highlighted sequence list
282 if (seqmappings != null)
285 Enumeration e = seqmappings.elements();
286 while (e.hasMoreElements())
289 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
290 mappings[j].sequence, indexpos, results);
297 if (results.getSize() > 0)
299 for (int i = 0; i < listeners.size(); i++)
301 Object li = listeners.elementAt(i);
302 if (li instanceof SequenceListener)
303 ((SequenceListener) li).highlightSequence(results);
308 Vector seqmappings = null; // should be a simpler list of mapped seuqence
311 * highlight regions associated with a position (indexpos) in seq
314 * the sequeence that the mouse over occured on
316 * the absolute position being mouseovered in seq (0 to seq.length())
318 * the sequence position (if -1, seq.findPosition is called to
319 * resolve the residue number)
321 public void mouseOverSequence(SequenceI seq, int indexpos, int index)
323 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
324 SearchResults results = null;
326 index = seq.findPosition(indexpos);
327 StructureListener sl;
329 for (int i = 0; i < listeners.size(); i++)
331 if (listeners.elementAt(i) instanceof StructureListener)
333 sl = (StructureListener) listeners.elementAt(i);
335 for (int j = 0; j < mappings.length; j++)
337 if (mappings[j].sequence == seq
338 || mappings[j].sequence == seq.getDatasetSequence())
340 atomNo = mappings[j].getAtomNum(index);
344 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
345 mappings[j].pdbchain, mappings[j].pdbfile);
352 if (relaySeqMappings && hasSequenceListeners
353 && listeners.elementAt(i) instanceof SequenceListener)
356 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
361 results = new SearchResults();
362 if (index >= seq.getStart() && index <= seq.getEnd())
364 // construct highlighted sequence list
366 if (seqmappings != null)
368 Enumeration e = seqmappings.elements();
369 while (e.hasMoreElements())
372 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
373 seq, index, results);
376 // hasSequenceListeners = results.getSize() > 0;
377 if (handlingVamsasMo)
379 // maybe have to resolve seq to a dataset seqeunce...
380 // add in additional direct sequence and/or dataset sequence
382 results.addResult(seq, index, index);
386 if (hasSequenceListeners)
388 ((SequenceListener) listeners.elementAt(i))
389 .highlightSequence(results);
392 else if (listeners.elementAt(i) instanceof VamsasListener
393 && !handlingVamsasMo)
396 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
399 // pass the mouse over and absolute position onto the
401 ((VamsasListener) listeners.elementAt(i))
402 .mouseOver(seq, indexpos);
409 * true if a mouse over event from an external (ie Vamsas) source is being
412 boolean handlingVamsasMo = false;
417 * as mouseOverSequence but only route event to SequenceListeners
421 * in an alignment sequence
423 public void mouseOverVamsasSequence(SequenceI sequenceI, int position)
425 handlingVamsasMo = true;
426 long msg = sequenceI.hashCode() * (1 + position);
430 mouseOverSequence(sequenceI, position, -1);
432 handlingVamsasMo = false;
435 public Annotation[] colourSequenceFromStructure(SequenceI seq,
439 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
440 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
442 * Annotation [] annotations = new Annotation[seq.getLength()];
444 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
445 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
446 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
448 * for (int j = 0; j < mappings.length; j++) {
450 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
451 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
452 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
453 * "+mappings[j].pdbfile);
455 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
456 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
458 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
459 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
460 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
461 * mappings[j].pdbfile); }
463 * annotations[index] = new Annotation("X",null,' ',0,col); } return
464 * annotations; } } } }
466 * return annotations;
470 public void structureSelectionChanged()
474 public void sequenceSelectionChanged()
478 public void sequenceColoursChanged(Object source)
480 StructureListener sl;
481 for (int i = 0; i < listeners.size(); i++)
483 if (listeners.elementAt(i) instanceof StructureListener)
485 sl = (StructureListener) listeners.elementAt(i);
486 sl.updateColours(source);
491 public StructureMapping[] getMapping(String pdbfile)
493 Vector tmp = new Vector();
494 for (int i = 0; i < mappings.length; i++)
496 if (mappings[i].pdbfile.equals(pdbfile))
498 tmp.addElement(mappings[i]);
502 StructureMapping[] ret = new StructureMapping[tmp.size()];
503 for (int i = 0; i < tmp.size(); i++)
505 ret[i] = (StructureMapping) tmp.elementAt(i);
511 public String printMapping(String pdbfile)
513 StringBuffer sb = new StringBuffer();
514 for (int i = 0; i < mappings.length; i++)
516 if (mappings[i].pdbfile.equals(pdbfile))
518 sb.append(mappings[i].mappingDetails);
522 return sb.toString();
525 private int[] seqmappingrefs = null; // refcount for seqmappings elements
527 private synchronized void modifySeqMappingList(boolean add,
528 AlignedCodonFrame[] codonFrames)
530 if (!add && (seqmappings == null || seqmappings.size() == 0))
532 if (seqmappings == null)
533 seqmappings = new Vector();
534 if (codonFrames != null && codonFrames.length > 0)
536 for (int cf = 0; cf < codonFrames.length; cf++)
538 if (seqmappings.contains(codonFrames[cf]))
542 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
546 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
548 int pos = seqmappings.indexOf(codonFrames[cf]);
549 int[] nr = new int[seqmappingrefs.length - 1];
552 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
554 if (pos < seqmappingrefs.length - 1)
556 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
557 seqmappingrefs.length - pos - 2);
566 seqmappings.addElement(codonFrames[cf]);
568 int[] nsr = new int[(seqmappingrefs == null) ? 1
569 : seqmappingrefs.length + 1];
570 if (seqmappingrefs != null && seqmappingrefs.length > 0)
571 System.arraycopy(seqmappingrefs, 0, nsr, 0,
572 seqmappingrefs.length);
573 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
574 seqmappingrefs = nsr;
581 public void removeMappings(AlignedCodonFrame[] codonFrames)
583 modifySeqMappingList(false, codonFrames);
586 public void addMappings(AlignedCodonFrame[] codonFrames)
588 modifySeqMappingList(true, codonFrames);
591 Vector sel_listeners = new Vector();
593 public void addSelectionListener(SelectionListener selecter)
595 if (!sel_listeners.contains(selecter))
597 sel_listeners.addElement(selecter);
601 public void removeSelectionListener(SelectionListener toremove)
603 if (sel_listeners.contains(toremove))
605 sel_listeners.removeElement(toremove);
609 public synchronized void sendSelection(
610 jalview.datamodel.SequenceGroup selection,
611 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
613 if (sel_listeners != null && sel_listeners.size() > 0)
615 Enumeration listeners = sel_listeners.elements();
616 while (listeners.hasMoreElements())
618 SelectionListener slis = ((SelectionListener) listeners
622 slis.selection(selection, colsel, source);