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.FileParse;
30 import jalview.util.MessageManager;
32 import java.awt.Color;
33 import java.io.IOException;
34 import java.lang.reflect.Constructor;
35 import java.util.ArrayList;
36 import java.util.Hashtable;
37 import java.util.List;
38 import java.util.Vector;
40 public class PDBfile extends jalview.io.AlignFile
42 private static String CALC_ID_PREFIX = "JalviewPDB";
44 public Vector<PDBChain> chains;
49 * set to true to add derived sequence annotations (temp factor read from
50 * file, or computed secondary structure) to the alignment
52 private boolean visibleChainAnnotation = false;
55 * Set true to predict secondary structure (using JMol for protein, Annotate3D
58 private boolean predictSecondaryStructure = true;
61 * Set true (with predictSecondaryStructure=true) to predict secondary
62 * structure using an external service (currently Annotate3D for RNA only)
64 private boolean externalSecondaryStructure = false;
66 public PDBfile(boolean addAlignmentAnnotations, boolean predictSecondaryStructure,
67 boolean externalSecStr)
70 this.visibleChainAnnotation = addAlignmentAnnotations;
71 this.predictSecondaryStructure = predictSecondaryStructure;
72 this.externalSecondaryStructure = externalSecStr;
75 public PDBfile(boolean addAlignmentAnnotations, boolean predictSecondaryStructure,
76 boolean externalSecStr, String file, String protocol)
79 super(false, file, protocol);
80 this.visibleChainAnnotation = addAlignmentAnnotations;
81 this.predictSecondaryStructure = predictSecondaryStructure;
82 this.externalSecondaryStructure = externalSecStr;
86 public PDBfile(boolean addAlignmentAnnotations, boolean predictSecondaryStructure,
87 boolean externalSecStr, FileParse source) throws IOException
90 this.visibleChainAnnotation = addAlignmentAnnotations;
91 this.predictSecondaryStructure = predictSecondaryStructure;
92 this.externalSecondaryStructure = externalSecStr;
101 public void parse() throws IOException
103 // TODO set the filename sensibly - try using data source name.
104 id = safeName(getDataName());
106 chains = new Vector<PDBChain>();
107 List<SequenceI> rna = new ArrayList<SequenceI>();
108 List<SequenceI> prot = new ArrayList<SequenceI>();
111 boolean modelFlag = false;
112 boolean terFlag = false;
116 String atomnam = null;
119 while ((line = nextLine()) != null)
121 if (line.indexOf("HEADER") == 0)
123 if (line.length() > 62)
126 if (line.length() > 67)
128 tid = line.substring(62, 67).trim();
132 tid = line.substring(62).trim();
134 if (tid.length() > 0)
141 // Were we to do anything with SEQRES - we start it here
142 if (line.indexOf("SEQRES") == 0)
146 if (line.indexOf("MODEL") == 0)
151 if (line.indexOf("TER") == 0)
156 if (modelFlag && line.indexOf("ENDMDL") == 0)
160 if (line.indexOf("ATOM") == 0
161 || (line.indexOf("HETATM") == 0 && !terFlag))
165 // Jalview is only interested in CA bonds????
166 atomnam = line.substring(12, 15).trim();
167 if (!atomnam.equals("CA") && !atomnam.equals("P"))
172 Atom tmpatom = new Atom(line);
173 tmpchain = findChain(tmpatom.chain);
174 if (tmpchain != null)
176 if (tmpatom.resNumIns.trim().equals(lastID))
178 // phosphorylated protein - seen both CA and P..
181 tmpchain.atoms.addElement(tmpatom);
185 tmpchain = new PDBChain(id, tmpatom.chain);
186 chains.addElement(tmpchain);
187 tmpchain.atoms.addElement(tmpatom);
189 lastID = tmpatom.resNumIns.trim();
199 id = inFile.getName();
201 for (PDBChain chain : chains)
203 SequenceI chainseq = postProcessChain(chain);
213 if (predictSecondaryStructure)
215 predictSecondaryStructure(rna, prot);
217 } catch (OutOfMemoryError er)
219 System.out.println("OUT OF MEMORY LOADING PDB FILE");
220 throw new IOException(
222 .getString("exception.outofmemory_loading_pdb_file"));
223 } catch (NumberFormatException ex)
227 System.err.println("Couldn't read number from line:");
228 System.err.println(line);
235 * Predict secondary structure for RNA and/or protein sequences and add as
238 * @param rnaSequences
239 * @param proteinSequences
241 protected void predictSecondaryStructure(List<SequenceI> rnaSequences,
242 List<SequenceI> proteinSequences)
245 * Currently using Annotate3D for RNA, but only if the 'use external
246 * prediction' flag is set
248 if (externalSecondaryStructure && rnaSequences.size() > 0)
252 processPdbFileWithAnnotate3d(rnaSequences);
253 } catch (Exception x)
256 .println("Exceptions when dealing with RNA in pdb file");
263 * Currently using JMol PDB parser for peptide
265 if (proteinSequences.size() > 0)
269 processPdbFileWithJmol(proteinSequences);
270 } catch (Exception x)
273 .println("Exceptions from Jmol when processing data in pdb file");
280 * Process a parsed chain to construct and return a Sequence, and add it to
281 * the list of sequences parsed.
286 protected SequenceI postProcessChain(PDBChain chain)
288 SequenceI dataset = chain.sequence;
289 dataset.setName(id + "|" + dataset.getName());
290 PDBEntry entry = new PDBEntry();
292 entry.setType(PDBEntry.Type.PDB);
293 entry.setProperty(new Hashtable());
294 if (chain.id != null)
296 // entry.getProperty().put("CHAIN", chains.elementAt(i).id);
297 entry.setChainCode(String.valueOf(chain.id));
301 entry.setFile(inFile.getAbsolutePath());
305 // TODO: decide if we should dump the datasource to disk
306 entry.setFile(getDataName());
308 dataset.addPDBId(entry);
309 // PDBChain objects maintain reference to dataset
310 SequenceI chainseq = dataset.deriveSequence();
311 seqs.addElement(chainseq);
313 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
315 if (chainannot != null && visibleChainAnnotation)
317 for (int ai = 0; ai < chainannot.length; ai++)
319 chainannot[ai].visible = visibleChainAnnotation;
320 annotations.addElement(chainannot[ai]);
326 public static boolean isCalcIdHandled(String calcId)
328 return calcId != null && (CALC_ID_PREFIX.equals(calcId));
331 public static boolean isCalcIdForFile(AlignmentAnnotation alan,
334 return alan.getCalcId() != null
335 && CALC_ID_PREFIX.equals(alan.getCalcId())
336 && pdbFile.equals(alan.getProperty("PDBID"));
339 public static String relocateCalcId(String calcId,
340 Hashtable<String, String> alreadyLoadedPDB) throws Exception
342 int s = CALC_ID_PREFIX.length(), end = calcId.indexOf(CALC_ID_PREFIX, s);
343 String between = calcId.substring(s, end - 1);
344 return CALC_ID_PREFIX + alreadyLoadedPDB.get(between) + ":"
345 + calcId.substring(end);
348 private void markCalcIds()
350 for (SequenceI sq : seqs)
352 if (sq.getAnnotation() != null)
354 for (AlignmentAnnotation aa : sq.getAnnotation())
356 String oldId = aa.getCalcId();
361 aa.setCalcId(CALC_ID_PREFIX);
362 aa.setProperty("PDBID", id);
363 aa.setProperty("oldCalcId", oldId);
369 private void processPdbFileWithJmol(List<SequenceI> prot)
374 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
377 final Constructor constructor = cl.getConstructor(new Class[]
378 { FileParse.class });
379 final Object[] args = new Object[]
380 { new FileParse(getDataName(), type) };
381 Object jmf = constructor.newInstance(args);
382 AlignmentI al = new Alignment((SequenceI[]) cl.getMethod(
383 "getSeqsAsArray", new Class[]
385 cl.getMethod("addAnnotations", new Class[]
386 { AlignmentI.class }).invoke(jmf, al);
387 for (SequenceI sq : al.getSequences())
389 if (sq.getDatasetSequence() != null)
391 sq.getDatasetSequence().getPDBId().clear();
395 sq.getPDBId().clear();
398 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
400 } catch (ClassNotFoundException q)
405 private void replaceAndUpdateChains(List<SequenceI> prot,
406 AlignmentI al, String pep, boolean b)
408 List<List<? extends Object>> replaced = AlignSeq
409 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
411 for (PDBChain ch : chains)
414 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
417 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
426 // set shadow entry for chains
427 ch.shadow = (SequenceI) replaced.get(1).get(p);
428 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
429 .getMappingFromS1(false);
434 private void processPdbFileWithAnnotate3d(List<SequenceI> rna)
437 // System.out.println("this is a PDB format and RNA sequence");
438 // note: we use reflection here so that the applet can compile and run
439 // without the HTTPClient bits and pieces needed for accessing Annotate3D
443 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
446 // TODO: use the PDB ID of the structure if one is available, to save
447 // bandwidth and avoid uploading the whole structure to the service
448 Object annotate3d = cl.getConstructor(new Class[]
449 {}).newInstance(new Object[]
451 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
453 { FileParse.class }).invoke(annotate3d, new Object[]
454 { new FileParse(getDataName(), type) }));
455 for (SequenceI sq : al.getSequences())
457 if (sq.getDatasetSequence() != null)
459 if (sq.getDatasetSequence().getPDBId() != null)
461 sq.getDatasetSequence().getPDBId().clear();
466 if (sq.getPDBId() != null)
468 sq.getPDBId().clear();
472 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
474 } catch (ClassNotFoundException x)
476 // ignore classnotfounds - occurs in applet
482 * make a friendly ID string.
485 * @return truncated dataName to after last '/'
487 private String safeName(String dataName)
490 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
492 dataName = dataName.substring(p + 1);
497 public void makeResidueList()
499 for (int i = 0; i < chains.size(); i++)
501 chains.elementAt(i).makeResidueList(visibleChainAnnotation);
505 public void makeCaBondList()
507 for (int i = 0; i < chains.size(); i++)
509 chains.elementAt(i).makeCaBondList();
513 public PDBChain findChain(String id)
515 for (int i = 0; i < chains.size(); i++)
517 if (chains.elementAt(i).id.equals(id))
519 return chains.elementAt(i);
526 public void setChargeColours()
528 for (int i = 0; i < chains.size(); i++)
530 chains.elementAt(i).setChargeColours();
534 public void setColours(jalview.schemes.ColourSchemeI cs)
536 for (int i = 0; i < chains.size(); i++)
538 chains.elementAt(i).setChainColours(cs);
542 public void setChainColours()
544 for (int i = 0; i < chains.size(); i++)
546 // divide by zero --> infinity --> 255 ;-)
547 chains.elementAt(i).setChainColours(
548 Color.getHSBColor(1.0f / i, .4f, 1.0f));
552 public static boolean isRNA(SequenceI seq)
554 for (char c : seq.getSequence())
556 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))