1 package jalview.structures.models;
3 import java.util.ArrayList;
4 import java.util.Arrays;
7 import jalview.api.StructureSelectionManagerProvider;
8 import jalview.datamodel.AlignmentI;
9 import jalview.datamodel.PDBEntry;
10 import jalview.datamodel.SequenceI;
11 import jalview.structure.AtomSpec;
12 import jalview.structure.StructureListener;
13 import jalview.structure.StructureMapping;
14 import jalview.structure.StructureSelectionManager;
15 import jalview.util.Comparison;
16 import jalview.util.MessageManager;
20 * A base class to hold common function for protein structure model binding.
21 * Initial version created by refactoring JMol and Chimera binding models, but
22 * other structure viewers could in principle be accommodated in future.
27 public abstract class AAStructureBindingModel extends
28 SequenceStructureBindingModel implements StructureListener,
29 StructureSelectionManagerProvider
32 private StructureSelectionManager ssm;
34 private PDBEntry[] pdbEntry;
37 * sequences mapped to each pdbentry
39 private SequenceI[][] sequence;
42 * array of target chains for sequences - tied to pdbentry and sequence[]
44 private String[][] chains;
47 * datasource protocol for access to PDBEntrylatest
49 String protocol = null;
51 protected boolean colourBySequence = true;
53 private boolean nucleotide;
56 * Data bean class to simplify parameterisation in superposeStructures
58 protected class SuperposeData
61 * Constructor with alignment width argument
65 public SuperposeData(int width)
67 pdbResNo = new int[width];
70 public String filename;
74 public String chain = "";
79 * The pdb residue number (if any) mapped to each column of the alignment
81 public int[] pdbResNo;
90 public AAStructureBindingModel(StructureSelectionManager ssm,
106 public AAStructureBindingModel(StructureSelectionManager ssm,
107 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
111 this.sequence = sequenceIs;
112 this.nucleotide = Comparison.isNucleotide(sequenceIs);
113 this.chains = chains;
114 this.pdbEntry = pdbentry;
115 this.protocol = protocol;
118 this.chains = new String[pdbentry.length][];
122 public StructureSelectionManager getSsm()
128 * Returns the i'th PDBEntry (or null)
133 public PDBEntry getPdbEntry(int i)
135 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
139 * Answers true if this binding includes the given PDB id, else false
144 public boolean hasPdbId(String pdbId)
146 if (pdbEntry != null)
148 for (PDBEntry pdb : pdbEntry)
150 if (pdb.getId().equals(pdbId))
160 * Returns the number of modelled PDB file entries.
164 public int getPdbCount()
166 return pdbEntry == null ? 0 : pdbEntry.length;
169 public SequenceI[][] getSequence()
174 public String[][] getChains()
179 public String getProtocol()
184 // TODO may remove this if calling methods can be pulled up here
185 protected void setPdbentry(PDBEntry[] pdbentry)
187 this.pdbEntry = pdbentry;
190 protected void setSequence(SequenceI[][] sequence)
192 this.sequence = sequence;
195 protected void setChains(String[][] chains)
197 this.chains = chains;
201 * Construct a title string for the viewer window based on the data Jalview
203 * @param viewerName TODO
208 public String getViewerTitle(String viewerName, boolean verbose)
210 if (getSequence() == null || getSequence().length < 1
212 || getSequence()[0].length < 1)
214 return ("Jalview " + viewerName + " Window");
216 // TODO: give a more informative title when multiple structures are
218 StringBuilder title = new StringBuilder(64);
219 final PDBEntry pdbEntry = getPdbEntry(0);
220 title.append(viewerName + " view for " + getSequence()[0][0].getName()
226 if (pdbEntry.getProperty() != null)
228 if (pdbEntry.getProperty().get("method") != null)
230 title.append(" Method: ");
231 title.append(pdbEntry.getProperty().get("method"));
233 if (pdbEntry.getProperty().get("chains") != null)
235 title.append(" Chain:");
236 title.append(pdbEntry.getProperty().get("chains"));
240 return title.toString();
244 * Called by after closeViewer is called, to release any resources and
245 * references so they can be garbage collected. Override if needed.
247 protected void releaseUIResources()
252 public boolean isColourBySequence()
254 return colourBySequence;
257 public void setColourBySequence(boolean colourBySequence)
259 this.colourBySequence = colourBySequence;
262 protected void addSequenceAndChain(int pe, SequenceI[] seq,
265 if (pe < 0 || pe >= getPdbCount())
267 throw new Error(MessageManager.formatMessage(
268 "error.implementation_error_no_pdbentry_from_index",
270 { Integer.valueOf(pe).toString() }));
272 final String nullChain = "TheNullChain";
273 List<SequenceI> s = new ArrayList<SequenceI>();
274 List<String> c = new ArrayList<String>();
275 if (getChains() == null)
277 setChains(new String[getPdbCount()][]);
279 if (getSequence()[pe] != null)
281 for (int i = 0; i < getSequence()[pe].length; i++)
283 s.add(getSequence()[pe][i]);
284 if (getChains()[pe] != null)
286 if (i < getChains()[pe].length)
288 c.add(getChains()[pe][i]);
297 if (tchain != null && tchain.length > 0)
304 for (int i = 0; i < seq.length; i++)
306 if (!s.contains(seq[i]))
309 if (tchain != null && i < tchain.length)
311 c.add(tchain[i] == null ? nullChain : tchain[i]);
315 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
316 getSequence()[pe] = tmp;
319 String[] tch = c.toArray(new String[c.size()]);
320 for (int i = 0; i < tch.length; i++)
322 if (tch[i] == nullChain)
327 getChains()[pe] = tch;
331 getChains()[pe] = null;
336 * add structures and any known sequence associations
338 * @returns the pdb entries added to the current set.
340 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe, SequenceI[][] seq,
343 List<PDBEntry> v = new ArrayList<PDBEntry>();
344 List<int[]> rtn = new ArrayList<int[]>();
345 for (int i = 0; i < getPdbCount(); i++)
347 v.add(getPdbEntry(i));
349 for (int i = 0; i < pdbe.length; i++)
351 int r = v.indexOf(pdbe[i]);
352 if (r == -1 || r >= getPdbCount())
360 // just make sure the sequence/chain entries are all up to date
361 addSequenceAndChain(r, seq[i], chns[i]);
364 pdbe = v.toArray(new PDBEntry[v.size()]);
368 // expand the tied sequence[] and string[] arrays
369 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
370 String[][] sch = new String[getPdbCount()][];
371 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
372 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
375 pdbe = new PDBEntry[rtn.size()];
376 for (int r = 0; r < pdbe.length; r++)
378 int[] stri = (rtn.get(r));
379 // record the pdb file as a new addition
380 pdbe[r] = getPdbEntry(stri[0]);
381 // and add the new sequence/chain entries
382 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
393 * Add sequences to the pe'th pdbentry's sequence set.
398 public void addSequence(int pe, SequenceI[] seq)
400 addSequenceAndChain(pe, seq, null);
404 * add the given sequences to the mapping scope for the given pdb file handle
407 * - pdbFile identifier
409 * - set of sequences it can be mapped to
411 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
413 for (int pe = 0; pe < getPdbCount(); pe++)
415 if (getPdbEntry(pe).getFile().equals(pdbFile))
417 addSequence(pe, seq);
423 public abstract void highlightAtoms(List<AtomSpec> atoms);
425 protected boolean isNucleotide()
427 return this.nucleotide;
431 * Returns a readable description of all mappings for the wrapped pdbfile to
432 * any mapped sequences
438 public String printMappings()
440 if (pdbEntry == null)
444 StringBuilder sb = new StringBuilder(128);
445 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
447 String pdbfile = getPdbEntry(pdbe).getFile();
448 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
449 sb.append(getSsm().printMappings(pdbfile, seqs));
451 return sb.toString();
455 * Returns the mapped structure position for a given aligned column of a given
456 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
457 * not mapped to structure.
464 protected int getMappedPosition(SequenceI seq, int alignedPos,
465 StructureMapping mapping)
467 if (alignedPos >= seq.getLength())
472 if (Comparison.isGap(seq.getCharAt(alignedPos)))
476 int seqPos = seq.findPosition(alignedPos);
477 int pos = mapping.getPDBResNum(seqPos);
482 * Helper method to identify residues that can participate in a structure
483 * superposition command. For each structure, identify a sequence in the
484 * alignment which is mapped to the structure. Identify non-gapped columns in
485 * the sequence which have a mapping to a residue in the structure. Returns
486 * the index of the first structure that has a mapping to the alignment.
489 * the sequence alignment which is the basis of structure
492 * an array of booleans, indexed by alignment column, where true
493 * indicates that every structure has a mapped residue present in the
494 * column (so the column can participate in structure alignment)
496 * an array of data beans corresponding to pdb file index
499 protected int findSuperposableResidues(AlignmentI alignment,
500 boolean[] matched, SuperposeData[] structures)
502 int refStructure = -1;
503 String[] files = getPdbFile();
504 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
506 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
510 * Find the first mapped sequence (if any) for this PDB entry which is in
513 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
514 for (int s = 0; s < seqCountForPdbFile; s++)
516 for (StructureMapping mapping : mappings)
518 final SequenceI theSequence = getSequence()[pdbfnum][s];
519 if (mapping.getSequence() == theSequence
520 && alignment.findIndex(theSequence) > -1)
522 if (refStructure < 0)
524 refStructure = pdbfnum;
526 for (int r = 0; r < matched.length; r++)
532 int pos = getMappedPosition(theSequence, r, mapping);
533 if (pos < 1 || pos == lastPos)
539 structures[pdbfnum].pdbResNo[r] = pos;
541 String chain = mapping.getChain();
542 if (chain != null && chain.trim().length() > 0)
544 structures[pdbfnum].chain = chain;
546 structures[pdbfnum].pdbId = mapping.getPdbId();
547 structures[pdbfnum].isRna = theSequence.getRNA() != null;
548 // move on to next pdb file
549 s = seqCountForPdbFile;
559 * Returns true if the structure viewer has loaded all of the files of
560 * interest (identified by the file mapping having been set up), or false if
561 * any are still not loaded after a timeout interval.
565 protected boolean waitForFileLoad(String[] files)
568 * give up after 10 secs plus 1 sec per file
570 long starttime = System.currentTimeMillis();
571 long endTime = 10000 + 1000 * files.length + starttime;
572 String notLoaded = null;
574 boolean waiting = true;
575 while (waiting && System.currentTimeMillis() < endTime)
578 for (String file : files)
583 StructureMapping[] sm = getSsm().getMapping(file);
584 if (sm == null || sm.length == 0)
588 } catch (Throwable x)
598 .println("Timed out waiting for structure viewer to load file "
606 public boolean isListeningFor(SequenceI seq)
608 if (sequence != null)
610 for (SequenceI[] seqs : sequence)
614 for (SequenceI s : seqs)