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/>.
26 import jalview.analysis.AlignSeq;
27 import jalview.datamodel.*;
28 import jalview.io.FileParse;
29 import jalview.ws.jws1.Annotate3D;
31 public class PDBfile extends jalview.io.AlignFile
38 * set to true to add chain alignment annotation as visible annotation.
40 boolean VisibleChainAnnotation = false;
42 public PDBfile(String inFile, String inType) throws Exception
44 super(inFile, inType);
47 public PDBfile(FileParse source) throws Exception
57 public void parse() throws Exception
59 // TODO set the filename sensibly - try using data source name.
60 id = safeName(getDataName());
62 chains = new Vector();
63 ArrayList<SequenceI> rna=new ArrayList<SequenceI>(), prot=new ArrayList<SequenceI>();
66 boolean modelFlag = false;
67 boolean terFlag = false;
71 String atomnam = null;
74 while ((line = nextLine()) != null)
76 if (line.indexOf("HEADER") == 0)
78 if (line.length() > 62)
81 if (line.length() > 67)
83 tid = line.substring(62, 67).trim();
87 tid = line.substring(62).trim();
96 // Were we to do anything with SEQRES - we start it here
97 if (line.indexOf("SEQRES") == 0)
101 if (line.indexOf("MODEL") == 0)
106 if (line.indexOf("TER") == 0)
111 if (modelFlag && line.indexOf("ENDMDL") == 0)
115 if (line.indexOf("ATOM") == 0
116 || (line.indexOf("HETATM") == 0 && !terFlag))
120 // Jalview is only interested in CA bonds????
121 atomnam = line.substring(12, 15).trim();
122 if (!atomnam.equals("CA") && !atomnam.equals("P"))
127 Atom tmpatom = new Atom(line);
128 tmpchain = findChain(tmpatom.chain);
129 if (tmpchain != null)
131 if (tmpatom.resNumIns.trim().equals(lastID))
133 // phosphorylated protein - seen both CA and P..
136 tmpchain.atoms.addElement(tmpatom);
140 tmpchain = new PDBChain(id, tmpatom.chain);
141 chains.addElement(tmpchain);
142 tmpchain.atoms.addElement(tmpatom);
144 lastID = tmpatom.resNumIns.trim();
154 id = inFile.getName();
156 for (int i = 0; i < chains.size(); i++)
158 SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
159 dataset.setName(id + "|" + dataset.getName());
160 PDBEntry entry = new PDBEntry();
162 entry.setProperty(new Hashtable());
163 entry.getProperty().put("CHAIN", ((PDBChain)chains.elementAt(i)).id);
166 entry.setFile(inFile.getAbsolutePath());
170 // TODO: decide if we should dump the datasource to disk
171 entry.setFile(getDataName());
173 dataset.addPDBId(entry);
174 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
175 // maintain reference to
177 seqs.addElement(chainseq);
178 if(isRNA(chainseq)==true)
185 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
187 if (chainannot != null)
189 for (int ai = 0; ai < chainannot.length; ai++)
192 chainannot[ai].visible = VisibleChainAnnotation;
193 annotations.addElement(chainannot[ai]);
199 processPdbFileWithAnnotate3d(rna);
200 } catch (Exception x)
202 System.err.println("Exceptions when dealing with RNA in pdb file");
208 processPdbFileWithJmol(prot);
209 } catch (Exception x)
211 System.err.println("Exceptions when dealing with RNA in pdb file");
215 } catch (OutOfMemoryError er)
217 System.out.println("OUT OF MEMORY LOADING PDB FILE");
218 throw new IOException("Out of memory loading PDB File");
219 } catch (NumberFormatException ex)
223 System.err.println("Couldn't read number from line:");
224 System.err.println(line);
228 private void processPdbFileWithJmol(ArrayList<SequenceI> prot) throws Exception
230 // process prot sequence with Jmol to get annotated alignment.
231 // replaceMatchingSeqsWith(prot, al, AlignSeq.PEP);
233 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna) throws Exception {
234 // System.out.println("this is a PDB format and RNA sequence");
235 Annotate3D an3d = new Annotate3D();
236 AlignmentI al = an3d.getRNAMLFor(new FileParse(getDataName(),type));
237 replaceMatchingSeqsWith(rna, al, AlignSeq.DNA);
239 private void replaceMatchingSeqsWith(ArrayList<SequenceI> ochains, AlignmentI al, String dnaOrProtein)
241 if (al!=null && al.getHeight()>0)
243 ArrayList<SequenceI> matches=new ArrayList<SequenceI>();
244 ArrayList<AlignSeq> aligns=new ArrayList<AlignSeq>();
246 for (SequenceI sq:ochains)
248 SequenceI bestm=null;
249 AlignSeq bestaseq=null;
251 for (SequenceI msq:al.getSequences())
253 AlignSeq aseq = AlignSeq.doGlobalNWAlignment(msq, sq, dnaOrProtein);
254 if (bestm==null || aseq.getMaxScore()>bestscore)
256 bestscore=aseq.getMaxScore();
261 System.out.println("Best Score for "+(matches.size()+1)+" :"+bestscore);
263 aligns.add(bestaseq);
264 al.deleteSequence(bestm);
266 for (int p=0,pSize=seqs.size();p<pSize;p++)
268 SequenceI sq,sp=seqs.get(p);
270 if ((q=ochains.indexOf(sp))>-1)
272 seqs.set(p, sq=matches.get(q));
273 sq.setName(sp.getName());
274 sq.setDescription(sp.getDescription());
275 sq.transferAnnotation(sp, aligns.get(q).getMappingFromS1(false));
277 for (int ap=0;ap<annotations.size();)
279 if (((AlignmentAnnotation)annotations.get(ap)).sequenceRef==sp) {
284 annotations.remove(ap);
289 annotations.addAll(inspos, Arrays.asList(sq.getAnnotation()));
295 * make a friendly ID string.
298 * @return truncated dataName to after last '/'
300 private String safeName(String dataName)
303 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
305 dataName = dataName.substring(p + 1);
310 public void makeResidueList()
312 for (int i = 0; i < chains.size(); i++)
314 ((PDBChain) chains.elementAt(i)).makeResidueList();
318 public void makeCaBondList()
320 for (int i = 0; i < chains.size(); i++)
322 ((PDBChain) chains.elementAt(i)).makeCaBondList();
326 public PDBChain findChain(String id)
328 for (int i = 0; i < chains.size(); i++)
330 if (((PDBChain) chains.elementAt(i)).id.equals(id))
332 return (PDBChain) chains.elementAt(i);
339 public void setChargeColours()
341 for (int i = 0; i < chains.size(); i++)
343 ((PDBChain) chains.elementAt(i)).setChargeColours();
347 public void setColours(jalview.schemes.ColourSchemeI cs)
349 for (int i = 0; i < chains.size(); i++)
351 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
355 public void setChainColours()
357 for (int i = 0; i < chains.size(); i++)
359 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
360 1.0f / (float) i, .4f, 1.0f));
363 public boolean isRNA(SequenceI seqs)
365 for (int i=0;i<seqs.getLength();i++){
366 if((seqs.getCharAt(i)!='A') &&(seqs.getCharAt(i)!='C')&&(seqs.getCharAt(i)!='G')&&(seqs.getCharAt(i)!='U'))