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.
28 import jalview.analysis.AlignSeq;
29 import jalview.datamodel.*;
30 import jalview.io.FileParse;
32 public class PDBfile extends jalview.io.AlignFile
39 * set to true to add chain alignment annotation as visible annotation.
41 boolean VisibleChainAnnotation = false;
43 public PDBfile(String inFile, String inType) throws IOException
45 super(inFile, inType);
48 public PDBfile(FileParse source) throws IOException
58 public void parse() throws IOException
60 // TODO set the filename sensibly - try using data source name.
61 id = safeName(getDataName());
63 chains = new Vector();
64 ArrayList<SequenceI> rna = new ArrayList<SequenceI>(), prot = new ArrayList<SequenceI>();
67 boolean modelFlag = false;
68 boolean terFlag = false;
72 String atomnam = null;
75 while ((line = nextLine()) != null)
77 if (line.indexOf("HEADER") == 0)
79 if (line.length() > 62)
82 if (line.length() > 67)
84 tid = line.substring(62, 67).trim();
88 tid = line.substring(62).trim();
97 // Were we to do anything with SEQRES - we start it here
98 if (line.indexOf("SEQRES") == 0)
102 if (line.indexOf("MODEL") == 0)
107 if (line.indexOf("TER") == 0)
112 if (modelFlag && line.indexOf("ENDMDL") == 0)
116 if (line.indexOf("ATOM") == 0
117 || (line.indexOf("HETATM") == 0 && !terFlag))
121 // Jalview is only interested in CA bonds????
122 atomnam = line.substring(12, 15).trim();
123 if (!atomnam.equals("CA") && !atomnam.equals("P"))
128 Atom tmpatom = new Atom(line);
129 tmpchain = findChain(tmpatom.chain);
130 if (tmpchain != null)
132 if (tmpatom.resNumIns.trim().equals(lastID))
134 // phosphorylated protein - seen both CA and P..
137 tmpchain.atoms.addElement(tmpatom);
141 tmpchain = new PDBChain(id, tmpatom.chain);
142 chains.addElement(tmpchain);
143 tmpchain.atoms.addElement(tmpatom);
145 lastID = tmpatom.resNumIns.trim();
155 id = inFile.getName();
157 for (int i = 0; i < chains.size(); i++)
159 SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
160 dataset.setName(id + "|" + dataset.getName());
161 PDBEntry entry = new PDBEntry();
163 entry.setProperty(new Hashtable());
164 if (((PDBChain) chains.elementAt(i)).id != null)
166 entry.getProperty().put("CHAIN",
167 ((PDBChain) chains.elementAt(i)).id);
171 entry.setFile(inFile.getAbsolutePath());
175 // TODO: decide if we should dump the datasource to disk
176 entry.setFile(getDataName());
178 dataset.addPDBId(entry);
179 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
180 // maintain reference to
182 seqs.addElement(chainseq);
183 if (isRNA(chainseq) == true)
192 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
194 if (chainannot != null)
196 for (int ai = 0; ai < chainannot.length; ai++)
199 chainannot[ai].visible = VisibleChainAnnotation;
200 annotations.addElement(chainannot[ai]);
207 processPdbFileWithAnnotate3d(rna);
208 } catch (Exception x)
211 .println("Exceptions when dealing with RNA in pdb file");
219 processPdbFileWithJmol(prot);
220 } catch (Exception x)
223 .println("Exceptions when dealing with RNA in pdb file");
231 processPdbFileWithJmol(prot);
232 } catch (Exception x)
235 .println("Exceptions when dealing with RNA in pdb file");
240 } catch (OutOfMemoryError er)
242 System.out.println("OUT OF MEMORY LOADING PDB FILE");
243 throw new IOException("Out of memory loading PDB File");
244 } catch (NumberFormatException ex)
248 System.err.println("Couldn't read number from line:");
249 System.err.println(line);
254 private void processPdbFileWithJmol(ArrayList<SequenceI> prot)
259 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
262 Object jmf = cl.getConstructor(new Class[]
263 { FileParse.class }).newInstance(new Object[]
264 { new FileParse(getDataName(), type) });
265 Alignment al = new Alignment((SequenceI[]) cl.getMethod(
266 "getSeqsAsArray", new Class[]
268 cl.getMethod("addAnnotations", new Class[]
269 { Alignment.class }).invoke(jmf, al);
270 replaceMatchingSeqsWith(prot, al, AlignSeq.PEP);
272 } catch (ClassNotFoundException q)
277 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna)
280 // System.out.println("this is a PDB format and RNA sequence");
281 // note: we use reflection here so that the applet can compile and run
282 // without the HTTPClient bits and pieces needed for accessing Annotate3D
286 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
289 // TODO: use the PDB ID of the structure if one is available, to save
290 // bandwidth and avoid uploading the whole structure to the service
291 Object annotate3d = cl.getConstructor(new Class[]
292 {}).newInstance(new Object[]
294 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
296 { FileParse.class }).invoke(annotate3d, new Object[]
297 { new FileParse(getDataName(), type) }));
298 replaceMatchingSeqsWith(rna, al, AlignSeq.DNA);
300 } catch (ClassNotFoundException x)
302 // ignore classnotfounds - occurs in applet
307 private void replaceMatchingSeqsWith(ArrayList<SequenceI> ochains,
308 AlignmentI al, String dnaOrProtein)
310 if (al != null && al.getHeight() > 0)
312 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
313 ArrayList<AlignSeq> aligns = new ArrayList<AlignSeq>();
315 for (SequenceI sq : ochains)
317 SequenceI bestm = null;
318 AlignSeq bestaseq = null;
320 for (SequenceI msq : al.getSequences())
322 AlignSeq aseq = AlignSeq.doGlobalNWAlignment(msq, sq,
324 if (bestm == null || aseq.getMaxScore() > bestscore)
326 bestscore = aseq.getMaxScore();
331 System.out.println("Best Score for " + (matches.size() + 1) + " :"
334 aligns.add(bestaseq);
335 al.deleteSequence(bestm);
337 for (int p = 0, pSize = seqs.size(); p < pSize; p++)
339 SequenceI sq, sp = seqs.get(p);
341 if ((q = ochains.indexOf(sp)) > -1)
343 seqs.set(p, sq = matches.get(q));
344 sq.setName(sp.getName());
345 sq.setDescription(sp.getDescription());
346 sq.transferAnnotation(sp, aligns.get(q).getMappingFromS1(false));
348 for (int ap = 0; ap < annotations.size();)
350 if (((AlignmentAnnotation) annotations.get(ap)).sequenceRef == sp)
356 annotations.remove(ap);
363 if (sq.getAnnotation() != null)
365 annotations.addAll(inspos, Arrays.asList(sq.getAnnotation()));
373 * make a friendly ID string.
376 * @return truncated dataName to after last '/'
378 private String safeName(String dataName)
381 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
383 dataName = dataName.substring(p + 1);
388 public void makeResidueList()
390 for (int i = 0; i < chains.size(); i++)
392 ((PDBChain) chains.elementAt(i)).makeResidueList();
396 public void makeCaBondList()
398 for (int i = 0; i < chains.size(); i++)
400 ((PDBChain) chains.elementAt(i)).makeCaBondList();
404 public PDBChain findChain(String id)
406 for (int i = 0; i < chains.size(); i++)
408 if (((PDBChain) chains.elementAt(i)).id.equals(id))
410 return (PDBChain) chains.elementAt(i);
417 public void setChargeColours()
419 for (int i = 0; i < chains.size(); i++)
421 ((PDBChain) chains.elementAt(i)).setChargeColours();
425 public void setColours(jalview.schemes.ColourSchemeI cs)
427 for (int i = 0; i < chains.size(); i++)
429 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
433 public void setChainColours()
435 for (int i = 0; i < chains.size(); i++)
437 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
438 1.0f / (float) i, .4f, 1.0f));
442 public boolean isRNA(SequenceI seqs)
444 for (int i = 0; i < seqs.getLength(); i++)
446 if ((seqs.getCharAt(i) != 'A') && (seqs.getCharAt(i) != 'C')
447 && (seqs.getCharAt(i) != 'G') && (seqs.getCharAt(i) != 'U'))