2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
26 import jalview.analysis.AlignSeq;
27 import jalview.datamodel.*;
28 import jalview.io.FileParse;
30 public class PDBfile extends jalview.io.AlignFile
37 * set to true to add chain alignment annotation as visible annotation.
39 boolean VisibleChainAnnotation = false;
41 public PDBfile(String inFile, String inType) throws IOException
43 super(inFile, inType);
46 public PDBfile(FileParse source) throws IOException
56 public void parse() throws IOException
58 // TODO set the filename sensibly - try using data source name.
59 id = safeName(getDataName());
61 chains = new Vector();
62 ArrayList<SequenceI> rna=new ArrayList<SequenceI>(), prot=new ArrayList<SequenceI>();
65 boolean modelFlag = false;
66 boolean terFlag = false;
70 String atomnam = null;
73 while ((line = nextLine()) != null)
75 if (line.indexOf("HEADER") == 0)
77 if (line.length() > 62)
80 if (line.length() > 67)
82 tid = line.substring(62, 67).trim();
86 tid = line.substring(62).trim();
95 // Were we to do anything with SEQRES - we start it here
96 if (line.indexOf("SEQRES") == 0)
100 if (line.indexOf("MODEL") == 0)
105 if (line.indexOf("TER") == 0)
110 if (modelFlag && line.indexOf("ENDMDL") == 0)
114 if (line.indexOf("ATOM") == 0
115 || (line.indexOf("HETATM") == 0 && !terFlag))
119 // Jalview is only interested in CA bonds????
120 atomnam = line.substring(12, 15).trim();
121 if (!atomnam.equals("CA") && !atomnam.equals("P"))
126 Atom tmpatom = new Atom(line);
127 tmpchain = findChain(tmpatom.chain);
128 if (tmpchain != null)
130 if (tmpatom.resNumIns.trim().equals(lastID))
132 // phosphorylated protein - seen both CA and P..
135 tmpchain.atoms.addElement(tmpatom);
139 tmpchain = new PDBChain(id, tmpatom.chain);
140 chains.addElement(tmpchain);
141 tmpchain.atoms.addElement(tmpatom);
143 lastID = tmpatom.resNumIns.trim();
153 id = inFile.getName();
155 for (int i = 0; i < chains.size(); i++)
157 SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
158 dataset.setName(id + "|" + dataset.getName());
159 PDBEntry entry = new PDBEntry();
161 entry.setProperty(new Hashtable());
162 if (((PDBChain)chains.elementAt(i)).id!=null) {
163 entry.getProperty().put("CHAIN", ((PDBChain)chains.elementAt(i)).id);
167 entry.setFile(inFile.getAbsolutePath());
171 // TODO: decide if we should dump the datasource to disk
172 entry.setFile(getDataName());
174 dataset.addPDBId(entry);
175 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
176 // maintain reference to
178 seqs.addElement(chainseq);
179 if(isRNA(chainseq)==true)
186 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
188 if (chainannot != null)
190 for (int ai = 0; ai < chainannot.length; ai++)
193 chainannot[ai].visible = VisibleChainAnnotation;
194 annotations.addElement(chainannot[ai]);
200 processPdbFileWithAnnotate3d(rna);
201 } catch (Exception x)
203 System.err.println("Exceptions when dealing with RNA in pdb file");
209 processPdbFileWithJmol(prot);
210 } catch (Exception x)
212 System.err.println("Exceptions when dealing with RNA in pdb file");
218 processPdbFileWithJmol(prot);
219 } catch (Exception x)
221 System.err.println("Exceptions when dealing with RNA in pdb file");
225 } catch (OutOfMemoryError er)
227 System.out.println("OUT OF MEMORY LOADING PDB FILE");
228 throw new IOException("Out of memory loading PDB File");
229 } catch (NumberFormatException ex)
233 System.err.println("Couldn't read number from line:");
234 System.err.println(line);
238 private void processPdbFileWithJmol(ArrayList<SequenceI> prot) throws Exception
241 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
244 Object jmf = cl.getConstructor(new Class[] {FileParse.class}).newInstance(new Object[] {new FileParse(getDataName(),type)});
245 Alignment al = new Alignment((SequenceI[]) cl.getMethod("getSeqsAsArray", new Class[] {}).invoke(jmf));
246 cl.getMethod("addAnnotations",new Class[] {Alignment.class}).invoke(jmf, al);
247 replaceMatchingSeqsWith(prot, al, AlignSeq.PEP);
249 } catch (ClassNotFoundException q)
252 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna) throws Exception {
253 // System.out.println("this is a PDB format and RNA sequence");
254 // 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
256 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
259 // TODO: use the PDB ID of the structure if one is available, to save bandwidth and avoid uploading the whole structure to the service
260 Object annotate3d = cl.getConstructor(new Class[] {}).newInstance(new Object[] {});
261 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor", new Class[] { FileParse.class}).invoke(annotate3d, new Object[] { new FileParse(getDataName(),type)}));
262 replaceMatchingSeqsWith(rna, al, AlignSeq.DNA);
264 } catch (ClassNotFoundException x)
266 //ignore classnotfounds - occurs in applet
269 private void replaceMatchingSeqsWith(ArrayList<SequenceI> ochains, AlignmentI al, String dnaOrProtein)
271 if (al!=null && al.getHeight()>0)
273 ArrayList<SequenceI> matches=new ArrayList<SequenceI>();
274 ArrayList<AlignSeq> aligns=new ArrayList<AlignSeq>();
276 for (SequenceI sq:ochains)
278 SequenceI bestm=null;
279 AlignSeq bestaseq=null;
281 for (SequenceI msq:al.getSequences())
283 AlignSeq aseq = AlignSeq.doGlobalNWAlignment(msq, sq, dnaOrProtein);
284 if (bestm==null || aseq.getMaxScore()>bestscore)
286 bestscore=aseq.getMaxScore();
291 System.out.println("Best Score for "+(matches.size()+1)+" :"+bestscore);
293 aligns.add(bestaseq);
294 al.deleteSequence(bestm);
296 for (int p=0,pSize=seqs.size();p<pSize;p++)
298 SequenceI sq,sp=seqs.get(p);
300 if ((q=ochains.indexOf(sp))>-1)
302 seqs.set(p, sq=matches.get(q));
303 sq.setName(sp.getName());
304 sq.setDescription(sp.getDescription());
305 sq.transferAnnotation(sp, aligns.get(q).getMappingFromS1(false));
307 for (int ap=0;ap<annotations.size();)
309 if (((AlignmentAnnotation)annotations.get(ap)).sequenceRef==sp) {
314 annotations.remove(ap);
319 if (sq.getAnnotation()!=null) {
320 annotations.addAll(inspos, Arrays.asList(sq.getAnnotation()));
327 * make a friendly ID string.
330 * @return truncated dataName to after last '/'
332 private String safeName(String dataName)
335 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
337 dataName = dataName.substring(p + 1);
342 public void makeResidueList()
344 for (int i = 0; i < chains.size(); i++)
346 ((PDBChain) chains.elementAt(i)).makeResidueList();
350 public void makeCaBondList()
352 for (int i = 0; i < chains.size(); i++)
354 ((PDBChain) chains.elementAt(i)).makeCaBondList();
358 public PDBChain findChain(String id)
360 for (int i = 0; i < chains.size(); i++)
362 if (((PDBChain) chains.elementAt(i)).id.equals(id))
364 return (PDBChain) chains.elementAt(i);
371 public void setChargeColours()
373 for (int i = 0; i < chains.size(); i++)
375 ((PDBChain) chains.elementAt(i)).setChargeColours();
379 public void setColours(jalview.schemes.ColourSchemeI cs)
381 for (int i = 0; i < chains.size(); i++)
383 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
387 public void setChainColours()
389 for (int i = 0; i < chains.size(); i++)
391 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
392 1.0f / (float) i, .4f, 1.0f));
395 public boolean isRNA(SequenceI seqs)
397 for (int i=0;i<seqs.getLength();i++){
398 if((seqs.getCharAt(i)!='A') &&(seqs.getCharAt(i)!='C')&&(seqs.getCharAt(i)!='G')&&(seqs.getCharAt(i)!='U'))