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;
43 * SequenceFeature group for PDB File features added to sequences
45 private static final String PDBFILEFEATURE = "PDBFile";
47 private static final String IEASTATUS = "IEA:jalview";
51 public Vector<Bond> bonds = new Vector<Bond>();
53 public Vector<Atom> atoms = new Vector<Atom>();
55 public Vector<Residue> residues = new Vector<Residue>();
60 * sequence is the sequence extracted by the chain parsing code
62 public SequenceI sequence;
65 * shadow is the sequence created by any other parsing processes (e.g. Jmol,
68 public SequenceI shadow = null;
70 public boolean isNa = false;
72 public boolean isVisible = true;
74 public int pdbstart = 0;
76 public int pdbend = 0;
78 public int seqstart = 0;
80 public int seqend = 0;
82 public String pdbid = "";
84 public PDBChain(String pdbid, String id)
86 this.pdbid = pdbid == null ? pdbid : pdbid.toLowerCase();
91 * character used to write newlines
93 protected String newline = System.getProperty("line.separator");
95 public Mapping shadowMap;
97 public void setNewlineString(String nl)
102 public String getNewlineString()
107 public String print()
109 StringBuilder tmp = new StringBuilder(256);
113 tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
114 .append(" ").append(offset).append(newline);
117 return tmp.toString();
121 * Annotate the residues with their corresponding positions in s1 using the
122 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
128 public void makeExactMapping(AlignSeq as, SequenceI s1)
130 int pdbpos = as.getSeq2Start() - 2;
131 int alignpos = s1.getStart() + as.getSeq1Start() - 3;
132 // first clear out any old alignmentMapping values:
133 for (Atom atom : atoms)
135 atom.alignmentMapping = -1;
137 // and now trace the alignment onto the atom set.
138 for (int i = 0; i < as.astr1.length(); i++)
140 if (as.astr1.charAt(i) != '-')
145 if (as.astr2.charAt(i) != '-')
150 boolean sameResidue = Comparison.isSameResidue(as.astr1.charAt(i),
151 as.astr2.charAt(i), false);
154 if (pdbpos >= residues.size())
158 Residue res = residues.elementAt(pdbpos);
159 for (Atom atom : res.atoms)
161 atom.alignmentMapping = alignpos;
168 * copy over the RESNUM seqfeatures from the internal chain sequence to the
173 * The Status of the transferred annotation
174 * @return the features added to sq (or its dataset)
176 public SequenceFeature[] transferRESNUMFeatures(SequenceI seq,
180 while (sq != null && sq.getDatasetSequence() != null)
182 sq = sq.getDatasetSequence();
189 * Remove any existing features for this chain if they exist ?
190 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
191 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
192 * ? for (int i=0; i<seqsfeatures.length; i++) { }
196 status = PDBChain.IEASTATUS;
198 SequenceFeature[] features = sequence.getSequenceFeatures();
199 if (features == null)
203 for (int i = 0; i < features.length; i++)
205 if (features[i].getFeatureGroup() != null
206 && features[i].getFeatureGroup().equals(pdbid))
208 SequenceFeature tx = new SequenceFeature(features[i]);
209 tx.setBegin(1 + residues.elementAt(tx.getBegin() - offset).atoms
210 .elementAt(0).alignmentMapping);
211 tx.setEnd(1 + residues.elementAt(tx.getEnd() - offset).atoms
212 .elementAt(0).alignmentMapping);
214 + ((tx.getStatus() == null || tx.getStatus().length() == 0) ? ""
215 : ":" + tx.getStatus()));
216 if (tx.begin != 0 && tx.end != 0)
218 sq.addSequenceFeature(tx);
226 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
227 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
228 * residues contain a P not a CA.
230 public void makeCaBondList()
234 for (int i = 0; i < (residues.size() - 1); i++)
236 Residue tmpres = residues.elementAt(i);
237 Residue tmpres2 = residues.elementAt(i + 1);
238 Atom at1 = tmpres.findAtom("CA");
239 Atom at2 = tmpres2.findAtom("CA");
241 if ((at1 == null) && (at2 == null))
244 at1 = tmpres.findAtom("P");
245 at2 = tmpres2.findAtom("P");
247 if ((at1 != null) && (at2 != null))
249 if (at1.chain.equals(at2.chain))
260 System.out.println("not found " + i);
265 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
268 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
275 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
281 public void makeBond(Atom at1, Atom at2)
283 bonds.addElement(new Bond(at1, at2));
287 * Traverses the list of atoms and
289 * <li>constructs a list of Residues, each containing all the atoms that share
290 * the same residue number</li>
291 * <li>adds a RESNUM sequence feature for each position</li>
292 * <li>creates the sequence string</li>
293 * <li>determines if nucleotide</li>
294 * <li>saves the residue number of the first atom as 'offset'</li>
295 * <li>adds temp factor annotation if the flag is set to do so</li>
298 * @param visibleChainAnnotation
300 public void makeResidueList(boolean visibleChainAnnotation)
304 boolean deoxyn = false;
305 boolean nucleotide = false;
306 StringBuilder seq = new StringBuilder(256);
307 Vector<SequenceFeature> resFeatures = new Vector<SequenceFeature>();
308 Vector<Annotation> resAnnotation = new Vector<Annotation>();
309 int i, iSize = atoms.size() - 1;
312 for (i = 0; i <= iSize; i++)
314 Atom tmp = atoms.elementAt(i);
315 resNumber = tmp.resNumber;
316 insCode = tmp.insCode;
326 Vector<Atom> resAtoms = new Vector<Atom>();
327 // Add atoms to a vector while the residue number
328 // remains the same as the first atom's resNumber (res)
329 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
331 resAtoms.add(atoms.elementAt(i));
334 if (i < atoms.size())
336 resNumber = atoms.elementAt(i).resNumber;
337 insCode = atoms.elementAt(i).insCode;
345 // We need this to keep in step with the outer for i = loop
348 // Add inserted residues as features to the base residue
349 Atom currAtom = resAtoms.get(0);
350 if (currAtom.insCode != ' '
351 && !residues.isEmpty()
352 && residues.lastElement().atoms.get(0).resNumber == currAtom.resNumber)
354 SequenceFeature sf = new SequenceFeature("INSERTION",
355 currAtom.resName + ":" + currAtom.resNumIns + " " + pdbid
356 + id, "", offset + count - 1, offset + count - 1,
358 resFeatures.addElement(sf);
359 residues.lastElement().atoms.addAll(resAtoms);
364 // Make a new Residue object with the new atoms vector
365 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
367 Residue tmpres = residues.lastElement();
368 Atom tmpat = tmpres.atoms.get(0);
369 // Make A new SequenceFeature for the current residue numbering
370 SequenceFeature sf = new SequenceFeature("RESNUM", tmpat.resName
371 + ":" + tmpat.resNumIns + " " + pdbid + id, "", offset
372 + count, offset + count, pdbid);
373 resFeatures.addElement(sf);
374 resAnnotation.addElement(new Annotation(tmpat.tfactor));
375 // Keep totting up the sequence
377 if ((symbol = ResidueProperties.getAA3Hash().get(tmpat.resName)) == null)
379 String nucname = tmpat.resName.trim();
380 // use the aaIndex rather than call 'toLower' - which would take a bit
382 deoxyn = nucname.length() == 2
383 && ResidueProperties.aaIndex[nucname.charAt(0)] == ResidueProperties.aaIndex['D'];
384 if (tmpat.name.equalsIgnoreCase("CA")
385 || ResidueProperties.nucleotideIndex[nucname
386 .charAt((deoxyn ? 1 : 0))] == -1)
388 char r = ResidueProperties
389 .getSingleCharacterCode(ResidueProperties
390 .getCanonicalAminoAcid(tmpat.resName));
391 seq.append(r == '0' ? 'X' : r);
392 // System.err.println("PDBReader:Null aa3Hash for " +
399 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
407 .println("Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
409 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
420 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
424 // Add normalised feature scores to RESNUM indicating start/end of sequence
425 // sf.setScore(offset+count);
427 // System.out.println("PDB Sequence is :\nSequence = " + seq);
428 // System.out.println("No of residues = " + residues.size());
430 if (StructureImportSettings.isShowSeqFeatures())
432 for (i = 0, iSize = resFeatures.size(); i < iSize; i++)
434 sequence.addSequenceFeature(resFeatures.elementAt(i));
435 resFeatures.setElementAt(null, i);
438 if (visibleChainAnnotation)
440 Annotation[] annots = new Annotation[resAnnotation.size()];
442 for (i = 0, iSize = annots.length; i < iSize; i++)
444 annots[i] = resAnnotation.elementAt(i);
445 if (annots[i].value > max)
447 max = annots[i].value;
449 resAnnotation.setElementAt(null, i);
452 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
453 "Temperature Factor", "Temperature Factor for " + pdbid + id,
454 annots, 0, max, AlignmentAnnotation.LINE_GRAPH);
455 tfactorann.setSequenceRef(sequence);
456 sequence.addAlignmentAnnotation(tfactorann);
461 * Colour start/end of bonds by charge
463 * <li>ASP and GLU red</li>
464 * <li>LYS and ARG blue</li>
465 * <li>CYS yellow</li>
466 * <li>others light gray</li>
469 public void setChargeColours()
473 if (b.at1 != null && b.at2 != null)
475 b.startCol = getChargeColour(b.at1.resName);
476 b.endCol = getChargeColour(b.at2.resName);
480 b.startCol = Color.gray;
481 b.endCol = Color.gray;
486 public static Color getChargeColour(String resName)
488 Color result = Color.lightGray;
489 if ("ASP".equals(resName) || "GLU".equals(resName))
493 else if ("LYS".equals(resName) || "ARG".equals(resName))
497 else if ("CYS".equals(resName))
499 result = Color.yellow;
505 * Sets the start/end colours of bonds to those of the start/end atoms
506 * according to the specified colour scheme. Note: currently only works for
511 public void setChainColours(ColourSchemeI cs)
518 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
519 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0),
522 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
523 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
526 } catch (Exception e)
528 b.startCol = Color.gray;
529 b.endCol = Color.gray;
534 public void setChainColours(Color col)
544 * copy any sequence annotation onto the sequence mapped using the provided
548 * - positional mapping between destination sequence and pdb resnum
550 * - mapping between destination sequence and local chain
552 public void transferResidueAnnotation(StructureMapping mapping,
553 jalview.datamodel.Mapping sqmpping)
555 SequenceI sq = mapping.getSequence();
559 while (dsq.getDatasetSequence() != null)
561 dsq = dsq.getDatasetSequence();
563 // any annotation will be transferred onto the dataset sequence
565 if (shadow != null && shadow.getAnnotation() != null)
568 for (AlignmentAnnotation ana : shadow.getAnnotation())
570 List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
571 ana.getCalcId(), ana.label);
572 if (transfer == null || transfer.size() == 0)
574 ana = new AlignmentAnnotation(ana);
575 ana.liftOver(sequence, shadowMap);
576 ana.liftOver(dsq, sqmpping);
577 dsq.addAlignmentAnnotation(ana);
587 if (sequence != null && sequence.getAnnotation() != null)
589 for (AlignmentAnnotation ana : sequence.getAnnotation())
591 List<AlignmentAnnotation> transfer = dsq
592 .getAlignmentAnnotations(ana.getCalcId(), ana.label);
593 if (transfer == null || transfer.size() == 0)
595 ana = new AlignmentAnnotation(ana);
596 ana.liftOver(dsq, sqmpping);
597 dsq.addAlignmentAnnotation(ana);
598 // mapping.transfer(ana);
609 // Useful for debugging mappings - adds annotation for mapped position
610 float min = -1, max = 0;
611 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
612 for (int i = sq.getStart(), j = sq.getEnd(), k = 0; i <= j; i++, k++)
614 int prn = mapping.getPDBResNum(k + 1);
616 an[k] = new Annotation(prn);
634 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
635 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
636 an, min, max, AlignmentAnnotation.LINE_GRAPH));