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.io.DataSourceType;
28 import jalview.structure.AtomSpec;
29 import jalview.structure.StructureListener;
30 import jalview.structure.StructureMapping;
31 import jalview.structure.StructureSelectionManager;
32 import jalview.util.Comparison;
33 import jalview.util.MessageManager;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
41 * A base class to hold common function for protein structure model binding.
42 * Initial version created by refactoring JMol and Chimera binding models, but
43 * other structure viewers could in principle be accommodated in future.
48 public abstract class AAStructureBindingModel extends
49 SequenceStructureBindingModel implements StructureListener,
50 StructureSelectionManagerProvider
53 private StructureSelectionManager ssm;
55 private PDBEntry[] pdbEntry;
58 * sequences mapped to each pdbentry
60 private SequenceI[][] sequence;
63 * array of target chains for sequences - tied to pdbentry and sequence[]
65 private String[][] chains;
68 * datasource protocol for access to PDBEntrylatest
70 DataSourceType protocol = null;
72 protected boolean colourBySequence = true;
74 private boolean nucleotide;
76 private boolean finishedInit = false;
79 * Data bean class to simplify parameterisation in superposeStructures
81 protected class SuperposeData
84 * Constructor with alignment width argument
88 public SuperposeData(int width)
90 pdbResNo = new int[width];
93 public String filename;
97 public String chain = "";
102 * The pdb residue number (if any) mapped to each column of the alignment
104 public int[] pdbResNo;
113 public AAStructureBindingModel(StructureSelectionManager ssm,
117 this.sequence = seqs;
129 public AAStructureBindingModel(StructureSelectionManager ssm,
130 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
131 DataSourceType protocol2)
134 this.sequence = sequenceIs;
135 this.nucleotide = Comparison.isNucleotide(sequenceIs);
136 this.chains = chains;
137 this.pdbEntry = pdbentry;
138 this.protocol = protocol2;
141 this.chains = new String[pdbentry.length][];
145 public StructureSelectionManager getSsm()
151 * Returns the i'th PDBEntry (or null)
156 public PDBEntry getPdbEntry(int i)
158 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
162 * Answers true if this binding includes the given PDB id, else false
167 public boolean hasPdbId(String pdbId)
169 if (pdbEntry != null)
171 for (PDBEntry pdb : pdbEntry)
173 if (pdb.getId().equals(pdbId))
183 * Returns the number of modelled PDB file entries.
187 public int getPdbCount()
189 return pdbEntry == null ? 0 : pdbEntry.length;
192 public SequenceI[][] getSequence()
197 public String[][] getChains()
202 public DataSourceType getProtocol()
207 // TODO may remove this if calling methods can be pulled up here
208 protected void setPdbentry(PDBEntry[] pdbentry)
210 this.pdbEntry = pdbentry;
213 protected void setSequence(SequenceI[][] sequence)
215 this.sequence = sequence;
218 protected void setChains(String[][] chains)
220 this.chains = chains;
224 * Construct a title string for the viewer window based on the data Jalview
233 public String getViewerTitle(String viewerName, boolean verbose)
235 if (getSequence() == null || getSequence().length < 1
236 || getPdbCount() < 1 || getSequence()[0].length < 1)
238 return ("Jalview " + viewerName + " Window");
240 // TODO: give a more informative title when multiple structures are
242 StringBuilder title = new StringBuilder(64);
243 final PDBEntry pdbEntry = getPdbEntry(0);
244 title.append(viewerName + " view for " + getSequence()[0][0].getName()
245 + ":" + pdbEntry.getId());
249 if (pdbEntry.getProperty() != null)
251 if (pdbEntry.getProperty().get("method") != null)
253 title.append(" Method: ");
254 title.append(pdbEntry.getProperty().get("method"));
256 if (pdbEntry.getProperty().get("chains") != null)
258 title.append(" Chain:");
259 title.append(pdbEntry.getProperty().get("chains"));
263 return title.toString();
267 * Called by after closeViewer is called, to release any resources and
268 * references so they can be garbage collected. Override if needed.
270 protected void releaseUIResources()
275 public boolean isColourBySequence()
277 return colourBySequence;
280 public void setColourBySequence(boolean colourBySequence)
282 this.colourBySequence = colourBySequence;
285 protected void addSequenceAndChain(int pe, SequenceI[] seq,
288 if (pe < 0 || pe >= getPdbCount())
290 throw new Error(MessageManager.formatMessage(
291 "error.implementation_error_no_pdbentry_from_index",
292 new Object[] { Integer.valueOf(pe).toString() }));
294 final String nullChain = "TheNullChain";
295 List<SequenceI> s = new ArrayList<SequenceI>();
296 List<String> c = new ArrayList<String>();
297 if (getChains() == null)
299 setChains(new String[getPdbCount()][]);
301 if (getSequence()[pe] != null)
303 for (int i = 0; i < getSequence()[pe].length; i++)
305 s.add(getSequence()[pe][i]);
306 if (getChains()[pe] != null)
308 if (i < getChains()[pe].length)
310 c.add(getChains()[pe][i]);
319 if (tchain != null && tchain.length > 0)
326 for (int i = 0; i < seq.length; i++)
328 if (!s.contains(seq[i]))
331 if (tchain != null && i < tchain.length)
333 c.add(tchain[i] == null ? nullChain : tchain[i]);
337 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
338 getSequence()[pe] = tmp;
341 String[] tch = c.toArray(new String[c.size()]);
342 for (int i = 0; i < tch.length; i++)
344 if (tch[i] == nullChain)
349 getChains()[pe] = tch;
353 getChains()[pe] = null;
358 * add structures and any known sequence associations
360 * @returns the pdb entries added to the current set.
362 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
363 SequenceI[][] seq, String[][] chns)
365 List<PDBEntry> v = new ArrayList<PDBEntry>();
366 List<int[]> rtn = new ArrayList<int[]>();
367 for (int i = 0; i < getPdbCount(); i++)
369 v.add(getPdbEntry(i));
371 for (int i = 0; i < pdbe.length; i++)
373 int r = v.indexOf(pdbe[i]);
374 if (r == -1 || r >= getPdbCount())
376 rtn.add(new int[] { v.size(), i });
381 // just make sure the sequence/chain entries are all up to date
382 addSequenceAndChain(r, seq[i], chns[i]);
385 pdbe = v.toArray(new PDBEntry[v.size()]);
389 // expand the tied sequence[] and string[] arrays
390 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
391 String[][] sch = new String[getPdbCount()][];
392 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
393 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
396 pdbe = new PDBEntry[rtn.size()];
397 for (int r = 0; r < pdbe.length; r++)
399 int[] stri = (rtn.get(r));
400 // record the pdb file as a new addition
401 pdbe[r] = getPdbEntry(stri[0]);
402 // and add the new sequence/chain entries
403 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
414 * Add sequences to the pe'th pdbentry's sequence set.
419 public void addSequence(int pe, SequenceI[] seq)
421 addSequenceAndChain(pe, seq, null);
425 * add the given sequences to the mapping scope for the given pdb file handle
428 * - pdbFile identifier
430 * - set of sequences it can be mapped to
432 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
434 for (int pe = 0; pe < getPdbCount(); pe++)
436 if (getPdbEntry(pe).getFile().equals(pdbFile))
438 addSequence(pe, seq);
444 public abstract void highlightAtoms(List<AtomSpec> atoms);
446 protected boolean isNucleotide()
448 return this.nucleotide;
452 * Returns a readable description of all mappings for the wrapped pdbfile to
453 * any mapped sequences
459 public String printMappings()
461 if (pdbEntry == null)
465 StringBuilder sb = new StringBuilder(128);
466 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
468 String pdbfile = getPdbEntry(pdbe).getFile();
469 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
470 sb.append(getSsm().printMappings(pdbfile, seqs));
472 return sb.toString();
476 * Returns the mapped structure position for a given aligned column of a given
477 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
478 * not mapped to structure.
485 protected int getMappedPosition(SequenceI seq, int alignedPos,
486 StructureMapping mapping)
488 if (alignedPos >= seq.getLength())
493 if (Comparison.isGap(seq.getCharAt(alignedPos)))
497 int seqPos = seq.findPosition(alignedPos);
498 int pos = mapping.getPDBResNum(seqPos);
503 * Helper method to identify residues that can participate in a structure
504 * superposition command. For each structure, identify a sequence in the
505 * alignment which is mapped to the structure. Identify non-gapped columns in
506 * the sequence which have a mapping to a residue in the structure. Returns
507 * the index of the first structure that has a mapping to the alignment.
510 * the sequence alignment which is the basis of structure
513 * an array of booleans, indexed by alignment column, where true
514 * indicates that every structure has a mapped residue present in the
515 * column (so the column can participate in structure alignment)
517 * an array of data beans corresponding to pdb file index
520 protected int findSuperposableResidues(AlignmentI alignment,
521 boolean[] matched, SuperposeData[] structures)
523 int refStructure = -1;
524 String[] files = getPdbFile();
525 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
527 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
531 * Find the first mapped sequence (if any) for this PDB entry which is in
534 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
535 for (int s = 0; s < seqCountForPdbFile; s++)
537 for (StructureMapping mapping : mappings)
539 final SequenceI theSequence = getSequence()[pdbfnum][s];
540 if (mapping.getSequence() == theSequence
541 && alignment.findIndex(theSequence) > -1)
543 if (refStructure < 0)
545 refStructure = pdbfnum;
547 for (int r = 0; r < matched.length; r++)
553 int pos = getMappedPosition(theSequence, r, mapping);
554 if (pos < 1 || pos == lastPos)
560 structures[pdbfnum].pdbResNo[r] = pos;
562 String chain = mapping.getChain();
563 if (chain != null && chain.trim().length() > 0)
565 structures[pdbfnum].chain = chain;
567 structures[pdbfnum].pdbId = mapping.getPdbId();
568 structures[pdbfnum].isRna = theSequence.getRNA() != null;
569 // move on to next pdb file
570 s = seqCountForPdbFile;
580 * Returns true if the structure viewer has loaded all of the files of
581 * interest (identified by the file mapping having been set up), or false if
582 * any are still not loaded after a timeout interval.
586 protected boolean waitForFileLoad(String[] files)
589 * give up after 10 secs plus 1 sec per file
591 long starttime = System.currentTimeMillis();
592 long endTime = 10000 + 1000 * files.length + starttime;
593 String notLoaded = null;
595 boolean waiting = true;
596 while (waiting && System.currentTimeMillis() < endTime)
599 for (String file : files)
608 StructureMapping[] sm = getSsm().getMapping(file);
609 if (sm == null || sm.length == 0)
613 } catch (Throwable x)
623 .println("Timed out waiting for structure viewer to load file "
631 public boolean isListeningFor(SequenceI seq)
633 if (sequence != null)
635 for (SequenceI[] seqs : sequence)
639 for (SequenceI s : seqs)
642 || (s.getDatasetSequence() != null && s
643 .getDatasetSequence() == seq
644 .getDatasetSequence()))
655 public boolean isFinishedInit()
660 public void setFinishedInit(boolean fi)
662 this.finishedInit = fi;