2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
25 import jalview.analysis.AlignSeq;
26 import jalview.datamodel.*;
27 import jalview.io.FileParse;
29 public class PDBfile extends jalview.io.AlignFile
36 * set to true to add chain alignment annotation as visible annotation.
38 boolean VisibleChainAnnotation = false;
40 public PDBfile(String inFile, String inType) throws IOException
42 super(inFile, inType);
45 public PDBfile(FileParse source) throws IOException
55 public void parse() throws IOException
57 // TODO set the filename sensibly - try using data source name.
58 id = safeName(getDataName());
60 chains = new Vector();
61 ArrayList<SequenceI> rna=new ArrayList<SequenceI>(), prot=new ArrayList<SequenceI>();
64 boolean modelFlag = false;
65 boolean terFlag = false;
69 String atomnam = null;
72 while ((line = nextLine()) != null)
74 if (line.indexOf("HEADER") == 0)
76 if (line.length() > 62)
79 if (line.length() > 67)
81 tid = line.substring(62, 67).trim();
85 tid = line.substring(62).trim();
94 // Were we to do anything with SEQRES - we start it here
95 if (line.indexOf("SEQRES") == 0)
99 if (line.indexOf("MODEL") == 0)
104 if (line.indexOf("TER") == 0)
109 if (modelFlag && line.indexOf("ENDMDL") == 0)
113 if (line.indexOf("ATOM") == 0
114 || (line.indexOf("HETATM") == 0 && !terFlag))
118 // Jalview is only interested in CA bonds????
119 atomnam = line.substring(12, 15).trim();
120 if (!atomnam.equals("CA") && !atomnam.equals("P"))
125 Atom tmpatom = new Atom(line);
126 tmpchain = findChain(tmpatom.chain);
127 if (tmpchain != null)
129 if (tmpatom.resNumIns.trim().equals(lastID))
131 // phosphorylated protein - seen both CA and P..
134 tmpchain.atoms.addElement(tmpatom);
138 tmpchain = new PDBChain(id, tmpatom.chain);
139 chains.addElement(tmpchain);
140 tmpchain.atoms.addElement(tmpatom);
142 lastID = tmpatom.resNumIns.trim();
152 id = inFile.getName();
154 for (int i = 0; i < chains.size(); i++)
156 SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
157 dataset.setName(id + "|" + dataset.getName());
158 PDBEntry entry = new PDBEntry();
160 entry.setProperty(new Hashtable());
161 entry.getProperty().put("CHAIN", ((PDBChain)chains.elementAt(i)).id);
164 entry.setFile(inFile.getAbsolutePath());
168 // TODO: decide if we should dump the datasource to disk
169 entry.setFile(getDataName());
171 dataset.addPDBId(entry);
172 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
173 // maintain reference to
175 seqs.addElement(chainseq);
176 if(isRNA(chainseq)==true)
183 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
185 if (chainannot != null)
187 for (int ai = 0; ai < chainannot.length; ai++)
190 chainannot[ai].visible = VisibleChainAnnotation;
191 annotations.addElement(chainannot[ai]);
197 processPdbFileWithAnnotate3d(rna);
198 } catch (Exception x)
200 System.err.println("Exceptions when dealing with RNA in pdb file");
206 processPdbFileWithJmol(prot);
207 } catch (Exception x)
209 System.err.println("Exceptions when dealing with RNA in pdb file");
213 } catch (OutOfMemoryError er)
215 System.out.println("OUT OF MEMORY LOADING PDB FILE");
216 throw new IOException("Out of memory loading PDB File");
217 } catch (NumberFormatException ex)
221 System.err.println("Couldn't read number from line:");
222 System.err.println(line);
226 private void processPdbFileWithJmol(ArrayList<SequenceI> prot) throws Exception
228 // process prot sequence with Jmol to get annotated alignment.
229 // replaceMatchingSeqsWith(prot, al, AlignSeq.PEP);
231 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna) throws Exception {
232 // System.out.println("this is a PDB format and RNA sequence");
233 // 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
235 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
238 // TODO: use the PDB ID of the structure if one is available, to save bandwidth and avoid uploading the whole structure to the service
239 Object annotate3d = cl.getConstructor(new Class[] {}).newInstance(new Object[] {});
240 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor", new Class[] { FileParse.class}).invoke(annotate3d, new Object[] { new FileParse(getDataName(),type)}));
241 replaceMatchingSeqsWith(rna, al, AlignSeq.DNA);
243 } catch (ClassNotFoundException x)
245 //ignore classnotfounds - occurs in applet
248 private void replaceMatchingSeqsWith(ArrayList<SequenceI> ochains, AlignmentI al, String dnaOrProtein)
250 if (al!=null && al.getHeight()>0)
252 ArrayList<SequenceI> matches=new ArrayList<SequenceI>();
253 ArrayList<AlignSeq> aligns=new ArrayList<AlignSeq>();
255 for (SequenceI sq:ochains)
257 SequenceI bestm=null;
258 AlignSeq bestaseq=null;
260 for (SequenceI msq:al.getSequences())
262 AlignSeq aseq = AlignSeq.doGlobalNWAlignment(msq, sq, dnaOrProtein);
263 if (bestm==null || aseq.getMaxScore()>bestscore)
265 bestscore=aseq.getMaxScore();
270 System.out.println("Best Score for "+(matches.size()+1)+" :"+bestscore);
272 aligns.add(bestaseq);
273 al.deleteSequence(bestm);
275 for (int p=0,pSize=seqs.size();p<pSize;p++)
277 SequenceI sq,sp=seqs.get(p);
279 if ((q=ochains.indexOf(sp))>-1)
281 seqs.set(p, sq=matches.get(q));
282 sq.setName(sp.getName());
283 sq.setDescription(sp.getDescription());
284 sq.transferAnnotation(sp, aligns.get(q).getMappingFromS1(false));
286 for (int ap=0;ap<annotations.size();)
288 if (((AlignmentAnnotation)annotations.get(ap)).sequenceRef==sp) {
293 annotations.remove(ap);
298 annotations.addAll(inspos, Arrays.asList(sq.getAnnotation()));
304 * make a friendly ID string.
307 * @return truncated dataName to after last '/'
309 private String safeName(String dataName)
312 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
314 dataName = dataName.substring(p + 1);
319 public void makeResidueList()
321 for (int i = 0; i < chains.size(); i++)
323 ((PDBChain) chains.elementAt(i)).makeResidueList();
327 public void makeCaBondList()
329 for (int i = 0; i < chains.size(); i++)
331 ((PDBChain) chains.elementAt(i)).makeCaBondList();
335 public PDBChain findChain(String id)
337 for (int i = 0; i < chains.size(); i++)
339 if (((PDBChain) chains.elementAt(i)).id.equals(id))
341 return (PDBChain) chains.elementAt(i);
348 public void setChargeColours()
350 for (int i = 0; i < chains.size(); i++)
352 ((PDBChain) chains.elementAt(i)).setChargeColours();
356 public void setColours(jalview.schemes.ColourSchemeI cs)
358 for (int i = 0; i < chains.size(); i++)
360 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
364 public void setChainColours()
366 for (int i = 0; i < chains.size(); i++)
368 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
369 1.0f / (float) i, .4f, 1.0f));
372 public boolean isRNA(SequenceI seqs)
374 for (int i=0;i<seqs.getLength();i++){
375 if((seqs.getCharAt(i)!='A') &&(seqs.getCharAt(i)!='C')&&(seqs.getCharAt(i)!='G')&&(seqs.getCharAt(i)!='U'))