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)
83 this.pdbid = thePdbid == null ? thePdbid : thePdbid.toLowerCase();
88 * character used to write newlines
90 protected String newline = System.getProperty("line.separator");
92 public Mapping shadowMap;
94 public void setNewlineString(String nl)
99 public String getNewlineString()
104 public String print()
106 StringBuilder tmp = new StringBuilder(256);
110 tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
111 .append(" ").append(offset).append(newline);
114 return tmp.toString();
118 * Annotate the residues with their corresponding positions in s1 using the
119 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
125 public void makeExactMapping(AlignSeq as, SequenceI s1)
127 int pdbpos = as.getSeq2Start() - 2;
128 int alignpos = s1.getStart() + as.getSeq1Start() - 3;
129 // first clear out any old alignmentMapping values:
130 for (Atom atom : atoms)
132 atom.alignmentMapping = -1;
134 // and now trace the alignment onto the atom set.
135 for (int i = 0; i < as.astr1.length(); i++)
137 if (as.astr1.charAt(i) != '-')
142 if (as.astr2.charAt(i) != '-')
147 boolean sameResidue = Comparison.isSameResidue(as.astr1.charAt(i),
148 as.astr2.charAt(i), false);
151 if (pdbpos >= residues.size())
155 Residue res = residues.elementAt(pdbpos);
156 for (Atom atom : res.atoms)
158 atom.alignmentMapping = alignpos;
165 * Annotate the residues with their corresponding positions in s1 using the
166 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
172 public void makeExactMapping(StructureMapping mapping, SequenceI s1)
174 // first clear out any old alignmentMapping values:
175 for (Atom atom : atoms)
177 atom.alignmentMapping = -1;
180 while (ds.getDatasetSequence() != null)
182 ds = ds.getDatasetSequence();
185 for (Residue res : residues)
187 // res.number isn't set correctly for discontinuous/mismapped residues
188 int seqpos = mapping.getSeqPos(res.atoms.get(0).resNumber);
189 char strchar = sequence.getCharAt(pdboffset++);
190 if (seqpos == StructureMapping.UNASSIGNED_VALUE)
194 char seqchar = ds.getCharAt(seqpos - ds.getStart());
196 boolean sameResidue = Comparison.isSameResidue(
197 seqchar, strchar, false);
200 for (Atom atom : res.atoms)
202 atom.alignmentMapping = seqpos - 1;
209 * Copies over the RESNUM seqfeatures from the internal chain sequence to the
214 * The Status of the transferred annotation
216 public void transferRESNUMFeatures(SequenceI seq,
220 while (sq != null && sq.getDatasetSequence() != null)
222 sq = sq.getDatasetSequence();
230 * Remove any existing features for this chain if they exist ?
231 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
232 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
233 * ? for (int i=0; i<seqsfeatures.length; i++) { }
237 status = PDBChain.IEASTATUS;
240 List<SequenceFeature> features = sequence.getSequenceFeatures();
241 for (SequenceFeature feature : features)
243 if (feature.getFeatureGroup() != null
244 && feature.getFeatureGroup().equals(pdbid))
246 int newBegin = 1 + residues.elementAt(feature.getBegin() - offset).atoms
247 .elementAt(0).alignmentMapping;
248 int newEnd = 1 + residues.elementAt(feature.getEnd() - offset).atoms
249 .elementAt(0).alignmentMapping;
250 SequenceFeature tx = new SequenceFeature(feature, newBegin, newEnd,
251 feature.getFeatureGroup(), feature.getScore());
253 + ((tx.getStatus() == null || tx.getStatus().length() == 0)
255 : ":" + tx.getStatus()));
256 if (tx.begin != 0 && tx.end != 0)
258 sq.addSequenceFeature(tx);
265 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
266 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
267 * residues contain a P not a CA.
269 public void makeCaBondList()
273 for (int i = 0; i < (residues.size() - 1); i++)
275 Residue tmpres = residues.elementAt(i);
276 Residue tmpres2 = residues.elementAt(i + 1);
277 Atom at1 = tmpres.findAtom("CA");
278 Atom at2 = tmpres2.findAtom("CA");
280 if ((at1 == null) && (at2 == null))
283 at1 = tmpres.findAtom("P");
284 at2 = tmpres2.findAtom("P");
286 if ((at1 != null) && (at2 != null))
288 if (at1.chain.equals(at2.chain))
299 System.out.println("not found " + i);
304 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
307 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
314 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
320 public void makeBond(Atom at1, Atom at2)
322 bonds.addElement(new Bond(at1, at2));
326 * Traverses the list of atoms and
328 * <li>constructs a list of Residues, each containing all the atoms that share
329 * the same residue number</li>
330 * <li>adds a RESNUM sequence feature for each position</li>
331 * <li>creates the sequence string</li>
332 * <li>determines if nucleotide</li>
333 * <li>saves the residue number of the first atom as 'offset'</li>
334 * <li>adds temp factor annotation if the flag is set to do so</li>
337 * @param visibleChainAnnotation
339 public void makeResidueList(boolean visibleChainAnnotation)
343 boolean deoxyn = false;
344 boolean nucleotide = false;
345 StringBuilder seq = new StringBuilder(256);
346 Vector<SequenceFeature> resFeatures = new Vector<>();
347 Vector<Annotation> resAnnotation = new Vector<>();
348 int iSize = atoms.size() - 1;
352 for (int i = 0; i <= iSize; i++)
354 Atom tmp = atoms.elementAt(i);
355 resNumber = tmp.resNumber;
356 insCode = tmp.insCode;
366 Vector<Atom> resAtoms = new Vector<>();
367 // Add atoms to a vector while the residue number
368 // remains the same as the first atom's resNumber (res)
369 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
371 resAtoms.add(atoms.elementAt(i));
374 if (i < atoms.size())
376 resNumber = atoms.elementAt(i).resNumber;
377 insCode = atoms.elementAt(i).insCode;
385 // We need this to keep in step with the outer for i = loop
388 // Add inserted residues as features to the base residue
389 Atom currAtom = resAtoms.get(0);
390 if (currAtom.insCode != ' ' && !residues.isEmpty()
391 && residues.lastElement().atoms
392 .get(0).resNumber == currAtom.resNumber)
394 String desc = currAtom.resName + ":" + currAtom.resNumIns + " "
396 SequenceFeature sf = new SequenceFeature("INSERTION", desc, offset
397 + count - 1, offset + count - 1, "PDB_INS");
398 resFeatures.addElement(sf);
399 residues.lastElement().atoms.addAll(resAtoms);
403 // Make a new Residue object with the new atoms vector
404 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
406 Residue tmpres = residues.lastElement();
407 Atom tmpat = tmpres.atoms.get(0);
408 // Make A new SequenceFeature for the current residue numbering
409 String desc = tmpat.resName
410 + ":" + tmpat.resNumIns + " " + pdbid + id;
411 SequenceFeature sf = new SequenceFeature(RESNUM_FEATURE, desc,
412 offset + count, offset + count, pdbid);
413 resFeatures.addElement(sf);
414 resAnnotation.addElement(new Annotation(tmpat.tfactor));
415 // Keep totting up the sequence
417 if ((symbol = ResidueProperties.getAA3Hash()
418 .get(tmpat.resName)) == null)
420 String nucname = tmpat.resName.trim();
421 // use the aaIndex rather than call 'toLower' - which would take a bit
423 deoxyn = nucname.length() == 2
424 && ResidueProperties.aaIndex[nucname
425 .charAt(0)] == ResidueProperties.aaIndex['D'];
426 if (tmpat.name.equalsIgnoreCase("CA")
427 || ResidueProperties.nucleotideIndex[nucname
428 .charAt((deoxyn ? 1 : 0))] == -1)
430 char r = ResidueProperties.getSingleCharacterCode(
431 ResidueProperties.getCanonicalAminoAcid(tmpat.resName));
432 seq.append(r == '0' ? 'X' : r);
433 // System.err.println("PDBReader:Null aa3Hash for " +
440 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
448 "Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
450 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
461 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
465 // Add normalised feature scores to RESNUM indicating start/end of sequence
466 // sf.setScore(offset+count);
468 // System.out.println("PDB Sequence is :\nSequence = " + seq);
469 // System.out.println("No of residues = " + residues.size());
471 if (StructureImportSettings.isShowSeqFeatures())
473 iSize = resFeatures.size();
474 for (int i = 0; i < iSize; i++)
476 sequence.addSequenceFeature(resFeatures.elementAt(i));
477 resFeatures.setElementAt(null, i);
480 if (visibleChainAnnotation)
482 Annotation[] annots = new Annotation[resAnnotation.size()];
485 iSize = annots.length;
486 for (int i = 0; i < iSize; i++)
488 annots[i] = resAnnotation.elementAt(i);
489 max = Math.max(max, annots[i].value);
490 min = Math.min(min, annots[i].value);
491 resAnnotation.setElementAt(null, i);
494 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
495 "Temperature Factor", "Temperature Factor for " + pdbid + id,
496 annots, min, max, AlignmentAnnotation.LINE_GRAPH);
497 tfactorann.setSequenceRef(sequence);
498 sequence.addAlignmentAnnotation(tfactorann);
503 * Colour start/end of bonds by charge
505 * <li>ASP and GLU red</li>
506 * <li>LYS and ARG blue</li>
507 * <li>CYS yellow</li>
508 * <li>others light gray</li>
511 public void setChargeColours()
515 if (b.at1 != null && b.at2 != null)
517 b.startCol = getChargeColour(b.at1.resName);
518 b.endCol = getChargeColour(b.at2.resName);
522 b.startCol = Color.gray;
523 b.endCol = Color.gray;
528 public static Color getChargeColour(String resName)
530 Color result = Color.lightGray;
531 if ("ASP".equals(resName) || "GLU".equals(resName))
535 else if ("LYS".equals(resName) || "ARG".equals(resName))
539 else if ("CYS".equals(resName))
541 result = Color.yellow;
547 * Sets the start/end colours of bonds to those of the start/end atoms
548 * according to the specified colour scheme. Note: currently only works for
553 public void setChainColours(ColourSchemeI cs)
560 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
561 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
564 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
565 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
568 } catch (Exception e)
570 b.startCol = Color.gray;
571 b.endCol = Color.gray;
576 public void setChainColours(Color col)
586 * copy any sequence annotation onto the sequence mapped using the provided
590 * - positional mapping between destination sequence and pdb resnum
592 * - mapping between destination sequence and local chain
594 public void transferResidueAnnotation(StructureMapping mapping,
595 jalview.datamodel.Mapping sqmpping)
597 SequenceI sq = mapping.getSequence();
599 if (sqmpping == null)
601 // SIFTS mappings are recorded in the StructureMapping object...
603 sqmpping = mapping.getSeqToPdbMapping();
607 while (dsq.getDatasetSequence() != null)
609 dsq = dsq.getDatasetSequence();
611 // any annotation will be transferred onto the dataset sequence
613 if (shadow != null && shadow.getAnnotation() != null)
616 for (AlignmentAnnotation ana : shadow.getAnnotation())
618 List<AlignmentAnnotation> transfer = sq
619 .getAlignmentAnnotations(ana.getCalcId(), ana.label);
620 if (transfer == null || transfer.size() == 0)
622 ana = new AlignmentAnnotation(ana);
623 ana.liftOver(sequence, shadowMap);
624 ana.liftOver(dsq, sqmpping);
625 dsq.addAlignmentAnnotation(ana);
635 if (sequence != null && sequence.getAnnotation() != null)
637 for (AlignmentAnnotation ana : sequence.getAnnotation())
639 List<AlignmentAnnotation> transfer = dsq
640 .getAlignmentAnnotations(ana.getCalcId(), ana.label);
641 if (transfer == null || transfer.size() == 0)
643 ana = new AlignmentAnnotation(ana);
644 ana.liftOver(dsq, sqmpping);
645 dsq.addAlignmentAnnotation(ana);
646 // mapping.transfer(ana);
657 // Useful for debugging mappings - adds annotation for mapped position
658 float min = -1, max = 0;
659 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
660 for (int i = sq.getStart(), j = sq
661 .getEnd(), k = 0; i <= j; i++, k++)
663 int prn = mapping.getPDBResNum(k + 1);
665 an[k] = new Annotation(prn);
683 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
684 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
685 an, min, max, AlignmentAnnotation.LINE_GRAPH));