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.datamodel.AlignmentI;
25 import jalview.datamodel.PDBEntry;
26 import jalview.datamodel.SequenceI;
27 import jalview.structure.AtomSpec;
28 import jalview.structure.StructureListener;
29 import jalview.structure.StructureMapping;
30 import jalview.structure.StructureSelectionManager;
31 import jalview.util.Comparison;
32 import jalview.util.MessageManager;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
40 * A base class to hold common function for protein structure model binding.
41 * Initial version created by refactoring JMol and Chimera binding models, but
42 * other structure viewers could in principle be accommodated in future.
47 public abstract class AAStructureBindingModel extends
48 SequenceStructureBindingModel implements StructureListener,
49 StructureSelectionManagerProvider
52 private StructureSelectionManager ssm;
55 * distinct PDB entries (pdb files) associated
58 private PDBEntry[] pdbEntry;
61 * sequences mapped to each pdbentry
63 private SequenceI[][] sequence;
66 * array of target chains for sequences - tied to pdbentry and sequence[]
68 private String[][] chains;
71 * datasource protocol for access to PDBEntrylatest
73 String protocol = null;
75 protected boolean colourBySequence = true;
77 private boolean nucleotide;
79 private boolean finishedInit = false;
82 * current set of model filenames loaded in the Jmol instance
84 protected String[] modelFileNames = null;
87 * Data bean class to simplify parameterisation in superposeStructures
89 protected class SuperposeData
92 * Constructor with alignment width argument
96 public SuperposeData(int width)
98 pdbResNo = new int[width];
101 public String filename;
105 public String chain = "";
107 public boolean isRna;
110 * The pdb residue number (if any) mapped to each column of the alignment
112 public int[] pdbResNo;
121 public AAStructureBindingModel(StructureSelectionManager ssm,
125 this.sequence = seqs;
137 public AAStructureBindingModel(StructureSelectionManager ssm,
138 PDBEntry[] pdbentry, SequenceI[][] sequenceIs,
142 this.sequence = sequenceIs;
143 this.nucleotide = Comparison.isNucleotide(sequenceIs);
144 this.pdbEntry = pdbentry;
145 this.protocol = protocol;
148 public StructureSelectionManager getSsm()
154 * Returns the i'th PDBEntry (or null)
159 public PDBEntry getPdbEntry(int i)
161 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
165 * Answers true if this binding includes the given PDB id, else false
170 public boolean hasPdbId(String pdbId)
172 if (pdbEntry != null)
174 for (PDBEntry pdb : pdbEntry)
176 if (pdb.getId().equals(pdbId))
186 * Returns the number of modelled PDB file entries.
190 public int getPdbCount()
192 return pdbEntry == null ? 0 : pdbEntry.length;
195 public SequenceI[][] getSequence()
200 public String[][] getChains()
205 public String getProtocol()
210 // TODO may remove this if calling methods can be pulled up here
211 protected void setPdbentry(PDBEntry[] pdbentry)
213 this.pdbEntry = pdbentry;
216 protected void setSequence(SequenceI[][] sequence)
218 this.sequence = sequence;
221 protected void setChains(String[][] chains)
223 this.chains = chains;
227 * Construct a title string for the viewer window based on the data Jalview
236 public String getViewerTitle(String viewerName, boolean verbose)
238 if (getSequence() == null || getSequence().length < 1
239 || getPdbCount() < 1 || getSequence()[0].length < 1)
241 return ("Jalview " + viewerName + " Window");
243 // TODO: give a more informative title when multiple structures are
245 StringBuilder title = new StringBuilder(64);
246 final PDBEntry pdbe = getPdbEntry(0);
247 title.append(viewerName + " view for " + getSequence()[0][0].getName()
248 + ":" + pdbe.getId());
252 String method = (String) pdbe.getProperty("method");
255 title.append(" Method: ").append(method);
257 String chain = (String) pdbe.getProperty("chains");
260 title.append(" Chain:").append(chain);
263 return title.toString();
267 * Called by after closeViewer is called, to release any resources and
268 * references so they can be garbage collected. Override if needed.
270 protected void releaseUIResources()
275 public boolean isColourBySequence()
277 return colourBySequence;
280 public void setColourBySequence(boolean colourBySequence)
282 this.colourBySequence = colourBySequence;
285 protected void addSequenceAndChain(int pe, SequenceI[] seq,
288 if (pe < 0 || pe >= getPdbCount())
290 throw new Error(MessageManager.formatMessage(
291 "error.implementation_error_no_pdbentry_from_index",
292 new Object[] { Integer.valueOf(pe).toString() }));
294 final String nullChain = "TheNullChain";
295 List<SequenceI> s = new ArrayList<SequenceI>();
296 List<String> c = new ArrayList<String>();
297 if (getChains() == null)
299 setChains(new String[getPdbCount()][]);
301 if (getSequence()[pe] != null)
303 for (int i = 0; i < getSequence()[pe].length; i++)
305 s.add(getSequence()[pe][i]);
306 if (getChains()[pe] != null)
308 if (i < getChains()[pe].length)
310 c.add(getChains()[pe][i]);
319 if (tchain != null && tchain.length > 0)
326 for (int i = 0; i < seq.length; i++)
328 if (!s.contains(seq[i]))
331 if (tchain != null && i < tchain.length)
333 c.add(tchain[i] == null ? nullChain : tchain[i]);
337 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
338 getSequence()[pe] = tmp;
341 String[] tch = c.toArray(new String[c.size()]);
342 for (int i = 0; i < tch.length; i++)
344 if (tch[i] == nullChain)
349 getChains()[pe] = tch;
353 getChains()[pe] = null;
358 * add structures and any known sequence associations
360 * @returns the pdb entries added to the current set.
362 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
363 SequenceI[][] seq, String[][] chns)
365 List<PDBEntry> v = new ArrayList<PDBEntry>();
366 List<int[]> rtn = new ArrayList<int[]>();
367 for (int i = 0; i < getPdbCount(); i++)
369 v.add(getPdbEntry(i));
371 for (int i = 0; i < pdbe.length; i++)
373 int r = v.indexOf(pdbe[i]);
374 if (r == -1 || r >= getPdbCount())
376 rtn.add(new int[] { v.size(), i });
381 // just make sure the sequence/chain entries are all up to date
382 addSequenceAndChain(r, seq[i], chns[i]);
385 pdbe = v.toArray(new PDBEntry[v.size()]);
389 // expand the tied sequence[] and string[] arrays
390 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
391 String[][] sch = new String[getPdbCount()][];
392 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
393 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
396 pdbe = new PDBEntry[rtn.size()];
397 for (int r = 0; r < pdbe.length; r++)
399 int[] stri = (rtn.get(r));
400 // record the pdb file as a new addition
401 pdbe[r] = getPdbEntry(stri[0]);
402 // and add the new sequence/chain entries
403 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
414 * Add sequences to the pe'th pdbentry's sequence set.
419 public void addSequence(int pe, SequenceI[] seq)
421 addSequenceAndChain(pe, seq, null);
425 * add the given sequences to the mapping scope for the given pdb file handle
428 * - pdbFile identifier
430 * - set of sequences it can be mapped to
432 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
434 for (int pe = 0; pe < getPdbCount(); pe++)
436 if (getPdbEntry(pe).getFile().equals(pdbFile))
438 addSequence(pe, seq);
444 public abstract void highlightAtoms(List<AtomSpec> atoms);
446 protected boolean isNucleotide()
448 return this.nucleotide;
452 * Returns a readable description of all mappings for the wrapped pdbfile to
453 * any mapped sequences
459 public String printMappings()
461 if (pdbEntry == null)
465 StringBuilder sb = new StringBuilder(128);
466 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
468 String pdbfile = getPdbEntry(pdbe).getFile();
469 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
470 sb.append(getSsm().printMappings(pdbfile, seqs));
472 return sb.toString();
476 * Returns the mapped structure position for a given aligned column of a given
477 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
478 * not mapped to structure.
485 protected int getMappedPosition(SequenceI seq, int alignedPos,
486 StructureMapping mapping)
488 if (alignedPos >= seq.getLength())
493 if (Comparison.isGap(seq.getCharAt(alignedPos)))
497 int seqPos = seq.findPosition(alignedPos);
498 int pos = mapping.getPDBResNum(seqPos);
503 * Helper method to identify residues that can participate in a structure
504 * superposition command. For each structure, identify a sequence in the
505 * alignment which is mapped to the structure. Identify non-gapped columns in
506 * the sequence which have a mapping to a residue in the structure. Returns
507 * the index of the first structure that has a mapping to the alignment.
510 * the sequence alignment which is the basis of structure
513 * an array of booleans, indexed by alignment column, where true
514 * indicates that every structure has a mapped residue present in the
515 * column (so the column can participate in structure alignment)
517 * an array of data beans corresponding to pdb file index
520 protected int findSuperposableResidues(AlignmentI alignment,
521 boolean[] matched, SuperposeData[] structures)
523 int refStructure = -1;
524 String[] files = getPdbFile();
529 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
531 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
535 * Find the first mapped sequence (if any) for this PDB entry which is in
538 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
539 for (int s = 0; s < seqCountForPdbFile; s++)
541 for (StructureMapping mapping : mappings)
543 final SequenceI theSequence = getSequence()[pdbfnum][s];
544 if (mapping.getSequence() == theSequence
545 && alignment.findIndex(theSequence) > -1)
547 if (refStructure < 0)
549 refStructure = pdbfnum;
551 for (int r = 0; r < matched.length; r++)
557 int pos = getMappedPosition(theSequence, r, mapping);
558 if (pos < 1 || pos == lastPos)
564 structures[pdbfnum].pdbResNo[r] = pos;
566 String chain = mapping.getChain();
567 if (chain != null && chain.trim().length() > 0)
569 structures[pdbfnum].chain = chain;
571 structures[pdbfnum].pdbId = mapping.getPdbId();
572 structures[pdbfnum].isRna = theSequence.getRNA() != null;
575 * move on to next pdb file (ignore sequences for other chains
576 * for the same structure)
578 s = seqCountForPdbFile;
588 * Returns true if the structure viewer has loaded all of the files of
589 * interest (identified by the file mapping having been set up), or false if
590 * any are still not loaded after a timeout interval.
594 protected boolean waitForFileLoad(String[] files)
597 * give up after 10 secs plus 1 sec per file
599 long starttime = System.currentTimeMillis();
600 long endTime = 10000 + 1000 * files.length + starttime;
601 String notLoaded = null;
603 boolean waiting = true;
604 while (waiting && System.currentTimeMillis() < endTime)
607 for (String file : files)
616 StructureMapping[] sm = getSsm().getMapping(file);
617 if (sm == null || sm.length == 0)
621 } catch (Throwable x)
631 .println("Timed out waiting for structure viewer to load file "
639 public boolean isListeningFor(SequenceI seq)
641 if (sequence != null)
643 for (SequenceI[] seqs : sequence)
647 for (SequenceI s : seqs)
650 || (s.getDatasetSequence() != null && s
651 .getDatasetSequence() == seq
652 .getDatasetSequence()))
663 public boolean isFinishedInit()
668 public void setFinishedInit(boolean fi)
670 this.finishedInit = fi;
674 * Returns a list of chains mapped in this viewer.
678 public abstract List<String> getChainNames();