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 public void transferRESNUMFeatures(SequenceI seq,
237 while (sq != null && sq.getDatasetSequence() != null)
239 sq = sq.getDatasetSequence();
247 * Remove any existing features for this chain if they exist ?
248 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
249 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
250 * ? for (int i=0; i<seqsfeatures.length; i++) { }
254 status = PDBChain.IEASTATUS;
257 List<SequenceFeature> features = sequence.getSequenceFeatures();
258 for (SequenceFeature feature : features)
260 if (feature.getFeatureGroup() != null
261 && feature.getFeatureGroup().equals(pdbid))
263 int newBegin = 1 + residues.elementAt(feature.getBegin() - offset).atoms
264 .elementAt(0).alignmentMapping;
265 int newEnd = 1 + residues.elementAt(feature.getEnd() - offset).atoms
266 .elementAt(0).alignmentMapping;
267 SequenceFeature tx = new SequenceFeature(feature, newBegin, newEnd,
268 feature.getFeatureGroup(), feature.getScore());
270 + ((tx.getStatus() == null || tx.getStatus().length() == 0)
272 : ":" + tx.getStatus()));
273 if (tx.begin != 0 && tx.end != 0)
275 sq.addSequenceFeature(tx);
282 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
283 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
284 * residues contain a P not a CA.
286 public void makeCaBondList()
290 for (int i = 0; i < (residues.size() - 1); i++)
292 Residue tmpres = residues.elementAt(i);
293 Residue tmpres2 = residues.elementAt(i + 1);
294 Atom at1 = tmpres.findAtom("CA");
295 Atom at2 = tmpres2.findAtom("CA");
297 if ((at1 == null) && (at2 == null))
300 at1 = tmpres.findAtom("P");
301 at2 = tmpres2.findAtom("P");
303 if ((at1 != null) && (at2 != null))
305 if (at1.chain.equals(at2.chain))
316 System.out.println("not found " + i);
321 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
324 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
331 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
337 public void makeBond(Atom at1, Atom at2)
339 bonds.addElement(new Bond(at1, at2));
343 * Traverses the list of atoms and
345 * <li>constructs a list of Residues, each containing all the atoms that share
346 * the same residue number</li>
347 * <li>adds a RESNUM sequence feature for each position</li>
348 * <li>creates the sequence string</li>
349 * <li>determines if nucleotide</li>
350 * <li>saves the residue number of the first atom as 'offset'</li>
351 * <li>adds temp factor annotation if the flag is set to do so</li>
354 * @param visibleChainAnnotation
356 public void makeResidueList(boolean visibleChainAnnotation)
360 boolean deoxyn = false;
361 boolean nucleotide = false;
362 StringBuilder seq = new StringBuilder(256);
363 Vector<SequenceFeature> resFeatures = new Vector<>();
364 Vector<Annotation> resAnnotation = new Vector<>();
365 int iSize = atoms.size() - 1;
369 for (int i = 0; i <= iSize; i++)
371 Atom tmp = atoms.elementAt(i);
372 resNumber = tmp.resNumber;
373 insCode = tmp.insCode;
383 Vector<Atom> resAtoms = new Vector<>();
384 // Add atoms to a vector while the residue number
385 // remains the same as the first atom's resNumber (res)
386 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
388 resAtoms.add(atoms.elementAt(i));
391 if (i < atoms.size())
393 resNumber = atoms.elementAt(i).resNumber;
394 insCode = atoms.elementAt(i).insCode;
402 // We need this to keep in step with the outer for i = loop
405 // Add inserted residues as features to the base residue
406 Atom currAtom = resAtoms.get(0);
407 if (currAtom.insCode != ' ' && !residues.isEmpty()
408 && residues.lastElement().atoms
409 .get(0).resNumber == currAtom.resNumber)
411 String desc = currAtom.resName + ":" + currAtom.resNumIns + " "
413 SequenceFeature sf = new SequenceFeature("INSERTION", desc, offset
414 + count - 1, offset + count - 1, "PDB_INS");
415 resFeatures.addElement(sf);
416 residues.lastElement().atoms.addAll(resAtoms);
420 // Make a new Residue object with the new atoms vector
421 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
423 Residue tmpres = residues.lastElement();
424 Atom tmpat = tmpres.atoms.get(0);
425 // Make A new SequenceFeature for the current residue numbering
426 String desc = tmpat.resName
427 + ":" + tmpat.resNumIns + " " + pdbid + id;
428 SequenceFeature sf = new SequenceFeature(RESNUM_FEATURE, desc,
429 offset + count, offset + count, pdbid);
430 resFeatures.addElement(sf);
431 resAnnotation.addElement(new Annotation(tmpat.tfactor));
432 // Keep totting up the sequence
434 if ((symbol = ResidueProperties.getAA3Hash()
435 .get(tmpat.resName)) == null)
437 String nucname = tmpat.resName.trim();
438 // use the aaIndex rather than call 'toLower' - which would take a bit
440 deoxyn = nucname.length() == 2
441 && ResidueProperties.aaIndex[nucname
442 .charAt(0)] == ResidueProperties.aaIndex['D'];
443 if (tmpat.name.equalsIgnoreCase("CA")
444 || ResidueProperties.nucleotideIndex[nucname
445 .charAt((deoxyn ? 1 : 0))] == -1)
447 char r = ResidueProperties.getSingleCharacterCode(
448 ResidueProperties.getCanonicalAminoAcid(tmpat.resName));
449 seq.append(r == '0' ? 'X' : r);
450 // System.err.println("PDBReader:Null aa3Hash for " +
457 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
465 "Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
467 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
478 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
482 // Add normalised feature scores to RESNUM indicating start/end of sequence
483 // sf.setScore(offset+count);
485 // System.out.println("PDB Sequence is :\nSequence = " + seq);
486 // System.out.println("No of residues = " + residues.size());
488 if (StructureImportSettings.isShowSeqFeatures())
490 iSize = resFeatures.size();
491 for (int i = 0; i < iSize; i++)
493 sequence.addSequenceFeature(resFeatures.elementAt(i));
494 resFeatures.setElementAt(null, i);
497 if (visibleChainAnnotation)
499 Annotation[] annots = new Annotation[resAnnotation.size()];
502 iSize = annots.length;
503 for (int i = 0; i < iSize; i++)
505 annots[i] = resAnnotation.elementAt(i);
506 max = Math.max(max, annots[i].value);
507 min = Math.min(min, annots[i].value);
508 resAnnotation.setElementAt(null, i);
510 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
511 tfacName, tfacName + " for " + pdbid + id,
512 annots, min, max, AlignmentAnnotation.LINE_GRAPH);
514 tfactorann.setCalcId(getClass().getName());
516 tfactorann.setSequenceRef(sequence);
517 sequence.addAlignmentAnnotation(tfactorann);
523 * Colour start/end of bonds by charge
525 * <li>ASP and GLU red</li>
526 * <li>LYS and ARG blue</li>
527 * <li>CYS yellow</li>
528 * <li>others light gray</li>
531 public void setChargeColours()
535 if (b.at1 != null && b.at2 != null)
537 b.startCol = getChargeColour(b.at1.resName);
538 b.endCol = getChargeColour(b.at2.resName);
542 b.startCol = Color.gray;
543 b.endCol = Color.gray;
548 public static Color getChargeColour(String resName)
550 Color result = Color.lightGray;
551 if ("ASP".equals(resName) || "GLU".equals(resName))
555 else if ("LYS".equals(resName) || "ARG".equals(resName))
559 else if ("CYS".equals(resName))
561 result = Color.yellow;
567 * Sets the start/end colours of bonds to those of the start/end atoms
568 * according to the specified colour scheme. Note: currently only works for
573 public void setChainColours(ColourSchemeI cs)
580 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
581 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
584 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
585 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
588 } catch (Exception e)
590 b.startCol = Color.gray;
591 b.endCol = Color.gray;
596 public void setChainColours(Color col)
606 * copy any sequence annotation onto the sequence mapped using the provided
610 * - positional mapping between destination sequence and pdb resnum
612 * - mapping between destination sequence and local chain
614 public void transferResidueAnnotation(StructureMapping mapping,
615 jalview.datamodel.Mapping sqmpping)
617 SequenceI sq = mapping.getSequence();
619 if (sqmpping == null)
621 // SIFTS mappings are recorded in the StructureMapping object...
623 sqmpping = mapping.getSeqToPdbMapping();
627 while (dsq.getDatasetSequence() != null)
629 dsq = dsq.getDatasetSequence();
631 // any annotation will be transferred onto the dataset sequence
633 if (shadow != null && shadow.getAnnotation() != null)
636 for (AlignmentAnnotation ana : shadow.getAnnotation())
638 // match on calcId, label and description so annotations from
639 // different structures are preserved
640 List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
641 ana.getCalcId(), ana.label, ana.description);
642 if (transfer == null || transfer.size() == 0)
644 ana = new AlignmentAnnotation(ana);
645 ana.liftOver(sequence, shadowMap);
646 ana.liftOver(dsq, sqmpping);
647 dsq.addAlignmentAnnotation(ana);
657 if (sequence != null && sequence.getAnnotation() != null)
659 for (AlignmentAnnotation ana : sequence.getAnnotation())
661 // match on calcId, label and description so annotations from
662 // different structures are preserved
663 List<AlignmentAnnotation> transfer = dsq
664 .getAlignmentAnnotations(ana.getCalcId(), ana.label,
666 if (transfer == null || transfer.size() == 0)
668 ana = new AlignmentAnnotation(ana);
669 ana.liftOver(dsq, sqmpping);
670 dsq.addAlignmentAnnotation(ana);
671 // mapping.transfer(ana);
682 // Useful for debugging mappings - adds annotation for mapped position
683 float min = -1, max = 0;
684 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
685 for (int i = sq.getStart(), j = sq
686 .getEnd(), k = 0; i <= j; i++, k++)
688 int prn = mapping.getPDBResNum(k + 1);
690 an[k] = new Annotation(prn);
708 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
709 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
710 an, min, max, AlignmentAnnotation.LINE_GRAPH));