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;
31 import jalview.util.MessageManager;
33 public class PDBfile extends jalview.io.AlignFile
40 * set to true to add chain alignment annotation as visible annotation.
42 boolean VisibleChainAnnotation = false;
44 public PDBfile(String inFile, String inType) throws IOException
46 super(inFile, inType);
49 public PDBfile(FileParse source) throws IOException
59 public void parse() throws IOException
61 // TODO set the filename sensibly - try using data source name.
62 id = safeName(getDataName());
64 chains = new Vector();
65 ArrayList<SequenceI> rna = new ArrayList<SequenceI>(), prot = new ArrayList<SequenceI>();
68 boolean modelFlag = false;
69 boolean terFlag = false;
73 String atomnam = null;
76 while ((line = nextLine()) != null)
78 if (line.indexOf("HEADER") == 0)
80 if (line.length() > 62)
83 if (line.length() > 67)
85 tid = line.substring(62, 67).trim();
89 tid = line.substring(62).trim();
98 // Were we to do anything with SEQRES - we start it here
99 if (line.indexOf("SEQRES") == 0)
103 if (line.indexOf("MODEL") == 0)
108 if (line.indexOf("TER") == 0)
113 if (modelFlag && line.indexOf("ENDMDL") == 0)
117 if (line.indexOf("ATOM") == 0
118 || (line.indexOf("HETATM") == 0 && !terFlag))
122 // Jalview is only interested in CA bonds????
123 atomnam = line.substring(12, 15).trim();
124 if (!atomnam.equals("CA") && !atomnam.equals("P"))
129 Atom tmpatom = new Atom(line);
130 tmpchain = findChain(tmpatom.chain);
131 if (tmpchain != null)
133 if (tmpatom.resNumIns.trim().equals(lastID))
135 // phosphorylated protein - seen both CA and P..
138 tmpchain.atoms.addElement(tmpatom);
142 tmpchain = new PDBChain(id, tmpatom.chain);
143 chains.addElement(tmpchain);
144 tmpchain.atoms.addElement(tmpatom);
146 lastID = tmpatom.resNumIns.trim();
156 id = inFile.getName();
158 for (int i = 0; i < chains.size(); i++)
160 SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
161 dataset.setName(id + "|" + dataset.getName());
162 PDBEntry entry = new PDBEntry();
164 entry.setProperty(new Hashtable());
165 if (((PDBChain) chains.elementAt(i)).id != null)
167 entry.getProperty().put("CHAIN",
168 ((PDBChain) chains.elementAt(i)).id);
172 entry.setFile(inFile.getAbsolutePath());
176 // TODO: decide if we should dump the datasource to disk
177 entry.setFile(getDataName());
179 dataset.addPDBId(entry);
180 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
181 // maintain reference to
183 seqs.addElement(chainseq);
184 if (isRNA(chainseq) == true)
193 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
195 if (chainannot != null)
197 for (int ai = 0; ai < chainannot.length; ai++)
200 chainannot[ai].visible = VisibleChainAnnotation;
201 annotations.addElement(chainannot[ai]);
208 processPdbFileWithAnnotate3d(rna);
209 } catch (Exception x)
212 .println("Exceptions when dealing with RNA in pdb file");
220 processPdbFileWithJmol(prot);
221 } catch (Exception x)
224 .println("Exceptions when dealing with RNA in pdb file");
232 processPdbFileWithJmol(prot);
233 } catch (Exception x)
236 .println("Exceptions when dealing with RNA in pdb file");
241 } catch (OutOfMemoryError er)
243 System.out.println("OUT OF MEMORY LOADING PDB FILE");
244 throw new IOException(MessageManager.getString("exception.outofmemory_loading_pdb_file"));
245 } catch (NumberFormatException ex)
249 System.err.println("Couldn't read number from line:");
250 System.err.println(line);
255 private void processPdbFileWithJmol(ArrayList<SequenceI> prot)
260 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
263 Object jmf = cl.getConstructor(new Class[]
264 { FileParse.class }).newInstance(new Object[]
265 { new FileParse(getDataName(), type) });
266 Alignment al = new Alignment((SequenceI[]) cl.getMethod(
267 "getSeqsAsArray", new Class[]
269 cl.getMethod("addAnnotations", new Class[]
270 { Alignment.class }).invoke(jmf, al);
271 replaceMatchingSeqsWith(prot, al, AlignSeq.PEP);
273 } catch (ClassNotFoundException q)
278 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna)
281 // System.out.println("this is a PDB format and RNA sequence");
282 // note: we use reflection here so that the applet can compile and run
283 // without the HTTPClient bits and pieces needed for accessing Annotate3D
287 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
290 // TODO: use the PDB ID of the structure if one is available, to save
291 // bandwidth and avoid uploading the whole structure to the service
292 Object annotate3d = cl.getConstructor(new Class[]
293 {}).newInstance(new Object[]
295 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
297 { FileParse.class }).invoke(annotate3d, new Object[]
298 { new FileParse(getDataName(), type) }));
299 replaceMatchingSeqsWith(rna, al, AlignSeq.DNA);
301 } catch (ClassNotFoundException x)
303 // ignore classnotfounds - occurs in applet
308 private void replaceMatchingSeqsWith(ArrayList<SequenceI> ochains,
309 AlignmentI al, String dnaOrProtein)
311 if (al != null && al.getHeight() > 0)
313 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
314 ArrayList<AlignSeq> aligns = new ArrayList<AlignSeq>();
316 for (SequenceI sq : ochains)
318 SequenceI bestm = null;
319 AlignSeq bestaseq = null;
321 for (SequenceI msq : al.getSequences())
323 AlignSeq aseq = AlignSeq.doGlobalNWAlignment(msq, sq,
325 if (bestm == null || aseq.getMaxScore() > bestscore)
327 bestscore = aseq.getMaxScore();
332 System.out.println("Best Score for " + (matches.size() + 1) + " :"
335 aligns.add(bestaseq);
336 al.deleteSequence(bestm);
338 for (int p = 0, pSize = seqs.size(); p < pSize; p++)
340 SequenceI sq, sp = seqs.get(p);
342 if ((q = ochains.indexOf(sp)) > -1)
344 seqs.set(p, sq = matches.get(q));
345 sq.setName(sp.getName());
346 sq.setDescription(sp.getDescription());
347 sq.transferAnnotation(sp, aligns.get(q).getMappingFromS1(false));
349 for (int ap = 0; ap < annotations.size();)
351 if (((AlignmentAnnotation) annotations.get(ap)).sequenceRef == sp)
357 annotations.remove(ap);
364 if (sq.getAnnotation() != null)
366 annotations.addAll(inspos, Arrays.asList(sq.getAnnotation()));
374 * make a friendly ID string.
377 * @return truncated dataName to after last '/'
379 private String safeName(String dataName)
382 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
384 dataName = dataName.substring(p + 1);
389 public void makeResidueList()
391 for (int i = 0; i < chains.size(); i++)
393 ((PDBChain) chains.elementAt(i)).makeResidueList();
397 public void makeCaBondList()
399 for (int i = 0; i < chains.size(); i++)
401 ((PDBChain) chains.elementAt(i)).makeCaBondList();
405 public PDBChain findChain(String id)
407 for (int i = 0; i < chains.size(); i++)
409 if (((PDBChain) chains.elementAt(i)).id.equals(id))
411 return (PDBChain) chains.elementAt(i);
418 public void setChargeColours()
420 for (int i = 0; i < chains.size(); i++)
422 ((PDBChain) chains.elementAt(i)).setChargeColours();
426 public void setColours(jalview.schemes.ColourSchemeI cs)
428 for (int i = 0; i < chains.size(); i++)
430 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
434 public void setChainColours()
436 for (int i = 0; i < chains.size(); i++)
438 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
439 1.0f / (float) i, .4f, 1.0f));
443 public boolean isRNA(SequenceI seqs)
445 for (int i = 0; i < seqs.getLength(); i++)
447 if ((seqs.getCharAt(i) != 'A') && (seqs.getCharAt(i) != 'C')
448 && (seqs.getCharAt(i) != 'G') && (seqs.getCharAt(i) != 'U'))