1 package jalview.structures.models;
3 import jalview.api.StructureSelectionManagerProvider;
4 import jalview.datamodel.AlignmentI;
5 import jalview.datamodel.PDBEntry;
6 import jalview.datamodel.SequenceI;
7 import jalview.structure.AtomSpec;
8 import jalview.structure.StructureListener;
9 import jalview.structure.StructureMapping;
10 import jalview.structure.StructureSelectionManager;
11 import jalview.util.Comparison;
12 import jalview.util.MessageManager;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
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;
55 private boolean finishedInit = false;
58 * Data bean class to simplify parameterisation in superposeStructures
60 protected class SuperposeData
63 * Constructor with alignment width argument
67 public SuperposeData(int width)
69 pdbResNo = new int[width];
72 public String filename;
76 public String chain = "";
81 * The pdb residue number (if any) mapped to each column of the alignment
83 public int[] pdbResNo;
92 public AAStructureBindingModel(StructureSelectionManager ssm,
108 public AAStructureBindingModel(StructureSelectionManager ssm,
109 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
113 this.sequence = sequenceIs;
114 this.nucleotide = Comparison.isNucleotide(sequenceIs);
115 this.chains = chains;
116 this.pdbEntry = pdbentry;
117 this.protocol = protocol;
120 this.chains = new String[pdbentry.length][];
124 public StructureSelectionManager getSsm()
130 * Returns the i'th PDBEntry (or null)
135 public PDBEntry getPdbEntry(int i)
137 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
141 * Answers true if this binding includes the given PDB id, else false
146 public boolean hasPdbId(String pdbId)
148 if (pdbEntry != null)
150 for (PDBEntry pdb : pdbEntry)
152 if (pdb.getId().equals(pdbId))
162 * Returns the number of modelled PDB file entries.
166 public int getPdbCount()
168 return pdbEntry == null ? 0 : pdbEntry.length;
171 public SequenceI[][] getSequence()
176 public String[][] getChains()
181 public String getProtocol()
186 // TODO may remove this if calling methods can be pulled up here
187 protected void setPdbentry(PDBEntry[] pdbentry)
189 this.pdbEntry = pdbentry;
192 protected void setSequence(SequenceI[][] sequence)
194 this.sequence = sequence;
197 protected void setChains(String[][] chains)
199 this.chains = chains;
203 * Construct a title string for the viewer window based on the data Jalview
212 public String getViewerTitle(String viewerName, boolean verbose)
214 if (getSequence() == null || getSequence().length < 1
215 || getPdbCount() < 1 || getSequence()[0].length < 1)
217 return ("Jalview " + viewerName + " Window");
219 // TODO: give a more informative title when multiple structures are
221 StringBuilder title = new StringBuilder(64);
222 final PDBEntry pdbEntry = getPdbEntry(0);
223 title.append(viewerName + " view for " + getSequence()[0][0].getName()
224 + ":" + pdbEntry.getId());
228 if (pdbEntry.getProperty() != null)
230 if (pdbEntry.getProperty().get("method") != null)
232 title.append(" Method: ");
233 title.append(pdbEntry.getProperty().get("method"));
235 if (pdbEntry.getProperty().get("chains") != null)
237 title.append(" Chain:");
238 title.append(pdbEntry.getProperty().get("chains"));
242 return title.toString();
246 * Called by after closeViewer is called, to release any resources and
247 * references so they can be garbage collected. Override if needed.
249 protected void releaseUIResources()
254 public boolean isColourBySequence()
256 return colourBySequence;
259 public void setColourBySequence(boolean colourBySequence)
261 this.colourBySequence = colourBySequence;
264 protected void addSequenceAndChain(int pe, SequenceI[] seq,
267 if (pe < 0 || pe >= getPdbCount())
269 throw new Error(MessageManager.formatMessage(
270 "error.implementation_error_no_pdbentry_from_index",
271 new Object[] { Integer.valueOf(pe).toString() }));
273 final String nullChain = "TheNullChain";
274 List<SequenceI> s = new ArrayList<SequenceI>();
275 List<String> c = new ArrayList<String>();
276 if (getChains() == null)
278 setChains(new String[getPdbCount()][]);
280 if (getSequence()[pe] != null)
282 for (int i = 0; i < getSequence()[pe].length; i++)
284 s.add(getSequence()[pe][i]);
285 if (getChains()[pe] != null)
287 if (i < getChains()[pe].length)
289 c.add(getChains()[pe][i]);
298 if (tchain != null && tchain.length > 0)
305 for (int i = 0; i < seq.length; i++)
307 if (!s.contains(seq[i]))
310 if (tchain != null && i < tchain.length)
312 c.add(tchain[i] == null ? nullChain : tchain[i]);
316 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
317 getSequence()[pe] = tmp;
320 String[] tch = c.toArray(new String[c.size()]);
321 for (int i = 0; i < tch.length; i++)
323 if (tch[i] == nullChain)
328 getChains()[pe] = tch;
332 getChains()[pe] = null;
337 * add structures and any known sequence associations
339 * @returns the pdb entries added to the current set.
341 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
342 SequenceI[][] seq, String[][] chns)
344 List<PDBEntry> v = new ArrayList<PDBEntry>();
345 List<int[]> rtn = new ArrayList<int[]>();
346 for (int i = 0; i < getPdbCount(); i++)
348 v.add(getPdbEntry(i));
350 for (int i = 0; i < pdbe.length; i++)
352 int r = v.indexOf(pdbe[i]);
353 if (r == -1 || r >= getPdbCount())
355 rtn.add(new int[] { v.size(), i });
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)
627 public boolean isFinishedInit()
632 public void setFinishedInit(boolean fi)
634 this.finishedInit = fi;