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.AlignmentViewPanel;
24 import jalview.api.SequenceRenderer;
25 import jalview.api.StructureSelectionManagerProvider;
26 import jalview.api.structures.JalviewStructureDisplayI;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.PDBEntry;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.DataSourceType;
32 import jalview.schemes.ColourSchemeI;
33 import jalview.structure.AtomSpec;
34 import jalview.structure.StructureListener;
35 import jalview.structure.StructureMapping;
36 import jalview.structure.StructureMappingcommandSet;
37 import jalview.structure.StructureSelectionManager;
38 import jalview.util.Comparison;
39 import jalview.util.MessageManager;
41 import java.awt.Color;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.BitSet;
45 import java.util.List;
49 * A base class to hold common function for protein structure model binding.
50 * Initial version created by refactoring JMol and Chimera binding models, but
51 * other structure viewers could in principle be accommodated in future.
56 public abstract class AAStructureBindingModel extends
57 SequenceStructureBindingModel implements StructureListener,
58 StructureSelectionManagerProvider
61 private StructureSelectionManager ssm;
64 * distinct PDB entries (pdb files) associated
67 private PDBEntry[] pdbEntry;
70 * sequences mapped to each pdbentry
72 private SequenceI[][] sequence;
75 * array of target chains for sequences - tied to pdbentry and sequence[]
77 private String[][] chains;
80 * datasource protocol for access to PDBEntrylatest
82 DataSourceType protocol = null;
84 protected boolean colourBySequence = true;
86 private boolean nucleotide;
88 private boolean finishedInit = false;
91 * current set of model filenames loaded in the Jmol instance
93 protected String[] modelFileNames = null;
95 public String fileLoadingError;
98 * Data bean class to simplify parameterisation in superposeStructures
100 protected class SuperposeData
103 * Constructor with alignment width argument
107 public SuperposeData(int width)
109 pdbResNo = new int[width];
112 public String filename;
116 public String chain = "";
118 public boolean isRna;
121 * The pdb residue number (if any) mapped to each column of the alignment
123 public int[] pdbResNo;
132 public AAStructureBindingModel(StructureSelectionManager ssm,
136 this.sequence = seqs;
148 public AAStructureBindingModel(StructureSelectionManager ssm,
149 PDBEntry[] pdbentry, SequenceI[][] sequenceIs,
150 DataSourceType protocol)
153 this.sequence = sequenceIs;
154 this.nucleotide = Comparison.isNucleotide(sequenceIs);
155 this.pdbEntry = pdbentry;
156 this.protocol = protocol;
159 public StructureSelectionManager getSsm()
165 * Returns the i'th PDBEntry (or null)
170 public PDBEntry getPdbEntry(int i)
172 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
176 * Answers true if this binding includes the given PDB id, else false
181 public boolean hasPdbId(String pdbId)
183 if (pdbEntry != null)
185 for (PDBEntry pdb : pdbEntry)
187 if (pdb.getId().equals(pdbId))
197 * Returns the number of modelled PDB file entries.
201 public int getPdbCount()
203 return pdbEntry == null ? 0 : pdbEntry.length;
206 public SequenceI[][] getSequence()
211 public String[][] getChains()
216 public DataSourceType getProtocol()
221 // TODO may remove this if calling methods can be pulled up here
222 protected void setPdbentry(PDBEntry[] pdbentry)
224 this.pdbEntry = pdbentry;
227 protected void setSequence(SequenceI[][] sequence)
229 this.sequence = sequence;
232 protected void setChains(String[][] chains)
234 this.chains = chains;
238 * Construct a title string for the viewer window based on the data Jalview
247 public String getViewerTitle(String viewerName, boolean verbose)
249 if (getSequence() == null || getSequence().length < 1
250 || getPdbCount() < 1 || getSequence()[0].length < 1)
252 return ("Jalview " + viewerName + " Window");
254 // TODO: give a more informative title when multiple structures are
256 StringBuilder title = new StringBuilder(64);
257 final PDBEntry pdbe = getPdbEntry(0);
258 title.append(viewerName + " view for " + getSequence()[0][0].getName()
259 + ":" + pdbe.getId());
263 String method = (String) pdbe.getProperty("method");
266 title.append(" Method: ").append(method);
268 String chain = (String) pdbe.getProperty("chains");
271 title.append(" Chain:").append(chain);
274 return title.toString();
278 * Called by after closeViewer is called, to release any resources and
279 * references so they can be garbage collected. Override if needed.
281 protected void releaseUIResources()
286 public boolean isColourBySequence()
288 return colourBySequence;
291 public void setColourBySequence(boolean colourBySequence)
293 this.colourBySequence = colourBySequence;
296 protected void addSequenceAndChain(int pe, SequenceI[] seq,
299 if (pe < 0 || pe >= getPdbCount())
301 throw new Error(MessageManager.formatMessage(
302 "error.implementation_error_no_pdbentry_from_index",
303 new Object[] { Integer.valueOf(pe).toString() }));
305 final String nullChain = "TheNullChain";
306 List<SequenceI> s = new ArrayList<SequenceI>();
307 List<String> c = new ArrayList<String>();
308 if (getChains() == null)
310 setChains(new String[getPdbCount()][]);
312 if (getSequence()[pe] != null)
314 for (int i = 0; i < getSequence()[pe].length; i++)
316 s.add(getSequence()[pe][i]);
317 if (getChains()[pe] != null)
319 if (i < getChains()[pe].length)
321 c.add(getChains()[pe][i]);
330 if (tchain != null && tchain.length > 0)
337 for (int i = 0; i < seq.length; i++)
339 if (!s.contains(seq[i]))
342 if (tchain != null && i < tchain.length)
344 c.add(tchain[i] == null ? nullChain : tchain[i]);
348 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
349 getSequence()[pe] = tmp;
352 String[] tch = c.toArray(new String[c.size()]);
353 for (int i = 0; i < tch.length; i++)
355 if (tch[i] == nullChain)
360 getChains()[pe] = tch;
364 getChains()[pe] = null;
369 * add structures and any known sequence associations
371 * @returns the pdb entries added to the current set.
373 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
374 SequenceI[][] seq, String[][] chns)
376 List<PDBEntry> v = new ArrayList<PDBEntry>();
377 List<int[]> rtn = new ArrayList<int[]>();
378 for (int i = 0; i < getPdbCount(); i++)
380 v.add(getPdbEntry(i));
382 for (int i = 0; i < pdbe.length; i++)
384 int r = v.indexOf(pdbe[i]);
385 if (r == -1 || r >= getPdbCount())
387 rtn.add(new int[] { v.size(), i });
392 // just make sure the sequence/chain entries are all up to date
393 addSequenceAndChain(r, seq[i], chns[i]);
396 pdbe = v.toArray(new PDBEntry[v.size()]);
400 // expand the tied sequence[] and string[] arrays
401 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
402 String[][] sch = new String[getPdbCount()][];
403 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
404 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
407 pdbe = new PDBEntry[rtn.size()];
408 for (int r = 0; r < pdbe.length; r++)
410 int[] stri = (rtn.get(r));
411 // record the pdb file as a new addition
412 pdbe[r] = getPdbEntry(stri[0]);
413 // and add the new sequence/chain entries
414 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
425 * Add sequences to the pe'th pdbentry's sequence set.
430 public void addSequence(int pe, SequenceI[] seq)
432 addSequenceAndChain(pe, seq, null);
436 * add the given sequences to the mapping scope for the given pdb file handle
439 * - pdbFile identifier
441 * - set of sequences it can be mapped to
443 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
445 for (int pe = 0; pe < getPdbCount(); pe++)
447 if (getPdbEntry(pe).getFile().equals(pdbFile))
449 addSequence(pe, seq);
455 public abstract void highlightAtoms(List<AtomSpec> atoms);
457 protected boolean isNucleotide()
459 return this.nucleotide;
463 * Returns a readable description of all mappings for the wrapped pdbfile to
464 * any mapped sequences
470 public String printMappings()
472 if (pdbEntry == null)
476 StringBuilder sb = new StringBuilder(128);
477 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
479 String pdbfile = getPdbEntry(pdbe).getFile();
480 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
481 sb.append(getSsm().printMappings(pdbfile, seqs));
483 return sb.toString();
487 * Returns the mapped structure position for a given aligned column of a given
488 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
489 * not mapped to structure.
496 protected int getMappedPosition(SequenceI seq, int alignedPos,
497 StructureMapping mapping)
499 if (alignedPos >= seq.getLength())
504 if (Comparison.isGap(seq.getCharAt(alignedPos)))
508 int seqPos = seq.findPosition(alignedPos);
509 int pos = mapping.getPDBResNum(seqPos);
514 * Helper method to identify residues that can participate in a structure
515 * superposition command. For each structure, identify a sequence in the
516 * alignment which is mapped to the structure. Identify non-gapped columns in
517 * the sequence which have a mapping to a residue in the structure. Returns
518 * the index of the first structure that has a mapping to the alignment.
521 * the sequence alignment which is the basis of structure
524 * a BitSet, where bit j is set to indicate that every structure has
525 * a mapped residue present in column j (so the column can
526 * participate in structure alignment)
528 * an array of data beans corresponding to pdb file index
531 protected int findSuperposableResidues(AlignmentI alignment,
532 BitSet matched, SuperposeData[] structures)
534 int refStructure = -1;
535 String[] files = getPdbFile();
540 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
542 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
546 * Find the first mapped sequence (if any) for this PDB entry which is in
549 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
550 for (int s = 0; s < seqCountForPdbFile; s++)
552 for (StructureMapping mapping : mappings)
554 final SequenceI theSequence = getSequence()[pdbfnum][s];
555 if (mapping.getSequence() == theSequence
556 && alignment.findIndex(theSequence) > -1)
558 if (refStructure < 0)
560 refStructure = pdbfnum;
562 for (int r = 0; r < alignment.getWidth(); r++)
568 int pos = getMappedPosition(theSequence, r, mapping);
569 if (pos < 1 || pos == lastPos)
575 structures[pdbfnum].pdbResNo[r] = pos;
577 String chain = mapping.getChain();
578 if (chain != null && chain.trim().length() > 0)
580 structures[pdbfnum].chain = chain;
582 structures[pdbfnum].pdbId = mapping.getPdbId();
583 structures[pdbfnum].isRna = theSequence.getRNA() != null;
586 * move on to next pdb file (ignore sequences for other chains
587 * for the same structure)
589 s = seqCountForPdbFile;
599 * Returns true if the structure viewer has loaded all of the files of
600 * interest (identified by the file mapping having been set up), or false if
601 * any are still not loaded after a timeout interval.
605 protected boolean waitForFileLoad(String[] files)
608 * give up after 10 secs plus 1 sec per file
610 long starttime = System.currentTimeMillis();
611 long endTime = 10000 + 1000 * files.length + starttime;
612 String notLoaded = null;
614 boolean waiting = true;
615 while (waiting && System.currentTimeMillis() < endTime)
618 for (String file : files)
627 StructureMapping[] sm = getSsm().getMapping(file);
628 if (sm == null || sm.length == 0)
632 } catch (Throwable x)
642 .println("Timed out waiting for structure viewer to load file "
650 public boolean isListeningFor(SequenceI seq)
652 if (sequence != null)
654 for (SequenceI[] seqs : sequence)
658 for (SequenceI s : seqs)
661 || (s.getDatasetSequence() != null && s
662 .getDatasetSequence() == seq
663 .getDatasetSequence()))
674 public boolean isFinishedInit()
679 public void setFinishedInit(boolean fi)
681 this.finishedInit = fi;
685 * Returns a list of chains mapped in this viewer.
689 public abstract List<String> getChainNames();
692 * Returns the Jalview panel hosting the structure viewer (if any)
696 public JalviewStructureDisplayI getViewer()
701 public abstract void setJalviewColourScheme(ColourSchemeI cs);
704 * Constructs and sends a command to align structures against a reference
705 * structure, based on one or more sequence alignments. May optionally return
706 * an error or warning message for the alignment command.
709 * an array of alignments to process
710 * @param structureIndices
711 * an array of corresponding reference structures (index into pdb
712 * file array); if a negative value is passed, the first PDB file
713 * mapped to an alignment sequence is used as the reference for
716 * an array of corresponding hidden columns for each alignment
719 public abstract String superposeStructures(AlignmentI[] alignments,
720 int[] structureIndices, HiddenColumns[] hiddenCols);
722 public abstract void setBackgroundColour(Color col);
724 protected abstract StructureMappingcommandSet[] getColourBySequenceCommands(
725 String[] files, SequenceRenderer sr, AlignmentViewPanel avp);
728 * returns the current sequenceRenderer that should be used to colour the
735 public abstract SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment);
737 protected abstract void colourBySequence(
738 StructureMappingcommandSet[] colourBySequenceCommands);
740 public abstract void colourByChain();
742 public abstract void colourByCharge();
745 * colour any structures associated with sequences in the given alignment
746 * using the getFeatureRenderer() and getSequenceRenderer() renderers but only
747 * if colourBySequence is enabled.
749 public void colourBySequence(AlignmentViewPanel alignmentv)
751 if (!colourBySequence || !isLoadingFinished())
755 if (getSsm() == null)
759 String[] files = getPdbFile();
761 SequenceRenderer sr = getSequenceRenderer(alignmentv);
763 StructureMappingcommandSet[] colourBySequenceCommands = getColourBySequenceCommands(
764 files, sr, alignmentv);
765 colourBySequence(colourBySequenceCommands);
768 public boolean hasFileLoadingError()
770 return fileLoadingError != null && fileLoadingError.length() > 0;
773 public abstract jalview.api.FeatureRenderer getFeatureRenderer(
774 AlignmentViewPanel alignment);