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 java.awt.Color;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.Vector;
28 import jalview.analysis.AlignSeq;
29 import jalview.datamodel.AlignmentAnnotation;
30 import jalview.datamodel.Annotation;
31 import jalview.datamodel.ContactMatrixI;
32 import jalview.datamodel.Mapping;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceI;
36 import jalview.datamodel.annotations.AnnotationRowBuilder;
37 import jalview.schemes.ColourSchemeI;
38 import jalview.schemes.ResidueProperties;
39 import jalview.structure.StructureImportSettings;
40 import jalview.structure.StructureMapping;
41 import jalview.util.Comparison;
45 public static final String RESNUM_FEATURE = "RESNUM";
47 private static final String IEASTATUS = "IEA:jalview";
51 public Vector<Bond> bonds = new Vector<>();
53 public Vector<Atom> atoms = new Vector<>();
55 public Vector<Residue> residues = new Vector<>();
60 * sequence is the sequence extracted by the chain parsing code
62 public SequenceI sequence;
65 * shadow is the sequence created by any other parsing processes (e.g. Jmol,
68 public SequenceI shadow = null;
70 public boolean isNa = false;
72 public boolean isVisible = true;
74 public int pdbstart = 0;
76 public int pdbend = 0;
78 public int seqstart = 0;
80 public int seqend = 0;
82 public String pdbid = "";
84 AnnotationRowBuilder tfacTemplate = new AnnotationRowBuilder(
85 "Temperature Factor");
87 public PDBChain(String thePdbid, String theId,
88 AnnotationRowBuilder template)
90 this(thePdbid, theId);
93 tfacTemplate = template;
98 * import chain data assuming Temperature Factor is in the Temperature Factor
104 public PDBChain(String thePdbid, String theId)
106 this.pdbid = thePdbid == null ? thePdbid
107 : thePdbid.toLowerCase(Locale.ROOT);
112 * character used to write newlines
114 protected String newline = System.getProperty("line.separator");
116 public Mapping shadowMap;
118 public void setNewlineString(String nl)
123 public String getNewlineString()
128 public String print()
130 StringBuilder tmp = new StringBuilder(256);
134 tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
135 .append(" ").append(offset).append(newline);
138 return tmp.toString();
142 * Annotate the residues with their corresponding positions in s1 using the
143 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
149 public void makeExactMapping(AlignSeq as, SequenceI s1)
151 int pdbpos = as.getSeq2Start() - 2;
152 int alignpos = s1.getStart() + as.getSeq1Start() - 3;
153 // first clear out any old alignmentMapping values:
154 for (Atom atom : atoms)
156 atom.alignmentMapping = -1;
158 // and now trace the alignment onto the atom set.
159 for (int i = 0; i < as.astr1.length(); i++)
161 if (as.astr1.charAt(i) != '-')
166 if (as.astr2.charAt(i) != '-')
171 boolean sameResidue = Comparison.isSameResidue(as.astr1.charAt(i),
172 as.astr2.charAt(i), false);
175 if (pdbpos >= residues.size())
179 Residue res = residues.elementAt(pdbpos);
180 for (Atom atom : res.atoms)
182 atom.alignmentMapping = alignpos;
189 * Annotate the residues with their corresponding positions in s1 using the
190 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
196 public void makeExactMapping(StructureMapping mapping, SequenceI s1)
198 // first clear out any old alignmentMapping values:
199 for (Atom atom : atoms)
201 atom.alignmentMapping = -1;
204 while (ds.getDatasetSequence() != null)
206 ds = ds.getDatasetSequence();
209 for (Residue res : residues)
211 // res.number isn't set correctly for discontinuous/mismapped residues
212 int seqpos = mapping.getSeqPos(res.atoms.get(0).resNumber);
213 char strchar = sequence.getCharAt(pdboffset++);
214 if (seqpos == StructureMapping.UNASSIGNED_VALUE)
218 char seqchar = ds.getCharAt(seqpos - ds.getStart());
220 boolean sameResidue = Comparison.isSameResidue(seqchar, strchar,
224 for (Atom atom : res.atoms)
226 atom.alignmentMapping = seqpos - 1;
233 * Copies over the RESNUM seqfeatures from the internal chain sequence to the
238 * The Status of the transferred annotation
241 * the group id for the features on the destination sequence (e.g.
242 * the official accession ID)
244 public void transferRESNUMFeatures(SequenceI seq, String status,
247 if (altPDBID == null)
252 while (sq != null && sq.getDatasetSequence() != null)
254 sq = sq.getDatasetSequence();
262 * Remove any existing features for this chain if they exist ?
263 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
264 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
265 * ? for (int i=0; i<seqsfeatures.length; i++) { }
269 status = PDBChain.IEASTATUS;
272 List<SequenceFeature> features = sequence.getSequenceFeatures();
273 for (SequenceFeature feature : features)
275 if (feature.getFeatureGroup() != null
276 && feature.getFeatureGroup().equals(pdbid))
279 + residues.elementAt(feature.getBegin() - offset).atoms
280 .elementAt(0).alignmentMapping;
281 int newEnd = 1 + residues.elementAt(feature.getEnd() - offset).atoms
282 .elementAt(0).alignmentMapping;
283 SequenceFeature tx = new SequenceFeature(feature, newBegin, newEnd,
284 altPDBID, feature.getScore());
286 + ((tx.getStatus() == null || tx.getStatus().length() == 0)
288 : ":" + tx.getStatus()));
289 if (tx.begin != 0 && tx.end != 0)
291 sq.addSequenceFeature(tx);
298 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
299 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
300 * residues contain a P not a CA.
302 public void makeCaBondList()
306 for (int i = 0; i < (residues.size() - 1); i++)
308 Residue tmpres = residues.elementAt(i);
309 Residue tmpres2 = residues.elementAt(i + 1);
310 Atom at1 = tmpres.findAtom("CA");
311 Atom at2 = tmpres2.findAtom("CA");
313 if ((at1 == null) && (at2 == null))
316 at1 = tmpres.findAtom("P");
317 at2 = tmpres2.findAtom("P");
319 if ((at1 != null) && (at2 != null))
321 if (at1.chain.equals(at2.chain))
332 System.out.println("not found " + i);
337 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
340 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
347 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
353 public void makeBond(Atom at1, Atom at2)
355 bonds.addElement(new Bond(at1, at2));
359 * Traverses the list of atoms and
361 * <li>constructs a list of Residues, each containing all the atoms that share
362 * the same residue number</li>
363 * <li>adds a RESNUM sequence feature for each position</li>
364 * <li>creates the sequence string</li>
365 * <li>determines if nucleotide</li>
366 * <li>saves the residue number of the first atom as 'offset'</li>
367 * <li>adds temp factor annotation if the flag is set to do so</li>
370 * @param visibleChainAnnotation
372 public void makeResidueList(boolean visibleChainAnnotation)
376 boolean deoxyn = false;
377 boolean nucleotide = false;
378 StringBuilder seq = new StringBuilder(256);
379 Vector<SequenceFeature> resFeatures = new Vector<>();
380 Vector<Annotation> resAnnotation = new Vector<>();
381 int iSize = atoms.size() - 1;
385 for (int i = 0; i <= iSize; i++)
387 Atom tmp = atoms.elementAt(i);
388 resNumber = tmp.resNumber;
389 insCode = tmp.insCode;
399 Vector<Atom> resAtoms = new Vector<>();
400 // Add atoms to a vector while the residue number
401 // remains the same as the first atom's resNumber (res)
402 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
404 resAtoms.add(atoms.elementAt(i));
407 if (i < atoms.size())
409 resNumber = atoms.elementAt(i).resNumber;
410 insCode = atoms.elementAt(i).insCode;
418 // We need this to keep in step with the outer for i = loop
421 // Add inserted residues as features to the base residue
422 Atom currAtom = resAtoms.get(0);
423 if (currAtom.insCode != ' ' && !residues.isEmpty()
424 && residues.lastElement().atoms
425 .get(0).resNumber == currAtom.resNumber)
427 String desc = currAtom.resName + ":" + currAtom.resNumIns + " "
429 SequenceFeature sf = new SequenceFeature("INSERTION", desc,
430 offset + count - 1, offset + count - 1, "PDB_INS");
431 resFeatures.addElement(sf);
432 residues.lastElement().atoms.addAll(resAtoms);
436 // Make a new Residue object with the new atoms vector
437 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
439 Residue tmpres = residues.lastElement();
440 Atom tmpat = tmpres.atoms.get(0);
441 // Make A new SequenceFeature for the current residue numbering
442 String desc = tmpat.resName + ":" + tmpat.resNumIns + " " + pdbid
444 SequenceFeature sf = new SequenceFeature(RESNUM_FEATURE, desc,
445 offset + count, offset + count, pdbid);
446 resFeatures.addElement(sf);
447 resAnnotation.addElement(new Annotation(tmpat.tfactor));
448 // Keep totting up the sequence
450 if ((symbol = ResidueProperties.getAA3Hash()
451 .get(tmpat.resName)) == null)
453 String nucname = tmpat.resName.trim();
454 // use the aaIndex rather than call 'toLower' - which would take a bit
456 deoxyn = nucname.length() == 2
457 && ResidueProperties.aaIndex[nucname
458 .charAt(0)] == ResidueProperties.aaIndex['D'];
459 if (tmpat.name.equalsIgnoreCase("CA")
460 || ResidueProperties.nucleotideIndex[nucname
461 .charAt((deoxyn ? 1 : 0))] == -1)
463 char r = ResidueProperties.getSingleCharacterCode(
464 ResidueProperties.getCanonicalAminoAcid(tmpat.resName));
465 seq.append(r == '0' ? 'X' : r);
466 // System.err.println("PDBReader:Null aa3Hash for " +
473 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
481 "Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
483 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
494 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
498 // Add normalised feature scores to RESNUM indicating start/end of sequence
499 // sf.setScore(offset+count);
501 // System.out.println("PDB Sequence is :\nSequence = " + seq);
502 // System.out.println("No of residues = " + residues.size());
504 if (StructureImportSettings.isShowSeqFeatures())
506 iSize = resFeatures.size();
507 for (int i = 0; i < iSize; i++)
509 sequence.addSequenceFeature(resFeatures.elementAt(i));
510 resFeatures.setElementAt(null, i);
513 if (visibleChainAnnotation)
515 Annotation[] annots = new Annotation[resAnnotation.size()];
518 iSize = annots.length;
519 for (int i = 0; i < iSize; i++)
521 annots[i] = resAnnotation.elementAt(i);
522 tfacTemplate.processAnnotation(annots[i]);
523 max = Math.max(max, annots[i].value);
524 min = Math.min(min, annots[i].value);
525 resAnnotation.setElementAt(null, i);
527 if (tfacTemplate.isHasMinMax())
529 max = tfacTemplate.getMax();
530 min = tfacTemplate.getMin();
533 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
534 tfacTemplate.getName(),
535 (tfacTemplate.isHasDescription()
536 ? tfacTemplate.getDescription()
537 : tfacTemplate.getName()) + " for " + pdbid + id,
538 annots, min, max, AlignmentAnnotation.LINE_GRAPH);
539 tfactorann.setTFType(tfacTemplate.getTFType());
540 tfactorann.setCalcId(getClass().getName());
542 tfactorann.setSequenceRef(sequence);
543 sequence.addAlignmentAnnotation(tfactorann);
548 * Colour start/end of bonds by charge
550 * <li>ASP and GLU red</li>
551 * <li>LYS and ARG blue</li>
552 * <li>CYS yellow</li>
553 * <li>others light gray</li>
556 public void setChargeColours()
560 if (b.at1 != null && b.at2 != null)
562 b.startCol = getChargeColour(b.at1.resName);
563 b.endCol = getChargeColour(b.at2.resName);
567 b.startCol = Color.gray;
568 b.endCol = Color.gray;
573 public static Color getChargeColour(String resName)
575 Color result = Color.lightGray;
576 if ("ASP".equals(resName) || "GLU".equals(resName))
580 else if ("LYS".equals(resName) || "ARG".equals(resName))
584 else if ("CYS".equals(resName))
586 result = Color.yellow;
592 * Sets the start/end colours of bonds to those of the start/end atoms
593 * according to the specified colour scheme. Note: currently only works for
598 public void setChainColours(ColourSchemeI cs)
605 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
606 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
609 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
610 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
613 } catch (Exception e)
615 b.startCol = Color.gray;
616 b.endCol = Color.gray;
621 public void setChainColours(Color col)
631 * copy any sequence annotation onto the sequence mapped using the provided
635 * - positional mapping between destination sequence and pdb resnum
637 * - mapping between destination sequence and local chain
639 public void transferResidueAnnotation(StructureMapping mapping,
640 jalview.datamodel.Mapping sqmpping)
642 SequenceI sq = mapping.getSequence();
644 if (sqmpping == null)
646 // SIFTS mappings are recorded in the StructureMapping object...
648 sqmpping = mapping.getSeqToPdbMapping();
652 while (dsq.getDatasetSequence() != null)
654 dsq = dsq.getDatasetSequence();
656 // any annotation will be transferred onto the dataset sequence
658 if (shadow != null && shadow.getAnnotation() != null)
661 for (AlignmentAnnotation ana : shadow.getAnnotation())
663 // match on calcId, label and description so annotations from
664 // different structures are preserved
665 List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
666 ana.getCalcId(), ana.label, ana.description);
667 if (transfer == null || transfer.size() == 0)
669 ContactMatrixI cm = shadow.getContactMatrixFor(ana);
670 ana = new AlignmentAnnotation(ana);
671 // TODO map contact matrix under mapping
672 ana.liftOver(sequence, shadowMap);
673 ana.liftOver(dsq, sqmpping);
674 dsq.addAlignmentAnnotation(ana);
677 dsq.addContactListFor(ana, cm);
688 if (sequence != null && sequence.getAnnotation() != null)
690 for (AlignmentAnnotation ana : sequence.getAnnotation())
692 // match on calcId, label and description so annotations from
693 // different structures are preserved
694 List<AlignmentAnnotation> transfer = dsq
695 .getAlignmentAnnotations(ana.getCalcId(), ana.label,
697 if (transfer == null || transfer.size() == 0)
699 ContactMatrixI cm = sequence.getContactMatrixFor(ana);
700 ana = new AlignmentAnnotation(ana);
701 ana.liftOver(dsq, sqmpping);
702 dsq.addAlignmentAnnotation(ana);
705 dsq.addContactListFor(ana, cm);
717 // Useful for debugging mappings - adds annotation for mapped position
718 float min = -1, max = 0;
719 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
720 for (int i = sq.getStart(), j = sq.getEnd(),
721 k = 0; i <= j; i++, k++)
723 int prn = mapping.getPDBResNum(k + 1);
725 an[k] = new Annotation(prn);
743 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
744 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
745 an, min, max, AlignmentAnnotation.LINE_GRAPH));