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.List;
37 import java.util.Vector;
39 public class PDBfile extends jalview.io.AlignFile
41 public Vector<PDBChain> chains;
46 * set to true to add chain alignment annotation as visible annotation.
48 boolean VisibleChainAnnotation = false;
50 boolean processSecondaryStructure=true;
53 public PDBfile(boolean visibleChainAnnotation,
54 boolean processSecondaryStructure)
57 VisibleChainAnnotation = visibleChainAnnotation;
58 this.processSecondaryStructure = processSecondaryStructure;
61 public PDBfile(boolean visibleChainAnnotation,
62 boolean processSecondaryStructure, String file, String protocol) throws IOException
64 super(false, file, protocol);
65 VisibleChainAnnotation = visibleChainAnnotation;
66 this.processSecondaryStructure = processSecondaryStructure;
70 public PDBfile(boolean visibleChainAnnotation,
71 boolean processSecondaryStructure, FileParse source) throws IOException
74 VisibleChainAnnotation = visibleChainAnnotation;
75 this.processSecondaryStructure = processSecondaryStructure;
84 public void parse() throws IOException
86 // TODO set the filename sensibly - try using data source name.
87 id = safeName(getDataName());
89 chains = new Vector();
90 ArrayList<SequenceI> rna = new ArrayList<SequenceI>(), prot = new ArrayList<SequenceI>();
93 boolean modelFlag = false;
94 boolean terFlag = false;
98 String atomnam = null;
101 while ((line = nextLine()) != null)
103 if (line.indexOf("HEADER") == 0)
105 if (line.length() > 62)
108 if (line.length() > 67)
110 tid = line.substring(62, 67).trim();
114 tid = line.substring(62).trim();
116 if (tid.length() > 0)
123 // Were we to do anything with SEQRES - we start it here
124 if (line.indexOf("SEQRES") == 0)
128 if (line.indexOf("MODEL") == 0)
133 if (line.indexOf("TER") == 0)
138 if (modelFlag && line.indexOf("ENDMDL") == 0)
142 if (line.indexOf("ATOM") == 0
143 || (line.indexOf("HETATM") == 0 && !terFlag))
147 // Jalview is only interested in CA bonds????
148 atomnam = line.substring(12, 15).trim();
149 if (!atomnam.equals("CA") && !atomnam.equals("P"))
154 Atom tmpatom = new Atom(line);
155 tmpchain = findChain(tmpatom.chain);
156 if (tmpchain != null)
158 if (tmpatom.resNumIns.trim().equals(lastID))
160 // phosphorylated protein - seen both CA and P..
163 tmpchain.atoms.addElement(tmpatom);
167 tmpchain = new PDBChain(id, tmpatom.chain);
168 chains.addElement(tmpchain);
169 tmpchain.atoms.addElement(tmpatom);
171 lastID = tmpatom.resNumIns.trim();
181 id = inFile.getName();
183 for (int i = 0; i < chains.size(); i++)
185 SequenceI dataset = chains.elementAt(i).sequence;
186 dataset.setName(id + "|" + dataset.getName());
187 PDBEntry entry = new PDBEntry();
189 entry.setProperty(new Hashtable());
190 if (chains.elementAt(i).id != null)
192 entry.getProperty().put("CHAIN",
193 chains.elementAt(i).id);
197 entry.setFile(inFile.getAbsolutePath());
201 // TODO: decide if we should dump the datasource to disk
202 entry.setFile(getDataName());
204 dataset.addPDBId(entry);
205 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
206 // maintain reference to
208 seqs.addElement(chainseq);
209 if (isRNA(chainseq) == true)
218 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
220 if (chainannot != null)
222 for (int ai = 0; ai < chainannot.length; ai++)
224 chainannot[ai].visible = VisibleChainAnnotation;
225 annotations.addElement(chainannot[ai]);
229 if (processSecondaryStructure)
235 processPdbFileWithAnnotate3d(rna);
236 } catch (Exception x)
239 .println("Exceptions when dealing with RNA in pdb file");
249 processPdbFileWithJmol(prot);
250 } catch (Exception x)
253 .println("Exceptions from Jmol when processing data in pdb file");
259 } catch (OutOfMemoryError er)
261 System.out.println("OUT OF MEMORY LOADING PDB FILE");
262 throw new IOException(
264 .getString("exception.outofmemory_loading_pdb_file"));
265 } catch (NumberFormatException ex)
269 System.err.println("Couldn't read number from line:");
270 System.err.println(line);
276 private static String calcIdPrefix = "JalviewPDB";
278 public static boolean isCalcIdHandled(String calcId)
280 return calcId != null
281 && (calcIdPrefix.equals(calcId));
284 public static boolean isCalcIdForFile(AlignmentAnnotation alan, String pdbFile)
286 return alan.getCalcId() != null
287 && calcIdPrefix.equals(alan.getCalcId())
288 && pdbFile.equals(alan.getProperty("PDBID"));
291 public static String relocateCalcId(String calcId,
292 Hashtable<String, String> alreadyLoadedPDB) throws Exception
294 int s = calcIdPrefix.length(), end = calcId.indexOf(calcIdPrefix, s);
295 String between = calcId.substring(s, end - 1);
296 return calcIdPrefix + alreadyLoadedPDB.get(between) + ":"
297 + calcId.substring(end);
300 private void markCalcIds()
302 for (SequenceI sq : seqs)
304 for (AlignmentAnnotation aa : sq.getAnnotation())
306 String oldId = aa.getCalcId();
311 aa.setCalcId(calcIdPrefix);
312 aa.setProperty("PDBID", id);
313 aa.setProperty("oldCalcId", oldId);
317 private void processPdbFileWithJmol(ArrayList<SequenceI> prot)
322 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
325 Object jmf = cl.getConstructor(new Class[]
326 { FileParse.class }).newInstance(new Object[]
327 { new FileParse(getDataName(), type) });
328 Alignment al = new Alignment((SequenceI[]) cl.getMethod(
329 "getSeqsAsArray", new Class[]
331 cl.getMethod("addAnnotations", new Class[]
332 { Alignment.class }).invoke(jmf, al);
333 for (SequenceI sq : al.getSequences())
335 if (sq.getDatasetSequence() != null)
337 sq.getDatasetSequence().getPDBId().clear();
341 sq.getPDBId().clear();
344 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
346 } catch (ClassNotFoundException q)
351 private void replaceAndUpdateChains(ArrayList<SequenceI> prot,
352 AlignmentI al, String pep, boolean b)
354 List<List<? extends Object>> replaced = AlignSeq
355 .replaceMatchingSeqsWith(seqs,
356 annotations, prot, al, AlignSeq.PEP, false);
357 for (PDBChain ch : chains)
360 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
363 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
372 // set shadow entry for chains
373 ch.shadow = (SequenceI) replaced.get(1).get(p);
374 ch.shadowMap = ((AlignSeq) replaced.get(2)
376 .getMappingFromS1(false);
381 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna)
384 // System.out.println("this is a PDB format and RNA sequence");
385 // note: we use reflection here so that the applet can compile and run
386 // without the HTTPClient bits and pieces needed for accessing Annotate3D
390 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
393 // TODO: use the PDB ID of the structure if one is available, to save
394 // bandwidth and avoid uploading the whole structure to the service
395 Object annotate3d = cl.getConstructor(new Class[]
396 {}).newInstance(new Object[]
398 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
400 { FileParse.class }).invoke(annotate3d, new Object[]
401 { new FileParse(getDataName(), type) }));
402 for (SequenceI sq : al.getSequences())
404 if (sq.getDatasetSequence() != null)
406 if (sq.getDatasetSequence().getPDBId() != null)
408 sq.getDatasetSequence().getPDBId().clear();
413 if (sq.getPDBId() != null)
415 sq.getPDBId().clear();
419 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
421 } catch (ClassNotFoundException x)
423 // ignore classnotfounds - occurs in applet
429 * make a friendly ID string.
432 * @return truncated dataName to after last '/'
434 private String safeName(String dataName)
437 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
439 dataName = dataName.substring(p + 1);
444 public void makeResidueList()
446 for (int i = 0; i < chains.size(); i++)
448 chains.elementAt(i).makeResidueList();
452 public void makeCaBondList()
454 for (int i = 0; i < chains.size(); i++)
456 chains.elementAt(i).makeCaBondList();
460 public PDBChain findChain(String id)
462 for (int i = 0; i < chains.size(); i++)
464 if (chains.elementAt(i).id.equals(id))
466 return chains.elementAt(i);
473 public void setChargeColours()
475 for (int i = 0; i < chains.size(); i++)
477 chains.elementAt(i).setChargeColours();
481 public void setColours(jalview.schemes.ColourSchemeI cs)
483 for (int i = 0; i < chains.size(); i++)
485 chains.elementAt(i).setChainColours(cs);
489 public void setChainColours()
491 for (int i = 0; i < chains.size(); i++)
493 chains.elementAt(i).setChainColours(Color.getHSBColor(
494 1.0f / i, .4f, 1.0f));
498 public boolean isRNA(SequenceI seqs)
500 for (int i = 0; i < seqs.getLength(); i++)
502 if ((seqs.getCharAt(i) != 'A') && (seqs.getCharAt(i) != 'C')
503 && (seqs.getCharAt(i) != 'G') && (seqs.getCharAt(i) != 'U'))