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,
67 boolean predictSecondaryStructure, boolean externalSecStr)
70 this.visibleChainAnnotation = addAlignmentAnnotations;
71 this.predictSecondaryStructure = predictSecondaryStructure;
72 this.externalSecondaryStructure = externalSecStr;
75 public PDBfile(boolean addAlignmentAnnotations,
76 boolean predictSecondaryStructure, boolean externalSecStr,
77 String file, String protocol) throws IOException
79 super(false, file, protocol);
80 this.visibleChainAnnotation = addAlignmentAnnotations;
81 this.predictSecondaryStructure = predictSecondaryStructure;
82 this.externalSecondaryStructure = externalSecStr;
86 public PDBfile(boolean addAlignmentAnnotations,
87 boolean predictSecondaryStructure, boolean externalSecStr,
88 FileParse source) throws IOException
91 this.visibleChainAnnotation = addAlignmentAnnotations;
92 this.predictSecondaryStructure = predictSecondaryStructure;
93 this.externalSecondaryStructure = externalSecStr;
102 public void parse() throws IOException
104 // TODO set the filename sensibly - try using data source name.
105 id = safeName(getDataName());
107 chains = new Vector<PDBChain>();
108 List<SequenceI> rna = new ArrayList<SequenceI>();
109 List<SequenceI> prot = new ArrayList<SequenceI>();
112 boolean modelFlag = false;
113 boolean terFlag = false;
117 String atomnam = null;
120 while ((line = nextLine()) != null)
122 if (line.indexOf("HEADER") == 0)
124 if (line.length() > 62)
127 if (line.length() > 67)
129 tid = line.substring(62, 67).trim();
133 tid = line.substring(62).trim();
135 if (tid.length() > 0)
142 // Were we to do anything with SEQRES - we start it here
143 if (line.indexOf("SEQRES") == 0)
147 if (line.indexOf("MODEL") == 0)
152 if (line.indexOf("TER") == 0)
157 if (modelFlag && line.indexOf("ENDMDL") == 0)
161 if (line.indexOf("ATOM") == 0
162 || (line.indexOf("HETATM") == 0 && !terFlag))
166 // Jalview is only interested in CA bonds????
167 atomnam = line.substring(12, 15).trim();
168 if (!atomnam.equals("CA") && !atomnam.equals("P"))
173 Atom tmpatom = new Atom(line);
174 tmpchain = findChain(tmpatom.chain);
175 if (tmpchain != null)
177 if (tmpatom.resNumIns.trim().equals(lastID))
179 // phosphorylated protein - seen both CA and P..
182 tmpchain.atoms.addElement(tmpatom);
186 tmpchain = new PDBChain(id, tmpatom.chain);
187 chains.addElement(tmpchain);
188 tmpchain.atoms.addElement(tmpatom);
190 lastID = tmpatom.resNumIns.trim();
200 id = inFile.getName();
202 for (PDBChain chain : chains)
204 SequenceI chainseq = postProcessChain(chain);
214 if (predictSecondaryStructure)
216 predictSecondaryStructure(rna, prot);
218 } catch (OutOfMemoryError er)
220 System.out.println("OUT OF MEMORY LOADING PDB FILE");
221 throw new IOException(
223 .getString("exception.outofmemory_loading_pdb_file"));
224 } catch (NumberFormatException ex)
228 System.err.println("Couldn't read number from line:");
229 System.err.println(line);
236 * Predict secondary structure for RNA and/or protein sequences and add as
239 * @param rnaSequences
240 * @param proteinSequences
242 protected void predictSecondaryStructure(List<SequenceI> rnaSequences,
243 List<SequenceI> proteinSequences)
246 * Currently using Annotate3D for RNA, but only if the 'use external
247 * prediction' flag is set
249 if (externalSecondaryStructure && rnaSequences.size() > 0)
253 processPdbFileWithAnnotate3d(rnaSequences);
254 } catch (Exception x)
256 System.err.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
343 .indexOf(CALC_ID_PREFIX, s);
344 String between = calcId.substring(s, end - 1);
345 return CALC_ID_PREFIX + alreadyLoadedPDB.get(between) + ":"
346 + calcId.substring(end);
349 private void markCalcIds()
351 for (SequenceI sq : seqs)
353 if (sq.getAnnotation() != null)
355 for (AlignmentAnnotation aa : sq.getAnnotation())
357 String oldId = aa.getCalcId();
362 aa.setCalcId(CALC_ID_PREFIX);
363 aa.setProperty("PDBID", id);
364 aa.setProperty("oldCalcId", oldId);
370 private void processPdbFileWithJmol(List<SequenceI> prot)
375 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
378 final Constructor constructor = cl
379 .getConstructor(new Class[] { FileParse.class });
380 final Object[] args = new Object[] { new FileParse(getDataName(),
382 Object jmf = constructor.newInstance(args);
383 AlignmentI al = new Alignment((SequenceI[]) cl.getMethod(
384 "getSeqsAsArray", new Class[] {}).invoke(jmf));
385 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
387 for (SequenceI sq : al.getSequences())
389 if (sq.getDatasetSequence() != null)
391 sq.getDatasetSequence().getAllPDBEntries().clear();
395 sq.getAllPDBEntries().clear();
398 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
400 } catch (ClassNotFoundException q)
405 private void replaceAndUpdateChains(List<SequenceI> prot, AlignmentI al,
406 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[] {}).newInstance(
450 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
451 new Class[] { FileParse.class }).invoke(annotate3d,
452 new Object[] { new FileParse(getDataName(), type) }));
453 for (SequenceI sq : al.getSequences())
455 if (sq.getDatasetSequence() != null)
457 if (sq.getDatasetSequence().getAllPDBEntries() != null)
459 sq.getDatasetSequence().getAllPDBEntries().clear();
464 if (sq.getAllPDBEntries() != null)
466 sq.getAllPDBEntries().clear();
470 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
472 } catch (ClassNotFoundException x)
474 // ignore classnotfounds - occurs in applet
480 * make a friendly ID string.
483 * @return truncated dataName to after last '/'
485 private String safeName(String dataName)
488 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
490 dataName = dataName.substring(p + 1);
495 public void makeResidueList()
497 for (int i = 0; i < chains.size(); i++)
499 chains.elementAt(i).makeResidueList(visibleChainAnnotation);
503 public void makeCaBondList()
505 for (int i = 0; i < chains.size(); i++)
507 chains.elementAt(i).makeCaBondList();
511 public PDBChain findChain(String id)
513 for (int i = 0; i < chains.size(); i++)
515 if (chains.elementAt(i).id.equals(id))
517 return chains.elementAt(i);
524 public void setChargeColours()
526 for (int i = 0; i < chains.size(); i++)
528 chains.elementAt(i).setChargeColours();
532 public void setColours(jalview.schemes.ColourSchemeI cs)
534 for (int i = 0; i < chains.size(); i++)
536 chains.elementAt(i).setChainColours(cs);
540 public void setChainColours()
542 for (int i = 0; i < chains.size(); i++)
544 // divide by zero --> infinity --> 255 ;-)
545 chains.elementAt(i).setChainColours(
546 Color.getHSBColor(1.0f / i, .4f, 1.0f));
550 public static boolean isRNA(SequenceI seq)
552 for (char c : seq.getSequence())
554 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))