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;
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
205 * @param viewerName TODO
210 public String getViewerTitle(String viewerName, boolean verbose)
212 if (getSequence() == null || getSequence().length < 1
214 || getSequence()[0].length < 1)
216 return ("Jalview " + viewerName + " Window");
218 // TODO: give a more informative title when multiple structures are
220 StringBuilder title = new StringBuilder(64);
221 final PDBEntry pdbEntry = getPdbEntry(0);
222 title.append(viewerName + " view for " + getSequence()[0][0].getName()
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",
272 { Integer.valueOf(pe).toString() }));
274 final String nullChain = "TheNullChain";
275 List<SequenceI> s = new ArrayList<SequenceI>();
276 List<String> c = new ArrayList<String>();
277 if (getChains() == null)
279 setChains(new String[getPdbCount()][]);
281 if (getSequence()[pe] != null)
283 for (int i = 0; i < getSequence()[pe].length; i++)
285 s.add(getSequence()[pe][i]);
286 if (getChains()[pe] != null)
288 if (i < getChains()[pe].length)
290 c.add(getChains()[pe][i]);
299 if (tchain != null && tchain.length > 0)
306 for (int i = 0; i < seq.length; i++)
308 if (!s.contains(seq[i]))
311 if (tchain != null && i < tchain.length)
313 c.add(tchain[i] == null ? nullChain : tchain[i]);
317 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
318 getSequence()[pe] = tmp;
321 String[] tch = c.toArray(new String[c.size()]);
322 for (int i = 0; i < tch.length; i++)
324 if (tch[i] == nullChain)
329 getChains()[pe] = tch;
333 getChains()[pe] = null;
338 * add structures and any known sequence associations
340 * @returns the pdb entries added to the current set.
342 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe, SequenceI[][] seq,
345 List<PDBEntry> v = new ArrayList<PDBEntry>();
346 List<int[]> rtn = new ArrayList<int[]>();
347 for (int i = 0; i < getPdbCount(); i++)
349 v.add(getPdbEntry(i));
351 for (int i = 0; i < pdbe.length; i++)
353 int r = v.indexOf(pdbe[i]);
354 if (r == -1 || r >= getPdbCount())
362 // just make sure the sequence/chain entries are all up to date
363 addSequenceAndChain(r, seq[i], chns[i]);
366 pdbe = v.toArray(new PDBEntry[v.size()]);
370 // expand the tied sequence[] and string[] arrays
371 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
372 String[][] sch = new String[getPdbCount()][];
373 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
374 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
377 pdbe = new PDBEntry[rtn.size()];
378 for (int r = 0; r < pdbe.length; r++)
380 int[] stri = (rtn.get(r));
381 // record the pdb file as a new addition
382 pdbe[r] = getPdbEntry(stri[0]);
383 // and add the new sequence/chain entries
384 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
395 * Add sequences to the pe'th pdbentry's sequence set.
400 public void addSequence(int pe, SequenceI[] seq)
402 addSequenceAndChain(pe, seq, null);
406 * add the given sequences to the mapping scope for the given pdb file handle
409 * - pdbFile identifier
411 * - set of sequences it can be mapped to
413 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
415 for (int pe = 0; pe < getPdbCount(); pe++)
417 if (getPdbEntry(pe).getFile().equals(pdbFile))
419 addSequence(pe, seq);
425 public abstract void highlightAtoms(List<AtomSpec> atoms);
427 protected boolean isNucleotide()
429 return this.nucleotide;
433 * Returns a readable description of all mappings for the wrapped pdbfile to
434 * any mapped sequences
440 public String printMappings()
442 if (pdbEntry == null)
446 StringBuilder sb = new StringBuilder(128);
447 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
449 String pdbfile = getPdbEntry(pdbe).getFile();
450 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
451 sb.append(getSsm().printMappings(pdbfile, seqs));
453 return sb.toString();
457 * Returns the mapped structure position for a given aligned column of a given
458 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
459 * not mapped to structure.
466 protected int getMappedPosition(SequenceI seq, int alignedPos,
467 StructureMapping mapping)
469 if (alignedPos >= seq.getLength())
474 if (Comparison.isGap(seq.getCharAt(alignedPos)))
478 int seqPos = seq.findPosition(alignedPos);
479 int pos = mapping.getPDBResNum(seqPos);
484 * Helper method to identify residues that can participate in a structure
485 * superposition command. For each structure, identify a sequence in the
486 * alignment which is mapped to the structure. Identify non-gapped columns in
487 * the sequence which have a mapping to a residue in the structure. Returns
488 * the index of the first structure that has a mapping to the alignment.
491 * the sequence alignment which is the basis of structure
494 * an array of booleans, indexed by alignment column, where true
495 * indicates that every structure has a mapped residue present in the
496 * column (so the column can participate in structure alignment)
498 * an array of data beans corresponding to pdb file index
501 protected int findSuperposableResidues(AlignmentI alignment,
502 boolean[] matched, SuperposeData[] structures)
504 int refStructure = -1;
505 String[] files = getPdbFile();
506 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
508 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
512 * Find the first mapped sequence (if any) for this PDB entry which is in
515 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
516 for (int s = 0; s < seqCountForPdbFile; s++)
518 for (StructureMapping mapping : mappings)
520 final SequenceI theSequence = getSequence()[pdbfnum][s];
521 if (mapping.getSequence() == theSequence
522 && alignment.findIndex(theSequence) > -1)
524 if (refStructure < 0)
526 refStructure = pdbfnum;
528 for (int r = 0; r < matched.length; r++)
534 int pos = getMappedPosition(theSequence, r, mapping);
535 if (pos < 1 || pos == lastPos)
541 structures[pdbfnum].pdbResNo[r] = pos;
543 String chain = mapping.getChain();
544 if (chain != null && chain.trim().length() > 0)
546 structures[pdbfnum].chain = chain;
548 structures[pdbfnum].pdbId = mapping.getPdbId();
549 structures[pdbfnum].isRna = theSequence.getRNA() != null;
550 // move on to next pdb file
551 s = seqCountForPdbFile;
561 * Returns true if the structure viewer has loaded all of the files of
562 * interest (identified by the file mapping having been set up), or false if
563 * any are still not loaded after a timeout interval.
567 protected boolean waitForFileLoad(String[] files)
570 * give up after 10 secs plus 1 sec per file
572 long starttime = System.currentTimeMillis();
573 long endTime = 10000 + 1000 * files.length + starttime;
574 String notLoaded = null;
576 boolean waiting = true;
577 while (waiting && System.currentTimeMillis() < endTime)
580 for (String file : files)
585 StructureMapping[] sm = getSsm().getMapping(file);
586 if (sm == null || sm.length == 0)
590 } catch (Throwable x)
600 .println("Timed out waiting for structure viewer to load file "
608 public boolean isListeningFor(SequenceI seq)
610 if (sequence != null)
612 for (SequenceI[] seqs : sequence)
616 for (SequenceI s : seqs)
629 public boolean isFinishedInit()
634 public void setFinishedInit(boolean fi)
636 this.finishedInit = fi;