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;
54 private PDBEntry[] pdbEntry;
57 * sequences mapped to each pdbentry
59 private SequenceI[][] sequence;
62 * array of target chains for sequences - tied to pdbentry and sequence[]
64 private String[][] chains;
67 * datasource protocol for access to PDBEntrylatest
69 String protocol = null;
71 protected boolean colourBySequence = true;
73 private boolean nucleotide;
75 private boolean finishedInit = false;
78 * Data bean class to simplify parameterisation in superposeStructures
80 protected class SuperposeData
83 * Constructor with alignment width argument
87 public SuperposeData(int width)
89 pdbResNo = new int[width];
92 public String filename;
96 public String chain = "";
101 * The pdb residue number (if any) mapped to each column of the alignment
103 public int[] pdbResNo;
112 public AAStructureBindingModel(StructureSelectionManager ssm,
116 this.sequence = seqs;
128 public AAStructureBindingModel(StructureSelectionManager ssm,
129 PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
133 this.sequence = sequenceIs;
134 this.nucleotide = Comparison.isNucleotide(sequenceIs);
135 this.chains = chains;
136 this.pdbEntry = pdbentry;
137 this.protocol = protocol;
140 this.chains = new String[pdbentry.length][];
144 public StructureSelectionManager getSsm()
150 * Returns the i'th PDBEntry (or null)
155 public PDBEntry getPdbEntry(int i)
157 return (pdbEntry != null && pdbEntry.length > i) ? pdbEntry[i] : null;
161 * Answers true if this binding includes the given PDB id, else false
166 public boolean hasPdbId(String pdbId)
168 if (pdbEntry != null)
170 for (PDBEntry pdb : pdbEntry)
172 if (pdb.getId().equals(pdbId))
182 * Returns the number of modelled PDB file entries.
186 public int getPdbCount()
188 return pdbEntry == null ? 0 : pdbEntry.length;
191 public SequenceI[][] getSequence()
196 public String[][] getChains()
201 public String getProtocol()
206 // TODO may remove this if calling methods can be pulled up here
207 protected void setPdbentry(PDBEntry[] pdbentry)
209 this.pdbEntry = pdbentry;
212 protected void setSequence(SequenceI[][] sequence)
214 this.sequence = sequence;
217 protected void setChains(String[][] chains)
219 this.chains = chains;
223 * Construct a title string for the viewer window based on the data Jalview
232 public String getViewerTitle(String viewerName, boolean verbose)
234 if (getSequence() == null || getSequence().length < 1
235 || getPdbCount() < 1 || getSequence()[0].length < 1)
237 return ("Jalview " + viewerName + " Window");
239 // TODO: give a more informative title when multiple structures are
241 StringBuilder title = new StringBuilder(64);
242 final PDBEntry pdbEntry = getPdbEntry(0);
243 title.append(viewerName + " view for " + getSequence()[0][0].getName()
244 + ":" + pdbEntry.getId());
248 if (pdbEntry.getProperty() != null)
250 if (pdbEntry.getProperty().get("method") != null)
252 title.append(" Method: ");
253 title.append(pdbEntry.getProperty().get("method"));
255 if (pdbEntry.getProperty().get("chains") != null)
257 title.append(" Chain:");
258 title.append(pdbEntry.getProperty().get("chains"));
262 return title.toString();
266 * Called by after closeViewer is called, to release any resources and
267 * references so they can be garbage collected. Override if needed.
269 protected void releaseUIResources()
274 public boolean isColourBySequence()
276 return colourBySequence;
279 public void setColourBySequence(boolean colourBySequence)
281 this.colourBySequence = colourBySequence;
284 protected void addSequenceAndChain(int pe, SequenceI[] seq,
287 if (pe < 0 || pe >= getPdbCount())
289 throw new Error(MessageManager.formatMessage(
290 "error.implementation_error_no_pdbentry_from_index",
291 new Object[] { Integer.valueOf(pe).toString() }));
293 final String nullChain = "TheNullChain";
294 List<SequenceI> s = new ArrayList<SequenceI>();
295 List<String> c = new ArrayList<String>();
296 if (getChains() == null)
298 setChains(new String[getPdbCount()][]);
300 if (getSequence()[pe] != null)
302 for (int i = 0; i < getSequence()[pe].length; i++)
304 s.add(getSequence()[pe][i]);
305 if (getChains()[pe] != null)
307 if (i < getChains()[pe].length)
309 c.add(getChains()[pe][i]);
318 if (tchain != null && tchain.length > 0)
325 for (int i = 0; i < seq.length; i++)
327 if (!s.contains(seq[i]))
330 if (tchain != null && i < tchain.length)
332 c.add(tchain[i] == null ? nullChain : tchain[i]);
336 SequenceI[] tmp = s.toArray(new SequenceI[s.size()]);
337 getSequence()[pe] = tmp;
340 String[] tch = c.toArray(new String[c.size()]);
341 for (int i = 0; i < tch.length; i++)
343 if (tch[i] == nullChain)
348 getChains()[pe] = tch;
352 getChains()[pe] = null;
357 * add structures and any known sequence associations
359 * @returns the pdb entries added to the current set.
361 public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
362 SequenceI[][] seq, String[][] chns)
364 List<PDBEntry> v = new ArrayList<PDBEntry>();
365 List<int[]> rtn = new ArrayList<int[]>();
366 for (int i = 0; i < getPdbCount(); i++)
368 v.add(getPdbEntry(i));
370 for (int i = 0; i < pdbe.length; i++)
372 int r = v.indexOf(pdbe[i]);
373 if (r == -1 || r >= getPdbCount())
375 rtn.add(new int[] { v.size(), i });
380 // just make sure the sequence/chain entries are all up to date
381 addSequenceAndChain(r, seq[i], chns[i]);
384 pdbe = v.toArray(new PDBEntry[v.size()]);
388 // expand the tied sequence[] and string[] arrays
389 SequenceI[][] sqs = new SequenceI[getPdbCount()][];
390 String[][] sch = new String[getPdbCount()][];
391 System.arraycopy(getSequence(), 0, sqs, 0, getSequence().length);
392 System.arraycopy(getChains(), 0, sch, 0, this.getChains().length);
395 pdbe = new PDBEntry[rtn.size()];
396 for (int r = 0; r < pdbe.length; r++)
398 int[] stri = (rtn.get(r));
399 // record the pdb file as a new addition
400 pdbe[r] = getPdbEntry(stri[0]);
401 // and add the new sequence/chain entries
402 addSequenceAndChain(stri[0], seq[stri[1]], chns[stri[1]]);
413 * Add sequences to the pe'th pdbentry's sequence set.
418 public void addSequence(int pe, SequenceI[] seq)
420 addSequenceAndChain(pe, seq, null);
424 * add the given sequences to the mapping scope for the given pdb file handle
427 * - pdbFile identifier
429 * - set of sequences it can be mapped to
431 public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
433 for (int pe = 0; pe < getPdbCount(); pe++)
435 if (getPdbEntry(pe).getFile().equals(pdbFile))
437 addSequence(pe, seq);
443 public abstract void highlightAtoms(List<AtomSpec> atoms);
445 protected boolean isNucleotide()
447 return this.nucleotide;
451 * Returns a readable description of all mappings for the wrapped pdbfile to
452 * any mapped sequences
458 public String printMappings()
460 if (pdbEntry == null)
464 StringBuilder sb = new StringBuilder(128);
465 for (int pdbe = 0; pdbe < getPdbCount(); pdbe++)
467 String pdbfile = getPdbEntry(pdbe).getFile();
468 List<SequenceI> seqs = Arrays.asList(getSequence()[pdbe]);
469 sb.append(getSsm().printMappings(pdbfile, seqs));
471 return sb.toString();
475 * Returns the mapped structure position for a given aligned column of a given
476 * sequence, or -1 if the column is gapped, beyond the end of the sequence, or
477 * not mapped to structure.
484 protected int getMappedPosition(SequenceI seq, int alignedPos,
485 StructureMapping mapping)
487 if (alignedPos >= seq.getLength())
492 if (Comparison.isGap(seq.getCharAt(alignedPos)))
496 int seqPos = seq.findPosition(alignedPos);
497 int pos = mapping.getPDBResNum(seqPos);
502 * Helper method to identify residues that can participate in a structure
503 * superposition command. For each structure, identify a sequence in the
504 * alignment which is mapped to the structure. Identify non-gapped columns in
505 * the sequence which have a mapping to a residue in the structure. Returns
506 * the index of the first structure that has a mapping to the alignment.
509 * the sequence alignment which is the basis of structure
512 * an array of booleans, indexed by alignment column, where true
513 * indicates that every structure has a mapped residue present in the
514 * column (so the column can participate in structure alignment)
516 * an array of data beans corresponding to pdb file index
519 protected int findSuperposableResidues(AlignmentI alignment,
520 boolean[] matched, SuperposeData[] structures)
522 int refStructure = -1;
523 String[] files = getPdbFile();
524 for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
526 StructureMapping[] mappings = getSsm().getMapping(files[pdbfnum]);
530 * Find the first mapped sequence (if any) for this PDB entry which is in
533 final int seqCountForPdbFile = getSequence()[pdbfnum].length;
534 for (int s = 0; s < seqCountForPdbFile; s++)
536 for (StructureMapping mapping : mappings)
538 final SequenceI theSequence = getSequence()[pdbfnum][s];
539 if (mapping.getSequence() == theSequence
540 && alignment.findIndex(theSequence) > -1)
542 if (refStructure < 0)
544 refStructure = pdbfnum;
546 for (int r = 0; r < matched.length; r++)
552 int pos = getMappedPosition(theSequence, r, mapping);
553 if (pos < 1 || pos == lastPos)
559 structures[pdbfnum].pdbResNo[r] = pos;
561 String chain = mapping.getChain();
562 if (chain != null && chain.trim().length() > 0)
564 structures[pdbfnum].chain = chain;
566 structures[pdbfnum].pdbId = mapping.getPdbId();
567 structures[pdbfnum].isRna = theSequence.getRNA() != null;
568 // move on to next pdb file
569 s = seqCountForPdbFile;
579 * Returns true if the structure viewer has loaded all of the files of
580 * interest (identified by the file mapping having been set up), or false if
581 * any are still not loaded after a timeout interval.
585 protected boolean waitForFileLoad(String[] files)
588 * give up after 10 secs plus 1 sec per file
590 long starttime = System.currentTimeMillis();
591 long endTime = 10000 + 1000 * files.length + starttime;
592 String notLoaded = null;
594 boolean waiting = true;
595 while (waiting && System.currentTimeMillis() < endTime)
598 for (String file : files)
603 StructureMapping[] sm = getSsm().getMapping(file);
604 if (sm == null || sm.length == 0)
608 } catch (Throwable x)
618 .println("Timed out waiting for structure viewer to load file "
626 public boolean isListeningFor(SequenceI seq)
628 if (sequence != null)
630 for (SequenceI[] seqs : sequence)
634 for (SequenceI s : seqs)
647 public boolean isFinishedInit()
652 public void setFinishedInit(boolean fi)
654 this.finishedInit = fi;