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.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);
199 entry.setChainCode(String.valueOf(chains.elementAt(i).id));
203 entry.setFile(inFile.getAbsolutePath());
207 // TODO: decide if we should dump the datasource to disk
208 entry.setFile(getDataName());
210 dataset.addPDBId(entry);
211 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
212 // maintain reference to
214 seqs.addElement(chainseq);
215 if (isRNA(chainseq) == true)
224 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
226 if (chainannot != null && VisibleChainAnnotation)
228 for (int ai = 0; ai < chainannot.length; ai++)
230 chainannot[ai].visible = VisibleChainAnnotation;
231 annotations.addElement(chainannot[ai]);
235 if (processSecondaryStructure)
237 if (externalSecondaryStructure && rna.size() > 0)
241 processPdbFileWithAnnotate3d(rna);
242 } catch (Exception x)
245 .println("Exceptions when dealing with RNA in pdb file");
255 processPdbFileWithJmol(prot);
256 } catch (Exception x)
259 .println("Exceptions from Jmol when processing data in pdb file");
265 } catch (OutOfMemoryError er)
267 System.out.println("OUT OF MEMORY LOADING PDB FILE");
268 throw new IOException(
270 .getString("exception.outofmemory_loading_pdb_file"));
271 } catch (NumberFormatException ex)
275 System.err.println("Couldn't read number from line:");
276 System.err.println(line);
282 private static String calcIdPrefix = "JalviewPDB";
284 public static boolean isCalcIdHandled(String calcId)
286 return calcId != null && (calcIdPrefix.equals(calcId));
289 public static boolean isCalcIdForFile(AlignmentAnnotation alan,
292 return alan.getCalcId() != null
293 && calcIdPrefix.equals(alan.getCalcId())
294 && pdbFile.equals(alan.getProperty("PDBID"));
297 public static String relocateCalcId(String calcId,
298 Hashtable<String, String> alreadyLoadedPDB) throws Exception
300 int s = calcIdPrefix.length(), end = calcId.indexOf(calcIdPrefix, s);
301 String between = calcId.substring(s, end - 1);
302 return calcIdPrefix + alreadyLoadedPDB.get(between) + ":"
303 + calcId.substring(end);
306 private void markCalcIds()
308 for (SequenceI sq : seqs)
310 if (sq.getAnnotation() != null)
312 for (AlignmentAnnotation aa : sq.getAnnotation())
314 String oldId = aa.getCalcId();
319 aa.setCalcId(calcIdPrefix);
320 aa.setProperty("PDBID", id);
321 aa.setProperty("oldCalcId", oldId);
327 private void processPdbFileWithJmol(ArrayList<SequenceI> prot)
332 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
335 Object jmf = cl.getConstructor(new Class[]
336 { FileParse.class }).newInstance(new Object[]
337 { new FileParse(getDataName(), type) });
338 Alignment al = new Alignment((SequenceI[]) cl.getMethod(
339 "getSeqsAsArray", new Class[]
341 cl.getMethod("addAnnotations", new Class[]
342 { Alignment.class }).invoke(jmf, al);
343 for (SequenceI sq : al.getSequences())
345 if (sq.getDatasetSequence() != null)
347 sq.getDatasetSequence().getPDBId().clear();
351 sq.getPDBId().clear();
354 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
356 } catch (ClassNotFoundException q)
361 private void replaceAndUpdateChains(ArrayList<SequenceI> prot,
362 AlignmentI al, String pep, boolean b)
364 List<List<? extends Object>> replaced = AlignSeq
365 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
367 for (PDBChain ch : chains)
370 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
373 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
382 // set shadow entry for chains
383 ch.shadow = (SequenceI) replaced.get(1).get(p);
384 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
385 .getMappingFromS1(false);
390 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna)
393 // System.out.println("this is a PDB format and RNA sequence");
394 // note: we use reflection here so that the applet can compile and run
395 // without the HTTPClient bits and pieces needed for accessing Annotate3D
399 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
402 // TODO: use the PDB ID of the structure if one is available, to save
403 // bandwidth and avoid uploading the whole structure to the service
404 Object annotate3d = cl.getConstructor(new Class[]
405 {}).newInstance(new Object[]
407 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
409 { FileParse.class }).invoke(annotate3d, new Object[]
410 { new FileParse(getDataName(), type) }));
411 for (SequenceI sq : al.getSequences())
413 if (sq.getDatasetSequence() != null)
415 if (sq.getDatasetSequence().getPDBId() != null)
417 sq.getDatasetSequence().getPDBId().clear();
422 if (sq.getPDBId() != null)
424 sq.getPDBId().clear();
428 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
430 } catch (ClassNotFoundException x)
432 // ignore classnotfounds - occurs in applet
438 * make a friendly ID string.
441 * @return truncated dataName to after last '/'
443 private String safeName(String dataName)
446 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
448 dataName = dataName.substring(p + 1);
453 public void makeResidueList()
455 for (int i = 0; i < chains.size(); i++)
457 chains.elementAt(i).makeResidueList(VisibleChainAnnotation);
461 public void makeCaBondList()
463 for (int i = 0; i < chains.size(); i++)
465 chains.elementAt(i).makeCaBondList();
469 public PDBChain findChain(String id)
471 for (int i = 0; i < chains.size(); i++)
473 if (chains.elementAt(i).id.equals(id))
475 return chains.elementAt(i);
482 public void setChargeColours()
484 for (int i = 0; i < chains.size(); i++)
486 chains.elementAt(i).setChargeColours();
490 public void setColours(jalview.schemes.ColourSchemeI cs)
492 for (int i = 0; i < chains.size(); i++)
494 chains.elementAt(i).setChainColours(cs);
498 public void setChainColours()
500 for (int i = 0; i < chains.size(); i++)
502 chains.elementAt(i).setChainColours(
503 Color.getHSBColor(1.0f / i, .4f, 1.0f));
507 public boolean isRNA(SequenceI seqs)
509 for (int i = 0; i < seqs.getLength(); i++)
511 if ((seqs.getCharAt(i) != 'A') && (seqs.getCharAt(i) != 'C')
512 && (seqs.getCharAt(i) != 'G') && (seqs.getCharAt(i) != 'U'))