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;
42 import jalview.ws.datamodel.MappableContactMatrixI;
46 public static final String RESNUM_FEATURE = "RESNUM";
48 private static final String IEASTATUS = "IEA:jalview";
52 public Vector<Bond> bonds = new Vector<>();
54 public Vector<Atom> atoms = new Vector<>();
56 public Vector<Residue> residues = new Vector<>();
61 * sequence is the sequence extracted by the chain parsing code
63 public SequenceI sequence;
66 * shadow is the sequence created by any other parsing processes (e.g. Jmol,
69 public SequenceI shadow = null;
71 public boolean isNa = false;
73 public boolean isVisible = true;
75 public int pdbstart = 0;
77 public int pdbend = 0;
79 public int seqstart = 0;
81 public int seqend = 0;
83 public String pdbid = "";
85 AnnotationRowBuilder tfacTemplate = new AnnotationRowBuilder(
86 "Temperature Factor");
88 public PDBChain(String thePdbid, String theId,
89 AnnotationRowBuilder template)
91 this(thePdbid, theId);
94 tfacTemplate = template;
99 * import chain data assuming Temperature Factor is in the Temperature Factor
105 public PDBChain(String thePdbid, String theId)
107 this.pdbid = thePdbid == null ? thePdbid
108 : thePdbid.toLowerCase(Locale.ROOT);
113 * character used to write newlines
115 protected String newline = System.getProperty("line.separator");
117 public Mapping shadowMap;
119 public void setNewlineString(String nl)
124 public String getNewlineString()
129 public String print()
131 StringBuilder tmp = new StringBuilder(256);
135 tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
136 .append(" ").append(offset).append(newline);
139 return tmp.toString();
143 * Annotate the residues with their corresponding positions in s1 using the
144 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
150 public void makeExactMapping(AlignSeq as, SequenceI s1)
152 int pdbpos = as.getSeq2Start() - 2;
153 int alignpos = s1.getStart() + as.getSeq1Start() - 3;
154 // first clear out any old alignmentMapping values:
155 for (Atom atom : atoms)
157 atom.alignmentMapping = -1;
159 // and now trace the alignment onto the atom set.
160 for (int i = 0; i < as.astr1.length(); i++)
162 if (as.astr1.charAt(i) != '-')
167 if (as.astr2.charAt(i) != '-')
172 boolean sameResidue = Comparison.isSameResidue(as.astr1.charAt(i),
173 as.astr2.charAt(i), false);
176 if (pdbpos >= residues.size())
180 Residue res = residues.elementAt(pdbpos);
181 for (Atom atom : res.atoms)
183 atom.alignmentMapping = alignpos;
190 * Annotate the residues with their corresponding positions in s1 using the
191 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
197 public void makeExactMapping(StructureMapping mapping, SequenceI s1)
199 // first clear out any old alignmentMapping values:
200 for (Atom atom : atoms)
202 atom.alignmentMapping = -1;
205 while (ds.getDatasetSequence() != null)
207 ds = ds.getDatasetSequence();
210 for (Residue res : residues)
212 // res.number isn't set correctly for discontinuous/mismapped residues
213 int seqpos = mapping.getSeqPos(res.atoms.get(0).resNumber);
214 char strchar = sequence.getCharAt(pdboffset++);
215 if (seqpos == StructureMapping.UNASSIGNED_VALUE)
219 char seqchar = ds.getCharAt(seqpos - ds.getStart());
221 boolean sameResidue = Comparison.isSameResidue(seqchar, strchar,
225 for (Atom atom : res.atoms)
227 atom.alignmentMapping = seqpos - 1;
234 * Copies over the RESNUM seqfeatures from the internal chain sequence to the
239 * The Status of the transferred annotation
242 * the group id for the features on the destination sequence (e.g.
243 * the official accession ID)
245 public void transferRESNUMFeatures(SequenceI seq, String status,
248 if (altPDBID == null)
253 while (sq != null && sq.getDatasetSequence() != null)
255 sq = sq.getDatasetSequence();
263 * Remove any existing features for this chain if they exist ?
264 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
265 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
266 * ? for (int i=0; i<seqsfeatures.length; i++) { }
270 status = PDBChain.IEASTATUS;
273 List<SequenceFeature> features = sequence.getSequenceFeatures();
274 for (SequenceFeature feature : features)
276 if (feature.getFeatureGroup() != null
277 && feature.getFeatureGroup().equals(pdbid))
280 + residues.elementAt(feature.getBegin() - offset).atoms
281 .elementAt(0).alignmentMapping;
282 int newEnd = 1 + residues.elementAt(feature.getEnd() - offset).atoms
283 .elementAt(0).alignmentMapping;
284 SequenceFeature tx = new SequenceFeature(feature, newBegin, newEnd,
285 altPDBID, feature.getScore());
287 + ((tx.getStatus() == null || tx.getStatus().length() == 0)
289 : ":" + tx.getStatus()));
290 if (tx.begin != 0 && tx.end != 0)
292 sq.addSequenceFeature(tx);
299 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
300 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
301 * residues contain a P not a CA.
303 public void makeCaBondList()
307 for (int i = 0; i < (residues.size() - 1); i++)
309 Residue tmpres = residues.elementAt(i);
310 Residue tmpres2 = residues.elementAt(i + 1);
311 Atom at1 = tmpres.findAtom("CA");
312 Atom at2 = tmpres2.findAtom("CA");
314 if ((at1 == null) && (at2 == null))
317 at1 = tmpres.findAtom("P");
318 at2 = tmpres2.findAtom("P");
320 if ((at1 != null) && (at2 != null))
322 if (at1.chain.equals(at2.chain))
333 System.out.println("not found " + i);
338 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
341 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
348 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
354 public void makeBond(Atom at1, Atom at2)
356 bonds.addElement(new Bond(at1, at2));
360 * Traverses the list of atoms and
362 * <li>constructs a list of Residues, each containing all the atoms that share
363 * the same residue number</li>
364 * <li>adds a RESNUM sequence feature for each position</li>
365 * <li>creates the sequence string</li>
366 * <li>determines if nucleotide</li>
367 * <li>saves the residue number of the first atom as 'offset'</li>
368 * <li>adds temp factor annotation if the flag is set to do so</li>
371 * @param visibleChainAnnotation
373 public void makeResidueList(boolean visibleChainAnnotation)
377 boolean deoxyn = false;
378 boolean nucleotide = false;
379 StringBuilder seq = new StringBuilder(256);
380 Vector<SequenceFeature> resFeatures = new Vector<>();
381 Vector<Annotation> resAnnotation = new Vector<>();
382 int iSize = atoms.size() - 1;
386 for (int i = 0; i <= iSize; i++)
388 Atom tmp = atoms.elementAt(i);
389 resNumber = tmp.resNumber;
390 insCode = tmp.insCode;
400 Vector<Atom> resAtoms = new Vector<>();
401 // Add atoms to a vector while the residue number
402 // remains the same as the first atom's resNumber (res)
403 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
405 resAtoms.add(atoms.elementAt(i));
408 if (i < atoms.size())
410 resNumber = atoms.elementAt(i).resNumber;
411 insCode = atoms.elementAt(i).insCode;
419 // We need this to keep in step with the outer for i = loop
422 // Add inserted residues as features to the base residue
423 Atom currAtom = resAtoms.get(0);
424 if (currAtom.insCode != ' ' && !residues.isEmpty()
425 && residues.lastElement().atoms
426 .get(0).resNumber == currAtom.resNumber)
428 String desc = currAtom.resName + ":" + currAtom.resNumIns + " "
430 SequenceFeature sf = new SequenceFeature("INSERTION", desc,
431 offset + count - 1, offset + count - 1, "PDB_INS");
432 resFeatures.addElement(sf);
433 residues.lastElement().atoms.addAll(resAtoms);
437 // Make a new Residue object with the new atoms vector
438 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
440 Residue tmpres = residues.lastElement();
441 Atom tmpat = tmpres.atoms.get(0);
442 // Make A new SequenceFeature for the current residue numbering
443 String desc = tmpat.resName + ":" + tmpat.resNumIns + " " + pdbid
445 SequenceFeature sf = new SequenceFeature(RESNUM_FEATURE, desc,
446 offset + count, offset + count, pdbid);
447 resFeatures.addElement(sf);
448 resAnnotation.addElement(new Annotation(tmpat.tfactor));
449 // Keep totting up the sequence
451 if ((symbol = ResidueProperties.getAA3Hash()
452 .get(tmpat.resName)) == null)
454 String nucname = tmpat.resName.trim();
455 // use the aaIndex rather than call 'toLower' - which would take a bit
457 deoxyn = nucname.length() == 2
458 && ResidueProperties.aaIndex[nucname
459 .charAt(0)] == ResidueProperties.aaIndex['D'];
460 if (tmpat.name.equalsIgnoreCase("CA")
461 || ResidueProperties.nucleotideIndex[nucname
462 .charAt((deoxyn ? 1 : 0))] == -1)
464 char r = ResidueProperties.getSingleCharacterCode(
465 ResidueProperties.getCanonicalAminoAcid(tmpat.resName));
466 seq.append(r == '0' ? 'X' : r);
467 // System.err.println("PDBReader:Null aa3Hash for " +
474 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
482 "Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
484 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
495 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
499 // Add normalised feature scores to RESNUM indicating start/end of sequence
500 // sf.setScore(offset+count);
502 // System.out.println("PDB Sequence is :\nSequence = " + seq);
503 // System.out.println("No of residues = " + residues.size());
505 if (StructureImportSettings.isShowSeqFeatures())
507 iSize = resFeatures.size();
508 for (int i = 0; i < iSize; i++)
510 sequence.addSequenceFeature(resFeatures.elementAt(i));
511 resFeatures.setElementAt(null, i);
514 if (visibleChainAnnotation)
516 Annotation[] annots = new Annotation[resAnnotation.size()];
519 iSize = annots.length;
520 for (int i = 0; i < iSize; i++)
522 annots[i] = resAnnotation.elementAt(i);
523 tfacTemplate.processAnnotation(annots[i]);
524 max = Math.max(max, annots[i].value);
525 min = Math.min(min, annots[i].value);
526 resAnnotation.setElementAt(null, i);
528 if (tfacTemplate.isHasMinMax())
530 max = tfacTemplate.getMax();
531 min = tfacTemplate.getMin();
534 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
535 tfacTemplate.getName(),
536 (tfacTemplate.isHasDescription()
537 ? tfacTemplate.getDescription()
538 : tfacTemplate.getName()) + " for " + pdbid + id,
539 annots, min, max, AlignmentAnnotation.LINE_GRAPH);
540 tfactorann.setTFType(tfacTemplate.getTFType());
541 tfactorann.setCalcId(getClass().getName());
543 tfactorann.setSequenceRef(sequence);
544 sequence.addAlignmentAnnotation(tfactorann);
549 * Colour start/end of bonds by charge
551 * <li>ASP and GLU red</li>
552 * <li>LYS and ARG blue</li>
553 * <li>CYS yellow</li>
554 * <li>others light gray</li>
557 public void setChargeColours()
561 if (b.at1 != null && b.at2 != null)
563 b.startCol = getChargeColour(b.at1.resName);
564 b.endCol = getChargeColour(b.at2.resName);
568 b.startCol = Color.gray;
569 b.endCol = Color.gray;
574 public static Color getChargeColour(String resName)
576 Color result = Color.lightGray;
577 if ("ASP".equals(resName) || "GLU".equals(resName))
581 else if ("LYS".equals(resName) || "ARG".equals(resName))
585 else if ("CYS".equals(resName))
587 result = Color.yellow;
593 * Sets the start/end colours of bonds to those of the start/end atoms
594 * according to the specified colour scheme. Note: currently only works for
599 public void setChainColours(ColourSchemeI cs)
606 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
607 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
610 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
611 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
614 } catch (Exception e)
616 b.startCol = Color.gray;
617 b.endCol = Color.gray;
622 public void setChainColours(Color col)
632 * copy any sequence annotation onto the sequence mapped using the provided
636 * - positional mapping between destination sequence and pdb resnum
638 * - mapping between destination sequence and local chain
640 public void transferResidueAnnotation(StructureMapping mapping,
641 jalview.datamodel.Mapping sqmpping)
643 SequenceI sq = mapping.getSequence();
645 if (sqmpping == null)
647 // SIFTS mappings are recorded in the StructureMapping object...
649 sqmpping = mapping.getSeqToPdbMapping();
653 while (dsq.getDatasetSequence() != null)
655 dsq = dsq.getDatasetSequence();
657 // any annotation will be transferred onto the dataset sequence
659 if (shadow != null && shadow.getAnnotation() != null)
662 for (AlignmentAnnotation ana : shadow.getAnnotation())
664 // match on calcId, label and description so annotations from
665 // different structures are preserved
666 List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
667 ana.getCalcId(), ana.label, ana.description);
668 if (transfer == null || transfer.size() == 0)
670 ContactMatrixI cm = shadow.getContactMatrixFor(ana);
671 ana = new AlignmentAnnotation(ana);
672 // TODO map contact matrix under mapping
673 ana.liftOver(sequence, shadowMap);
674 ana.liftOver(dsq, sqmpping);
675 dsq.addAlignmentAnnotation(ana);
678 dsq.addContactListFor(ana, cm);
689 if (sequence != null && sequence.getAnnotation() != null)
691 for (AlignmentAnnotation ana : sequence.getAnnotation())
693 // match on calcId, label and description so annotations from
694 // different structures are preserved
695 List<AlignmentAnnotation> transfer = dsq
696 .getAlignmentAnnotations(ana.getCalcId(), ana.label,
698 if (transfer == null || transfer.size() == 0)
700 ContactMatrixI cm = sequence.getContactMatrixFor(ana);
701 ana = new AlignmentAnnotation(ana);
702 ana.liftOver(dsq, sqmpping);
703 dsq.addAlignmentAnnotation(ana);
704 if (cm != null && cm instanceof MappableContactMatrixI)
706 dsq.addContactListFor(ana, ((MappableContactMatrixI) cm)
707 .liftOver(dsq, sqmpping));
719 // Useful for debugging mappings - adds annotation for mapped position
720 float min = -1, max = 0;
721 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
722 for (int i = sq.getStart(), j = sq
723 .getEnd(), k = 0; i <= j; i++, k++)
725 int prn = mapping.getPDBResNum(k + 1);
727 an[k] = new Annotation(prn);
745 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
746 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
747 an, min, max, AlignmentAnnotation.LINE_GRAPH));