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;
52 boolean externalSecondaryStructure = false;
54 public PDBfile(boolean visibleChainAnnotation,
55 boolean processSecondaryStructure, boolean externalSecStr)
58 VisibleChainAnnotation = visibleChainAnnotation;
59 this.processSecondaryStructure = processSecondaryStructure;
60 this.externalSecondaryStructure = externalSecStr;
63 public PDBfile(boolean visibleChainAnnotation,
64 boolean processSecondaryStructure, boolean externalSecStr,
65 String file, String protocol) throws IOException
67 super(false, file, protocol);
68 VisibleChainAnnotation = visibleChainAnnotation;
69 this.processSecondaryStructure = processSecondaryStructure;
70 this.externalSecondaryStructure = externalSecStr;
74 public PDBfile(boolean visibleChainAnnotation,
75 boolean processSecondaryStructure, boolean externalSecStr,
76 FileParse source) throws IOException
79 VisibleChainAnnotation = visibleChainAnnotation;
80 this.processSecondaryStructure = processSecondaryStructure;
81 this.externalSecondaryStructure = externalSecStr;
90 public void parse() throws IOException
92 // TODO set the filename sensibly - try using data source name.
93 id = safeName(getDataName());
95 chains = new Vector();
96 ArrayList<SequenceI> rna = new ArrayList<SequenceI>(), prot = new ArrayList<SequenceI>();
99 boolean modelFlag = false;
100 boolean terFlag = false;
104 String atomnam = null;
107 while ((line = nextLine()) != null)
109 if (line.indexOf("HEADER") == 0)
111 if (line.length() > 62)
114 if (line.length() > 67)
116 tid = line.substring(62, 67).trim();
120 tid = line.substring(62).trim();
122 if (tid.length() > 0)
129 // Were we to do anything with SEQRES - we start it here
130 if (line.indexOf("SEQRES") == 0)
134 if (line.indexOf("MODEL") == 0)
139 if (line.indexOf("TER") == 0)
144 if (modelFlag && line.indexOf("ENDMDL") == 0)
148 if (line.indexOf("ATOM") == 0
149 || (line.indexOf("HETATM") == 0 && !terFlag))
153 // Jalview is only interested in CA bonds????
154 atomnam = line.substring(12, 15).trim();
155 if (!atomnam.equals("CA") && !atomnam.equals("P"))
160 Atom tmpatom = new Atom(line);
161 tmpchain = findChain(tmpatom.chain);
162 if (tmpchain != null)
164 if (tmpatom.resNumIns.trim().equals(lastID))
166 // phosphorylated protein - seen both CA and P..
169 tmpchain.atoms.addElement(tmpatom);
173 tmpchain = new PDBChain(id, tmpatom.chain);
174 chains.addElement(tmpchain);
175 tmpchain.atoms.addElement(tmpatom);
177 lastID = tmpatom.resNumIns.trim();
187 id = inFile.getName();
189 for (int i = 0; i < chains.size(); i++)
191 SequenceI dataset = chains.elementAt(i).sequence;
192 dataset.setName(id + "|" + dataset.getName());
193 PDBEntry entry = new PDBEntry();
195 entry.setProperty(new Hashtable());
196 if (chains.elementAt(i).id != null)
198 entry.getProperty().put("CHAIN", chains.elementAt(i).id);
202 entry.setFile(inFile.getAbsolutePath());
206 // TODO: decide if we should dump the datasource to disk
207 entry.setFile(getDataName());
209 dataset.addPDBId(entry);
210 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
211 // maintain reference to
213 seqs.addElement(chainseq);
214 if (isRNA(chainseq) == true)
223 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
225 if (chainannot != null && VisibleChainAnnotation)
227 for (int ai = 0; ai < chainannot.length; ai++)
229 chainannot[ai].visible = VisibleChainAnnotation;
230 annotations.addElement(chainannot[ai]);
234 if (processSecondaryStructure)
236 if (externalSecondaryStructure && rna.size() > 0)
240 processPdbFileWithAnnotate3d(rna);
241 } catch (Exception x)
244 .println("Exceptions when dealing with RNA in pdb file");
254 processPdbFileWithJmol(prot);
255 } catch (Exception x)
258 .println("Exceptions from Jmol when processing data in pdb file");
264 } catch (OutOfMemoryError er)
266 System.out.println("OUT OF MEMORY LOADING PDB FILE");
267 throw new IOException(
269 .getString("exception.outofmemory_loading_pdb_file"));
270 } catch (NumberFormatException ex)
274 System.err.println("Couldn't read number from line:");
275 System.err.println(line);
281 private static String calcIdPrefix = "JalviewPDB";
283 public static boolean isCalcIdHandled(String calcId)
285 return calcId != null && (calcIdPrefix.equals(calcId));
288 public static boolean isCalcIdForFile(AlignmentAnnotation alan,
291 return alan.getCalcId() != null
292 && calcIdPrefix.equals(alan.getCalcId())
293 && pdbFile.equals(alan.getProperty("PDBID"));
296 public static String relocateCalcId(String calcId,
297 Hashtable<String, String> alreadyLoadedPDB) throws Exception
299 int s = calcIdPrefix.length(), end = calcId.indexOf(calcIdPrefix, s);
300 String between = calcId.substring(s, end - 1);
301 return calcIdPrefix + alreadyLoadedPDB.get(between) + ":"
302 + calcId.substring(end);
305 private void markCalcIds()
307 for (SequenceI sq : seqs)
309 if (sq.getAnnotation() != null)
311 for (AlignmentAnnotation aa : sq.getAnnotation())
313 String oldId = aa.getCalcId();
318 aa.setCalcId(calcIdPrefix);
319 aa.setProperty("PDBID", id);
320 aa.setProperty("oldCalcId", oldId);
326 private void processPdbFileWithJmol(ArrayList<SequenceI> prot)
331 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
334 Object jmf = cl.getConstructor(new Class[]
335 { FileParse.class }).newInstance(new Object[]
336 { new FileParse(getDataName(), type) });
337 Alignment al = new Alignment((SequenceI[]) cl.getMethod(
338 "getSeqsAsArray", new Class[]
340 cl.getMethod("addAnnotations", new Class[]
341 { Alignment.class }).invoke(jmf, al);
342 for (SequenceI sq : al.getSequences())
344 if (sq.getDatasetSequence() != null)
346 sq.getDatasetSequence().getPDBId().clear();
350 sq.getPDBId().clear();
353 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
355 } catch (ClassNotFoundException q)
360 private void replaceAndUpdateChains(ArrayList<SequenceI> prot,
361 AlignmentI al, String pep, boolean b)
363 List<List<? extends Object>> replaced = AlignSeq
364 .replaceMatchingSeqsWith(seqs, annotations, prot, al,
365 AlignSeq.PEP, false);
366 for (PDBChain ch : chains)
369 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
372 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
381 // set shadow entry for chains
382 ch.shadow = (SequenceI) replaced.get(1).get(p);
383 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
384 .getMappingFromS1(false);
389 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna)
392 // System.out.println("this is a PDB format and RNA sequence");
393 // note: we use reflection here so that the applet can compile and run
394 // without the HTTPClient bits and pieces needed for accessing Annotate3D
398 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
401 // TODO: use the PDB ID of the structure if one is available, to save
402 // bandwidth and avoid uploading the whole structure to the service
403 Object annotate3d = cl.getConstructor(new Class[]
404 {}).newInstance(new Object[]
406 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
408 { FileParse.class }).invoke(annotate3d, new Object[]
409 { new FileParse(getDataName(), type) }));
410 for (SequenceI sq : al.getSequences())
412 if (sq.getDatasetSequence() != null)
414 if (sq.getDatasetSequence().getPDBId() != null)
416 sq.getDatasetSequence().getPDBId().clear();
421 if (sq.getPDBId() != null)
423 sq.getPDBId().clear();
427 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
429 } catch (ClassNotFoundException x)
431 // ignore classnotfounds - occurs in applet
437 * make a friendly ID string.
440 * @return truncated dataName to after last '/'
442 private String safeName(String dataName)
445 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
447 dataName = dataName.substring(p + 1);
452 public void makeResidueList()
454 for (int i = 0; i < chains.size(); i++)
456 chains.elementAt(i).makeResidueList(VisibleChainAnnotation);
460 public void makeCaBondList()
462 for (int i = 0; i < chains.size(); i++)
464 chains.elementAt(i).makeCaBondList();
468 public PDBChain findChain(String id)
470 for (int i = 0; i < chains.size(); i++)
472 if (chains.elementAt(i).id.equals(id))
474 return chains.elementAt(i);
481 public void setChargeColours()
483 for (int i = 0; i < chains.size(); i++)
485 chains.elementAt(i).setChargeColours();
489 public void setColours(jalview.schemes.ColourSchemeI cs)
491 for (int i = 0; i < chains.size(); i++)
493 chains.elementAt(i).setChainColours(cs);
497 public void setChainColours()
499 for (int i = 0; i < chains.size(); i++)
501 chains.elementAt(i).setChainColours(
502 Color.getHSBColor(1.0f / i, .4f, 1.0f));
506 public boolean isRNA(SequenceI seqs)
508 for (int i = 0; i < seqs.getLength(); i++)
510 if ((seqs.getCharAt(i) != 'A') && (seqs.getCharAt(i) != 'C')
511 && (seqs.getCharAt(i) != 'G') && (seqs.getCharAt(i) != 'U'))