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
139 AlignSeq as = new AlignSeq(sequence[s], ((PDBChain) pdb.chains
140 .elementAt(i)).sequence, ((PDBChain) pdb.chains
141 .elementAt(i)).isNa ? AlignSeq.DNA : AlignSeq.PEP);
142 as.calcScoreMatrix();
144 PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
146 if (as.maxscore > max
147 || (as.maxscore == max && chain.id.equals(targetChain)))
152 maxChainId = chain.id;
156 final StringBuffer mappingDetails = new StringBuffer();
157 mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
158 + maxChain.sequence.getSequenceAsString());
159 mappingDetails.append("\nNo of residues = "
160 + maxChain.residues.size() + "\n\n");
161 PrintStream ps = new PrintStream(System.out)
163 public void print(String x)
165 mappingDetails.append(x);
168 public void println()
170 mappingDetails.append("\n");
174 maxAlignseq.printAlignment(ps);
176 mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
177 + " " + maxAlignseq.seq2end);
178 mappingDetails.append("\nSEQ start/end "
179 + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
180 + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
182 maxChain.makeExactMapping(maxAlignseq, sequence[s]);
184 maxChain.transferRESNUMFeatures(sequence[s], null);
186 int[][] mapping = new int[sequence[s].getEnd() + 2][2];
192 Atom tmp = (Atom) maxChain.atoms.elementAt(index);
193 if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
195 resNum = tmp.resNumber;
196 mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
197 mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
201 } while (index < maxChain.atoms.size());
203 if (mappings == null)
205 mappings = new StructureMapping[1];
209 StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
210 System.arraycopy(mappings, 0, tmp, 0, mappings.length);
214 if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
215 pdbFile = "INLINE" + pdb.id;
217 mappings[mappings.length - 1] = new StructureMapping(sequence[s],
218 pdbFile, pdb.id, maxChainId, mapping, mappingDetails
220 maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
227 public void removeStructureViewerListener(Object svl, String[] pdbfiles)
229 listeners.removeElement(svl);
234 boolean removeMapping = true;
236 Vector pdbs = new Vector();
237 for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
239 StructureListener sl;
240 for (int i = 0; i < listeners.size(); i++)
242 if (listeners.elementAt(i) instanceof StructureListener)
244 sl = (StructureListener) listeners.elementAt(i);
245 handlepdbs = sl.getPdbFile();
246 for (int j = 0; j < handlepdbs.length; j++)
248 if (pdbs.contains(handlepdbs[j]))
250 pdbs.removeElement(handlepdbs[j]);
257 if (pdbs.size()>0 && mappings != null)
259 Vector tmp = new Vector();
260 for (int i = 0; i < mappings.length; i++)
262 if (!pdbs.contains(mappings[i].pdbfile))
264 tmp.addElement(mappings[i]);
268 mappings = new StructureMapping[tmp.size()];
269 tmp.copyInto(mappings);
273 public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
275 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
276 SearchResults results = null;
277 for (int i = 0; i < listeners.size(); i++)
279 if (listeners.elementAt(i) instanceof SequenceListener)
283 results = new SearchResults();
286 for (int j = 0; j < mappings.length; j++)
288 if (mappings[j].pdbfile.equals(pdbfile)
289 && mappings[j].pdbchain.equals(chain))
291 indexpos = mappings[j].getSeqPos(pdbResNum);
292 results.addResult(mappings[j].sequence, indexpos, indexpos);
293 // construct highlighted sequence list
294 if (seqmappings != null)
297 Enumeration e = seqmappings.elements();
298 while (e.hasMoreElements())
301 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
302 mappings[j].sequence, indexpos, results);
309 if (results.getSize() > 0)
311 for (int i = 0; i < listeners.size(); i++)
313 Object li = listeners.elementAt(i);
314 if (li instanceof SequenceListener)
315 ((SequenceListener) li).highlightSequence(results);
320 Vector seqmappings = null; // should be a simpler list of mapped seuqence
323 * highlight regions associated with a position (indexpos) in seq
326 * the sequeence that the mouse over occured on
328 * the absolute position being mouseovered in seq (0 to seq.length())
330 * the sequence position (if -1, seq.findPosition is called to
331 * resolve the residue number)
333 public void mouseOverSequence(SequenceI seq, int indexpos, int index)
335 boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
336 SearchResults results = null;
338 index = seq.findPosition(indexpos);
339 StructureListener sl;
341 for (int i = 0; i < listeners.size(); i++)
343 if (listeners.elementAt(i) instanceof StructureListener)
345 sl = (StructureListener) listeners.elementAt(i);
347 for (int j = 0; j < mappings.length; j++)
349 if (mappings[j].sequence == seq
350 || mappings[j].sequence == seq.getDatasetSequence())
352 atomNo = mappings[j].getAtomNum(index);
356 sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
357 mappings[j].pdbchain, mappings[j].pdbfile);
364 if (relaySeqMappings && hasSequenceListeners
365 && listeners.elementAt(i) instanceof SequenceListener)
368 // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
373 results = new SearchResults();
374 if (index >= seq.getStart() && index <= seq.getEnd())
376 // construct highlighted sequence list
378 if (seqmappings != null)
380 Enumeration e = seqmappings.elements();
381 while (e.hasMoreElements())
384 ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
385 seq, index, results);
388 // hasSequenceListeners = results.getSize() > 0;
389 if (handlingVamsasMo)
391 // maybe have to resolve seq to a dataset seqeunce...
392 // add in additional direct sequence and/or dataset sequence
394 results.addResult(seq, index, index);
398 if (hasSequenceListeners)
400 ((SequenceListener) listeners.elementAt(i))
401 .highlightSequence(results);
404 else if (listeners.elementAt(i) instanceof VamsasListener
405 && !handlingVamsasMo)
408 // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
411 // pass the mouse over and absolute position onto the
413 ((VamsasListener) listeners.elementAt(i))
414 .mouseOver(seq, indexpos);
421 * true if a mouse over event from an external (ie Vamsas) source is being
424 boolean handlingVamsasMo = false;
429 * as mouseOverSequence but only route event to SequenceListeners
433 * in an alignment sequence
435 public void mouseOverVamsasSequence(SequenceI sequenceI, int position)
437 handlingVamsasMo = true;
438 long msg = sequenceI.hashCode() * (1 + position);
442 mouseOverSequence(sequenceI, position, -1);
444 handlingVamsasMo = false;
447 public Annotation[] colourSequenceFromStructure(SequenceI seq,
451 // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
452 // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
454 * Annotation [] annotations = new Annotation[seq.getLength()];
456 * StructureListener sl; int atomNo = 0; for (int i = 0; i <
457 * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
458 * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
460 * for (int j = 0; j < mappings.length; j++) {
462 * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
463 * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
464 * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
465 * "+mappings[j].pdbfile);
467 * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
468 * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
470 * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
471 * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
472 * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
473 * mappings[j].pdbfile); }
475 * annotations[index] = new Annotation("X",null,' ',0,col); } return
476 * annotations; } } } }
478 * return annotations;
482 public void structureSelectionChanged()
486 public void sequenceSelectionChanged()
490 public void sequenceColoursChanged(Object source)
492 StructureListener sl;
493 for (int i = 0; i < listeners.size(); i++)
495 if (listeners.elementAt(i) instanceof StructureListener)
497 sl = (StructureListener) listeners.elementAt(i);
498 sl.updateColours(source);
503 public StructureMapping[] getMapping(String pdbfile)
505 Vector tmp = new Vector();
506 for (int i = 0; i < mappings.length; i++)
508 if (mappings[i].pdbfile.equals(pdbfile))
510 tmp.addElement(mappings[i]);
514 StructureMapping[] ret = new StructureMapping[tmp.size()];
515 for (int i = 0; i < tmp.size(); i++)
517 ret[i] = (StructureMapping) tmp.elementAt(i);
523 public String printMapping(String pdbfile)
525 StringBuffer sb = new StringBuffer();
526 for (int i = 0; i < mappings.length; i++)
528 if (mappings[i].pdbfile.equals(pdbfile))
530 sb.append(mappings[i].mappingDetails);
534 return sb.toString();
537 private int[] seqmappingrefs = null; // refcount for seqmappings elements
539 private synchronized void modifySeqMappingList(boolean add,
540 AlignedCodonFrame[] codonFrames)
542 if (!add && (seqmappings == null || seqmappings.size() == 0))
544 if (seqmappings == null)
545 seqmappings = new Vector();
546 if (codonFrames != null && codonFrames.length > 0)
548 for (int cf = 0; cf < codonFrames.length; cf++)
550 if (seqmappings.contains(codonFrames[cf]))
554 seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
558 if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
560 int pos = seqmappings.indexOf(codonFrames[cf]);
561 int[] nr = new int[seqmappingrefs.length - 1];
564 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
566 if (pos < seqmappingrefs.length - 1)
568 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
569 seqmappingrefs.length - pos - 2);
578 seqmappings.addElement(codonFrames[cf]);
580 int[] nsr = new int[(seqmappingrefs == null) ? 1
581 : seqmappingrefs.length + 1];
582 if (seqmappingrefs != null && seqmappingrefs.length > 0)
583 System.arraycopy(seqmappingrefs, 0, nsr, 0,
584 seqmappingrefs.length);
585 nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
586 seqmappingrefs = nsr;
593 public void removeMappings(AlignedCodonFrame[] codonFrames)
595 modifySeqMappingList(false, codonFrames);
598 public void addMappings(AlignedCodonFrame[] codonFrames)
600 modifySeqMappingList(true, codonFrames);
603 Vector sel_listeners = new Vector();
605 public void addSelectionListener(SelectionListener selecter)
607 if (!sel_listeners.contains(selecter))
609 sel_listeners.addElement(selecter);
613 public void removeSelectionListener(SelectionListener toremove)
615 if (sel_listeners.contains(toremove))
617 sel_listeners.removeElement(toremove);
621 public synchronized void sendSelection(
622 jalview.datamodel.SequenceGroup selection,
623 jalview.datamodel.ColumnSelection colsel, SelectionSource source)
625 if (sel_listeners != null && sel_listeners.size() > 0)
627 Enumeration listeners = sel_listeners.elements();
628 while (listeners.hasMoreElements())
630 SelectionListener slis = ((SelectionListener) listeners
634 slis.selection(selection, colsel, source);