2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
25 import java.util.List;
28 import jalview.analysis.AlignSeq;
29 import jalview.datamodel.*;
30 import jalview.io.FileParse;
31 import jalview.util.MessageManager;
33 public class PDBfile extends jalview.io.AlignFile
40 * set to true to add chain alignment annotation as visible annotation.
42 boolean VisibleChainAnnotation = false;
44 boolean processSecondaryStructure=true;
47 public PDBfile(boolean visibleChainAnnotation,
48 boolean processSecondaryStructure)
51 VisibleChainAnnotation = visibleChainAnnotation;
52 this.processSecondaryStructure = processSecondaryStructure;
55 public PDBfile(boolean visibleChainAnnotation,
56 boolean processSecondaryStructure, String file, String protocol) throws IOException
58 super(false, file, protocol);
59 VisibleChainAnnotation = visibleChainAnnotation;
60 this.processSecondaryStructure = processSecondaryStructure;
64 public PDBfile(boolean visibleChainAnnotation,
65 boolean processSecondaryStructure, FileParse source) throws IOException
68 VisibleChainAnnotation = visibleChainAnnotation;
69 this.processSecondaryStructure = processSecondaryStructure;
78 public void parse() throws IOException
80 // TODO set the filename sensibly - try using data source name.
81 id = safeName(getDataName());
83 chains = new Vector();
84 ArrayList<SequenceI> rna = new ArrayList<SequenceI>(), prot = new ArrayList<SequenceI>();
87 boolean modelFlag = false;
88 boolean terFlag = false;
92 String atomnam = null;
95 while ((line = nextLine()) != null)
97 if (line.indexOf("HEADER") == 0)
99 if (line.length() > 62)
102 if (line.length() > 67)
104 tid = line.substring(62, 67).trim();
108 tid = line.substring(62).trim();
110 if (tid.length() > 0)
117 // Were we to do anything with SEQRES - we start it here
118 if (line.indexOf("SEQRES") == 0)
122 if (line.indexOf("MODEL") == 0)
127 if (line.indexOf("TER") == 0)
132 if (modelFlag && line.indexOf("ENDMDL") == 0)
136 if (line.indexOf("ATOM") == 0
137 || (line.indexOf("HETATM") == 0 && !terFlag))
141 // Jalview is only interested in CA bonds????
142 atomnam = line.substring(12, 15).trim();
143 if (!atomnam.equals("CA") && !atomnam.equals("P"))
148 Atom tmpatom = new Atom(line);
149 tmpchain = findChain(tmpatom.chain);
150 if (tmpchain != null)
152 if (tmpatom.resNumIns.trim().equals(lastID))
154 // phosphorylated protein - seen both CA and P..
157 tmpchain.atoms.addElement(tmpatom);
161 tmpchain = new PDBChain(id, tmpatom.chain);
162 chains.addElement(tmpchain);
163 tmpchain.atoms.addElement(tmpatom);
165 lastID = tmpatom.resNumIns.trim();
175 id = inFile.getName();
177 for (int i = 0; i < chains.size(); i++)
179 SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
180 dataset.setName(id + "|" + dataset.getName());
181 PDBEntry entry = new PDBEntry();
183 entry.setProperty(new Hashtable());
184 if (((PDBChain) chains.elementAt(i)).id != null)
186 entry.getProperty().put("CHAIN",
187 ((PDBChain) chains.elementAt(i)).id);
191 entry.setFile(inFile.getAbsolutePath());
195 // TODO: decide if we should dump the datasource to disk
196 entry.setFile(getDataName());
198 dataset.addPDBId(entry);
199 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
200 // maintain reference to
202 seqs.addElement(chainseq);
203 if (isRNA(chainseq) == true)
212 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
214 if (chainannot != null)
216 for (int ai = 0; ai < chainannot.length; ai++)
219 chainannot[ai].visible = VisibleChainAnnotation;
220 annotations.addElement(chainannot[ai]);
224 if (processSecondaryStructure)
229 processPdbFileWithAnnotate3d(rna);
230 } catch (Exception x)
233 .println("Exceptions when dealing with RNA in pdb file");
241 processPdbFileWithJmol(prot);
242 } catch (Exception x)
245 .println("Exceptions from Jmol when processing data in pdb file");
250 } catch (OutOfMemoryError er)
252 System.out.println("OUT OF MEMORY LOADING PDB FILE");
253 throw new IOException(
255 .getString("exception.outofmemory_loading_pdb_file"));
256 } catch (NumberFormatException ex)
260 System.err.println("Couldn't read number from line:");
261 System.err.println(line);
266 private void processPdbFileWithJmol(ArrayList<SequenceI> prot)
271 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
274 Object jmf = cl.getConstructor(new Class[]
275 { FileParse.class }).newInstance(new Object[]
276 { new FileParse(getDataName(), type) });
277 Alignment al = new Alignment((SequenceI[]) cl.getMethod(
278 "getSeqsAsArray", new Class[]
280 cl.getMethod("addAnnotations", new Class[]
281 { Alignment.class }).invoke(jmf, al);
282 AlignSeq.replaceMatchingSeqsWith(seqs, annotations, prot, al, AlignSeq.PEP, false);
284 } catch (ClassNotFoundException q)
289 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna)
292 // System.out.println("this is a PDB format and RNA sequence");
293 // note: we use reflection here so that the applet can compile and run
294 // without the HTTPClient bits and pieces needed for accessing Annotate3D
298 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
301 // TODO: use the PDB ID of the structure if one is available, to save
302 // bandwidth and avoid uploading the whole structure to the service
303 Object annotate3d = cl.getConstructor(new Class[]
304 {}).newInstance(new Object[]
306 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
308 { FileParse.class }).invoke(annotate3d, new Object[]
309 { new FileParse(getDataName(), type) }));
310 AlignSeq.replaceMatchingSeqsWith(seqs, annotations, rna, al, AlignSeq.DNA, false);
312 } catch (ClassNotFoundException x)
314 // ignore classnotfounds - occurs in applet
320 * make a friendly ID string.
323 * @return truncated dataName to after last '/'
325 private String safeName(String dataName)
328 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
330 dataName = dataName.substring(p + 1);
335 public void makeResidueList()
337 for (int i = 0; i < chains.size(); i++)
339 ((PDBChain) chains.elementAt(i)).makeResidueList();
343 public void makeCaBondList()
345 for (int i = 0; i < chains.size(); i++)
347 ((PDBChain) chains.elementAt(i)).makeCaBondList();
351 public PDBChain findChain(String id)
353 for (int i = 0; i < chains.size(); i++)
355 if (((PDBChain) chains.elementAt(i)).id.equals(id))
357 return (PDBChain) chains.elementAt(i);
364 public void setChargeColours()
366 for (int i = 0; i < chains.size(); i++)
368 ((PDBChain) chains.elementAt(i)).setChargeColours();
372 public void setColours(jalview.schemes.ColourSchemeI cs)
374 for (int i = 0; i < chains.size(); i++)
376 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
380 public void setChainColours()
382 for (int i = 0; i < chains.size(); i++)
384 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
385 1.0f / (float) i, .4f, 1.0f));
389 public boolean isRNA(SequenceI seqs)
391 for (int i = 0; i < seqs.getLength(); i++)
393 if ((seqs.getCharAt(i) != 'A') && (seqs.getCharAt(i) != 'C')
394 && (seqs.getCharAt(i) != 'G') && (seqs.getCharAt(i) != 'U'))