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.Locale;
39 import java.util.Vector;
43 public static final String RESNUM_FEATURE = "RESNUM";
45 private static final String IEASTATUS = "IEA:jalview";
49 public Vector<Bond> bonds = new Vector<>();
51 public Vector<Atom> atoms = new Vector<>();
53 public Vector<Residue> residues = new Vector<>();
58 * sequence is the sequence extracted by the chain parsing code
60 public SequenceI sequence;
63 * shadow is the sequence created by any other parsing processes (e.g. Jmol,
66 public SequenceI shadow = null;
68 public boolean isNa = false;
70 public boolean isVisible = true;
72 public int pdbstart = 0;
74 public int pdbend = 0;
76 public int seqstart = 0;
78 public int seqend = 0;
80 public String pdbid = "";
82 String tfacName = "Temperature Factor";
84 public PDBChain(String thePdbid, String theId,
85 String tempFactorColumnName)
87 this.pdbid = thePdbid == null ? thePdbid
88 : thePdbid.toLowerCase(Locale.ROOT);
90 if (tempFactorColumnName != null && tempFactorColumnName.length() > 0)
92 tfacName = tempFactorColumnName;
97 * import chain data assuming Temperature Factor is in the Temperature Factor
103 public PDBChain(String thePdbid, String theId)
105 this(thePdbid, theId, null);
109 * character used to write newlines
111 protected String newline = System.getProperty("line.separator");
113 public Mapping shadowMap;
115 public void setNewlineString(String nl)
120 public String getNewlineString()
125 public String print()
127 StringBuilder tmp = new StringBuilder(256);
131 tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
132 .append(" ").append(offset).append(newline);
135 return tmp.toString();
139 * Annotate the residues with their corresponding positions in s1 using the
140 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
146 public void makeExactMapping(AlignSeq as, SequenceI s1)
148 int pdbpos = as.getSeq2Start() - 2;
149 int alignpos = s1.getStart() + as.getSeq1Start() - 3;
150 // first clear out any old alignmentMapping values:
151 for (Atom atom : atoms)
153 atom.alignmentMapping = -1;
155 // and now trace the alignment onto the atom set.
156 for (int i = 0; i < as.astr1.length(); i++)
158 if (as.astr1.charAt(i) != '-')
163 if (as.astr2.charAt(i) != '-')
168 boolean sameResidue = Comparison.isSameResidue(as.astr1.charAt(i),
169 as.astr2.charAt(i), false);
172 if (pdbpos >= residues.size())
176 Residue res = residues.elementAt(pdbpos);
177 for (Atom atom : res.atoms)
179 atom.alignmentMapping = alignpos;
186 * Annotate the residues with their corresponding positions in s1 using the
187 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
193 public void makeExactMapping(StructureMapping mapping, SequenceI s1)
195 // first clear out any old alignmentMapping values:
196 for (Atom atom : atoms)
198 atom.alignmentMapping = -1;
201 while (ds.getDatasetSequence() != null)
203 ds = ds.getDatasetSequence();
206 for (Residue res : residues)
208 // res.number isn't set correctly for discontinuous/mismapped residues
209 int seqpos = mapping.getSeqPos(res.atoms.get(0).resNumber);
210 char strchar = sequence.getCharAt(pdboffset++);
211 if (seqpos == StructureMapping.UNASSIGNED_VALUE)
215 char seqchar = ds.getCharAt(seqpos - ds.getStart());
217 boolean sameResidue = Comparison.isSameResidue(seqchar, strchar,
221 for (Atom atom : res.atoms)
223 atom.alignmentMapping = seqpos - 1;
230 * Copies over the RESNUM seqfeatures from the internal chain sequence to the
235 * The Status of the transferred annotation
238 * the group id for the features on the destination sequence (e.g.
239 * the official accession ID)
241 public void transferRESNUMFeatures(SequenceI seq, String status,
244 if (altPDBID == null)
249 while (sq != null && sq.getDatasetSequence() != null)
251 sq = sq.getDatasetSequence();
259 * Remove any existing features for this chain if they exist ?
260 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
261 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
262 * ? for (int i=0; i<seqsfeatures.length; i++) { }
266 status = PDBChain.IEASTATUS;
269 List<SequenceFeature> features = sequence.getSequenceFeatures();
270 for (SequenceFeature feature : features)
272 if (feature.getFeatureGroup() != null
273 && feature.getFeatureGroup().equals(pdbid))
276 + residues.elementAt(feature.getBegin() - offset).atoms
277 .elementAt(0).alignmentMapping;
278 int newEnd = 1 + residues.elementAt(feature.getEnd() - offset).atoms
279 .elementAt(0).alignmentMapping;
280 SequenceFeature tx = new SequenceFeature(feature, newBegin, newEnd,
281 altPDBID, feature.getScore());
283 + ((tx.getStatus() == null || tx.getStatus().length() == 0)
285 : ":" + tx.getStatus()));
286 if (tx.begin != 0 && tx.end != 0)
288 sq.addSequenceFeature(tx);
295 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
296 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
297 * residues contain a P not a CA.
299 public void makeCaBondList()
303 for (int i = 0; i < (residues.size() - 1); i++)
305 Residue tmpres = residues.elementAt(i);
306 Residue tmpres2 = residues.elementAt(i + 1);
307 Atom at1 = tmpres.findAtom("CA");
308 Atom at2 = tmpres2.findAtom("CA");
310 if ((at1 == null) && (at2 == null))
313 at1 = tmpres.findAtom("P");
314 at2 = tmpres2.findAtom("P");
316 if ((at1 != null) && (at2 != null))
318 if (at1.chain.equals(at2.chain))
329 System.out.println("not found " + i);
334 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
337 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
344 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
350 public void makeBond(Atom at1, Atom at2)
352 bonds.addElement(new Bond(at1, at2));
356 * Traverses the list of atoms and
358 * <li>constructs a list of Residues, each containing all the atoms that share
359 * the same residue number</li>
360 * <li>adds a RESNUM sequence feature for each position</li>
361 * <li>creates the sequence string</li>
362 * <li>determines if nucleotide</li>
363 * <li>saves the residue number of the first atom as 'offset'</li>
364 * <li>adds temp factor annotation if the flag is set to do so</li>
367 * @param visibleChainAnnotation
369 public void makeResidueList(boolean visibleChainAnnotation)
373 boolean deoxyn = false;
374 boolean nucleotide = false;
375 StringBuilder seq = new StringBuilder(256);
376 Vector<SequenceFeature> resFeatures = new Vector<>();
377 Vector<Annotation> resAnnotation = new Vector<>();
378 int iSize = atoms.size() - 1;
382 for (int i = 0; i <= iSize; i++)
384 Atom tmp = atoms.elementAt(i);
385 resNumber = tmp.resNumber;
386 insCode = tmp.insCode;
396 Vector<Atom> resAtoms = new Vector<>();
397 // Add atoms to a vector while the residue number
398 // remains the same as the first atom's resNumber (res)
399 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
401 resAtoms.add(atoms.elementAt(i));
404 if (i < atoms.size())
406 resNumber = atoms.elementAt(i).resNumber;
407 insCode = atoms.elementAt(i).insCode;
415 // We need this to keep in step with the outer for i = loop
418 // Add inserted residues as features to the base residue
419 Atom currAtom = resAtoms.get(0);
420 if (currAtom.insCode != ' ' && !residues.isEmpty()
421 && residues.lastElement().atoms
422 .get(0).resNumber == currAtom.resNumber)
424 String desc = currAtom.resName + ":" + currAtom.resNumIns + " "
426 SequenceFeature sf = new SequenceFeature("INSERTION", desc,
427 offset + count - 1, offset + count - 1, "PDB_INS");
428 resFeatures.addElement(sf);
429 residues.lastElement().atoms.addAll(resAtoms);
433 // Make a new Residue object with the new atoms vector
434 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
436 Residue tmpres = residues.lastElement();
437 Atom tmpat = tmpres.atoms.get(0);
438 // Make A new SequenceFeature for the current residue numbering
439 String desc = tmpat.resName + ":" + tmpat.resNumIns + " " + pdbid
441 SequenceFeature sf = new SequenceFeature(RESNUM_FEATURE, desc,
442 offset + count, offset + count, pdbid);
443 resFeatures.addElement(sf);
444 resAnnotation.addElement(new Annotation(tmpat.tfactor));
445 // Keep totting up the sequence
447 if ((symbol = ResidueProperties.getAA3Hash()
448 .get(tmpat.resName)) == null)
450 String nucname = tmpat.resName.trim();
451 // use the aaIndex rather than call 'toLower' - which would take a bit
453 deoxyn = nucname.length() == 2
454 && ResidueProperties.aaIndex[nucname
455 .charAt(0)] == ResidueProperties.aaIndex['D'];
456 if (tmpat.name.equalsIgnoreCase("CA")
457 || ResidueProperties.nucleotideIndex[nucname
458 .charAt((deoxyn ? 1 : 0))] == -1)
460 char r = ResidueProperties.getSingleCharacterCode(
461 ResidueProperties.getCanonicalAminoAcid(tmpat.resName));
462 seq.append(r == '0' ? 'X' : r);
463 // System.err.println("PDBReader:Null aa3Hash for " +
470 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
478 "Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
480 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
491 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
495 // Add normalised feature scores to RESNUM indicating start/end of sequence
496 // sf.setScore(offset+count);
498 // System.out.println("PDB Sequence is :\nSequence = " + seq);
499 // System.out.println("No of residues = " + residues.size());
501 if (StructureImportSettings.isShowSeqFeatures())
503 iSize = resFeatures.size();
504 for (int i = 0; i < iSize; i++)
506 sequence.addSequenceFeature(resFeatures.elementAt(i));
507 resFeatures.setElementAt(null, i);
510 if (visibleChainAnnotation)
512 Annotation[] annots = new Annotation[resAnnotation.size()];
515 iSize = annots.length;
516 for (int i = 0; i < iSize; i++)
518 annots[i] = resAnnotation.elementAt(i);
519 max = Math.max(max, annots[i].value);
520 min = Math.min(min, annots[i].value);
521 resAnnotation.setElementAt(null, i);
523 AlignmentAnnotation tfactorann = new AlignmentAnnotation(tfacName,
524 tfacName + " for " + pdbid + id, annots, min, max,
525 AlignmentAnnotation.LINE_GRAPH);
527 tfactorann.setCalcId(getClass().getName());
529 tfactorann.setSequenceRef(sequence);
530 sequence.addAlignmentAnnotation(tfactorann);
535 * Colour start/end of bonds by charge
537 * <li>ASP and GLU red</li>
538 * <li>LYS and ARG blue</li>
539 * <li>CYS yellow</li>
540 * <li>others light gray</li>
543 public void setChargeColours()
547 if (b.at1 != null && b.at2 != null)
549 b.startCol = getChargeColour(b.at1.resName);
550 b.endCol = getChargeColour(b.at2.resName);
554 b.startCol = Color.gray;
555 b.endCol = Color.gray;
560 public static Color getChargeColour(String resName)
562 Color result = Color.lightGray;
563 if ("ASP".equals(resName) || "GLU".equals(resName))
567 else if ("LYS".equals(resName) || "ARG".equals(resName))
571 else if ("CYS".equals(resName))
573 result = Color.yellow;
579 * Sets the start/end colours of bonds to those of the start/end atoms
580 * according to the specified colour scheme. Note: currently only works for
585 public void setChainColours(ColourSchemeI cs)
592 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
593 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
596 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
597 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
600 } catch (Exception e)
602 b.startCol = Color.gray;
603 b.endCol = Color.gray;
608 public void setChainColours(Color col)
618 * copy any sequence annotation onto the sequence mapped using the provided
622 * - positional mapping between destination sequence and pdb resnum
624 * - mapping between destination sequence and local chain
626 public void transferResidueAnnotation(StructureMapping mapping,
627 jalview.datamodel.Mapping sqmpping)
629 SequenceI sq = mapping.getSequence();
631 if (sqmpping == null)
633 // SIFTS mappings are recorded in the StructureMapping object...
635 sqmpping = mapping.getSeqToPdbMapping();
639 while (dsq.getDatasetSequence() != null)
641 dsq = dsq.getDatasetSequence();
643 // any annotation will be transferred onto the dataset sequence
645 if (shadow != null && shadow.getAnnotation() != null)
648 for (AlignmentAnnotation ana : shadow.getAnnotation())
650 // match on calcId, label and description so annotations from
651 // different structures are preserved
652 List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
653 ana.getCalcId(), ana.label, ana.description);
654 if (transfer == null || transfer.size() == 0)
656 ana = new AlignmentAnnotation(ana);
657 ana.liftOver(sequence, shadowMap);
658 ana.liftOver(dsq, sqmpping);
659 dsq.addAlignmentAnnotation(ana);
669 if (sequence != null && sequence.getAnnotation() != null)
671 for (AlignmentAnnotation ana : sequence.getAnnotation())
673 // match on calcId, label and description so annotations from
674 // different structures are preserved
675 List<AlignmentAnnotation> transfer = dsq
676 .getAlignmentAnnotations(ana.getCalcId(), ana.label,
678 if (transfer == null || transfer.size() == 0)
680 ana = new AlignmentAnnotation(ana);
681 ana.liftOver(dsq, sqmpping);
682 dsq.addAlignmentAnnotation(ana);
683 // mapping.transfer(ana);
694 // Useful for debugging mappings - adds annotation for mapped position
695 float min = -1, max = 0;
696 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
697 for (int i = sq.getStart(), j = sq
698 .getEnd(), k = 0; i <= j; i++, k++)
700 int prn = mapping.getPDBResNum(k + 1);
702 an[k] = new Annotation(prn);
720 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
721 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
722 an, min, max, AlignmentAnnotation.LINE_GRAPH));