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 String tfacName = "Temperature Factor";
84 public PDBChain(String thePdbid, String theId, String tempFactorColumnName)
86 this.pdbid = thePdbid == null ? thePdbid : thePdbid.toLowerCase();
88 if (tempFactorColumnName!=null && tempFactorColumnName.length()>0)
90 tfacName = tempFactorColumnName;
95 * import chain data assuming Temperature Factor is in the Temperature Factor column
99 public PDBChain(String thePdbid, String theId)
101 this(thePdbid,theId, null);
105 * character used to write newlines
107 protected String newline = System.getProperty("line.separator");
109 public Mapping shadowMap;
111 public void setNewlineString(String nl)
116 public String getNewlineString()
121 public String print()
123 StringBuilder tmp = new StringBuilder(256);
127 tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
128 .append(" ").append(offset).append(newline);
131 return tmp.toString();
135 * Annotate the residues with their corresponding positions in s1 using the
136 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
142 public void makeExactMapping(AlignSeq as, SequenceI s1)
144 int pdbpos = as.getSeq2Start() - 2;
145 int alignpos = s1.getStart() + as.getSeq1Start() - 3;
146 // first clear out any old alignmentMapping values:
147 for (Atom atom : atoms)
149 atom.alignmentMapping = -1;
151 // and now trace the alignment onto the atom set.
152 for (int i = 0; i < as.astr1.length(); i++)
154 if (as.astr1.charAt(i) != '-')
159 if (as.astr2.charAt(i) != '-')
164 boolean sameResidue = Comparison.isSameResidue(as.astr1.charAt(i),
165 as.astr2.charAt(i), false);
168 if (pdbpos >= residues.size())
172 Residue res = residues.elementAt(pdbpos);
173 for (Atom atom : res.atoms)
175 atom.alignmentMapping = alignpos;
182 * Annotate the residues with their corresponding positions in s1 using the
183 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
189 public void makeExactMapping(StructureMapping mapping, SequenceI s1)
191 // first clear out any old alignmentMapping values:
192 for (Atom atom : atoms)
194 atom.alignmentMapping = -1;
197 while (ds.getDatasetSequence() != null)
199 ds = ds.getDatasetSequence();
202 for (Residue res : residues)
204 // res.number isn't set correctly for discontinuous/mismapped residues
205 int seqpos = mapping.getSeqPos(res.atoms.get(0).resNumber);
206 char strchar = sequence.getCharAt(pdboffset++);
207 if (seqpos == StructureMapping.UNASSIGNED_VALUE)
211 char seqchar = ds.getCharAt(seqpos - ds.getStart());
213 boolean sameResidue = Comparison.isSameResidue(
214 seqchar, strchar, false);
217 for (Atom atom : res.atoms)
219 atom.alignmentMapping = seqpos - 1;
226 * Copies over the RESNUM seqfeatures from the internal chain sequence to the
231 * The Status of the transferred annotation
233 * @param altPDBID the group id for the features on the destination sequence (e.g. the official accession ID)
235 public void transferRESNUMFeatures(SequenceI seq,
236 String status, String altPDBID)
243 while (sq != null && sq.getDatasetSequence() != null)
245 sq = sq.getDatasetSequence();
253 * Remove any existing features for this chain if they exist ?
254 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
255 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
256 * ? for (int i=0; i<seqsfeatures.length; i++) { }
260 status = PDBChain.IEASTATUS;
263 List<SequenceFeature> features = sequence.getSequenceFeatures();
264 for (SequenceFeature feature : features)
266 if (feature.getFeatureGroup() != null
267 && feature.getFeatureGroup().equals(pdbid))
269 int newBegin = 1 + residues.elementAt(feature.getBegin() - offset).atoms
270 .elementAt(0).alignmentMapping;
271 int newEnd = 1 + residues.elementAt(feature.getEnd() - offset).atoms
272 .elementAt(0).alignmentMapping;
273 SequenceFeature tx = new SequenceFeature(feature, newBegin, newEnd,
274 altPDBID, feature.getScore());
276 + ((tx.getStatus() == null || tx.getStatus().length() == 0)
278 : ":" + tx.getStatus()));
279 if (tx.begin != 0 && tx.end != 0)
281 sq.addSequenceFeature(tx);
288 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
289 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
290 * residues contain a P not a CA.
292 public void makeCaBondList()
296 for (int i = 0; i < (residues.size() - 1); i++)
298 Residue tmpres = residues.elementAt(i);
299 Residue tmpres2 = residues.elementAt(i + 1);
300 Atom at1 = tmpres.findAtom("CA");
301 Atom at2 = tmpres2.findAtom("CA");
303 if ((at1 == null) && (at2 == null))
306 at1 = tmpres.findAtom("P");
307 at2 = tmpres2.findAtom("P");
309 if ((at1 != null) && (at2 != null))
311 if (at1.chain.equals(at2.chain))
322 System.out.println("not found " + i);
327 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
330 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
337 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
343 public void makeBond(Atom at1, Atom at2)
345 bonds.addElement(new Bond(at1, at2));
349 * Traverses the list of atoms and
351 * <li>constructs a list of Residues, each containing all the atoms that share
352 * the same residue number</li>
353 * <li>adds a RESNUM sequence feature for each position</li>
354 * <li>creates the sequence string</li>
355 * <li>determines if nucleotide</li>
356 * <li>saves the residue number of the first atom as 'offset'</li>
357 * <li>adds temp factor annotation if the flag is set to do so</li>
360 * @param visibleChainAnnotation
362 public void makeResidueList(boolean visibleChainAnnotation)
366 boolean deoxyn = false;
367 boolean nucleotide = false;
368 StringBuilder seq = new StringBuilder(256);
369 Vector<SequenceFeature> resFeatures = new Vector<>();
370 Vector<Annotation> resAnnotation = new Vector<>();
371 int iSize = atoms.size() - 1;
375 for (int i = 0; i <= iSize; i++)
377 Atom tmp = atoms.elementAt(i);
378 resNumber = tmp.resNumber;
379 insCode = tmp.insCode;
389 Vector<Atom> resAtoms = new Vector<>();
390 // Add atoms to a vector while the residue number
391 // remains the same as the first atom's resNumber (res)
392 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
394 resAtoms.add(atoms.elementAt(i));
397 if (i < atoms.size())
399 resNumber = atoms.elementAt(i).resNumber;
400 insCode = atoms.elementAt(i).insCode;
408 // We need this to keep in step with the outer for i = loop
411 // Add inserted residues as features to the base residue
412 Atom currAtom = resAtoms.get(0);
413 if (currAtom.insCode != ' ' && !residues.isEmpty()
414 && residues.lastElement().atoms
415 .get(0).resNumber == currAtom.resNumber)
417 String desc = currAtom.resName + ":" + currAtom.resNumIns + " "
419 SequenceFeature sf = new SequenceFeature("INSERTION", desc, offset
420 + count - 1, offset + count - 1, "PDB_INS");
421 resFeatures.addElement(sf);
422 residues.lastElement().atoms.addAll(resAtoms);
426 // Make a new Residue object with the new atoms vector
427 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
429 Residue tmpres = residues.lastElement();
430 Atom tmpat = tmpres.atoms.get(0);
431 // Make A new SequenceFeature for the current residue numbering
432 String desc = tmpat.resName
433 + ":" + tmpat.resNumIns + " " + pdbid + id;
434 SequenceFeature sf = new SequenceFeature(RESNUM_FEATURE, desc,
435 offset + count, offset + count, pdbid);
436 resFeatures.addElement(sf);
437 resAnnotation.addElement(new Annotation(tmpat.tfactor));
438 // Keep totting up the sequence
440 if ((symbol = ResidueProperties.getAA3Hash()
441 .get(tmpat.resName)) == null)
443 String nucname = tmpat.resName.trim();
444 // use the aaIndex rather than call 'toLower' - which would take a bit
446 deoxyn = nucname.length() == 2
447 && ResidueProperties.aaIndex[nucname
448 .charAt(0)] == ResidueProperties.aaIndex['D'];
449 if (tmpat.name.equalsIgnoreCase("CA")
450 || ResidueProperties.nucleotideIndex[nucname
451 .charAt((deoxyn ? 1 : 0))] == -1)
453 char r = ResidueProperties.getSingleCharacterCode(
454 ResidueProperties.getCanonicalAminoAcid(tmpat.resName));
455 seq.append(r == '0' ? 'X' : r);
456 // System.err.println("PDBReader:Null aa3Hash for " +
463 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
471 "Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
473 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
484 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
488 // Add normalised feature scores to RESNUM indicating start/end of sequence
489 // sf.setScore(offset+count);
491 // System.out.println("PDB Sequence is :\nSequence = " + seq);
492 // System.out.println("No of residues = " + residues.size());
494 if (StructureImportSettings.isShowSeqFeatures())
496 iSize = resFeatures.size();
497 for (int i = 0; i < iSize; i++)
499 sequence.addSequenceFeature(resFeatures.elementAt(i));
500 resFeatures.setElementAt(null, i);
503 if (visibleChainAnnotation)
505 Annotation[] annots = new Annotation[resAnnotation.size()];
508 iSize = annots.length;
509 for (int i = 0; i < iSize; i++)
511 annots[i] = resAnnotation.elementAt(i);
512 max = Math.max(max, annots[i].value);
513 min = Math.min(min, annots[i].value);
514 resAnnotation.setElementAt(null, i);
516 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
517 tfacName, tfacName + " for " + pdbid + id,
518 annots, min, max, AlignmentAnnotation.LINE_GRAPH);
520 tfactorann.setCalcId(getClass().getName());
522 tfactorann.setSequenceRef(sequence);
523 sequence.addAlignmentAnnotation(tfactorann);
529 * Colour start/end of bonds by charge
531 * <li>ASP and GLU red</li>
532 * <li>LYS and ARG blue</li>
533 * <li>CYS yellow</li>
534 * <li>others light gray</li>
537 public void setChargeColours()
541 if (b.at1 != null && b.at2 != null)
543 b.startCol = getChargeColour(b.at1.resName);
544 b.endCol = getChargeColour(b.at2.resName);
548 b.startCol = Color.gray;
549 b.endCol = Color.gray;
554 public static Color getChargeColour(String resName)
556 Color result = Color.lightGray;
557 if ("ASP".equals(resName) || "GLU".equals(resName))
561 else if ("LYS".equals(resName) || "ARG".equals(resName))
565 else if ("CYS".equals(resName))
567 result = Color.yellow;
573 * Sets the start/end colours of bonds to those of the start/end atoms
574 * according to the specified colour scheme. Note: currently only works for
579 public void setChainColours(ColourSchemeI cs)
586 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
587 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
590 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
591 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
594 } catch (Exception e)
596 b.startCol = Color.gray;
597 b.endCol = Color.gray;
602 public void setChainColours(Color col)
612 * copy any sequence annotation onto the sequence mapped using the provided
616 * - positional mapping between destination sequence and pdb resnum
618 * - mapping between destination sequence and local chain
620 public void transferResidueAnnotation(StructureMapping mapping,
621 jalview.datamodel.Mapping sqmpping)
623 SequenceI sq = mapping.getSequence();
625 if (sqmpping == null)
627 // SIFTS mappings are recorded in the StructureMapping object...
629 sqmpping = mapping.getSeqToPdbMapping();
633 while (dsq.getDatasetSequence() != null)
635 dsq = dsq.getDatasetSequence();
637 // any annotation will be transferred onto the dataset sequence
639 if (shadow != null && shadow.getAnnotation() != null)
642 for (AlignmentAnnotation ana : shadow.getAnnotation())
644 // match on calcId, label and description so annotations from
645 // different structures are preserved
646 List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
647 ana.getCalcId(), ana.label, ana.description);
648 if (transfer == null || transfer.size() == 0)
650 ana = new AlignmentAnnotation(ana);
651 ana.liftOver(sequence, shadowMap);
652 ana.liftOver(dsq, sqmpping);
653 dsq.addAlignmentAnnotation(ana);
663 if (sequence != null && sequence.getAnnotation() != null)
665 for (AlignmentAnnotation ana : sequence.getAnnotation())
667 // match on calcId, label and description so annotations from
668 // different structures are preserved
669 List<AlignmentAnnotation> transfer = dsq
670 .getAlignmentAnnotations(ana.getCalcId(), ana.label,
672 if (transfer == null || transfer.size() == 0)
674 ana = new AlignmentAnnotation(ana);
675 ana.liftOver(dsq, sqmpping);
676 dsq.addAlignmentAnnotation(ana);
677 // mapping.transfer(ana);
688 // Useful for debugging mappings - adds annotation for mapped position
689 float min = -1, max = 0;
690 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
691 for (int i = sq.getStart(), j = sq
692 .getEnd(), k = 0; i <= j; i++, k++)
694 int prn = mapping.getPDBResNum(k + 1);
696 an[k] = new Annotation(prn);
714 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
715 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
716 an, min, max, AlignmentAnnotation.LINE_GRAPH));