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.
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.util.ArrayList;
35 import java.util.Hashtable;
36 import java.util.Vector;
38 public class PDBfile extends jalview.io.AlignFile
45 * set to true to add chain alignment annotation as visible annotation.
47 boolean VisibleChainAnnotation = false;
49 boolean processSecondaryStructure=true;
52 public PDBfile(boolean visibleChainAnnotation,
53 boolean processSecondaryStructure)
56 VisibleChainAnnotation = visibleChainAnnotation;
57 this.processSecondaryStructure = processSecondaryStructure;
60 public PDBfile(boolean visibleChainAnnotation,
61 boolean processSecondaryStructure, String file, String protocol) throws IOException
63 super(false, file, protocol);
64 VisibleChainAnnotation = visibleChainAnnotation;
65 this.processSecondaryStructure = processSecondaryStructure;
69 public PDBfile(boolean visibleChainAnnotation,
70 boolean processSecondaryStructure, FileParse source) throws IOException
73 VisibleChainAnnotation = visibleChainAnnotation;
74 this.processSecondaryStructure = processSecondaryStructure;
83 public void parse() throws IOException
85 // TODO set the filename sensibly - try using data source name.
86 id = safeName(getDataName());
88 chains = new Vector();
89 ArrayList<SequenceI> rna = new ArrayList<SequenceI>(), prot = new ArrayList<SequenceI>();
92 boolean modelFlag = false;
93 boolean terFlag = false;
97 String atomnam = null;
100 while ((line = nextLine()) != null)
102 if (line.indexOf("HEADER") == 0)
104 if (line.length() > 62)
107 if (line.length() > 67)
109 tid = line.substring(62, 67).trim();
113 tid = line.substring(62).trim();
115 if (tid.length() > 0)
122 // Were we to do anything with SEQRES - we start it here
123 if (line.indexOf("SEQRES") == 0)
127 if (line.indexOf("MODEL") == 0)
132 if (line.indexOf("TER") == 0)
137 if (modelFlag && line.indexOf("ENDMDL") == 0)
141 if (line.indexOf("ATOM") == 0
142 || (line.indexOf("HETATM") == 0 && !terFlag))
146 // Jalview is only interested in CA bonds????
147 atomnam = line.substring(12, 15).trim();
148 if (!atomnam.equals("CA") && !atomnam.equals("P"))
153 Atom tmpatom = new Atom(line);
154 tmpchain = findChain(tmpatom.chain);
155 if (tmpchain != null)
157 if (tmpatom.resNumIns.trim().equals(lastID))
159 // phosphorylated protein - seen both CA and P..
162 tmpchain.atoms.addElement(tmpatom);
166 tmpchain = new PDBChain(id, tmpatom.chain);
167 chains.addElement(tmpchain);
168 tmpchain.atoms.addElement(tmpatom);
170 lastID = tmpatom.resNumIns.trim();
180 id = inFile.getName();
182 for (int i = 0; i < chains.size(); i++)
184 SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
185 dataset.setName(id + "|" + dataset.getName());
186 PDBEntry entry = new PDBEntry();
188 entry.setProperty(new Hashtable());
189 if (((PDBChain) chains.elementAt(i)).id != null)
191 entry.getProperty().put("CHAIN",
192 ((PDBChain) chains.elementAt(i)).id);
196 entry.setFile(inFile.getAbsolutePath());
200 // TODO: decide if we should dump the datasource to disk
201 entry.setFile(getDataName());
203 dataset.addPDBId(entry);
204 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
205 // maintain reference to
207 seqs.addElement(chainseq);
208 if (isRNA(chainseq) == true)
217 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
219 if (chainannot != null)
221 for (int ai = 0; ai < chainannot.length; ai++)
223 chainannot[ai].visible = VisibleChainAnnotation;
224 annotations.addElement(chainannot[ai]);
228 if (processSecondaryStructure)
234 processPdbFileWithAnnotate3d(rna);
235 } catch (Exception x)
238 .println("Exceptions when dealing with RNA in pdb file");
248 processPdbFileWithJmol(prot);
249 } catch (Exception x)
252 .println("Exceptions from Jmol when processing data in pdb file");
258 } catch (OutOfMemoryError er)
260 System.out.println("OUT OF MEMORY LOADING PDB FILE");
261 throw new IOException(
263 .getString("exception.outofmemory_loading_pdb_file"));
264 } catch (NumberFormatException ex)
268 System.err.println("Couldn't read number from line:");
269 System.err.println(line);
275 private static String calcIdPrefix = "JalviewPDB:";
277 public static boolean isCalcIdHandled(String calcId)
279 return calcId != null
280 && (calcId.startsWith(calcIdPrefix) && calcId.indexOf(
282 calcIdPrefix.length() + 1) > -1);
284 public static boolean isCalcIdForFile(String calcId, String pdbFile)
286 return (calcId != null && calcId.startsWith(calcIdPrefix + pdbFile
287 + ":" + calcIdPrefix));
290 public static String relocateCalcId(String calcId,
291 Hashtable<String, String> alreadyLoadedPDB) throws Exception
293 int s = calcIdPrefix.length(), end = calcId.indexOf(calcIdPrefix, s);
294 String between = calcId.substring(s, end - 1);
295 return calcIdPrefix + alreadyLoadedPDB.get(between) + ":"
296 + calcId.substring(end);
299 private void markCalcIds()
301 for (SequenceI sq : seqs)
303 for (AlignmentAnnotation aa : sq.getAnnotation())
305 String oldId = aa.getCalcId();
310 aa.setCalcId("JalviewPDB:" + id + ":JalviewPDB:" + oldId);
314 private void processPdbFileWithJmol(ArrayList<SequenceI> prot)
319 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
322 Object jmf = cl.getConstructor(new Class[]
323 { FileParse.class }).newInstance(new Object[]
324 { new FileParse(getDataName(), type) });
325 Alignment al = new Alignment((SequenceI[]) cl.getMethod(
326 "getSeqsAsArray", new Class[]
328 cl.getMethod("addAnnotations", new Class[]
329 { Alignment.class }).invoke(jmf, al);
330 for (SequenceI sq : al.getSequences())
332 if (sq.getDatasetSequence() != null)
334 if (sq.getDatasetSequence().getPDBId() != null)
336 sq.getDatasetSequence().getPDBId().clear();
341 if (sq.getPDBId() != null)
343 sq.getPDBId().clear();
347 AlignSeq.replaceMatchingSeqsWith(seqs, annotations, prot, al, AlignSeq.PEP, false);
349 } catch (ClassNotFoundException q)
354 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna)
357 // System.out.println("this is a PDB format and RNA sequence");
358 // note: we use reflection here so that the applet can compile and run
359 // without the HTTPClient bits and pieces needed for accessing Annotate3D
363 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
366 // TODO: use the PDB ID of the structure if one is available, to save
367 // bandwidth and avoid uploading the whole structure to the service
368 Object annotate3d = cl.getConstructor(new Class[]
369 {}).newInstance(new Object[]
371 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
373 { FileParse.class }).invoke(annotate3d, new Object[]
374 { new FileParse(getDataName(), type) }));
375 for (SequenceI sq : al.getSequences())
377 if (sq.getDatasetSequence() != null)
379 if (sq.getDatasetSequence().getPDBId() != null)
381 sq.getDatasetSequence().getPDBId().clear();
386 if (sq.getPDBId() != null)
388 sq.getDatasetSequence().getPDBId().clear();
392 AlignSeq.replaceMatchingSeqsWith(seqs, annotations, rna, al, AlignSeq.DNA, false);
394 } catch (ClassNotFoundException x)
396 // ignore classnotfounds - occurs in applet
402 * make a friendly ID string.
405 * @return truncated dataName to after last '/'
407 private String safeName(String dataName)
410 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
412 dataName = dataName.substring(p + 1);
417 public void makeResidueList()
419 for (int i = 0; i < chains.size(); i++)
421 ((PDBChain) chains.elementAt(i)).makeResidueList();
425 public void makeCaBondList()
427 for (int i = 0; i < chains.size(); i++)
429 ((PDBChain) chains.elementAt(i)).makeCaBondList();
433 public PDBChain findChain(String id)
435 for (int i = 0; i < chains.size(); i++)
437 if (((PDBChain) chains.elementAt(i)).id.equals(id))
439 return (PDBChain) chains.elementAt(i);
446 public void setChargeColours()
448 for (int i = 0; i < chains.size(); i++)
450 ((PDBChain) chains.elementAt(i)).setChargeColours();
454 public void setColours(jalview.schemes.ColourSchemeI cs)
456 for (int i = 0; i < chains.size(); i++)
458 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
462 public void setChainColours()
464 for (int i = 0; i < chains.size(); i++)
466 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
467 1.0f / i, .4f, 1.0f));
471 public boolean isRNA(SequenceI seqs)
473 for (int i = 0; i < seqs.getLength(); i++)
475 if ((seqs.getCharAt(i) != 'A') && (seqs.getCharAt(i) != 'C')
476 && (seqs.getCharAt(i) != 'G') && (seqs.getCharAt(i) != 'U'))