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.Alignment;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.PDBEntry;
28 import jalview.datamodel.SequenceI;
29 import jalview.io.AlignFile;
30 import jalview.io.FileParse;
31 import jalview.schemes.ColourSchemeI;
32 import jalview.util.MessageManager;
34 import java.awt.Color;
35 import java.io.IOException;
36 import java.lang.reflect.Constructor;
37 import java.util.ArrayList;
38 import java.util.Hashtable;
39 import java.util.List;
40 import java.util.Vector;
42 public class PDBfile extends AlignFile
44 private static String CALC_ID_PREFIX = "JalviewPDB";
46 public Vector<PDBChain> chains;
51 * set to true to add derived sequence annotations (temp factor read from
52 * file, or computed secondary structure) to the alignment
54 private boolean visibleChainAnnotation = false;
57 * Set true to predict secondary structure (using JMol for protein, Annotate3D
60 private boolean predictSecondaryStructure = true;
63 * Set true (with predictSecondaryStructure=true) to predict secondary
64 * structure using an external service (currently Annotate3D for RNA only)
66 private boolean externalSecondaryStructure = false;
68 public PDBfile(boolean addAlignmentAnnotations, boolean predictSecondaryStructure,
69 boolean externalSecStr)
72 this.visibleChainAnnotation = addAlignmentAnnotations;
73 this.predictSecondaryStructure = predictSecondaryStructure;
74 this.externalSecondaryStructure = externalSecStr;
77 public PDBfile(boolean addAlignmentAnnotations, boolean predictSecondaryStructure,
78 boolean externalSecStr, String file, String protocol)
81 super(false, file, protocol);
82 this.visibleChainAnnotation = addAlignmentAnnotations;
83 this.predictSecondaryStructure = predictSecondaryStructure;
84 this.externalSecondaryStructure = externalSecStr;
88 public PDBfile(boolean addAlignmentAnnotations, boolean predictSecondaryStructure,
89 boolean externalSecStr, FileParse source) throws IOException
92 this.visibleChainAnnotation = addAlignmentAnnotations;
93 this.predictSecondaryStructure = predictSecondaryStructure;
94 this.externalSecondaryStructure = externalSecStr;
103 public void parse() throws IOException
105 // TODO set the filename sensibly - try using data source name.
106 id = safeName(getDataName());
108 chains = new Vector<PDBChain>();
109 List<SequenceI> rna = new ArrayList<SequenceI>();
110 List<SequenceI> prot = new ArrayList<SequenceI>();
113 boolean modelFlag = false;
114 boolean terFlag = false;
118 String atomnam = null;
121 while ((line = nextLine()) != null)
123 if (line.indexOf("HEADER") == 0)
125 if (line.length() > 62)
128 if (line.length() > 67)
130 tid = line.substring(62, 67).trim();
134 tid = line.substring(62).trim();
136 if (tid.length() > 0)
143 // Were we to do anything with SEQRES - we start it here
144 if (line.indexOf("SEQRES") == 0)
148 if (line.indexOf("MODEL") == 0)
153 if (line.indexOf("TER") == 0)
158 if (modelFlag && line.indexOf("ENDMDL") == 0)
162 if (line.indexOf("ATOM") == 0
163 || (line.indexOf("HETATM") == 0 && !terFlag))
167 // Jalview is only interested in CA bonds????
168 atomnam = line.substring(12, 15).trim();
169 if (!atomnam.equals("CA") && !atomnam.equals("P"))
174 Atom tmpatom = new Atom(line);
175 tmpchain = findChain(tmpatom.chain);
176 if (tmpchain != null)
178 if (tmpatom.resNumIns.trim().equals(lastID))
180 // phosphorylated protein - seen both CA and P..
183 tmpchain.atoms.addElement(tmpatom);
187 tmpchain = new PDBChain(id, tmpatom.chain);
188 chains.addElement(tmpchain);
189 tmpchain.atoms.addElement(tmpatom);
191 lastID = tmpatom.resNumIns.trim();
201 id = inFile.getName();
203 for (PDBChain chain : chains)
205 SequenceI chainseq = postProcessChain(chain);
215 if (predictSecondaryStructure)
217 predictSecondaryStructure(rna, prot);
219 } catch (OutOfMemoryError er)
221 System.out.println("OUT OF MEMORY LOADING PDB FILE");
222 throw new IOException(
224 .getString("exception.outofmemory_loading_pdb_file"));
225 } catch (NumberFormatException ex)
229 System.err.println("Couldn't read number from line:");
230 System.err.println(line);
237 * Predict secondary structure for RNA and/or protein sequences and add as
240 * @param rnaSequences
241 * @param proteinSequences
243 protected void predictSecondaryStructure(List<SequenceI> rnaSequences,
244 List<SequenceI> proteinSequences)
247 * Currently using Annotate3D for RNA, but only if the 'use external
248 * prediction' flag is set
250 if (externalSecondaryStructure && rnaSequences.size() > 0)
254 processPdbFileWithAnnotate3d(rnaSequences);
255 } catch (Exception x)
258 .println("Exceptions when dealing with RNA in pdb file");
265 * Currently using JMol PDB parser for peptide
267 if (proteinSequences.size() > 0)
271 processPdbFileWithJmol(proteinSequences);
272 } catch (Exception x)
275 .println("Exceptions from Jmol when processing data in pdb file");
282 * Process a parsed chain to construct and return a Sequence, and add it to
283 * the list of sequences parsed.
288 protected SequenceI postProcessChain(PDBChain chain)
290 SequenceI dataset = chain.sequence;
291 dataset.setName(id + "|" + dataset.getName());
292 PDBEntry entry = new PDBEntry();
294 entry.setType(PDBEntry.Type.PDB);
295 entry.setProperty(new Hashtable());
296 if (chain.id != null)
298 // entry.getProperty().put("CHAIN", chains.elementAt(i).id);
299 entry.setChainCode(String.valueOf(chain.id));
303 entry.setFile(inFile.getAbsolutePath());
307 // TODO: decide if we should dump the datasource to disk
308 entry.setFile(getDataName());
310 dataset.addPDBId(entry);
311 // PDBChain objects maintain reference to dataset
312 SequenceI chainseq = dataset.deriveSequence();
313 seqs.addElement(chainseq);
315 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
317 if (chainannot != null && visibleChainAnnotation)
319 for (int ai = 0; ai < chainannot.length; ai++)
321 chainannot[ai].visible = visibleChainAnnotation;
322 annotations.addElement(chainannot[ai]);
328 public static boolean isCalcIdHandled(String calcId)
330 return calcId != null && (CALC_ID_PREFIX.equals(calcId));
333 public static boolean isCalcIdForFile(AlignmentAnnotation alan,
336 return alan.getCalcId() != null
337 && CALC_ID_PREFIX.equals(alan.getCalcId())
338 && pdbFile.equals(alan.getProperty("PDBID"));
341 public static String relocateCalcId(String calcId,
342 Hashtable<String, String> alreadyLoadedPDB) throws Exception
344 int s = CALC_ID_PREFIX.length(), end = calcId.indexOf(CALC_ID_PREFIX, s);
345 String between = calcId.substring(s, end - 1);
346 return CALC_ID_PREFIX + alreadyLoadedPDB.get(between) + ":"
347 + calcId.substring(end);
350 private void markCalcIds()
352 for (SequenceI sq : seqs)
354 if (sq.getAnnotation() != null)
356 for (AlignmentAnnotation aa : sq.getAnnotation())
358 String oldId = aa.getCalcId();
363 aa.setCalcId(CALC_ID_PREFIX);
364 aa.setProperty("PDBID", id);
365 aa.setProperty("oldCalcId", oldId);
371 private void processPdbFileWithJmol(List<SequenceI> prot)
376 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
379 final Constructor constructor = cl.getConstructor(new Class[]
380 { FileParse.class });
381 final Object[] args = new Object[]
382 { new FileParse(getDataName(), type) };
383 Object jmf = constructor.newInstance(args);
384 AlignmentI al = new Alignment((SequenceI[]) cl.getMethod(
385 "getSeqsAsArray", new Class[]
387 cl.getMethod("addAnnotations", new Class[]
388 { AlignmentI.class }).invoke(jmf, al);
389 for (SequenceI sq : al.getSequences())
391 if (sq.getDatasetSequence() != null)
393 sq.getDatasetSequence().getPDBId().clear();
397 sq.getPDBId().clear();
400 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
402 } catch (ClassNotFoundException q)
407 private void replaceAndUpdateChains(List<SequenceI> prot,
408 AlignmentI al, String pep, boolean b)
410 List<List<? extends Object>> replaced = AlignSeq
411 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
413 for (PDBChain ch : chains)
416 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
419 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
428 // set shadow entry for chains
429 ch.shadow = (SequenceI) replaced.get(1).get(p);
430 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
431 .getMappingFromS1(false);
436 private void processPdbFileWithAnnotate3d(List<SequenceI> rna)
439 // System.out.println("this is a PDB format and RNA sequence");
440 // note: we use reflection here so that the applet can compile and run
441 // without the HTTPClient bits and pieces needed for accessing Annotate3D
445 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
448 // TODO: use the PDB ID of the structure if one is available, to save
449 // bandwidth and avoid uploading the whole structure to the service
450 Object annotate3d = cl.getConstructor(new Class[]
451 {}).newInstance(new Object[]
453 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
455 { FileParse.class }).invoke(annotate3d, new Object[]
456 { new FileParse(getDataName(), type) }));
457 for (SequenceI sq : al.getSequences())
459 if (sq.getDatasetSequence() != null)
461 if (sq.getDatasetSequence().getPDBId() != null)
463 sq.getDatasetSequence().getPDBId().clear();
468 if (sq.getPDBId() != null)
470 sq.getPDBId().clear();
474 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
476 } catch (ClassNotFoundException x)
478 // ignore classnotfounds - occurs in applet
484 * make a friendly ID string.
487 * @return truncated dataName to after last '/'
489 private String safeName(String dataName)
492 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
494 dataName = dataName.substring(p + 1);
499 public void makeResidueList()
501 for (int i = 0; i < chains.size(); i++)
503 chains.elementAt(i).makeResidueList(visibleChainAnnotation);
507 public void makeCaBondList()
509 for (int i = 0; i < chains.size(); i++)
511 chains.elementAt(i).makeCaBondList();
515 public PDBChain findChain(String id)
517 for (int i = 0; i < chains.size(); i++)
519 if (chains.elementAt(i).id.equals(id))
521 return chains.elementAt(i);
528 public void setChargeColours()
530 for (int i = 0; i < chains.size(); i++)
532 chains.elementAt(i).setChargeColours();
536 public void setColours(ColourSchemeI cs)
538 for (int i = 0; i < chains.size(); i++)
540 chains.elementAt(i).setChainColours(cs);
544 public void setChainColours()
546 for (int i = 0; i < chains.size(); i++)
548 // divide by zero --> infinity --> 255 ;-)
549 chains.elementAt(i).setChainColours(
550 Color.getHSBColor(1.0f / i, .4f, 1.0f));
554 public static boolean isRNA(SequenceI seq)
556 for (char c : seq.getSequence())
558 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))