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) {
165 entry.getProperty().put("CHAIN", ((PDBChain)chains.elementAt(i)).id);
169 entry.setFile(inFile.getAbsolutePath());
173 // TODO: decide if we should dump the datasource to disk
174 entry.setFile(getDataName());
176 dataset.addPDBId(entry);
177 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
178 // maintain reference to
180 seqs.addElement(chainseq);
181 if(isRNA(chainseq)==true)
188 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
190 if (chainannot != null)
192 for (int ai = 0; ai < chainannot.length; ai++)
195 chainannot[ai].visible = VisibleChainAnnotation;
196 annotations.addElement(chainannot[ai]);
202 processPdbFileWithAnnotate3d(rna);
203 } catch (Exception x)
205 System.err.println("Exceptions when dealing with RNA in pdb file");
211 processPdbFileWithJmol(prot);
212 } catch (Exception x)
214 System.err.println("Exceptions when dealing with RNA in pdb file");
220 processPdbFileWithJmol(prot);
221 } catch (Exception x)
223 System.err.println("Exceptions when dealing with RNA in pdb file");
227 } catch (OutOfMemoryError er)
229 System.out.println("OUT OF MEMORY LOADING PDB FILE");
230 throw new IOException("Out of memory loading PDB File");
231 } catch (NumberFormatException ex)
235 System.err.println("Couldn't read number from line:");
236 System.err.println(line);
240 private void processPdbFileWithJmol(ArrayList<SequenceI> prot) throws Exception
243 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
246 Object jmf = cl.getConstructor(new Class[] {FileParse.class}).newInstance(new Object[] {new FileParse(getDataName(),type)});
247 Alignment al = new Alignment((SequenceI[]) cl.getMethod("getSeqsAsArray", new Class[] {}).invoke(jmf));
248 cl.getMethod("addAnnotations",new Class[] {Alignment.class}).invoke(jmf, al);
249 replaceMatchingSeqsWith(prot, al, AlignSeq.PEP);
251 } catch (ClassNotFoundException q)
254 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna) throws Exception {
255 // System.out.println("this is a PDB format and RNA sequence");
256 // note: we use reflection here so that the applet can compile and run without the HTTPClient bits and pieces needed for accessing Annotate3D web service
258 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
261 // TODO: use the PDB ID of the structure if one is available, to save bandwidth and avoid uploading the whole structure to the service
262 Object annotate3d = cl.getConstructor(new Class[] {}).newInstance(new Object[] {});
263 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor", new Class[] { FileParse.class}).invoke(annotate3d, new Object[] { new FileParse(getDataName(),type)}));
264 replaceMatchingSeqsWith(rna, al, AlignSeq.DNA);
266 } catch (ClassNotFoundException x)
268 //ignore classnotfounds - occurs in applet
271 private void replaceMatchingSeqsWith(ArrayList<SequenceI> ochains, AlignmentI al, String dnaOrProtein)
273 if (al!=null && al.getHeight()>0)
275 ArrayList<SequenceI> matches=new ArrayList<SequenceI>();
276 ArrayList<AlignSeq> aligns=new ArrayList<AlignSeq>();
278 for (SequenceI sq:ochains)
280 SequenceI bestm=null;
281 AlignSeq bestaseq=null;
283 for (SequenceI msq:al.getSequences())
285 AlignSeq aseq = AlignSeq.doGlobalNWAlignment(msq, sq, dnaOrProtein);
286 if (bestm==null || aseq.getMaxScore()>bestscore)
288 bestscore=aseq.getMaxScore();
293 System.out.println("Best Score for "+(matches.size()+1)+" :"+bestscore);
295 aligns.add(bestaseq);
296 al.deleteSequence(bestm);
298 for (int p=0,pSize=seqs.size();p<pSize;p++)
300 SequenceI sq,sp=seqs.get(p);
302 if ((q=ochains.indexOf(sp))>-1)
304 seqs.set(p, sq=matches.get(q));
305 sq.setName(sp.getName());
306 sq.setDescription(sp.getDescription());
307 sq.transferAnnotation(sp, aligns.get(q).getMappingFromS1(false));
309 for (int ap=0;ap<annotations.size();)
311 if (((AlignmentAnnotation)annotations.get(ap)).sequenceRef==sp) {
316 annotations.remove(ap);
321 if (sq.getAnnotation()!=null) {
322 annotations.addAll(inspos, Arrays.asList(sq.getAnnotation()));
329 * make a friendly ID string.
332 * @return truncated dataName to after last '/'
334 private String safeName(String dataName)
337 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
339 dataName = dataName.substring(p + 1);
344 public void makeResidueList()
346 for (int i = 0; i < chains.size(); i++)
348 ((PDBChain) chains.elementAt(i)).makeResidueList();
352 public void makeCaBondList()
354 for (int i = 0; i < chains.size(); i++)
356 ((PDBChain) chains.elementAt(i)).makeCaBondList();
360 public PDBChain findChain(String id)
362 for (int i = 0; i < chains.size(); i++)
364 if (((PDBChain) chains.elementAt(i)).id.equals(id))
366 return (PDBChain) chains.elementAt(i);
373 public void setChargeColours()
375 for (int i = 0; i < chains.size(); i++)
377 ((PDBChain) chains.elementAt(i)).setChargeColours();
381 public void setColours(jalview.schemes.ColourSchemeI cs)
383 for (int i = 0; i < chains.size(); i++)
385 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
389 public void setChainColours()
391 for (int i = 0; i < chains.size(); i++)
393 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
394 1.0f / (float) i, .4f, 1.0f));
397 public boolean isRNA(SequenceI seqs)
399 for (int i=0;i<seqs.getLength();i++){
400 if((seqs.getCharAt(i)!='A') &&(seqs.getCharAt(i)!='C')&&(seqs.getCharAt(i)!='G')&&(seqs.getCharAt(i)!='U'))