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;
55 * distinct PDB entries (pdb files) associated
58 private PDBEntry[] pdbEntry;
61 * sequences mapped to each pdbentry
63 private SequenceI[][] sequence;
66 * array of target chains for sequences - tied to pdbentry and sequence[]
68 private String[][] chains;
71 * datasource protocol for access to PDBEntrylatest
73 String protocol = null;
75 protected boolean colourBySequence = true;
77 private boolean nucleotide;
79 private boolean finishedInit = false;
82 * current set of model filenames loaded in the Jmol instance
84 protected String[] modelFileNames = null;
87 * Data bean class to simplify parameterisation in superposeStructures
89 protected class SuperposeData
92 * Constructor with alignment width argument
96 public SuperposeData(int width)
98 pdbResNo = new int[width];
101 public String filename;
105 public String chain = "";
107 public boolean isRna;
110 * The pdb residue number (if any) mapped to each column of the alignment
112 public int[] pdbResNo;
121 public AAStructureBindingModel(StructureSelectionManager ssm,
125 this.sequence = seqs;
137 public AAStructureBindingModel(StructureSelectionManager ssm,
138 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
142 this.sequence = sequenceIs;
143 this.nucleotide = Comparison.isNucleotide(sequenceIs);
144 this.chains = chains;
145 this.pdbEntry = pdbentry;
146 this.protocol = protocol;
149 this.chains = new String[pdbentry.length][];
153 public StructureSelectionManager getSsm()
159 * Returns the i'th PDBEntry (or null)
164 public PDBEntry getPdbEntry(int i)
166 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
170 * Answers true if this binding includes the given PDB id, else false
175 public boolean hasPdbId(String pdbId)
177 if (pdbEntry != null)
179 for (PDBEntry pdb : pdbEntry)
181 if (pdb.getId().equals(pdbId))
191 * Returns the number of modelled PDB file entries.
195 public int getPdbCount()
197 return pdbEntry == null ? 0 : pdbEntry.length;
200 public SequenceI[][] getSequence()
205 public String[][] getChains()
210 public String getProtocol()
215 // TODO may remove this if calling methods can be pulled up here
216 protected void setPdbentry(PDBEntry[] pdbentry)
218 this.pdbEntry = pdbentry;
221 protected void setSequence(SequenceI[][] sequence)
223 this.sequence = sequence;
226 protected void setChains(String[][] chains)
228 this.chains = chains;
232 * Construct a title string for the viewer window based on the data Jalview
241 public String getViewerTitle(String viewerName, boolean verbose)
243 if (getSequence() == null || getSequence().length < 1
244 || getPdbCount() < 1 || getSequence()[0].length < 1)
246 return ("Jalview " + viewerName + " Window");
248 // TODO: give a more informative title when multiple structures are
250 StringBuilder title = new StringBuilder(64);
251 final PDBEntry pdbe = getPdbEntry(0);
252 title.append(viewerName + " view for " + getSequence()[0][0].getName()
253 + ":" + pdbe.getId());
257 String method = (String) pdbe.getProperty("method");
260 title.append(" Method: ").append(method);
262 String chain = (String) pdbe.getProperty("chains");
265 title.append(" Chain:").append(chain);
268 return title.toString();
272 * Called by after closeViewer is called, to release any resources and
273 * references so they can be garbage collected. Override if needed.
275 protected void releaseUIResources()
280 public boolean isColourBySequence()
282 return colourBySequence;
285 public void setColourBySequence(boolean colourBySequence)
287 this.colourBySequence = colourBySequence;
290 protected void addSequenceAndChain(int pe, SequenceI[] seq,
293 if (pe < 0 || pe >= getPdbCount())
295 throw new Error(MessageManager.formatMessage(
296 "error.implementation_error_no_pdbentry_from_index",
297 new Object[] { Integer.valueOf(pe).toString() }));
299 final String nullChain = "TheNullChain";
300 List<SequenceI> s = new ArrayList<SequenceI>();
301 List<String> c = new ArrayList<String>();
302 if (getChains() == null)
304 setChains(new String[getPdbCount()][]);
306 if (getSequence()[pe] != null)
308 for (int i = 0; i < getSequence()[pe].length; i++)
310 s.add(getSequence()[pe][i]);
311 if (getChains()[pe] != null)
313 if (i < getChains()[pe].length)
315 c.add(getChains()[pe][i]);
324 if (tchain != null && tchain.length > 0)
331 for (int i = 0; i < seq.length; i++)
333 if (!s.contains(seq[i]))
336 if (tchain != null && i < tchain.length)
338 c.add(tchain[i] == null ? nullChain : tchain[i]);
342 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
343 getSequence()[pe] = tmp;
346 String[] tch = c.toArray(new String[c.size()]);
347 for (int i = 0; i < tch.length; i++)
349 if (tch[i] == nullChain)
354 getChains()[pe] = tch;
358 getChains()[pe] = null;
363 * add structures and any known sequence associations
365 * @returns the pdb entries added to the current set.
367 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
368 SequenceI[][] seq, String[][] chns)
370 List<PDBEntry> v = new ArrayList<PDBEntry>();
371 List<int[]> rtn = new ArrayList<int[]>();
372 for (int i = 0; i < getPdbCount(); i++)
374 v.add(getPdbEntry(i));
376 for (int i = 0; i < pdbe.length; i++)
378 int r = v.indexOf(pdbe[i]);
379 if (r == -1 || r >= getPdbCount())
381 rtn.add(new int[] { v.size(), i });
386 // just make sure the sequence/chain entries are all up to date
387 addSequenceAndChain(r, seq[i], chns[i]);
390 pdbe = v.toArray(new PDBEntry[v.size()]);
394 // expand the tied sequence[] and string[] arrays
395 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
396 String[][] sch = new String[getPdbCount()][];
397 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
398 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
401 pdbe = new PDBEntry[rtn.size()];
402 for (int r = 0; r < pdbe.length; r++)
404 int[] stri = (rtn.get(r));
405 // record the pdb file as a new addition
406 pdbe[r] = getPdbEntry(stri[0]);
407 // and add the new sequence/chain entries
408 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
419 * Add sequences to the pe'th pdbentry's sequence set.
424 public void addSequence(int pe, SequenceI[] seq)
426 addSequenceAndChain(pe, seq, null);
430 * add the given sequences to the mapping scope for the given pdb file handle
433 * - pdbFile identifier
435 * - set of sequences it can be mapped to
437 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
439 for (int pe = 0; pe < getPdbCount(); pe++)
441 if (getPdbEntry(pe).getFile().equals(pdbFile))
443 addSequence(pe, seq);
449 public abstract void highlightAtoms(List<AtomSpec> atoms);
451 protected boolean isNucleotide()
453 return this.nucleotide;
457 * Returns a readable description of all mappings for the wrapped pdbfile to
458 * any mapped sequences
464 public String printMappings()
466 if (pdbEntry == null)
470 StringBuilder sb = new StringBuilder(128);
471 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
473 String pdbfile = getPdbEntry(pdbe).getFile();
474 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
475 sb.append(getSsm().printMappings(pdbfile, seqs));
477 return sb.toString();
481 * Returns the mapped structure position for a given aligned column of a given
482 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
483 * not mapped to structure.
490 protected int getMappedPosition(SequenceI seq, int alignedPos,
491 StructureMapping mapping)
493 if (alignedPos >= seq.getLength())
498 if (Comparison.isGap(seq.getCharAt(alignedPos)))
502 int seqPos = seq.findPosition(alignedPos);
503 int pos = mapping.getPDBResNum(seqPos);
508 * Helper method to identify residues that can participate in a structure
509 * superposition command. For each structure, identify a sequence in the
510 * alignment which is mapped to the structure. Identify non-gapped columns in
511 * the sequence which have a mapping to a residue in the structure. Returns
512 * the index of the first structure that has a mapping to the alignment.
515 * the sequence alignment which is the basis of structure
518 * an array of booleans, indexed by alignment column, where true
519 * indicates that every structure has a mapped residue present in the
520 * column (so the column can participate in structure alignment)
522 * an array of data beans corresponding to pdb file index
525 protected int findSuperposableResidues(AlignmentI alignment,
526 boolean[] matched, SuperposeData[] structures)
528 int refStructure = -1;
529 String[] files = getPdbFile();
534 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
536 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
540 * Find the first mapped sequence (if any) for this PDB entry which is in
543 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
544 for (int s = 0; s < seqCountForPdbFile; s++)
546 for (StructureMapping mapping : mappings)
548 final SequenceI theSequence = getSequence()[pdbfnum][s];
549 if (mapping.getSequence() == theSequence
550 && alignment.findIndex(theSequence) > -1)
552 if (refStructure < 0)
554 refStructure = pdbfnum;
556 for (int r = 0; r < matched.length; r++)
562 int pos = getMappedPosition(theSequence, r, mapping);
563 if (pos < 1 || pos == lastPos)
569 structures[pdbfnum].pdbResNo[r] = pos;
571 String chain = mapping.getChain();
572 if (chain != null && chain.trim().length() > 0)
574 structures[pdbfnum].chain = chain;
576 structures[pdbfnum].pdbId = mapping.getPdbId();
577 structures[pdbfnum].isRna = theSequence.getRNA() != null;
580 * move on to next pdb file (ignore sequences for other chains
581 * for the same structure)
583 s = seqCountForPdbFile;
593 * Returns true if the structure viewer has loaded all of the files of
594 * interest (identified by the file mapping having been set up), or false if
595 * any are still not loaded after a timeout interval.
599 protected boolean waitForFileLoad(String[] files)
602 * give up after 10 secs plus 1 sec per file
604 long starttime = System.currentTimeMillis();
605 long endTime = 10000 + 1000 * files.length + starttime;
606 String notLoaded = null;
608 boolean waiting = true;
609 while (waiting && System.currentTimeMillis() < endTime)
612 for (String file : files)
621 StructureMapping[] sm = getSsm().getMapping(file);
622 if (sm == null || sm.length == 0)
626 } catch (Throwable x)
636 .println("Timed out waiting for structure viewer to load file "
644 public boolean isListeningFor(SequenceI seq)
646 if (sequence != null)
648 for (SequenceI[] seqs : sequence)
652 for (SequenceI s : seqs)
655 || (s.getDatasetSequence() != null && s
656 .getDatasetSequence() == seq
657 .getDatasetSequence()))
668 public boolean isFinishedInit()
673 public void setFinishedInit(boolean fi)
675 this.finishedInit = fi;