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.api.structures.JalviewStructureDisplayI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.PDBEntry;
27 import jalview.datamodel.SequenceI;
28 import jalview.io.DataSourceType;
29 import jalview.structure.AtomSpec;
30 import jalview.structure.StructureListener;
31 import jalview.structure.StructureMapping;
32 import jalview.structure.StructureSelectionManager;
33 import jalview.util.Comparison;
34 import jalview.util.MessageManager;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
42 * A base class to hold common function for protein structure model binding.
43 * Initial version created by refactoring JMol and Chimera binding models, but
44 * other structure viewers could in principle be accommodated in future.
49 public abstract class AAStructureBindingModel extends
50 SequenceStructureBindingModel implements StructureListener,
51 StructureSelectionManagerProvider
54 private StructureSelectionManager ssm;
57 * distinct PDB entries (pdb files) associated
60 private PDBEntry[] pdbEntry;
63 * sequences mapped to each pdbentry
65 private SequenceI[][] sequence;
68 * array of target chains for sequences - tied to pdbentry and sequence[]
70 private String[][] chains;
73 * datasource protocol for access to PDBEntrylatest
75 DataSourceType protocol = null;
77 protected boolean colourBySequence = true;
79 private boolean nucleotide;
81 private boolean finishedInit = false;
84 * current set of model filenames loaded in the Jmol instance
86 protected String[] modelFileNames = null;
89 * Data bean class to simplify parameterisation in superposeStructures
91 protected class SuperposeData
94 * Constructor with alignment width argument
98 public SuperposeData(int width)
100 pdbResNo = new int[width];
103 public String filename;
107 public String chain = "";
109 public boolean isRna;
112 * The pdb residue number (if any) mapped to each column of the alignment
114 public int[] pdbResNo;
123 public AAStructureBindingModel(StructureSelectionManager ssm,
127 this.sequence = seqs;
139 public AAStructureBindingModel(StructureSelectionManager ssm,
140 PDBEntry[] pdbentry, SequenceI[][] sequenceIs,
141 DataSourceType protocol)
144 this.sequence = sequenceIs;
145 this.nucleotide = Comparison.isNucleotide(sequenceIs);
146 this.pdbEntry = pdbentry;
147 this.protocol = protocol;
150 public StructureSelectionManager getSsm()
156 * Returns the i'th PDBEntry (or null)
161 public PDBEntry getPdbEntry(int i)
163 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
167 * Answers true if this binding includes the given PDB id, else false
172 public boolean hasPdbId(String pdbId)
174 if (pdbEntry != null)
176 for (PDBEntry pdb : pdbEntry)
178 if (pdb.getId().equals(pdbId))
188 * Returns the number of modelled PDB file entries.
192 public int getPdbCount()
194 return pdbEntry == null ? 0 : pdbEntry.length;
197 public SequenceI[][] getSequence()
202 public String[][] getChains()
207 public DataSourceType getProtocol()
212 // TODO may remove this if calling methods can be pulled up here
213 protected void setPdbentry(PDBEntry[] pdbentry)
215 this.pdbEntry = pdbentry;
218 protected void setSequence(SequenceI[][] sequence)
220 this.sequence = sequence;
223 protected void setChains(String[][] chains)
225 this.chains = chains;
229 * Construct a title string for the viewer window based on the data Jalview
238 public String getViewerTitle(String viewerName, boolean verbose)
240 if (getSequence() == null || getSequence().length < 1
241 || getPdbCount() < 1 || getSequence()[0].length < 1)
243 return ("Jalview " + viewerName + " Window");
245 // TODO: give a more informative title when multiple structures are
247 StringBuilder title = new StringBuilder(64);
248 final PDBEntry pdbe = getPdbEntry(0);
249 title.append(viewerName + " view for " + getSequence()[0][0].getName()
250 + ":" + pdbe.getId());
254 String method = (String) pdbe.getProperty("method");
257 title.append(" Method: ").append(method);
259 String chain = (String) pdbe.getProperty("chains");
262 title.append(" Chain:").append(chain);
265 return title.toString();
269 * Called by after closeViewer is called, to release any resources and
270 * references so they can be garbage collected. Override if needed.
272 protected void releaseUIResources()
277 public boolean isColourBySequence()
279 return colourBySequence;
282 public void setColourBySequence(boolean colourBySequence)
284 this.colourBySequence = colourBySequence;
287 protected void addSequenceAndChain(int pe, SequenceI[] seq,
290 if (pe < 0 || pe >= getPdbCount())
292 throw new Error(MessageManager.formatMessage(
293 "error.implementation_error_no_pdbentry_from_index",
294 new Object[] { Integer.valueOf(pe).toString() }));
296 final String nullChain = "TheNullChain";
297 List<SequenceI> s = new ArrayList<SequenceI>();
298 List<String> c = new ArrayList<String>();
299 if (getChains() == null)
301 setChains(new String[getPdbCount()][]);
303 if (getSequence()[pe] != null)
305 for (int i = 0; i < getSequence()[pe].length; i++)
307 s.add(getSequence()[pe][i]);
308 if (getChains()[pe] != null)
310 if (i < getChains()[pe].length)
312 c.add(getChains()[pe][i]);
321 if (tchain != null && tchain.length > 0)
328 for (int i = 0; i < seq.length; i++)
330 if (!s.contains(seq[i]))
333 if (tchain != null && i < tchain.length)
335 c.add(tchain[i] == null ? nullChain : tchain[i]);
339 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
340 getSequence()[pe] = tmp;
343 String[] tch = c.toArray(new String[c.size()]);
344 for (int i = 0; i < tch.length; i++)
346 if (tch[i] == nullChain)
351 getChains()[pe] = tch;
355 getChains()[pe] = null;
360 * add structures and any known sequence associations
362 * @returns the pdb entries added to the current set.
364 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
365 SequenceI[][] seq, String[][] chns)
367 List<PDBEntry> v = new ArrayList<PDBEntry>();
368 List<int[]> rtn = new ArrayList<int[]>();
369 for (int i = 0; i < getPdbCount(); i++)
371 v.add(getPdbEntry(i));
373 for (int i = 0; i < pdbe.length; i++)
375 int r = v.indexOf(pdbe[i]);
376 if (r == -1 || r >= getPdbCount())
378 rtn.add(new int[] { v.size(), i });
383 // just make sure the sequence/chain entries are all up to date
384 addSequenceAndChain(r, seq[i], chns[i]);
387 pdbe = v.toArray(new PDBEntry[v.size()]);
391 // expand the tied sequence[] and string[] arrays
392 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
393 String[][] sch = new String[getPdbCount()][];
394 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
395 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
398 pdbe = new PDBEntry[rtn.size()];
399 for (int r = 0; r < pdbe.length; r++)
401 int[] stri = (rtn.get(r));
402 // record the pdb file as a new addition
403 pdbe[r] = getPdbEntry(stri[0]);
404 // and add the new sequence/chain entries
405 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
416 * Add sequences to the pe'th pdbentry's sequence set.
421 public void addSequence(int pe, SequenceI[] seq)
423 addSequenceAndChain(pe, seq, null);
427 * add the given sequences to the mapping scope for the given pdb file handle
430 * - pdbFile identifier
432 * - set of sequences it can be mapped to
434 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
436 for (int pe = 0; pe < getPdbCount(); pe++)
438 if (getPdbEntry(pe).getFile().equals(pdbFile))
440 addSequence(pe, seq);
446 public abstract void highlightAtoms(List<AtomSpec> atoms);
448 protected boolean isNucleotide()
450 return this.nucleotide;
454 * Returns a readable description of all mappings for the wrapped pdbfile to
455 * any mapped sequences
461 public String printMappings()
463 if (pdbEntry == null)
467 StringBuilder sb = new StringBuilder(128);
468 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
470 String pdbfile = getPdbEntry(pdbe).getFile();
471 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
472 sb.append(getSsm().printMappings(pdbfile, seqs));
474 return sb.toString();
478 * Returns the mapped structure position for a given aligned column of a given
479 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
480 * not mapped to structure.
487 protected int getMappedPosition(SequenceI seq, int alignedPos,
488 StructureMapping mapping)
490 if (alignedPos >= seq.getLength())
495 if (Comparison.isGap(seq.getCharAt(alignedPos)))
499 int seqPos = seq.findPosition(alignedPos);
500 int pos = mapping.getPDBResNum(seqPos);
505 * Helper method to identify residues that can participate in a structure
506 * superposition command. For each structure, identify a sequence in the
507 * alignment which is mapped to the structure. Identify non-gapped columns in
508 * the sequence which have a mapping to a residue in the structure. Returns
509 * the index of the first structure that has a mapping to the alignment.
512 * the sequence alignment which is the basis of structure
515 * an array of booleans, indexed by alignment column, where true
516 * indicates that every structure has a mapped residue present in the
517 * column (so the column can participate in structure alignment)
519 * an array of data beans corresponding to pdb file index
522 protected int findSuperposableResidues(AlignmentI alignment,
523 boolean[] matched, SuperposeData[] structures)
525 int refStructure = -1;
526 String[] files = getPdbFile();
531 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
533 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
537 * Find the first mapped sequence (if any) for this PDB entry which is in
540 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
541 for (int s = 0; s < seqCountForPdbFile; s++)
543 for (StructureMapping mapping : mappings)
545 final SequenceI theSequence = getSequence()[pdbfnum][s];
546 if (mapping.getSequence() == theSequence
547 && alignment.findIndex(theSequence) > -1)
549 if (refStructure < 0)
551 refStructure = pdbfnum;
553 for (int r = 0; r < matched.length; r++)
559 int pos = getMappedPosition(theSequence, r, mapping);
560 if (pos < 1 || pos == lastPos)
566 structures[pdbfnum].pdbResNo[r] = pos;
568 String chain = mapping.getChain();
569 if (chain != null && chain.trim().length() > 0)
571 structures[pdbfnum].chain = chain;
573 structures[pdbfnum].pdbId = mapping.getPdbId();
574 structures[pdbfnum].isRna = theSequence.getRNA() != null;
577 * move on to next pdb file (ignore sequences for other chains
578 * for the same structure)
580 s = seqCountForPdbFile;
590 * Returns true if the structure viewer has loaded all of the files of
591 * interest (identified by the file mapping having been set up), or false if
592 * any are still not loaded after a timeout interval.
596 protected boolean waitForFileLoad(String[] files)
599 * give up after 10 secs plus 1 sec per file
601 long starttime = System.currentTimeMillis();
602 long endTime = 10000 + 1000 * files.length + starttime;
603 String notLoaded = null;
605 boolean waiting = true;
606 while (waiting && System.currentTimeMillis() < endTime)
609 for (String file : files)
618 StructureMapping[] sm = getSsm().getMapping(file);
619 if (sm == null || sm.length == 0)
623 } catch (Throwable x)
633 .println("Timed out waiting for structure viewer to load file "
641 public boolean isListeningFor(SequenceI seq)
643 if (sequence != null)
645 for (SequenceI[] seqs : sequence)
649 for (SequenceI s : seqs)
652 || (s.getDatasetSequence() != null && s
653 .getDatasetSequence() == seq
654 .getDatasetSequence()))
665 public boolean isFinishedInit()
670 public void setFinishedInit(boolean fi)
672 this.finishedInit = fi;
676 * Returns a list of chains mapped in this viewer.
680 public abstract List<String> getChainNames();
683 * Returns the Jalview panel hosting the structure viewer (if any)
687 public JalviewStructureDisplayI getViewer()