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
232 * @param actualGroupId the group id for the features on the destination sequence (e.g. the official accession ID)
234 public void transferRESNUMFeatures(SequenceI seq,
238 while (sq != null && sq.getDatasetSequence() != null)
240 sq = sq.getDatasetSequence();
248 * Remove any existing features for this chain if they exist ?
249 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
250 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
251 * ? for (int i=0; i<seqsfeatures.length; i++) { }
255 status = PDBChain.IEASTATUS;
258 List<SequenceFeature> features = sequence.getSequenceFeatures();
259 for (SequenceFeature feature : features)
261 if (feature.getFeatureGroup() != null
262 && feature.getFeatureGroup().equals(pdbid))
264 int newBegin = 1 + residues.elementAt(feature.getBegin() - offset).atoms
265 .elementAt(0).alignmentMapping;
266 int newEnd = 1 + residues.elementAt(feature.getEnd() - offset).atoms
267 .elementAt(0).alignmentMapping;
268 SequenceFeature tx = new SequenceFeature(feature, newBegin, newEnd,
269 pdbid, feature.getScore());
271 + ((tx.getStatus() == null || tx.getStatus().length() == 0)
273 : ":" + tx.getStatus()));
274 if (tx.begin != 0 && tx.end != 0)
276 sq.addSequenceFeature(tx);
283 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
284 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
285 * residues contain a P not a CA.
287 public void makeCaBondList()
291 for (int i = 0; i < (residues.size() - 1); i++)
293 Residue tmpres = residues.elementAt(i);
294 Residue tmpres2 = residues.elementAt(i + 1);
295 Atom at1 = tmpres.findAtom("CA");
296 Atom at2 = tmpres2.findAtom("CA");
298 if ((at1 == null) && (at2 == null))
301 at1 = tmpres.findAtom("P");
302 at2 = tmpres2.findAtom("P");
304 if ((at1 != null) && (at2 != null))
306 if (at1.chain.equals(at2.chain))
317 System.out.println("not found " + i);
322 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
325 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
332 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
338 public void makeBond(Atom at1, Atom at2)
340 bonds.addElement(new Bond(at1, at2));
344 * Traverses the list of atoms and
346 * <li>constructs a list of Residues, each containing all the atoms that share
347 * the same residue number</li>
348 * <li>adds a RESNUM sequence feature for each position</li>
349 * <li>creates the sequence string</li>
350 * <li>determines if nucleotide</li>
351 * <li>saves the residue number of the first atom as 'offset'</li>
352 * <li>adds temp factor annotation if the flag is set to do so</li>
355 * @param visibleChainAnnotation
357 public void makeResidueList(boolean visibleChainAnnotation)
361 boolean deoxyn = false;
362 boolean nucleotide = false;
363 StringBuilder seq = new StringBuilder(256);
364 Vector<SequenceFeature> resFeatures = new Vector<>();
365 Vector<Annotation> resAnnotation = new Vector<>();
366 int iSize = atoms.size() - 1;
370 for (int i = 0; i <= iSize; i++)
372 Atom tmp = atoms.elementAt(i);
373 resNumber = tmp.resNumber;
374 insCode = tmp.insCode;
384 Vector<Atom> resAtoms = new Vector<>();
385 // Add atoms to a vector while the residue number
386 // remains the same as the first atom's resNumber (res)
387 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
389 resAtoms.add(atoms.elementAt(i));
392 if (i < atoms.size())
394 resNumber = atoms.elementAt(i).resNumber;
395 insCode = atoms.elementAt(i).insCode;
403 // We need this to keep in step with the outer for i = loop
406 // Add inserted residues as features to the base residue
407 Atom currAtom = resAtoms.get(0);
408 if (currAtom.insCode != ' ' && !residues.isEmpty()
409 && residues.lastElement().atoms
410 .get(0).resNumber == currAtom.resNumber)
412 String desc = currAtom.resName + ":" + currAtom.resNumIns + " "
414 SequenceFeature sf = new SequenceFeature("INSERTION", desc, offset
415 + count - 1, offset + count - 1, "PDB_INS");
416 resFeatures.addElement(sf);
417 residues.lastElement().atoms.addAll(resAtoms);
421 // Make a new Residue object with the new atoms vector
422 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
424 Residue tmpres = residues.lastElement();
425 Atom tmpat = tmpres.atoms.get(0);
426 // Make A new SequenceFeature for the current residue numbering
427 String desc = tmpat.resName
428 + ":" + tmpat.resNumIns + " " + pdbid + id;
429 SequenceFeature sf = new SequenceFeature(RESNUM_FEATURE, desc,
430 offset + count, offset + count, pdbid);
431 resFeatures.addElement(sf);
432 resAnnotation.addElement(new Annotation(tmpat.tfactor));
433 // Keep totting up the sequence
435 if ((symbol = ResidueProperties.getAA3Hash()
436 .get(tmpat.resName)) == null)
438 String nucname = tmpat.resName.trim();
439 // use the aaIndex rather than call 'toLower' - which would take a bit
441 deoxyn = nucname.length() == 2
442 && ResidueProperties.aaIndex[nucname
443 .charAt(0)] == ResidueProperties.aaIndex['D'];
444 if (tmpat.name.equalsIgnoreCase("CA")
445 || ResidueProperties.nucleotideIndex[nucname
446 .charAt((deoxyn ? 1 : 0))] == -1)
448 char r = ResidueProperties.getSingleCharacterCode(
449 ResidueProperties.getCanonicalAminoAcid(tmpat.resName));
450 seq.append(r == '0' ? 'X' : r);
451 // System.err.println("PDBReader:Null aa3Hash for " +
458 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
466 "Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
468 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
479 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
483 // Add normalised feature scores to RESNUM indicating start/end of sequence
484 // sf.setScore(offset+count);
486 // System.out.println("PDB Sequence is :\nSequence = " + seq);
487 // System.out.println("No of residues = " + residues.size());
489 if (StructureImportSettings.isShowSeqFeatures())
491 iSize = resFeatures.size();
492 for (int i = 0; i < iSize; i++)
494 sequence.addSequenceFeature(resFeatures.elementAt(i));
495 resFeatures.setElementAt(null, i);
498 if (visibleChainAnnotation)
500 Annotation[] annots = new Annotation[resAnnotation.size()];
503 iSize = annots.length;
504 for (int i = 0; i < iSize; i++)
506 annots[i] = resAnnotation.elementAt(i);
507 max = Math.max(max, annots[i].value);
508 min = Math.min(min, annots[i].value);
509 resAnnotation.setElementAt(null, i);
511 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
512 tfacName, tfacName + " for " + pdbid + id,
513 annots, min, max, AlignmentAnnotation.LINE_GRAPH);
515 tfactorann.setCalcId(getClass().getName());
517 tfactorann.setSequenceRef(sequence);
518 sequence.addAlignmentAnnotation(tfactorann);
524 * Colour start/end of bonds by charge
526 * <li>ASP and GLU red</li>
527 * <li>LYS and ARG blue</li>
528 * <li>CYS yellow</li>
529 * <li>others light gray</li>
532 public void setChargeColours()
536 if (b.at1 != null && b.at2 != null)
538 b.startCol = getChargeColour(b.at1.resName);
539 b.endCol = getChargeColour(b.at2.resName);
543 b.startCol = Color.gray;
544 b.endCol = Color.gray;
549 public static Color getChargeColour(String resName)
551 Color result = Color.lightGray;
552 if ("ASP".equals(resName) || "GLU".equals(resName))
556 else if ("LYS".equals(resName) || "ARG".equals(resName))
560 else if ("CYS".equals(resName))
562 result = Color.yellow;
568 * Sets the start/end colours of bonds to those of the start/end atoms
569 * according to the specified colour scheme. Note: currently only works for
574 public void setChainColours(ColourSchemeI cs)
581 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
582 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
585 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
586 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
589 } catch (Exception e)
591 b.startCol = Color.gray;
592 b.endCol = Color.gray;
597 public void setChainColours(Color col)
607 * copy any sequence annotation onto the sequence mapped using the provided
611 * - positional mapping between destination sequence and pdb resnum
613 * - mapping between destination sequence and local chain
615 public void transferResidueAnnotation(StructureMapping mapping,
616 jalview.datamodel.Mapping sqmpping)
618 SequenceI sq = mapping.getSequence();
620 if (sqmpping == null)
622 // SIFTS mappings are recorded in the StructureMapping object...
624 sqmpping = mapping.getSeqToPdbMapping();
628 while (dsq.getDatasetSequence() != null)
630 dsq = dsq.getDatasetSequence();
632 // any annotation will be transferred onto the dataset sequence
634 if (shadow != null && shadow.getAnnotation() != null)
637 for (AlignmentAnnotation ana : shadow.getAnnotation())
639 // match on calcId, label and description so annotations from
640 // different structures are preserved
641 List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
642 ana.getCalcId(), ana.label, ana.description);
643 if (transfer == null || transfer.size() == 0)
645 ana = new AlignmentAnnotation(ana);
646 ana.liftOver(sequence, shadowMap);
647 ana.liftOver(dsq, sqmpping);
648 dsq.addAlignmentAnnotation(ana);
658 if (sequence != null && sequence.getAnnotation() != null)
660 for (AlignmentAnnotation ana : sequence.getAnnotation())
662 // match on calcId, label and description so annotations from
663 // different structures are preserved
664 List<AlignmentAnnotation> transfer = dsq
665 .getAlignmentAnnotations(ana.getCalcId(), ana.label,
667 if (transfer == null || transfer.size() == 0)
669 ana = new AlignmentAnnotation(ana);
670 ana.liftOver(dsq, sqmpping);
671 dsq.addAlignmentAnnotation(ana);
672 // mapping.transfer(ana);
683 // Useful for debugging mappings - adds annotation for mapped position
684 float min = -1, max = 0;
685 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
686 for (int i = sq.getStart(), j = sq
687 .getEnd(), k = 0; i <= j; i++, k++)
689 int prn = mapping.getPDBResNum(k + 1);
691 an[k] = new Annotation(prn);
709 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
710 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
711 an, min, max, AlignmentAnnotation.LINE_GRAPH));