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
45 boolean processSecondaryStructure=true;
47 super(inFile, inType);
50 public PDBfile(FileParse source) throws IOException
60 public void parse() throws IOException
62 // TODO set the filename sensibly - try using data source name.
63 id = safeName(getDataName());
65 chains = new Vector();
66 ArrayList<SequenceI> rna = new ArrayList<SequenceI>(), prot = new ArrayList<SequenceI>();
69 boolean modelFlag = false;
70 boolean terFlag = false;
74 String atomnam = null;
77 while ((line = nextLine()) != null)
79 if (line.indexOf("HEADER") == 0)
81 if (line.length() > 62)
84 if (line.length() > 67)
86 tid = line.substring(62, 67).trim();
90 tid = line.substring(62).trim();
99 // Were we to do anything with SEQRES - we start it here
100 if (line.indexOf("SEQRES") == 0)
104 if (line.indexOf("MODEL") == 0)
109 if (line.indexOf("TER") == 0)
114 if (modelFlag && line.indexOf("ENDMDL") == 0)
118 if (line.indexOf("ATOM") == 0
119 || (line.indexOf("HETATM") == 0 && !terFlag))
123 // Jalview is only interested in CA bonds????
124 atomnam = line.substring(12, 15).trim();
125 if (!atomnam.equals("CA") && !atomnam.equals("P"))
130 Atom tmpatom = new Atom(line);
131 tmpchain = findChain(tmpatom.chain);
132 if (tmpchain != null)
134 if (tmpatom.resNumIns.trim().equals(lastID))
136 // phosphorylated protein - seen both CA and P..
139 tmpchain.atoms.addElement(tmpatom);
143 tmpchain = new PDBChain(id, tmpatom.chain);
144 chains.addElement(tmpchain);
145 tmpchain.atoms.addElement(tmpatom);
147 lastID = tmpatom.resNumIns.trim();
157 id = inFile.getName();
159 for (int i = 0; i < chains.size(); i++)
161 SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
162 dataset.setName(id + "|" + dataset.getName());
163 PDBEntry entry = new PDBEntry();
165 entry.setProperty(new Hashtable());
166 if (((PDBChain) chains.elementAt(i)).id != null)
168 entry.getProperty().put("CHAIN",
169 ((PDBChain) chains.elementAt(i)).id);
173 entry.setFile(inFile.getAbsolutePath());
177 // TODO: decide if we should dump the datasource to disk
178 entry.setFile(getDataName());
180 dataset.addPDBId(entry);
181 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
182 // maintain reference to
184 seqs.addElement(chainseq);
185 if (isRNA(chainseq) == true)
194 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
196 if (chainannot != null)
198 for (int ai = 0; ai < chainannot.length; ai++)
201 chainannot[ai].visible = VisibleChainAnnotation;
202 annotations.addElement(chainannot[ai]);
206 if (processSecondaryStructure)
211 processPdbFileWithAnnotate3d(rna);
212 } catch (Exception x)
215 .println("Exceptions when dealing with RNA in pdb file");
223 processPdbFileWithJmol(prot);
224 } catch (Exception x)
227 .println("Exceptions when dealing with RNA in pdb file");
232 } catch (OutOfMemoryError er)
234 System.out.println("OUT OF MEMORY LOADING PDB FILE");
235 throw new IOException(
237 .getString("exception.outofmemory_loading_pdb_file"));
238 } catch (NumberFormatException ex)
242 System.err.println("Couldn't read number from line:");
243 System.err.println(line);
248 private void processPdbFileWithJmol(ArrayList<SequenceI> prot)
253 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
256 Object jmf = cl.getConstructor(new Class[]
257 { FileParse.class }).newInstance(new Object[]
258 { new FileParse(getDataName(), type) });
259 Alignment al = new Alignment((SequenceI[]) cl.getMethod(
260 "getSeqsAsArray", new Class[]
262 cl.getMethod("addAnnotations", new Class[]
263 { Alignment.class }).invoke(jmf, al);
264 replaceMatchingSeqsWith(prot, al, AlignSeq.PEP);
266 } catch (ClassNotFoundException q)
271 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna)
274 // System.out.println("this is a PDB format and RNA sequence");
275 // note: we use reflection here so that the applet can compile and run
276 // without the HTTPClient bits and pieces needed for accessing Annotate3D
280 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
283 // TODO: use the PDB ID of the structure if one is available, to save
284 // bandwidth and avoid uploading the whole structure to the service
285 Object annotate3d = cl.getConstructor(new Class[]
286 {}).newInstance(new Object[]
288 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
290 { FileParse.class }).invoke(annotate3d, new Object[]
291 { new FileParse(getDataName(), type) }));
292 replaceMatchingSeqsWith(rna, al, AlignSeq.DNA);
294 } catch (ClassNotFoundException x)
296 // ignore classnotfounds - occurs in applet
302 * matches ochains against al and populates seqs with the best match between
303 * each ochain and the set in al
307 * @param dnaOrProtein
309 private void replaceMatchingSeqsWith(ArrayList<SequenceI> ochains,
310 AlignmentI al, String dnaOrProtein)
312 if (al != null && al.getHeight() > 0)
314 ArrayList<SequenceI> matches = new ArrayList<SequenceI>();
315 ArrayList<AlignSeq> aligns = new ArrayList<AlignSeq>();
317 for (SequenceI sq : ochains)
319 SequenceI bestm = null;
320 AlignSeq bestaseq = null;
322 for (SequenceI msq : al.getSequences())
324 AlignSeq aseq = AlignSeq.doGlobalNWAlignment(msq, sq,
326 if (bestm == null || aseq.getMaxScore() > bestscore)
328 bestscore = aseq.getMaxScore();
333 System.out.println("Best Score for " + (matches.size() + 1) + " :"
336 aligns.add(bestaseq);
337 al.deleteSequence(bestm);
339 for (int p = 0, pSize = seqs.size(); p < pSize; p++)
341 SequenceI sq, sp = seqs.get(p);
343 if ((q = ochains.indexOf(sp)) > -1)
345 seqs.set(p, sq = matches.get(q));
346 sq.setName(sp.getName());
347 sq.setDescription(sp.getDescription());
348 sq.transferAnnotation(sp, aligns.get(q).getMappingFromS1(false));
350 for (int ap = 0; ap < annotations.size();)
352 if (((AlignmentAnnotation) annotations.get(ap)).sequenceRef == sp)
358 annotations.remove(ap);
365 if (sq.getAnnotation() != null)
367 annotations.addAll(inspos, Arrays.asList(sq.getAnnotation()));
375 * make a friendly ID string.
378 * @return truncated dataName to after last '/'
380 private String safeName(String dataName)
383 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
385 dataName = dataName.substring(p + 1);
390 public void makeResidueList()
392 for (int i = 0; i < chains.size(); i++)
394 ((PDBChain) chains.elementAt(i)).makeResidueList();
398 public void makeCaBondList()
400 for (int i = 0; i < chains.size(); i++)
402 ((PDBChain) chains.elementAt(i)).makeCaBondList();
406 public PDBChain findChain(String id)
408 for (int i = 0; i < chains.size(); i++)
410 if (((PDBChain) chains.elementAt(i)).id.equals(id))
412 return (PDBChain) chains.elementAt(i);
419 public void setChargeColours()
421 for (int i = 0; i < chains.size(); i++)
423 ((PDBChain) chains.elementAt(i)).setChargeColours();
427 public void setColours(jalview.schemes.ColourSchemeI cs)
429 for (int i = 0; i < chains.size(); i++)
431 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
435 public void setChainColours()
437 for (int i = 0; i < chains.size(); i++)
439 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
440 1.0f / (float) i, .4f, 1.0f));
444 public boolean isRNA(SequenceI seqs)
446 for (int i = 0; i < seqs.getLength(); i++)
448 if ((seqs.getCharAt(i) != 'A') && (seqs.getCharAt(i) != 'C')
449 && (seqs.getCharAt(i) != 'G') && (seqs.getCharAt(i) != 'U'))