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.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.api.FeatureRenderer;
26 import jalview.api.SequenceRenderer;
27 import jalview.api.StructureSelectionManagerProvider;
28 import jalview.api.structures.JalviewStructureDisplayI;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.ColumnSelection;
31 import jalview.datamodel.PDBEntry;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.DataSourceType;
34 import jalview.schemes.ColourSchemeI;
35 import jalview.structure.AtomSpec;
36 import jalview.structure.StructureListener;
37 import jalview.structure.StructureMapping;
38 import jalview.structure.StructureMappingcommandSet;
39 import jalview.structure.StructureSelectionManager;
40 import jalview.util.Comparison;
41 import jalview.util.MessageManager;
43 import java.awt.Color;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.BitSet;
47 import java.util.List;
51 * A base class to hold common function for protein structure model binding.
52 * Initial version created by refactoring JMol and Chimera binding models, but
53 * other structure viewers could in principle be accommodated in future.
58 public abstract class AAStructureBindingModel extends
59 SequenceStructureBindingModel implements StructureListener,
60 StructureSelectionManagerProvider
63 private StructureSelectionManager ssm;
66 * distinct PDB entries (pdb files) associated
69 private PDBEntry[] pdbEntry;
72 * sequences mapped to each pdbentry
74 private SequenceI[][] sequence;
77 * array of target chains for sequences - tied to pdbentry and sequence[]
79 private String[][] chains;
82 * datasource protocol for access to PDBEntrylatest
84 DataSourceType protocol = null;
86 protected boolean colourBySequence = true;
88 private boolean nucleotide;
90 private boolean finishedInit = false;
93 * current set of model filenames loaded in the Jmol instance
95 protected String[] modelFileNames = null;
97 public String fileLoadingError;
100 * Data bean class to simplify parameterisation in superposeStructures
102 protected class SuperposeData
105 * Constructor with alignment width argument
109 public SuperposeData(int width)
111 pdbResNo = new int[width];
114 public String filename;
118 public String chain = "";
120 public boolean isRna;
123 * The pdb residue number (if any) mapped to each column of the alignment
125 public int[] pdbResNo;
134 public AAStructureBindingModel(StructureSelectionManager ssm,
138 this.sequence = seqs;
150 public AAStructureBindingModel(StructureSelectionManager ssm,
151 PDBEntry[] pdbentry, SequenceI[][] sequenceIs,
152 DataSourceType protocol)
155 this.sequence = sequenceIs;
156 this.nucleotide = Comparison.isNucleotide(sequenceIs);
157 this.pdbEntry = pdbentry;
158 this.protocol = protocol;
161 public StructureSelectionManager getSsm()
167 * Returns the i'th PDBEntry (or null)
172 public PDBEntry getPdbEntry(int i)
174 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
178 * Answers true if this binding includes the given PDB id, else false
183 public boolean hasPdbId(String pdbId)
185 if (pdbEntry != null)
187 for (PDBEntry pdb : pdbEntry)
189 if (pdb.getId().equals(pdbId))
199 * Returns the number of modelled PDB file entries.
203 public int getPdbCount()
205 return pdbEntry == null ? 0 : pdbEntry.length;
208 public SequenceI[][] getSequence()
213 public String[][] getChains()
218 public DataSourceType getProtocol()
223 // TODO may remove this if calling methods can be pulled up here
224 protected void setPdbentry(PDBEntry[] pdbentry)
226 this.pdbEntry = pdbentry;
229 protected void setSequence(SequenceI[][] sequence)
231 this.sequence = sequence;
234 protected void setChains(String[][] chains)
236 this.chains = chains;
240 * Construct a title string for the viewer window based on the data Jalview
249 public String getViewerTitle(String viewerName, boolean verbose)
251 if (getSequence() == null || getSequence().length < 1
252 || getPdbCount() < 1 || getSequence()[0].length < 1)
254 return ("Jalview " + viewerName + " Window");
256 // TODO: give a more informative title when multiple structures are
258 StringBuilder title = new StringBuilder(64);
259 final PDBEntry pdbe = getPdbEntry(0);
260 title.append(viewerName + " view for " + getSequence()[0][0].getName()
261 + ":" + pdbe.getId());
265 String method = (String) pdbe.getProperty("method");
268 title.append(" Method: ").append(method);
270 String chain = (String) pdbe.getProperty("chains");
273 title.append(" Chain:").append(chain);
276 return title.toString();
280 * Called by after closeViewer is called, to release any resources and
281 * references so they can be garbage collected. Override if needed.
283 protected void releaseUIResources()
288 public boolean isColourBySequence()
290 return colourBySequence;
293 public void setColourBySequence(boolean colourBySequence)
295 this.colourBySequence = colourBySequence;
298 protected void addSequenceAndChain(int pe, SequenceI[] seq,
301 if (pe < 0 || pe >= getPdbCount())
303 throw new Error(MessageManager.formatMessage(
304 "error.implementation_error_no_pdbentry_from_index",
305 new Object[] { Integer.valueOf(pe).toString() }));
307 final String nullChain = "TheNullChain";
308 List<SequenceI> s = new ArrayList<SequenceI>();
309 List<String> c = new ArrayList<String>();
310 if (getChains() == null)
312 setChains(new String[getPdbCount()][]);
314 if (getSequence()[pe] != null)
316 for (int i = 0; i < getSequence()[pe].length; i++)
318 s.add(getSequence()[pe][i]);
319 if (getChains()[pe] != null)
321 if (i < getChains()[pe].length)
323 c.add(getChains()[pe][i]);
332 if (tchain != null && tchain.length > 0)
339 for (int i = 0; i < seq.length; i++)
341 if (!s.contains(seq[i]))
344 if (tchain != null && i < tchain.length)
346 c.add(tchain[i] == null ? nullChain : tchain[i]);
350 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
351 getSequence()[pe] = tmp;
354 String[] tch = c.toArray(new String[c.size()]);
355 for (int i = 0; i < tch.length; i++)
357 if (tch[i] == nullChain)
362 getChains()[pe] = tch;
366 getChains()[pe] = null;
371 * add structures and any known sequence associations
373 * @returns the pdb entries added to the current set.
375 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
376 SequenceI[][] seq, String[][] chns)
378 List<PDBEntry> v = new ArrayList<PDBEntry>();
379 List<int[]> rtn = new ArrayList<int[]>();
380 for (int i = 0; i < getPdbCount(); i++)
382 v.add(getPdbEntry(i));
384 for (int i = 0; i < pdbe.length; i++)
386 int r = v.indexOf(pdbe[i]);
387 if (r == -1 || r >= getPdbCount())
389 rtn.add(new int[] { v.size(), i });
394 // just make sure the sequence/chain entries are all up to date
395 addSequenceAndChain(r, seq[i], chns[i]);
398 pdbe = v.toArray(new PDBEntry[v.size()]);
402 // expand the tied sequence[] and string[] arrays
403 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
404 String[][] sch = new String[getPdbCount()][];
405 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
406 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
409 pdbe = new PDBEntry[rtn.size()];
410 for (int r = 0; r < pdbe.length; r++)
412 int[] stri = (rtn.get(r));
413 // record the pdb file as a new addition
414 pdbe[r] = getPdbEntry(stri[0]);
415 // and add the new sequence/chain entries
416 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
427 * Add sequences to the pe'th pdbentry's sequence set.
432 public void addSequence(int pe, SequenceI[] seq)
434 addSequenceAndChain(pe, seq, null);
438 * add the given sequences to the mapping scope for the given pdb file handle
441 * - pdbFile identifier
443 * - set of sequences it can be mapped to
445 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
447 for (int pe = 0; pe < getPdbCount(); pe++)
449 if (getPdbEntry(pe).getFile().equals(pdbFile))
451 addSequence(pe, seq);
457 public abstract void highlightAtoms(List<AtomSpec> atoms);
459 protected boolean isNucleotide()
461 return this.nucleotide;
465 * Returns a readable description of all mappings for the wrapped pdbfile to
466 * any mapped sequences
472 public String printMappings()
474 if (pdbEntry == null)
478 StringBuilder sb = new StringBuilder(128);
479 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
481 String pdbfile = getPdbEntry(pdbe).getFile();
482 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
483 sb.append(getSsm().printMappings(pdbfile, seqs));
485 return sb.toString();
489 * Returns the mapped structure position for a given aligned column of a given
490 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
491 * not mapped to structure.
498 protected int getMappedPosition(SequenceI seq, int alignedPos,
499 StructureMapping mapping)
501 if (alignedPos >= seq.getLength())
506 if (Comparison.isGap(seq.getCharAt(alignedPos)))
510 int seqPos = seq.findPosition(alignedPos);
511 int pos = mapping.getPDBResNum(seqPos);
516 * Helper method to identify residues that can participate in a structure
517 * superposition command. For each structure, identify a sequence in the
518 * alignment which is mapped to the structure. Identify non-gapped columns in
519 * the sequence which have a mapping to a residue in the structure. Returns
520 * the index of the first structure that has a mapping to the alignment.
523 * the sequence alignment which is the basis of structure
526 * a BitSet, where bit j is set to indicate that every structure has
527 * a mapped residue present in column j (so the column can
528 * participate in structure alignment)
530 * an array of data beans corresponding to pdb file index
533 protected int findSuperposableResidues(AlignmentI alignment,
534 BitSet matched, SuperposeData[] structures)
536 int refStructure = -1;
537 String[] files = getPdbFile();
542 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
544 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
548 * Find the first mapped sequence (if any) for this PDB entry which is in
551 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
552 for (int s = 0; s < seqCountForPdbFile; s++)
554 for (StructureMapping mapping : mappings)
556 final SequenceI theSequence = getSequence()[pdbfnum][s];
557 if (mapping.getSequence() == theSequence
558 && alignment.findIndex(theSequence) > -1)
560 if (refStructure < 0)
562 refStructure = pdbfnum;
564 for (int r = 0; r < alignment.getWidth(); r++)
570 int pos = getMappedPosition(theSequence, r, mapping);
571 if (pos < 1 || pos == lastPos)
577 structures[pdbfnum].pdbResNo[r] = pos;
579 String chain = mapping.getChain();
580 if (chain != null && chain.trim().length() > 0)
582 structures[pdbfnum].chain = chain;
584 structures[pdbfnum].pdbId = mapping.getPdbId();
585 structures[pdbfnum].isRna = theSequence.getRNA() != null;
588 * move on to next pdb file (ignore sequences for other chains
589 * for the same structure)
591 s = seqCountForPdbFile;
601 * Returns true if the structure viewer has loaded all of the files of
602 * interest (identified by the file mapping having been set up), or false if
603 * any are still not loaded after a timeout interval.
607 protected boolean waitForFileLoad(String[] files)
610 * give up after 10 secs plus 1 sec per file
612 long starttime = System.currentTimeMillis();
613 long endTime = 10000 + 1000 * files.length + starttime;
614 String notLoaded = null;
616 boolean waiting = true;
617 while (waiting && System.currentTimeMillis() < endTime)
620 for (String file : files)
629 StructureMapping[] sm = getSsm().getMapping(file);
630 if (sm == null || sm.length == 0)
634 } catch (Throwable x)
644 .println("Timed out waiting for structure viewer to load file "
652 public boolean isListeningFor(SequenceI seq)
654 if (sequence != null)
656 for (SequenceI[] seqs : sequence)
660 for (SequenceI s : seqs)
663 || (s.getDatasetSequence() != null && s
664 .getDatasetSequence() == seq
665 .getDatasetSequence()))
676 public boolean isFinishedInit()
681 public void setFinishedInit(boolean fi)
683 this.finishedInit = fi;
687 * Returns a list of chains mapped in this viewer.
691 public abstract List<String> getChainNames();
694 * Returns the Jalview panel hosting the structure viewer (if any)
698 public JalviewStructureDisplayI getViewer()
703 public abstract void setJalviewColourScheme(ColourSchemeI cs);
706 * Constructs and sends a command to align structures against a reference
707 * structure, based on one or more sequence alignments. May optionally return
708 * an error or warning message for the alignment command.
711 * an array of alignments to process
712 * @param structureIndices
713 * an array of corresponding reference structures (index into pdb
714 * file array); if a negative value is passed, the first PDB file
715 * mapped to an alignment sequence is used as the reference for
718 * an array of corresponding hidden columns for each alignment
721 public abstract String superposeStructures(AlignmentI[] alignments, int[] structureIndices,
722 ColumnSelection[] hiddenCols);
724 public abstract void setBackgroundColour(Color col);
726 protected abstract StructureMappingcommandSet[] getColourBySequenceCommands(
727 String[] files, SequenceRenderer sr, FeatureRenderer fr,
728 AlignViewportI alignViewportI);
731 * returns the current featureRenderer that should be used to colour the
738 public abstract FeatureRenderer getFeatureRenderer(AlignmentViewPanel alignment);
741 * returns the current sequenceRenderer that should be used to colour the
748 public abstract SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment);
750 protected abstract void colourBySequence(
751 StructureMappingcommandSet[] colourBySequenceCommands);
753 public abstract void colourByChain();
755 public abstract void colourByCharge();
758 * colour any structures associated with sequences in the given alignment
759 * using the getFeatureRenderer() and getSequenceRenderer() renderers but only
760 * if colourBySequence is enabled.
762 public void colourBySequence(AlignmentViewPanel alignmentv)
764 if (!colourBySequence || !isLoadingFinished())
768 if (getSsm() == null)
772 String[] files = getPdbFile();
774 SequenceRenderer sr = getSequenceRenderer(alignmentv);
776 FeatureRenderer fr = null;
777 boolean showFeatures = alignmentv.getAlignViewport()
778 .isShowSequenceFeatures();
781 fr = getFeatureRenderer(alignmentv);
784 StructureMappingcommandSet[] colourBySequenceCommands = getColourBySequenceCommands(
785 files, sr, fr, alignmentv.getAlignViewport());
786 colourBySequence(colourBySequenceCommands);
789 public boolean hasFileLoadingError()
791 return fileLoadingError != null && fileLoadingError.length() > 0;