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.io.DataSourceType;
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 DataSourceType 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,
140 DataSourceType protocol)
143 this.sequence = sequenceIs;
144 this.nucleotide = Comparison.isNucleotide(sequenceIs);
145 this.pdbEntry = pdbentry;
146 this.protocol = protocol;
149 public StructureSelectionManager getSsm()
155 * Returns the i'th PDBEntry (or null)
160 public PDBEntry getPdbEntry(int i)
162 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
166 * Answers true if this binding includes the given PDB id, else false
171 public boolean hasPdbId(String pdbId)
173 if (pdbEntry != null)
175 for (PDBEntry pdb : pdbEntry)
177 if (pdb.getId().equals(pdbId))
187 * Returns the number of modelled PDB file entries.
191 public int getPdbCount()
193 return pdbEntry == null ? 0 : pdbEntry.length;
196 public SequenceI[][] getSequence()
201 public String[][] getChains()
206 public DataSourceType getProtocol()
211 // TODO may remove this if calling methods can be pulled up here
212 protected void setPdbentry(PDBEntry[] pdbentry)
214 this.pdbEntry = pdbentry;
217 protected void setSequence(SequenceI[][] sequence)
219 this.sequence = sequence;
222 protected void setChains(String[][] chains)
224 this.chains = chains;
228 * Construct a title string for the viewer window based on the data Jalview
237 public String getViewerTitle(String viewerName, boolean verbose)
239 if (getSequence() == null || getSequence().length < 1
240 || getPdbCount() < 1 || getSequence()[0].length < 1)
242 return ("Jalview " + viewerName + " Window");
244 // TODO: give a more informative title when multiple structures are
246 StringBuilder title = new StringBuilder(64);
247 final PDBEntry pdbe = getPdbEntry(0);
248 title.append(viewerName + " view for " + getSequence()[0][0].getName()
249 + ":" + pdbe.getId());
253 String method = (String) pdbe.getProperty("method");
256 title.append(" Method: ").append(method);
258 String chain = (String) pdbe.getProperty("chains");
261 title.append(" Chain:").append(chain);
264 return title.toString();
268 * Called by after closeViewer is called, to release any resources and
269 * references so they can be garbage collected. Override if needed.
271 protected void releaseUIResources()
276 public boolean isColourBySequence()
278 return colourBySequence;
281 public void setColourBySequence(boolean colourBySequence)
283 this.colourBySequence = colourBySequence;
286 protected void addSequenceAndChain(int pe, SequenceI[] seq,
289 if (pe < 0 || pe >= getPdbCount())
291 throw new Error(MessageManager.formatMessage(
292 "error.implementation_error_no_pdbentry_from_index",
293 new Object[] { Integer.valueOf(pe).toString() }));
295 final String nullChain = "TheNullChain";
296 List<SequenceI> s = new ArrayList<SequenceI>();
297 List<String> c = new ArrayList<String>();
298 if (getChains() == null)
300 setChains(new String[getPdbCount()][]);
302 if (getSequence()[pe] != null)
304 for (int i = 0; i < getSequence()[pe].length; i++)
306 s.add(getSequence()[pe][i]);
307 if (getChains()[pe] != null)
309 if (i < getChains()[pe].length)
311 c.add(getChains()[pe][i]);
320 if (tchain != null && tchain.length > 0)
327 for (int i = 0; i < seq.length; i++)
329 if (!s.contains(seq[i]))
332 if (tchain != null && i < tchain.length)
334 c.add(tchain[i] == null ? nullChain : tchain[i]);
338 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
339 getSequence()[pe] = tmp;
342 String[] tch = c.toArray(new String[c.size()]);
343 for (int i = 0; i < tch.length; i++)
345 if (tch[i] == nullChain)
350 getChains()[pe] = tch;
354 getChains()[pe] = null;
359 * add structures and any known sequence associations
361 * @returns the pdb entries added to the current set.
363 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
364 SequenceI[][] seq, String[][] chns)
366 List<PDBEntry> v = new ArrayList<PDBEntry>();
367 List<int[]> rtn = new ArrayList<int[]>();
368 for (int i = 0; i < getPdbCount(); i++)
370 v.add(getPdbEntry(i));
372 for (int i = 0; i < pdbe.length; i++)
374 int r = v.indexOf(pdbe[i]);
375 if (r == -1 || r >= getPdbCount())
377 rtn.add(new int[] { v.size(), i });
382 // just make sure the sequence/chain entries are all up to date
383 addSequenceAndChain(r, seq[i], chns[i]);
386 pdbe = v.toArray(new PDBEntry[v.size()]);
390 // expand the tied sequence[] and string[] arrays
391 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
392 String[][] sch = new String[getPdbCount()][];
393 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
394 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
397 pdbe = new PDBEntry[rtn.size()];
398 for (int r = 0; r < pdbe.length; r++)
400 int[] stri = (rtn.get(r));
401 // record the pdb file as a new addition
402 pdbe[r] = getPdbEntry(stri[0]);
403 // and add the new sequence/chain entries
404 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
415 * Add sequences to the pe'th pdbentry's sequence set.
420 public void addSequence(int pe, SequenceI[] seq)
422 addSequenceAndChain(pe, seq, null);
426 * add the given sequences to the mapping scope for the given pdb file handle
429 * - pdbFile identifier
431 * - set of sequences it can be mapped to
433 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
435 for (int pe = 0; pe < getPdbCount(); pe++)
437 if (getPdbEntry(pe).getFile().equals(pdbFile))
439 addSequence(pe, seq);
445 public abstract void highlightAtoms(List<AtomSpec> atoms);
447 protected boolean isNucleotide()
449 return this.nucleotide;
453 * Returns a readable description of all mappings for the wrapped pdbfile to
454 * any mapped sequences
460 public String printMappings()
462 if (pdbEntry == null)
466 StringBuilder sb = new StringBuilder(128);
467 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
469 String pdbfile = getPdbEntry(pdbe).getFile();
470 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
471 sb.append(getSsm().printMappings(pdbfile, seqs));
473 return sb.toString();
477 * Returns the mapped structure position for a given aligned column of a given
478 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
479 * not mapped to structure.
486 protected int getMappedPosition(SequenceI seq, int alignedPos,
487 StructureMapping mapping)
489 if (alignedPos >= seq.getLength())
494 if (Comparison.isGap(seq.getCharAt(alignedPos)))
498 int seqPos = seq.findPosition(alignedPos);
499 int pos = mapping.getPDBResNum(seqPos);
504 * Helper method to identify residues that can participate in a structure
505 * superposition command. For each structure, identify a sequence in the
506 * alignment which is mapped to the structure. Identify non-gapped columns in
507 * the sequence which have a mapping to a residue in the structure. Returns
508 * the index of the first structure that has a mapping to the alignment.
511 * the sequence alignment which is the basis of structure
514 * an array of booleans, indexed by alignment column, where true
515 * indicates that every structure has a mapped residue present in the
516 * column (so the column can participate in structure alignment)
518 * an array of data beans corresponding to pdb file index
521 protected int findSuperposableResidues(AlignmentI alignment,
522 boolean[] matched, SuperposeData[] structures)
524 int refStructure = -1;
525 String[] files = getPdbFile();
530 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
532 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
536 * Find the first mapped sequence (if any) for this PDB entry which is in
539 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
540 for (int s = 0; s < seqCountForPdbFile; s++)
542 for (StructureMapping mapping : mappings)
544 final SequenceI theSequence = getSequence()[pdbfnum][s];
545 if (mapping.getSequence() == theSequence
546 && alignment.findIndex(theSequence) > -1)
548 if (refStructure < 0)
550 refStructure = pdbfnum;
552 for (int r = 0; r < matched.length; r++)
558 int pos = getMappedPosition(theSequence, r, mapping);
559 if (pos < 1 || pos == lastPos)
565 structures[pdbfnum].pdbResNo[r] = pos;
567 String chain = mapping.getChain();
568 if (chain != null && chain.trim().length() > 0)
570 structures[pdbfnum].chain = chain;
572 structures[pdbfnum].pdbId = mapping.getPdbId();
573 structures[pdbfnum].isRna = theSequence.getRNA() != null;
576 * move on to next pdb file (ignore sequences for other chains
577 * for the same structure)
579 s = seqCountForPdbFile;
589 * Returns true if the structure viewer has loaded all of the files of
590 * interest (identified by the file mapping having been set up), or false if
591 * any are still not loaded after a timeout interval.
595 protected boolean waitForFileLoad(String[] files)
598 * give up after 10 secs plus 1 sec per file
600 long starttime = System.currentTimeMillis();
601 long endTime = 10000 + 1000 * files.length + starttime;
602 String notLoaded = null;
604 boolean waiting = true;
605 while (waiting && System.currentTimeMillis() < endTime)
608 for (String file : files)
617 StructureMapping[] sm = getSsm().getMapping(file);
618 if (sm == null || sm.length == 0)
622 } catch (Throwable x)
632 .println("Timed out waiting for structure viewer to load file "
640 public boolean isListeningFor(SequenceI seq)
642 if (sequence != null)
644 for (SequenceI[] seqs : sequence)
648 for (SequenceI s : seqs)
651 || (s.getDatasetSequence() != null && s
652 .getDatasetSequence() == seq
653 .getDatasetSequence()))
664 public boolean isFinishedInit()
669 public void setFinishedInit(boolean fi)
671 this.finishedInit = fi;
675 * Returns a list of chains mapped in this viewer.
679 public abstract List<String> getChainNames();