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.StructureMapping;
34 import java.awt.Color;
35 import java.util.List;
36 import java.util.Vector;
41 * SequenceFeature group for PDB File features added to sequences
43 private static final String PDBFILEFEATURE = "PDBFile";
45 private static final String IEASTATUS = "IEA:jalview";
49 public Vector<Bond> bonds = new Vector<Bond>();
51 public Vector<Atom> atoms = new Vector<Atom>();
53 public Vector<Residue> residues = new Vector<Residue>();
58 * sequence is the sequence extracted by the chain parsing code
60 public SequenceI sequence;
63 * shadow is the sequence created by any other parsing processes (e.g. Jmol,
66 public SequenceI shadow = null;
68 public boolean isNa = false;
70 public boolean isVisible = true;
72 public int pdbstart = 0;
74 public int pdbend = 0;
76 public int seqstart = 0;
78 public int seqend = 0;
80 public String pdbid = "";
82 public PDBChain(String pdbid, String id)
84 this.pdbid = pdbid.toLowerCase();
89 * character used to write newlines
91 protected String newline = System.getProperty("line.separator");
93 public Mapping shadowMap;
95 public void setNewlineString(String nl)
100 public String getNewlineString()
105 public String print()
107 StringBuilder tmp = new StringBuilder(256);
111 tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
112 .append(" ").append(offset).append(newline);
115 return tmp.toString();
119 * Annotate the residues with their corresponding positions in s1 using the
120 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
126 public void makeExactMapping(AlignSeq as, SequenceI s1)
128 int pdbpos = as.getSeq2Start() - 2;
129 int alignpos = s1.getStart() + as.getSeq1Start() - 3;
130 // first clear out any old alignmentMapping values:
131 for (Atom atom : atoms)
133 atom.alignmentMapping = -1;
135 // and now trace the alignment onto the atom set.
136 for (int i = 0; i < as.astr1.length(); i++)
138 if (as.astr1.charAt(i) != '-')
143 if (as.astr2.charAt(i) != '-')
148 if (as.astr1.charAt(i) == as.astr2.charAt(i))
150 Residue res = residues.elementAt(pdbpos);
151 for (Atom atom : res.atoms)
153 atom.alignmentMapping = alignpos;
160 * copy over the RESNUM seqfeatures from the internal chain sequence to the
165 * The Status of the transferred annotation
166 * @return the features added to sq (or its dataset)
168 public SequenceFeature[] transferRESNUMFeatures(SequenceI seq,
172 while (sq != null && sq.getDatasetSequence() != null)
174 sq = sq.getDatasetSequence();
181 * Remove any existing features for this chain if they exist ?
182 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
183 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
184 * ? for (int i=0; i<seqsfeatures.length; i++) { }
188 status = PDBChain.IEASTATUS;
190 SequenceFeature[] features = sequence.getSequenceFeatures();
191 for (int i = 0; i < features.length; i++)
193 if (features[i].getFeatureGroup().equals(pdbid))
195 SequenceFeature tx = new SequenceFeature(features[i]);
196 tx.setBegin(1 + residues.elementAt(tx.getBegin()
197 - offset).atoms.elementAt(0).alignmentMapping);
198 tx.setEnd(1 + residues.elementAt(tx.getEnd()
199 - offset).atoms.elementAt(0).alignmentMapping);
201 + ((tx.getStatus() == null || tx.getStatus().length() == 0) ? ""
202 : ":" + tx.getStatus()));
203 if (tx.begin != 0 && tx.end != 0)
205 sq.addSequenceFeature(tx);
213 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
214 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
215 * residues contain a P not a CA.
217 public void makeCaBondList()
221 for (int i = 0; i < (residues.size() - 1); i++)
223 Residue tmpres = residues.elementAt(i);
224 Residue tmpres2 = residues.elementAt(i + 1);
225 Atom at1 = tmpres.findAtom("CA");
226 Atom at2 = tmpres2.findAtom("CA");
228 if ((at1 == null) && (at2 == null))
231 at1 = tmpres.findAtom("P");
232 at2 = tmpres2.findAtom("P");
234 if ((at1 != null) && (at2 != null))
236 if (at1.chain.equals(at2.chain))
247 System.out.println("not found " + i);
252 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
255 if (residues.size() > 0 && (numNa / (residues.size() - 1) > 0.99))
262 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
268 public void makeBond(Atom at1, Atom at2)
270 bonds.addElement(new Bond(at1, at2));
274 * Traverses the list of atoms and
276 * <li>constructs a list of Residues, each containing all the atoms that share
277 * the same residue number</li>
278 * <li>adds a RESNUM sequence feature for each position</li>
279 * <li>creates the sequence string</li>
280 * <li>determines if nucleotide</li>
281 * <li>saves the residue number of the first atom as 'offset'</li>
282 * <li>adds temp factor annotation if the flag is set to do so</li>
285 * @param visibleChainAnnotation
287 public void makeResidueList(boolean visibleChainAnnotation)
291 boolean deoxyn = false;
292 boolean nucleotide = false;
293 StringBuilder seq = new StringBuilder(256);
294 Vector<SequenceFeature> resFeatures = new Vector<SequenceFeature>();
295 Vector<Annotation> resAnnotation = new Vector<Annotation>();
296 int i, iSize = atoms.size() - 1;
298 for (i = 0; i <= iSize; i++)
300 Atom tmp = atoms.elementAt(i);
301 resNumber = tmp.resNumber;
309 Vector<Atom> resAtoms = new Vector<Atom>();
310 // Add atoms to a vector while the residue number
311 // remains the same as the first atom's resNumber (res)
312 while ((resNumber == res) && (i < atoms.size()))
314 resAtoms.add(atoms.elementAt(i));
317 if (i < atoms.size())
319 resNumber = atoms.elementAt(i).resNumber;
327 // We need this to keep in step with the outer for i = loop
330 // Make a new Residue object with the new atoms vector
331 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
333 Residue tmpres = residues.lastElement();
334 Atom tmpat = tmpres.atoms.get(0);
335 // Make A new SequenceFeature for the current residue numbering
336 SequenceFeature sf = new SequenceFeature("RESNUM", tmpat.resName
337 + ":" + tmpat.resNumIns + " " + pdbid + id, "", offset
338 + count, offset + count, pdbid);
339 // MCview.PDBChain.PDBFILEFEATURE);
340 resFeatures.addElement(sf);
341 resAnnotation.addElement(new Annotation(tmpat.tfactor));
342 // Keep totting up the sequence
343 if ((symbol = ResidueProperties.getAA3Hash().get(tmpat.resName)) == null)
345 String nucname = tmpat.resName.trim();
346 // use the aaIndex rather than call 'toLower' - which would take a bit
348 deoxyn = nucname.length() == 2
349 && ResidueProperties.aaIndex[nucname.charAt(0)] == ResidueProperties.aaIndex['D'];
350 if (tmpat.name.equalsIgnoreCase("CA")
351 || ResidueProperties.nucleotideIndex[nucname
352 .charAt((deoxyn ? 1 : 0))] == -1)
355 // System.err.println("PDBReader:Null aa3Hash for " +
362 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
370 .println("Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
372 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
382 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
386 // Add normalised feature scores to RESNUM indicating start/end of sequence
387 // sf.setScore(offset+count);
389 // System.out.println("PDB Sequence is :\nSequence = " + seq);
390 // System.out.println("No of residues = " + residues.size());
391 for (i = 0, iSize = resFeatures.size(); i < iSize; i++)
393 sequence.addSequenceFeature(resFeatures.elementAt(i));
394 resFeatures.setElementAt(null, i);
396 if (visibleChainAnnotation)
398 Annotation[] annots = new Annotation[resAnnotation.size()];
400 for (i = 0, iSize = annots.length; i < iSize; i++)
402 annots[i] = resAnnotation.elementAt(i);
403 if (annots[i].value > max)
405 max = annots[i].value;
407 resAnnotation.setElementAt(null, i);
410 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
411 "Temperature Factor", "Temperature Factor for " + pdbid + id,
413 AlignmentAnnotation.LINE_GRAPH);
414 tfactorann.setSequenceRef(sequence);
415 sequence.addAlignmentAnnotation(tfactorann);
420 * Colour start/end of bonds by charge
422 * <li>ASP and GLU red</li>
423 * <li>LYS and ARG blue</li>
424 * <li>CYS yellow</li>
425 * <li>others light gray</li>
428 public void setChargeColours()
432 if (b.at1 != null && b.at2 != null)
434 b.startCol = getChargeColour(b.at1.resName);
435 b.endCol = getChargeColour(b.at2.resName);
439 b.startCol = Color.gray;
440 b.endCol = Color.gray;
445 public static Color getChargeColour(String resName)
447 Color result = Color.lightGray;
448 if ("ASP".equals(resName) || "GLU".equals(resName))
452 else if ("LYS".equals(resName) || "ARG".equals(resName))
456 else if ("CYS".equals(resName))
458 result = Color.yellow;
464 * Sets the start/end colours of bonds to those of the start/end atoms
465 * according to the specified colour scheme. Note: currently only works for
470 public void setChainColours(ColourSchemeI cs)
477 index = ResidueProperties.aa3Hash.get(b.at1.resName)
479 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
481 index = ResidueProperties.aa3Hash.get(b.at2.resName)
483 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
485 } catch (Exception e)
487 b.startCol = Color.gray;
488 b.endCol = Color.gray;
493 public void setChainColours(Color col)
503 * copy any sequence annotation onto the sequence mapped using the provided
507 * - positional mapping between destination sequence and pdb resnum
509 * - mapping between destination sequence and local chain
511 public void transferResidueAnnotation(StructureMapping mapping,
514 SequenceI sq = mapping.getSequence();
518 while (dsq.getDatasetSequence() != null)
520 dsq = dsq.getDatasetSequence();
522 // any annotation will be transferred onto the dataset sequence
524 if (shadow != null && shadow.getAnnotation() != null)
527 for (AlignmentAnnotation ana : shadow.getAnnotation())
529 List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
530 ana.getCalcId(), ana.label);
531 if (transfer == null || transfer.size() == 0)
533 ana = new AlignmentAnnotation(ana);
534 ana.liftOver(sequence, shadowMap);
535 ana.liftOver(dsq, sqmpping);
536 dsq.addAlignmentAnnotation(ana);
546 if (sequence != null && sequence.getAnnotation() != null)
548 for (AlignmentAnnotation ana : sequence.getAnnotation())
550 List<AlignmentAnnotation> transfer = sq
551 .getAlignmentAnnotations(ana.getCalcId(), ana.label);
552 if (transfer == null || transfer.size() == 0)
554 ana = new AlignmentAnnotation(ana);
555 ana.liftOver(dsq, sqmpping);
556 // mapping.transfer(ana);
567 // Useful for debugging mappings - adds annotation for mapped position
568 float min = -1, max = 0;
569 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
570 for (int i = sq.getStart(), j = sq.getEnd(), k = 0; i <= j; i++, k++)
572 int prn = mapping.getPDBResNum(k + 1);
574 an[k] = new Annotation(prn);
592 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
593 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
594 an, min, max, AlignmentAnnotation.LINE_GRAPH));