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.
23 import jalview.analysis.AlignSeq;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.Annotation;
26 import jalview.datamodel.Mapping;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.schemes.ColourSchemeI;
31 import jalview.schemes.ResidueProperties;
32 import jalview.structure.StructureImportSettings;
33 import jalview.structure.StructureMapping;
34 import jalview.util.Comparison;
36 import java.awt.Color;
37 import java.util.List;
38 import java.util.Vector;
42 public static final String RESNUM_FEATURE = "RESNUM";
44 private static final String IEASTATUS = "IEA:jalview";
48 public Vector<Bond> bonds = new Vector<>();
50 public Vector<Atom> atoms = new Vector<>();
52 public Vector<Residue> residues = new Vector<>();
57 * sequence is the sequence extracted by the chain parsing code
59 public SequenceI sequence;
62 * shadow is the sequence created by any other parsing processes (e.g. Jmol,
65 public SequenceI shadow = null;
67 public boolean isNa = false;
69 public boolean isVisible = true;
71 public int pdbstart = 0;
73 public int pdbend = 0;
75 public int seqstart = 0;
77 public int seqend = 0;
79 public String pdbid = "";
81 public PDBChain(String thePdbid, String theId, boolean isAlphaFoldModel)
83 this.pdbid = thePdbid == null ? thePdbid : thePdbid.toLowerCase();
85 this.alphaFoldModel = isAlphaFoldModel;
89 * character used to write newlines
91 protected String newline = System.getProperty("line.separator");
93 public Mapping shadowMap;
95 private boolean alphaFoldModel;
97 public void setNewlineString(String nl)
102 public String getNewlineString()
107 public String print()
109 StringBuilder tmp = new StringBuilder(256);
113 tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
114 .append(" ").append(offset).append(newline);
117 return tmp.toString();
121 * Annotate the residues with their corresponding positions in s1 using the
122 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
128 public void makeExactMapping(AlignSeq as, SequenceI s1)
130 int pdbpos = as.getSeq2Start() - 2;
131 int alignpos = s1.getStart() + as.getSeq1Start() - 3;
132 // first clear out any old alignmentMapping values:
133 for (Atom atom : atoms)
135 atom.alignmentMapping = -1;
137 // and now trace the alignment onto the atom set.
138 for (int i = 0; i < as.astr1.length(); i++)
140 if (as.astr1.charAt(i) != '-')
145 if (as.astr2.charAt(i) != '-')
150 boolean sameResidue = Comparison.isSameResidue(as.astr1.charAt(i),
151 as.astr2.charAt(i), false);
154 if (pdbpos >= residues.size())
158 Residue res = residues.elementAt(pdbpos);
159 for (Atom atom : res.atoms)
161 atom.alignmentMapping = alignpos;
168 * Annotate the residues with their corresponding positions in s1 using the
169 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
175 public void makeExactMapping(StructureMapping mapping, SequenceI s1)
177 // first clear out any old alignmentMapping values:
178 for (Atom atom : atoms)
180 atom.alignmentMapping = -1;
183 while (ds.getDatasetSequence() != null)
185 ds = ds.getDatasetSequence();
188 for (Residue res : residues)
190 // res.number isn't set correctly for discontinuous/mismapped residues
191 int seqpos = mapping.getSeqPos(res.atoms.get(0).resNumber);
192 char strchar = sequence.getCharAt(pdboffset++);
193 if (seqpos == StructureMapping.UNASSIGNED_VALUE)
197 char seqchar = ds.getCharAt(seqpos - ds.getStart());
199 boolean sameResidue = Comparison.isSameResidue(
200 seqchar, strchar, false);
203 for (Atom atom : res.atoms)
205 atom.alignmentMapping = seqpos - 1;
212 * Copies over the RESNUM seqfeatures from the internal chain sequence to the
217 * The Status of the transferred annotation
219 public void transferRESNUMFeatures(SequenceI seq,
223 while (sq != null && sq.getDatasetSequence() != null)
225 sq = sq.getDatasetSequence();
233 * Remove any existing features for this chain if they exist ?
234 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
235 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
236 * ? for (int i=0; i<seqsfeatures.length; i++) { }
240 status = PDBChain.IEASTATUS;
243 List<SequenceFeature> features = sequence.getSequenceFeatures();
244 for (SequenceFeature feature : features)
246 if (feature.getFeatureGroup() != null
247 && feature.getFeatureGroup().equals(pdbid))
249 int newBegin = 1 + residues.elementAt(feature.getBegin() - offset).atoms
250 .elementAt(0).alignmentMapping;
251 int newEnd = 1 + residues.elementAt(feature.getEnd() - offset).atoms
252 .elementAt(0).alignmentMapping;
253 SequenceFeature tx = new SequenceFeature(feature, newBegin, newEnd,
254 feature.getFeatureGroup(), feature.getScore());
256 + ((tx.getStatus() == null || tx.getStatus().length() == 0)
258 : ":" + tx.getStatus()));
259 if (tx.begin != 0 && tx.end != 0)
261 sq.addSequenceFeature(tx);
268 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
269 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
270 * residues contain a P not a CA.
272 public void makeCaBondList()
276 for (int i = 0; i < (residues.size() - 1); i++)
278 Residue tmpres = residues.elementAt(i);
279 Residue tmpres2 = residues.elementAt(i + 1);
280 Atom at1 = tmpres.findAtom("CA");
281 Atom at2 = tmpres2.findAtom("CA");
283 if ((at1 == null) && (at2 == null))
286 at1 = tmpres.findAtom("P");
287 at2 = tmpres2.findAtom("P");
289 if ((at1 != null) && (at2 != null))
291 if (at1.chain.equals(at2.chain))
302 System.out.println("not found " + i);
307 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
310 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
317 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
323 public void makeBond(Atom at1, Atom at2)
325 bonds.addElement(new Bond(at1, at2));
329 * Traverses the list of atoms and
331 * <li>constructs a list of Residues, each containing all the atoms that share
332 * the same residue number</li>
333 * <li>adds a RESNUM sequence feature for each position</li>
334 * <li>creates the sequence string</li>
335 * <li>determines if nucleotide</li>
336 * <li>saves the residue number of the first atom as 'offset'</li>
337 * <li>adds temp factor annotation if the flag is set to do so</li>
340 * @param visibleChainAnnotation
342 public void makeResidueList(boolean visibleChainAnnotation)
346 boolean deoxyn = false;
347 boolean nucleotide = false;
348 StringBuilder seq = new StringBuilder(256);
349 Vector<SequenceFeature> resFeatures = new Vector<>();
350 Vector<Annotation> resAnnotation = new Vector<>();
351 int iSize = atoms.size() - 1;
355 for (int i = 0; i <= iSize; i++)
357 Atom tmp = atoms.elementAt(i);
358 resNumber = tmp.resNumber;
359 insCode = tmp.insCode;
369 Vector<Atom> resAtoms = new Vector<>();
370 // Add atoms to a vector while the residue number
371 // remains the same as the first atom's resNumber (res)
372 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
374 resAtoms.add(atoms.elementAt(i));
377 if (i < atoms.size())
379 resNumber = atoms.elementAt(i).resNumber;
380 insCode = atoms.elementAt(i).insCode;
388 // We need this to keep in step with the outer for i = loop
391 // Add inserted residues as features to the base residue
392 Atom currAtom = resAtoms.get(0);
393 if (currAtom.insCode != ' ' && !residues.isEmpty()
394 && residues.lastElement().atoms
395 .get(0).resNumber == currAtom.resNumber)
397 String desc = currAtom.resName + ":" + currAtom.resNumIns + " "
399 SequenceFeature sf = new SequenceFeature("INSERTION", desc, offset
400 + count - 1, offset + count - 1, "PDB_INS");
401 resFeatures.addElement(sf);
402 residues.lastElement().atoms.addAll(resAtoms);
406 // Make a new Residue object with the new atoms vector
407 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
409 Residue tmpres = residues.lastElement();
410 Atom tmpat = tmpres.atoms.get(0);
411 // Make A new SequenceFeature for the current residue numbering
412 String desc = tmpat.resName
413 + ":" + tmpat.resNumIns + " " + pdbid + id;
414 SequenceFeature sf = new SequenceFeature(RESNUM_FEATURE, desc,
415 offset + count, offset + count, pdbid);
416 resFeatures.addElement(sf);
417 resAnnotation.addElement(new Annotation(tmpat.tfactor));
418 // Keep totting up the sequence
420 if ((symbol = ResidueProperties.getAA3Hash()
421 .get(tmpat.resName)) == null)
423 String nucname = tmpat.resName.trim();
424 // use the aaIndex rather than call 'toLower' - which would take a bit
426 deoxyn = nucname.length() == 2
427 && ResidueProperties.aaIndex[nucname
428 .charAt(0)] == ResidueProperties.aaIndex['D'];
429 if (tmpat.name.equalsIgnoreCase("CA")
430 || ResidueProperties.nucleotideIndex[nucname
431 .charAt((deoxyn ? 1 : 0))] == -1)
433 char r = ResidueProperties.getSingleCharacterCode(
434 ResidueProperties.getCanonicalAminoAcid(tmpat.resName));
435 seq.append(r == '0' ? 'X' : r);
436 // System.err.println("PDBReader:Null aa3Hash for " +
443 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
451 "Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
453 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
464 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
468 // Add normalised feature scores to RESNUM indicating start/end of sequence
469 // sf.setScore(offset+count);
471 // System.out.println("PDB Sequence is :\nSequence = " + seq);
472 // System.out.println("No of residues = " + residues.size());
474 if (StructureImportSettings.isShowSeqFeatures())
476 iSize = resFeatures.size();
477 for (int i = 0; i < iSize; i++)
479 sequence.addSequenceFeature(resFeatures.elementAt(i));
480 resFeatures.setElementAt(null, i);
483 if (visibleChainAnnotation)
485 Annotation[] annots = new Annotation[resAnnotation.size()];
488 iSize = annots.length;
489 for (int i = 0; i < iSize; i++)
491 annots[i] = resAnnotation.elementAt(i);
492 max = Math.max(max, annots[i].value);
493 min = Math.min(min, annots[i].value);
494 resAnnotation.setElementAt(null, i);
496 String tfacName = "Temperature Factor";
497 if (isAlphaFoldModel())
499 tfacName = "Alphafold Reliability";
501 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
502 tfacName, tfacName + " for " + pdbid + id,
503 annots, min, max, AlignmentAnnotation.LINE_GRAPH);
504 tfactorann.setSequenceRef(sequence);
505 sequence.addAlignmentAnnotation(tfactorann);
509 private boolean isAlphaFoldModel()
511 return alphaFoldModel;
515 * Colour start/end of bonds by charge
517 * <li>ASP and GLU red</li>
518 * <li>LYS and ARG blue</li>
519 * <li>CYS yellow</li>
520 * <li>others light gray</li>
523 public void setChargeColours()
527 if (b.at1 != null && b.at2 != null)
529 b.startCol = getChargeColour(b.at1.resName);
530 b.endCol = getChargeColour(b.at2.resName);
534 b.startCol = Color.gray;
535 b.endCol = Color.gray;
540 public static Color getChargeColour(String resName)
542 Color result = Color.lightGray;
543 if ("ASP".equals(resName) || "GLU".equals(resName))
547 else if ("LYS".equals(resName) || "ARG".equals(resName))
551 else if ("CYS".equals(resName))
553 result = Color.yellow;
559 * Sets the start/end colours of bonds to those of the start/end atoms
560 * according to the specified colour scheme. Note: currently only works for
565 public void setChainColours(ColourSchemeI cs)
572 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
573 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
576 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
577 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
580 } catch (Exception e)
582 b.startCol = Color.gray;
583 b.endCol = Color.gray;
588 public void setChainColours(Color col)
598 * copy any sequence annotation onto the sequence mapped using the provided
602 * - positional mapping between destination sequence and pdb resnum
604 * - mapping between destination sequence and local chain
606 public void transferResidueAnnotation(StructureMapping mapping,
607 jalview.datamodel.Mapping sqmpping)
609 SequenceI sq = mapping.getSequence();
611 if (sqmpping == null)
613 // SIFTS mappings are recorded in the StructureMapping object...
615 sqmpping = mapping.getSeqToPdbMapping();
619 while (dsq.getDatasetSequence() != null)
621 dsq = dsq.getDatasetSequence();
623 // any annotation will be transferred onto the dataset sequence
625 if (shadow != null && shadow.getAnnotation() != null)
628 for (AlignmentAnnotation ana : shadow.getAnnotation())
630 List<AlignmentAnnotation> transfer = sq
631 .getAlignmentAnnotations(ana.getCalcId(), ana.label);
632 if (transfer == null || transfer.size() == 0)
634 ana = new AlignmentAnnotation(ana);
635 ana.liftOver(sequence, shadowMap);
636 ana.liftOver(dsq, sqmpping);
637 dsq.addAlignmentAnnotation(ana);
647 if (sequence != null && sequence.getAnnotation() != null)
649 for (AlignmentAnnotation ana : sequence.getAnnotation())
651 List<AlignmentAnnotation> transfer = dsq
652 .getAlignmentAnnotations(ana.getCalcId(), ana.label);
653 if (transfer == null || transfer.size() == 0)
655 ana = new AlignmentAnnotation(ana);
656 ana.liftOver(dsq, sqmpping);
657 dsq.addAlignmentAnnotation(ana);
658 // mapping.transfer(ana);
669 // Useful for debugging mappings - adds annotation for mapped position
670 float min = -1, max = 0;
671 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
672 for (int i = sq.getStart(), j = sq
673 .getEnd(), k = 0; i <= j; i++, k++)
675 int prn = mapping.getPDBResNum(k + 1);
677 an[k] = new Annotation(prn);
695 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
696 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
697 an, min, max, AlignmentAnnotation.LINE_GRAPH));