2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.structures.models;
23 import jalview.api.StructureSelectionManagerProvider;
24 import jalview.api.structures.JalviewStructureDisplayI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.PDBEntry;
27 import jalview.datamodel.SequenceI;
28 import jalview.structure.AtomSpec;
29 import jalview.structure.StructureListener;
30 import jalview.structure.StructureMapping;
31 import jalview.structure.StructureSelectionManager;
32 import jalview.util.Comparison;
33 import jalview.util.MessageManager;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
41 * A base class to hold common function for protein structure model binding.
42 * Initial version created by refactoring JMol and Chimera binding models, but
43 * other structure viewers could in principle be accommodated in future.
48 public abstract class AAStructureBindingModel extends
49 SequenceStructureBindingModel implements StructureListener,
50 StructureSelectionManagerProvider
53 private StructureSelectionManager ssm;
56 * distinct PDB entries (pdb files) associated
59 private PDBEntry[] pdbEntry;
62 * sequences mapped to each pdbentry
64 private SequenceI[][] sequence;
67 * array of target chains for sequences - tied to pdbentry and sequence[]
69 private String[][] chains;
72 * datasource protocol for access to PDBEntrylatest
74 String protocol = null;
76 protected boolean colourBySequence = true;
78 private boolean nucleotide;
80 private boolean finishedInit = false;
83 * current set of model filenames loaded in the Jmol instance
85 protected String[] modelFileNames = null;
88 * Data bean class to simplify parameterisation in superposeStructures
90 protected class SuperposeData
93 * Constructor with alignment width argument
97 public SuperposeData(int width)
99 pdbResNo = new int[width];
102 public String filename;
106 public String chain = "";
108 public boolean isRna;
111 * The pdb residue number (if any) mapped to each column of the alignment
113 public int[] pdbResNo;
122 public AAStructureBindingModel(StructureSelectionManager ssm,
126 this.sequence = seqs;
138 public AAStructureBindingModel(StructureSelectionManager ssm,
139 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
143 this.sequence = sequenceIs;
144 this.nucleotide = Comparison.isNucleotide(sequenceIs);
145 this.chains = chains;
146 this.pdbEntry = pdbentry;
147 this.protocol = protocol;
150 this.chains = new String[pdbentry.length][];
154 public StructureSelectionManager getSsm()
160 * Returns the i'th PDBEntry (or null)
165 public PDBEntry getPdbEntry(int i)
167 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
171 * Answers true if this binding includes the given PDB id, else false
176 public boolean hasPdbId(String pdbId)
178 if (pdbEntry != null)
180 for (PDBEntry pdb : pdbEntry)
182 if (pdb.getId().equals(pdbId))
192 * Returns the number of modelled PDB file entries.
196 public int getPdbCount()
198 return pdbEntry == null ? 0 : pdbEntry.length;
201 public SequenceI[][] getSequence()
206 public String[][] getChains()
211 public String getProtocol()
216 // TODO may remove this if calling methods can be pulled up here
217 protected void setPdbentry(PDBEntry[] pdbentry)
219 this.pdbEntry = pdbentry;
222 protected void setSequence(SequenceI[][] sequence)
224 this.sequence = sequence;
227 protected void setChains(String[][] chains)
229 this.chains = chains;
233 * Construct a title string for the viewer window based on the data Jalview
242 public String getViewerTitle(String viewerName, boolean verbose)
244 if (getSequence() == null || getSequence().length < 1
245 || getPdbCount() < 1 || getSequence()[0].length < 1)
247 return ("Jalview " + viewerName + " Window");
249 // TODO: give a more informative title when multiple structures are
251 StringBuilder title = new StringBuilder(64);
252 final PDBEntry pdbe = getPdbEntry(0);
253 title.append(viewerName + " view for " + getSequence()[0][0].getName()
254 + ":" + pdbe.getId());
258 String method = (String) pdbe.getProperty("method");
261 title.append(" Method: ").append(method);
263 String chain = (String) pdbe.getProperty("chains");
266 title.append(" Chain:").append(chain);
269 return title.toString();
273 * Called by after closeViewer is called, to release any resources and
274 * references so they can be garbage collected. Override if needed.
276 protected void releaseUIResources()
281 public boolean isColourBySequence()
283 return colourBySequence;
286 public void setColourBySequence(boolean colourBySequence)
288 this.colourBySequence = colourBySequence;
291 protected void addSequenceAndChain(int pe, SequenceI[] seq,
294 if (pe < 0 || pe >= getPdbCount())
296 throw new Error(MessageManager.formatMessage(
297 "error.implementation_error_no_pdbentry_from_index",
298 new Object[] { Integer.valueOf(pe).toString() }));
300 final String nullChain = "TheNullChain";
301 List<SequenceI> s = new ArrayList<SequenceI>();
302 List<String> c = new ArrayList<String>();
303 if (getChains() == null)
305 setChains(new String[getPdbCount()][]);
307 if (getSequence()[pe] != null)
309 for (int i = 0; i < getSequence()[pe].length; i++)
311 s.add(getSequence()[pe][i]);
312 if (getChains()[pe] != null)
314 if (i < getChains()[pe].length)
316 c.add(getChains()[pe][i]);
325 if (tchain != null && tchain.length > 0)
332 for (int i = 0; i < seq.length; i++)
334 if (!s.contains(seq[i]))
337 if (tchain != null && i < tchain.length)
339 c.add(tchain[i] == null ? nullChain : tchain[i]);
343 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
344 getSequence()[pe] = tmp;
347 String[] tch = c.toArray(new String[c.size()]);
348 for (int i = 0; i < tch.length; i++)
350 if (tch[i] == nullChain)
355 getChains()[pe] = tch;
359 getChains()[pe] = null;
364 * add structures and any known sequence associations
366 * @returns the pdb entries added to the current set.
368 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
369 SequenceI[][] seq, String[][] chns)
371 List<PDBEntry> v = new ArrayList<PDBEntry>();
372 List<int[]> rtn = new ArrayList<int[]>();
373 for (int i = 0; i < getPdbCount(); i++)
375 v.add(getPdbEntry(i));
377 for (int i = 0; i < pdbe.length; i++)
379 int r = v.indexOf(pdbe[i]);
380 if (r == -1 || r >= getPdbCount())
382 rtn.add(new int[] { v.size(), i });
387 // just make sure the sequence/chain entries are all up to date
388 addSequenceAndChain(r, seq[i], chns[i]);
391 pdbe = v.toArray(new PDBEntry[v.size()]);
395 // expand the tied sequence[] and string[] arrays
396 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
397 String[][] sch = new String[getPdbCount()][];
398 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
399 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
402 pdbe = new PDBEntry[rtn.size()];
403 for (int r = 0; r < pdbe.length; r++)
405 int[] stri = (rtn.get(r));
406 // record the pdb file as a new addition
407 pdbe[r] = getPdbEntry(stri[0]);
408 // and add the new sequence/chain entries
409 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
420 * Add sequences to the pe'th pdbentry's sequence set.
425 public void addSequence(int pe, SequenceI[] seq)
427 addSequenceAndChain(pe, seq, null);
431 * add the given sequences to the mapping scope for the given pdb file handle
434 * - pdbFile identifier
436 * - set of sequences it can be mapped to
438 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
440 for (int pe = 0; pe < getPdbCount(); pe++)
442 if (getPdbEntry(pe).getFile().equals(pdbFile))
444 addSequence(pe, seq);
450 public abstract void highlightAtoms(List<AtomSpec> atoms);
452 protected boolean isNucleotide()
454 return this.nucleotide;
458 * Returns a readable description of all mappings for the wrapped pdbfile to
459 * any mapped sequences
465 public String printMappings()
467 if (pdbEntry == null)
471 StringBuilder sb = new StringBuilder(128);
472 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
474 String pdbfile = getPdbEntry(pdbe).getFile();
475 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
476 sb.append(getSsm().printMappings(pdbfile, seqs));
478 return sb.toString();
482 * Returns the mapped structure position for a given aligned column of a given
483 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
484 * not mapped to structure.
491 protected int getMappedPosition(SequenceI seq, int alignedPos,
492 StructureMapping mapping)
494 if (alignedPos >= seq.getLength())
499 if (Comparison.isGap(seq.getCharAt(alignedPos)))
503 int seqPos = seq.findPosition(alignedPos);
504 int pos = mapping.getPDBResNum(seqPos);
509 * Helper method to identify residues that can participate in a structure
510 * superposition command. For each structure, identify a sequence in the
511 * alignment which is mapped to the structure. Identify non-gapped columns in
512 * the sequence which have a mapping to a residue in the structure. Returns
513 * the index of the first structure that has a mapping to the alignment.
516 * the sequence alignment which is the basis of structure
519 * an array of booleans, indexed by alignment column, where true
520 * indicates that every structure has a mapped residue present in the
521 * column (so the column can participate in structure alignment)
523 * an array of data beans corresponding to pdb file index
526 protected int findSuperposableResidues(AlignmentI alignment,
527 boolean[] matched, SuperposeData[] structures)
529 int refStructure = -1;
530 String[] files = getPdbFile();
535 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
537 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
541 * Find the first mapped sequence (if any) for this PDB entry which is in
544 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
545 for (int s = 0; s < seqCountForPdbFile; s++)
547 for (StructureMapping mapping : mappings)
549 final SequenceI theSequence = getSequence()[pdbfnum][s];
550 if (mapping.getSequence() == theSequence
551 && alignment.findIndex(theSequence) > -1)
553 if (refStructure < 0)
555 refStructure = pdbfnum;
557 for (int r = 0; r < matched.length; r++)
563 int pos = getMappedPosition(theSequence, r, mapping);
564 if (pos < 1 || pos == lastPos)
570 structures[pdbfnum].pdbResNo[r] = pos;
572 String chain = mapping.getChain();
573 if (chain != null && chain.trim().length() > 0)
575 structures[pdbfnum].chain = chain;
577 structures[pdbfnum].pdbId = mapping.getPdbId();
578 structures[pdbfnum].isRna = theSequence.getRNA() != null;
581 * move on to next pdb file (ignore sequences for other chains
582 * for the same structure)
584 s = seqCountForPdbFile;
594 * Returns true if the structure viewer has loaded all of the files of
595 * interest (identified by the file mapping having been set up), or false if
596 * any are still not loaded after a timeout interval.
600 protected boolean waitForFileLoad(String[] files)
603 * give up after 10 secs plus 1 sec per file
605 long starttime = System.currentTimeMillis();
606 long endTime = 10000 + 1000 * files.length + starttime;
607 String notLoaded = null;
609 boolean waiting = true;
610 while (waiting && System.currentTimeMillis() < endTime)
613 for (String file : files)
622 StructureMapping[] sm = getSsm().getMapping(file);
623 if (sm == null || sm.length == 0)
627 } catch (Throwable x)
637 .println("Timed out waiting for structure viewer to load file "
645 public boolean isListeningFor(SequenceI seq)
647 if (sequence != null)
649 for (SequenceI[] seqs : sequence)
653 for (SequenceI s : seqs)
656 || (s.getDatasetSequence() != null && s
657 .getDatasetSequence() == seq
658 .getDatasetSequence()))
669 public boolean isFinishedInit()
674 public void setFinishedInit(boolean fi)
676 this.finishedInit = fi;
680 * Returns the Jalview panel hosting the structure viewer (if any)
684 public JalviewStructureDisplayI getViewer()