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;
33 import jalview.structure.StructureViewSettings;
35 import java.awt.Color;
36 import java.util.List;
37 import java.util.Vector;
42 * SequenceFeature group for PDB File features added to sequences
44 private static final String PDBFILEFEATURE = "PDBFile";
46 private static final String IEASTATUS = "IEA:jalview";
50 public Vector<Bond> bonds = new Vector<Bond>();
52 public Vector<Atom> atoms = new Vector<Atom>();
54 public Vector<Residue> residues = new Vector<Residue>();
59 * sequence is the sequence extracted by the chain parsing code
61 public SequenceI sequence;
64 * shadow is the sequence created by any other parsing processes (e.g. Jmol,
67 public SequenceI shadow = null;
69 public boolean isNa = false;
71 public boolean isVisible = true;
73 public int pdbstart = 0;
75 public int pdbend = 0;
77 public int seqstart = 0;
79 public int seqend = 0;
81 public String pdbid = "";
83 public PDBChain(String pdbid, String id)
85 this.pdbid = pdbid.toLowerCase();
90 * character used to write newlines
92 protected String newline = System.getProperty("line.separator");
94 public Mapping shadowMap;
96 public void setNewlineString(String nl)
101 public String getNewlineString()
106 public String print()
108 StringBuilder tmp = new StringBuilder(256);
112 tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
113 .append(" ").append(offset).append(newline);
116 return tmp.toString();
120 * Annotate the residues with their corresponding positions in s1 using the
121 * alignment in as NOTE: This clears all atom.alignmentMapping values on the
127 public void makeExactMapping(AlignSeq as, SequenceI s1)
129 int pdbpos = as.getSeq2Start() - 2;
130 int alignpos = s1.getStart() + as.getSeq1Start() - 3;
131 // first clear out any old alignmentMapping values:
132 for (Atom atom : atoms)
134 atom.alignmentMapping = -1;
136 // and now trace the alignment onto the atom set.
137 for (int i = 0; i < as.astr1.length(); i++)
139 if (as.astr1.charAt(i) != '-')
144 if (as.astr2.charAt(i) != '-')
149 if (as.astr1.charAt(i) == as.astr2.charAt(i))
151 if (pdbpos >= residues.size())
155 Residue res = residues.elementAt(pdbpos);
156 for (Atom atom : res.atoms)
158 atom.alignmentMapping = alignpos;
165 * copy over the RESNUM seqfeatures from the internal chain sequence to the
170 * The Status of the transferred annotation
171 * @return the features added to sq (or its dataset)
173 public SequenceFeature[] transferRESNUMFeatures(SequenceI seq,
177 while (sq != null && sq.getDatasetSequence() != null)
179 sq = sq.getDatasetSequence();
186 * Remove any existing features for this chain if they exist ?
187 * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
188 * totfeat=seqsfeatures.length; // Remove any features for this exact chain
189 * ? for (int i=0; i<seqsfeatures.length; i++) { }
193 status = PDBChain.IEASTATUS;
195 SequenceFeature[] features = sequence.getSequenceFeatures();
196 if (features == null)
200 for (int i = 0; i < features.length; i++)
202 if (features[i].getFeatureGroup().equals(pdbid))
204 SequenceFeature tx = new SequenceFeature(features[i]);
205 tx.setBegin(1 + residues.elementAt(tx.getBegin() - offset).atoms
206 .elementAt(0).alignmentMapping);
207 tx.setEnd(1 + residues.elementAt(tx.getEnd() - offset).atoms
208 .elementAt(0).alignmentMapping);
210 + ((tx.getStatus() == null || tx.getStatus().length() == 0) ? ""
211 : ":" + tx.getStatus()));
212 if (tx.begin != 0 && tx.end != 0)
214 sq.addSequenceFeature(tx);
222 * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
223 * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
224 * residues contain a P not a CA.
226 public void makeCaBondList()
230 for (int i = 0; i < (residues.size() - 1); i++)
232 Residue tmpres = residues.elementAt(i);
233 Residue tmpres2 = residues.elementAt(i + 1);
234 Atom at1 = tmpres.findAtom("CA");
235 Atom at2 = tmpres2.findAtom("CA");
237 if ((at1 == null) && (at2 == null))
240 at1 = tmpres.findAtom("P");
241 at2 = tmpres2.findAtom("P");
243 if ((at1 != null) && (at2 != null))
245 if (at1.chain.equals(at2.chain))
256 System.out.println("not found " + i);
261 * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
264 if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
271 * Construct a bond from atom1 to atom2 and add it to the list of bonds for
277 public void makeBond(Atom at1, Atom at2)
279 bonds.addElement(new Bond(at1, at2));
283 * Traverses the list of atoms and
285 * <li>constructs a list of Residues, each containing all the atoms that share
286 * the same residue number</li>
287 * <li>adds a RESNUM sequence feature for each position</li>
288 * <li>creates the sequence string</li>
289 * <li>determines if nucleotide</li>
290 * <li>saves the residue number of the first atom as 'offset'</li>
291 * <li>adds temp factor annotation if the flag is set to do so</li>
294 * @param visibleChainAnnotation
296 public void makeResidueList(boolean visibleChainAnnotation)
300 boolean deoxyn = false;
301 boolean nucleotide = false;
302 StringBuilder seq = new StringBuilder(256);
303 Vector<SequenceFeature> resFeatures = new Vector<SequenceFeature>();
304 Vector<Annotation> resAnnotation = new Vector<Annotation>();
305 int i, iSize = atoms.size() - 1;
308 for (i = 0; i <= iSize; i++)
310 Atom tmp = atoms.elementAt(i);
311 resNumber = tmp.resNumber;
312 insCode = tmp.insCode;
322 Vector<Atom> resAtoms = new Vector<Atom>();
323 // Add atoms to a vector while the residue number
324 // remains the same as the first atom's resNumber (res)
325 while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
327 resAtoms.add(atoms.elementAt(i));
330 if (i < atoms.size())
332 resNumber = atoms.elementAt(i).resNumber;
333 insCode = atoms.elementAt(i).insCode;
341 // We need this to keep in step with the outer for i = loop
344 // Add inserted residues as features to the base residue
345 Atom currAtom = resAtoms.get(0);
346 if (currAtom.insCode != ' '
347 && !residues.isEmpty()
348 && residues.lastElement().atoms.get(0).resNumber == currAtom.resNumber)
350 SequenceFeature sf = new SequenceFeature("INSERTION",
351 currAtom.resName + ":" + currAtom.resNumIns + " " + pdbid
352 + id, "", offset + count - 1, offset + count - 1,
354 resFeatures.addElement(sf);
355 residues.lastElement().atoms.addAll(resAtoms);
360 // boolean baseDetected = false;
361 // for (Atom resAtom : resAtoms)
363 // if (resAtom.insCode == ' ')
365 // baseDetected = true;
368 // if (!baseDetected)
372 // Make a new Residue object with the new atoms vector
373 residues.addElement(new Residue(resAtoms, resNumber - 1, count));
375 Residue tmpres = residues.lastElement();
376 Atom tmpat = tmpres.atoms.get(0);
377 // Make A new SequenceFeature for the current residue numbering
378 SequenceFeature sf = new SequenceFeature("RESNUM", tmpat.resName
379 + ":" + tmpat.resNumIns + " " + pdbid + id, "", offset
380 + count, offset + count, pdbid);
381 // MCview.PDBChain.PDBFILEFEATURE);
382 resFeatures.addElement(sf);
383 resAnnotation.addElement(new Annotation(tmpat.tfactor));
384 // Keep totting up the sequence
386 if ((symbol = ResidueProperties.getAA3Hash().get(tmpat.resName)) == null)
388 String nucname = tmpat.resName.trim();
389 // use the aaIndex rather than call 'toLower' - which would take a bit
391 deoxyn = nucname.length() == 2
392 && ResidueProperties.aaIndex[nucname.charAt(0)] == ResidueProperties.aaIndex['D'];
393 if (tmpat.name.equalsIgnoreCase("CA")
394 || ResidueProperties.nucleotideIndex[nucname
395 .charAt((deoxyn ? 1 : 0))] == -1)
397 char r = ResidueProperties
398 .getSingleCharacterCode(ResidueProperties
399 .getCanonicalAminoAcid(tmpat.resName));
400 seq.append(r == '0' ? 'X' : r);
401 // System.err.println("PDBReader:Null aa3Hash for " +
408 seq.append(nucname.charAt((deoxyn ? 1 : 0)));
416 .println("Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
418 seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
429 sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
433 // Add normalised feature scores to RESNUM indicating start/end of sequence
434 // sf.setScore(offset+count);
436 // System.out.println("PDB Sequence is :\nSequence = " + seq);
437 // System.out.println("No of residues = " + residues.size());
439 if (StructureViewSettings.isShowSeqFeatures())
441 for (i = 0, iSize = resFeatures.size(); i < iSize; i++)
443 sequence.addSequenceFeature(resFeatures.elementAt(i));
444 resFeatures.setElementAt(null, i);
447 if (visibleChainAnnotation)
449 Annotation[] annots = new Annotation[resAnnotation.size()];
451 for (i = 0, iSize = annots.length; i < iSize; i++)
453 annots[i] = resAnnotation.elementAt(i);
454 if (annots[i].value > max)
456 max = annots[i].value;
458 resAnnotation.setElementAt(null, i);
461 AlignmentAnnotation tfactorann = new AlignmentAnnotation(
462 "Temperature Factor", "Temperature Factor for " + pdbid + id,
463 annots, 0, max, AlignmentAnnotation.LINE_GRAPH);
464 tfactorann.setSequenceRef(sequence);
465 sequence.addAlignmentAnnotation(tfactorann);
470 * Colour start/end of bonds by charge
472 * <li>ASP and GLU red</li>
473 * <li>LYS and ARG blue</li>
474 * <li>CYS yellow</li>
475 * <li>others light gray</li>
478 public void setChargeColours()
482 if (b.at1 != null && b.at2 != null)
484 b.startCol = getChargeColour(b.at1.resName);
485 b.endCol = getChargeColour(b.at2.resName);
489 b.startCol = Color.gray;
490 b.endCol = Color.gray;
495 public static Color getChargeColour(String resName)
497 Color result = Color.lightGray;
498 if ("ASP".equals(resName) || "GLU".equals(resName))
502 else if ("LYS".equals(resName) || "ARG".equals(resName))
506 else if ("CYS".equals(resName))
508 result = Color.yellow;
514 * Sets the start/end colours of bonds to those of the start/end atoms
515 * according to the specified colour scheme. Note: currently only works for
520 public void setChainColours(ColourSchemeI cs)
527 index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
528 b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
530 index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
531 b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
533 } catch (Exception e)
535 b.startCol = Color.gray;
536 b.endCol = Color.gray;
541 public void setChainColours(Color col)
551 * copy any sequence annotation onto the sequence mapped using the provided
555 * - positional mapping between destination sequence and pdb resnum
557 * - mapping between destination sequence and local chain
559 public void transferResidueAnnotation(StructureMapping mapping,
560 jalview.datamodel.Mapping sqmpping)
562 SequenceI sq = mapping.getSequence();
566 while (dsq.getDatasetSequence() != null)
568 dsq = dsq.getDatasetSequence();
570 // any annotation will be transferred onto the dataset sequence
572 if (shadow != null && shadow.getAnnotation() != null)
575 for (AlignmentAnnotation ana : shadow.getAnnotation())
577 List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
578 ana.getCalcId(), ana.label);
579 if (transfer == null || transfer.size() == 0)
581 ana = new AlignmentAnnotation(ana);
582 ana.liftOver(sequence, shadowMap);
583 ana.liftOver(dsq, sqmpping);
584 dsq.addAlignmentAnnotation(ana);
594 if (sequence != null && sequence.getAnnotation() != null)
596 for (AlignmentAnnotation ana : sequence.getAnnotation())
598 List<AlignmentAnnotation> transfer = sq
599 .getAlignmentAnnotations(ana.getCalcId(), ana.label);
600 if (transfer == null || transfer.size() == 0)
602 ana = new AlignmentAnnotation(ana);
603 ana.liftOver(dsq, sqmpping);
604 // mapping.transfer(ana);
615 // Useful for debugging mappings - adds annotation for mapped position
616 float min = -1, max = 0;
617 Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
618 for (int i = sq.getStart(), j = sq.getEnd(), k = 0; i <= j; i++, k++)
620 int prn = mapping.getPDBResNum(k + 1);
622 an[k] = new Annotation(prn);
640 sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
641 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
642 an, min, max, AlignmentAnnotation.LINE_GRAPH));