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.api.ColorI;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.Mapping;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
31 import jalview.schemes.Colour;
32 import jalview.schemes.ColourSchemeI;
33 import jalview.schemes.ResidueProperties;
34 import jalview.structure.StructureImportSettings;
35 import jalview.structure.StructureMapping;
37 import java.awt.Color;
38 import java.util.List;
39 import java.util.Vector;
44 * SequenceFeature group for PDB File features added to sequences
46 private static final String PDBFILEFEATURE = "PDBFile";
48 private static final String IEASTATUS = "IEA:jalview";
52 public Vector<Bond> bonds = new Vector<Bond>();
54 public Vector<Atom> atoms = new Vector<Atom>();
56 public Vector<Residue> residues = new Vector<Residue>();
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 public PDBChain(String pdbid, String id)
87 this.pdbid = pdbid == null ? pdbid : pdbid.toLowerCase();
92 * character used to write newlines
94 protected String newline = System.getProperty("line.separator");
96 public Mapping shadowMap;
98 public void setNewlineString(String nl)
103 public String getNewlineString()
108 public String print()
110 StringBuilder tmp = new StringBuilder(256);
114 tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
115 .append(" ").append(offset).append(newline);
118 return tmp.toString();
122 * Annotate the residues with their corresponding positions in s1 using the
123 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
129 public void makeExactMapping(AlignSeq as, SequenceI s1)
131 int pdbpos = as.getSeq2Start() - 2;
132 int alignpos = s1.getStart() + as.getSeq1Start() - 3;
133 // first clear out any old alignmentMapping values:
134 for (Atom atom : atoms)
136 atom.alignmentMapping = -1;
138 // and now trace the alignment onto the atom set.
139 for (int i = 0; i < as.astr1.length(); i++)
141 if (as.astr1.charAt(i) != '-')
146 if (as.astr2.charAt(i) != '-')
151 if (as.astr1.charAt(i) == as.astr2.charAt(i))
153 if (pdbpos >= residues.size())
157 Residue res = residues.elementAt(pdbpos);
158 for (Atom atom : res.atoms)
160 atom.alignmentMapping = alignpos;
167 * copy over the RESNUM seqfeatures from the internal chain sequence to the
172 * The Status of the transferred annotation
173 * @return the features added to sq (or its dataset)
175 public SequenceFeature[] transferRESNUMFeatures(SequenceI seq,
179 while (sq != null && sq.getDatasetSequence() != null)
181 sq = sq.getDatasetSequence();
188 * Remove any existing features for this chain if they exist ?
189 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
190 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
191 * ? for (int i=0; i<seqsfeatures.length; i++) { }
195 status = PDBChain.IEASTATUS;
197 SequenceFeature[] features = sequence.getSequenceFeatures();
198 if (features == null)
202 for (int i = 0; i < features.length; i++)
204 if (features[i].getFeatureGroup().equals(pdbid))
206 SequenceFeature tx = new SequenceFeature(features[i]);
207 tx.setBegin(1 + residues.elementAt(tx.getBegin() - offset).atoms
208 .elementAt(0).alignmentMapping);
209 tx.setEnd(1 + residues.elementAt(tx.getEnd() - offset).atoms
210 .elementAt(0).alignmentMapping);
212 + ((tx.getStatus() == null || tx.getStatus().length() == 0) ? ""
213 : ":" + tx.getStatus()));
214 if (tx.begin != 0 && tx.end != 0)
216 sq.addSequenceFeature(tx);
224 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
225 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
226 * residues contain a P not a CA.
228 public void makeCaBondList()
232 for (int i = 0; i < (residues.size() - 1); i++)
234 Residue tmpres = residues.elementAt(i);
235 Residue tmpres2 = residues.elementAt(i + 1);
236 Atom at1 = tmpres.findAtom("CA");
237 Atom at2 = tmpres2.findAtom("CA");
239 if ((at1 == null) && (at2 == null))
242 at1 = tmpres.findAtom("P");
243 at2 = tmpres2.findAtom("P");
245 if ((at1 != null) && (at2 != null))
247 if (at1.chain.equals(at2.chain))
258 System.out.println("not found " + i);
263 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
266 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
273 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
279 public void makeBond(Atom at1, Atom at2)
281 bonds.addElement(new Bond(at1, at2));
285 * Traverses the list of atoms and
287 * <li>constructs a list of Residues, each containing all the atoms that share
288 * the same residue number</li>
289 * <li>adds a RESNUM sequence feature for each position</li>
290 * <li>creates the sequence string</li>
291 * <li>determines if nucleotide</li>
292 * <li>saves the residue number of the first atom as 'offset'</li>
293 * <li>adds temp factor annotation if the flag is set to do so</li>
296 * @param visibleChainAnnotation
298 public void makeResidueList(boolean visibleChainAnnotation)
302 boolean deoxyn = false;
303 boolean nucleotide = false;
304 StringBuilder seq = new StringBuilder(256);
305 Vector<SequenceFeature> resFeatures = new Vector<SequenceFeature>();
306 Vector<Annotation> resAnnotation = new Vector<Annotation>();
307 int i, iSize = atoms.size() - 1;
310 for (i = 0; i <= iSize; i++)
312 Atom tmp = atoms.elementAt(i);
313 resNumber = tmp.resNumber;
314 insCode = tmp.insCode;
324 Vector<Atom> resAtoms = new Vector<Atom>();
325 // Add atoms to a vector while the residue number
326 // remains the same as the first atom's resNumber (res)
327 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
329 resAtoms.add(atoms.elementAt(i));
332 if (i < atoms.size())
334 resNumber = atoms.elementAt(i).resNumber;
335 insCode = atoms.elementAt(i).insCode;
343 // We need this to keep in step with the outer for i = loop
346 // Add inserted residues as features to the base residue
347 Atom currAtom = resAtoms.get(0);
348 if (currAtom.insCode != ' '
349 && !residues.isEmpty()
350 && residues.lastElement().atoms.get(0).resNumber == currAtom.resNumber)
352 SequenceFeature sf = new SequenceFeature("INSERTION",
353 currAtom.resName + ":" + currAtom.resNumIns + " " + pdbid
354 + id, "", offset + count - 1, offset + count - 1,
356 resFeatures.addElement(sf);
357 residues.lastElement().atoms.addAll(resAtoms);
362 // Make a new Residue object with the new atoms vector
363 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
365 Residue tmpres = residues.lastElement();
366 Atom tmpat = tmpres.atoms.get(0);
367 // Make A new SequenceFeature for the current residue numbering
368 SequenceFeature sf = new SequenceFeature("RESNUM", tmpat.resName
369 + ":" + tmpat.resNumIns + " " + pdbid + id, "", offset
370 + count, offset + count, pdbid);
371 resFeatures.addElement(sf);
372 resAnnotation.addElement(new Annotation(tmpat.tfactor));
373 // Keep totting up the sequence
375 if ((symbol = ResidueProperties.getAA3Hash().get(tmpat.resName)) == null)
377 String nucname = tmpat.resName.trim();
378 // use the aaIndex rather than call 'toLower' - which would take a bit
380 deoxyn = nucname.length() == 2
381 && ResidueProperties.aaIndex[nucname.charAt(0)] == ResidueProperties.aaIndex['D'];
382 if (tmpat.name.equalsIgnoreCase("CA")
383 || ResidueProperties.nucleotideIndex[nucname
384 .charAt((deoxyn ? 1 : 0))] == -1)
386 char r = ResidueProperties
387 .getSingleCharacterCode(ResidueProperties
388 .getCanonicalAminoAcid(tmpat.resName));
389 seq.append(r == '0' ? 'X' : r);
390 // System.err.println("PDBReader:Null aa3Hash for " +
397 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
405 .println("Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
407 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
418 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
422 // Add normalised feature scores to RESNUM indicating start/end of sequence
423 // sf.setScore(offset+count);
425 // System.out.println("PDB Sequence is :\nSequence = " + seq);
426 // System.out.println("No of residues = " + residues.size());
428 if (StructureImportSettings.isShowSeqFeatures())
430 for (i = 0, iSize = resFeatures.size(); i < iSize; i++)
432 sequence.addSequenceFeature(resFeatures.elementAt(i));
433 resFeatures.setElementAt(null, i);
436 if (visibleChainAnnotation)
438 Annotation[] annots = new Annotation[resAnnotation.size()];
440 for (i = 0, iSize = annots.length; i < iSize; i++)
442 annots[i] = resAnnotation.elementAt(i);
443 if (annots[i].value > max)
445 max = annots[i].value;
447 resAnnotation.setElementAt(null, i);
450 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
451 "Temperature Factor", "Temperature Factor for " + pdbid + id,
452 annots, 0, max, AlignmentAnnotation.LINE_GRAPH);
453 tfactorann.setSequenceRef(sequence);
454 sequence.addAlignmentAnnotation(tfactorann);
459 * Colour start/end of bonds by charge
461 * <li>ASP and GLU red</li>
462 * <li>LYS and ARG blue</li>
463 * <li>CYS yellow</li>
464 * <li>others light gray</li>
467 public void setChargeColours()
471 if (b.at1 != null && b.at2 != null)
473 b.startCol = getChargeColour(b.at1.resName);
474 b.endCol = getChargeColour(b.at2.resName);
478 b.startCol = Colour.gray;
479 b.endCol = Colour.gray;
484 public static ColorI getChargeColour(String resName)
486 ColorI result = Colour.lightGray;
487 if ("ASP".equals(resName) || "GLU".equals(resName))
491 else if ("LYS".equals(resName) || "ARG".equals(resName))
493 result = Colour.blue;
495 else if ("CYS".equals(resName))
497 result = Colour.yellow;
503 * Sets the start/end colours of bonds to those of the start/end atoms
504 * according to the specified colour scheme. Note: currently only works for
509 public void setChainColours(ColourSchemeI cs)
516 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
517 b.startCol = new Colour(cs.findColour(ResidueProperties.aa[index]
520 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
521 b.endCol = new Colour(cs.findColour(ResidueProperties.aa[index]
524 } catch (Exception e)
526 b.startCol = Colour.gray;
527 b.endCol = Colour.gray;
532 public void setChainColours(Color col)
536 b.startCol = new Colour(col);
537 b.endCol = new Colour(col);
542 * copy any sequence annotation onto the sequence mapped using the provided
546 * - positional mapping between destination sequence and pdb resnum
548 * - mapping between destination sequence and local chain
550 public void transferResidueAnnotation(StructureMapping mapping,
551 jalview.datamodel.Mapping sqmpping)
553 SequenceI sq = mapping.getSequence();
557 while (dsq.getDatasetSequence() != null)
559 dsq = dsq.getDatasetSequence();
561 // any annotation will be transferred onto the dataset sequence
563 if (shadow != null && shadow.getAnnotation() != null)
566 for (AlignmentAnnotation ana : shadow.getAnnotation())
568 List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
569 ana.getCalcId(), ana.label);
570 if (transfer == null || transfer.size() == 0)
572 ana = new AlignmentAnnotation(ana);
573 ana.liftOver(sequence, shadowMap);
574 ana.liftOver(dsq, sqmpping);
575 dsq.addAlignmentAnnotation(ana);
585 if (sequence != null && sequence.getAnnotation() != null)
587 for (AlignmentAnnotation ana : sequence.getAnnotation())
589 List<AlignmentAnnotation> transfer = dsq
590 .getAlignmentAnnotations(ana.getCalcId(), ana.label);
591 if (transfer == null || transfer.size() == 0)
593 ana = new AlignmentAnnotation(ana);
594 ana.liftOver(dsq, sqmpping);
595 dsq.addAlignmentAnnotation(ana);
596 // mapping.transfer(ana);
607 // Useful for debugging mappings - adds annotation for mapped position
608 float min = -1, max = 0;
609 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
610 for (int i = sq.getStart(), j = sq.getEnd(), k = 0; i <= j; i++, k++)
612 int prn = mapping.getPDBResNum(k + 1);
614 an[k] = new Annotation(prn);
632 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
633 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
634 an, min, max, AlignmentAnnotation.LINE_GRAPH));