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;
54 private PDBEntry[] pdbEntry;
57 * sequences mapped to each pdbentry
59 private SequenceI[][] sequence;
62 * array of target chains for sequences - tied to pdbentry and sequence[]
64 private String[][] chains;
67 * datasource protocol for access to PDBEntrylatest
69 String protocol = null;
71 protected boolean colourBySequence = true;
73 private boolean nucleotide;
75 private boolean finishedInit = false;
78 * Data bean class to simplify parameterisation in superposeStructures
80 protected class SuperposeData
83 * Constructor with alignment width argument
87 public SuperposeData(int width)
89 pdbResNo = new int[width];
92 public String filename;
96 public String chain = "";
101 * The pdb residue number (if any) mapped to each column of the alignment
103 public int[] pdbResNo;
112 public AAStructureBindingModel(StructureSelectionManager ssm,
116 this.sequence = seqs;
128 public AAStructureBindingModel(StructureSelectionManager ssm,
129 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
133 this.sequence = sequenceIs;
134 this.nucleotide = Comparison.isNucleotide(sequenceIs);
135 this.chains = chains;
136 this.pdbEntry = pdbentry;
137 this.protocol = protocol;
140 this.chains = new String[pdbentry.length][];
144 public StructureSelectionManager getSsm()
150 * Returns the i'th PDBEntry (or null)
155 public PDBEntry getPdbEntry(int i)
157 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
161 * Answers true if this binding includes the given PDB id, else false
166 public boolean hasPdbId(String pdbId)
168 if (pdbEntry != null)
170 for (PDBEntry pdb : pdbEntry)
172 if (pdb.getId().equals(pdbId))
182 * Returns the number of modelled PDB file entries.
186 public int getPdbCount()
188 return pdbEntry == null ? 0 : pdbEntry.length;
191 public SequenceI[][] getSequence()
196 public String[][] getChains()
201 public String getProtocol()
206 // TODO may remove this if calling methods can be pulled up here
207 protected void setPdbentry(PDBEntry[] pdbentry)
209 this.pdbEntry = pdbentry;
212 protected void setSequence(SequenceI[][] sequence)
214 this.sequence = sequence;
217 protected void setChains(String[][] chains)
219 this.chains = chains;
223 * Construct a title string for the viewer window based on the data Jalview
232 public String getViewerTitle(String viewerName, boolean verbose)
234 if (getSequence() == null || getSequence().length < 1
235 || getPdbCount() < 1 || getSequence()[0].length < 1)
237 return ("Jalview " + viewerName + " Window");
239 // TODO: give a more informative title when multiple structures are
241 StringBuilder title = new StringBuilder(64);
242 final PDBEntry pdbe = getPdbEntry(0);
243 title.append(viewerName + " view for " + getSequence()[0][0].getName()
244 + ":" + pdbe.getId());
248 String method = (String) pdbe.getProperty("method");
251 title.append(" Method: ").append(method);
253 String chain = (String) pdbe.getProperty("chains");
256 title.append(" Chain:").append(chain);
259 return title.toString();
263 * Called by after closeViewer is called, to release any resources and
264 * references so they can be garbage collected. Override if needed.
266 protected void releaseUIResources()
271 public boolean isColourBySequence()
273 return colourBySequence;
276 public void setColourBySequence(boolean colourBySequence)
278 this.colourBySequence = colourBySequence;
281 protected void addSequenceAndChain(int pe, SequenceI[] seq,
284 if (pe < 0 || pe >= getPdbCount())
286 throw new Error(MessageManager.formatMessage(
287 "error.implementation_error_no_pdbentry_from_index",
288 new Object[] { Integer.valueOf(pe).toString() }));
290 final String nullChain = "TheNullChain";
291 List<SequenceI> s = new ArrayList<SequenceI>();
292 List<String> c = new ArrayList<String>();
293 if (getChains() == null)
295 setChains(new String[getPdbCount()][]);
297 if (getSequence()[pe] != null)
299 for (int i = 0; i < getSequence()[pe].length; i++)
301 s.add(getSequence()[pe][i]);
302 if (getChains()[pe] != null)
304 if (i < getChains()[pe].length)
306 c.add(getChains()[pe][i]);
315 if (tchain != null && tchain.length > 0)
322 for (int i = 0; i < seq.length; i++)
324 if (!s.contains(seq[i]))
327 if (tchain != null && i < tchain.length)
329 c.add(tchain[i] == null ? nullChain : tchain[i]);
333 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
334 getSequence()[pe] = tmp;
337 String[] tch = c.toArray(new String[c.size()]);
338 for (int i = 0; i < tch.length; i++)
340 if (tch[i] == nullChain)
345 getChains()[pe] = tch;
349 getChains()[pe] = null;
354 * add structures and any known sequence associations
356 * @returns the pdb entries added to the current set.
358 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
359 SequenceI[][] seq, String[][] chns)
361 List<PDBEntry> v = new ArrayList<PDBEntry>();
362 List<int[]> rtn = new ArrayList<int[]>();
363 for (int i = 0; i < getPdbCount(); i++)
365 v.add(getPdbEntry(i));
367 for (int i = 0; i < pdbe.length; i++)
369 int r = v.indexOf(pdbe[i]);
370 if (r == -1 || r >= getPdbCount())
372 rtn.add(new int[] { v.size(), i });
377 // just make sure the sequence/chain entries are all up to date
378 addSequenceAndChain(r, seq[i], chns[i]);
381 pdbe = v.toArray(new PDBEntry[v.size()]);
385 // expand the tied sequence[] and string[] arrays
386 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
387 String[][] sch = new String[getPdbCount()][];
388 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
389 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
392 pdbe = new PDBEntry[rtn.size()];
393 for (int r = 0; r < pdbe.length; r++)
395 int[] stri = (rtn.get(r));
396 // record the pdb file as a new addition
397 pdbe[r] = getPdbEntry(stri[0]);
398 // and add the new sequence/chain entries
399 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
410 * Add sequences to the pe'th pdbentry's sequence set.
415 public void addSequence(int pe, SequenceI[] seq)
417 addSequenceAndChain(pe, seq, null);
421 * add the given sequences to the mapping scope for the given pdb file handle
424 * - pdbFile identifier
426 * - set of sequences it can be mapped to
428 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
430 for (int pe = 0; pe < getPdbCount(); pe++)
432 if (getPdbEntry(pe).getFile().equals(pdbFile))
434 addSequence(pe, seq);
440 public abstract void highlightAtoms(List<AtomSpec> atoms);
442 protected boolean isNucleotide()
444 return this.nucleotide;
448 * Returns a readable description of all mappings for the wrapped pdbfile to
449 * any mapped sequences
455 public String printMappings()
457 if (pdbEntry == null)
461 StringBuilder sb = new StringBuilder(128);
462 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
464 String pdbfile = getPdbEntry(pdbe).getFile();
465 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
466 sb.append(getSsm().printMappings(pdbfile, seqs));
468 return sb.toString();
472 * Returns the mapped structure position for a given aligned column of a given
473 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
474 * not mapped to structure.
481 protected int getMappedPosition(SequenceI seq, int alignedPos,
482 StructureMapping mapping)
484 if (alignedPos >= seq.getLength())
489 if (Comparison.isGap(seq.getCharAt(alignedPos)))
493 int seqPos = seq.findPosition(alignedPos);
494 int pos = mapping.getPDBResNum(seqPos);
499 * Helper method to identify residues that can participate in a structure
500 * superposition command. For each structure, identify a sequence in the
501 * alignment which is mapped to the structure. Identify non-gapped columns in
502 * the sequence which have a mapping to a residue in the structure. Returns
503 * the index of the first structure that has a mapping to the alignment.
506 * the sequence alignment which is the basis of structure
509 * an array of booleans, indexed by alignment column, where true
510 * indicates that every structure has a mapped residue present in the
511 * column (so the column can participate in structure alignment)
513 * an array of data beans corresponding to pdb file index
516 protected int findSuperposableResidues(AlignmentI alignment,
517 boolean[] matched, SuperposeData[] structures)
519 int refStructure = -1;
520 String[] files = getPdbFile();
521 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
523 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
527 * Find the first mapped sequence (if any) for this PDB entry which is in
530 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
531 for (int s = 0; s < seqCountForPdbFile; s++)
533 for (StructureMapping mapping : mappings)
535 final SequenceI theSequence = getSequence()[pdbfnum][s];
536 if (mapping.getSequence() == theSequence
537 && alignment.findIndex(theSequence) > -1)
539 if (refStructure < 0)
541 refStructure = pdbfnum;
543 for (int r = 0; r < matched.length; r++)
549 int pos = getMappedPosition(theSequence, r, mapping);
550 if (pos < 1 || pos == lastPos)
556 structures[pdbfnum].pdbResNo[r] = pos;
558 String chain = mapping.getChain();
559 if (chain != null && chain.trim().length() > 0)
561 structures[pdbfnum].chain = chain;
563 structures[pdbfnum].pdbId = mapping.getPdbId();
564 structures[pdbfnum].isRna = theSequence.getRNA() != null;
565 // move on to next pdb file
566 s = seqCountForPdbFile;
576 * Returns true if the structure viewer has loaded all of the files of
577 * interest (identified by the file mapping having been set up), or false if
578 * any are still not loaded after a timeout interval.
582 protected boolean waitForFileLoad(String[] files)
585 * give up after 10 secs plus 1 sec per file
587 long starttime = System.currentTimeMillis();
588 long endTime = 10000 + 1000 * files.length + starttime;
589 String notLoaded = null;
591 boolean waiting = true;
592 while (waiting && System.currentTimeMillis() < endTime)
595 for (String file : files)
604 StructureMapping[] sm = getSsm().getMapping(file);
605 if (sm == null || sm.length == 0)
609 } catch (Throwable x)
619 .println("Timed out waiting for structure viewer to load file "
627 public boolean isListeningFor(SequenceI seq)
629 if (sequence != null)
631 for (SequenceI[] seqs : sequence)
635 for (SequenceI s : seqs)
638 || (s.getDatasetSequence() != null && s
639 .getDatasetSequence() == seq
640 .getDatasetSequence()))
651 public boolean isFinishedInit()
656 public void setFinishedInit(boolean fi)
658 this.finishedInit = fi;