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 javax.xml.parsers.ParserConfigurationException;
27 import org.xml.sax.SAXException;
29 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
30 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
31 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
32 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
34 import jalview.analysis.AlignSeq;
35 import jalview.datamodel.*;
36 import jalview.io.AppletFormatAdapter;
37 import jalview.io.FileParse;
38 import jalview.io.RnamlFile;
39 import jalview.ws.jws1.Annotate3D;
41 public class PDBfile extends jalview.io.AlignFile
48 * set to true to add chain alignment annotation as visible annotation.
50 boolean VisibleChainAnnotation = false;
52 public PDBfile(String inFile, String inType) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
54 super(inFile, inType);
57 public PDBfile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
67 public void parse() throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
69 // TODO set the filename sensibly - try using data source name.
70 id = safeName(getDataName());
72 chains = new Vector();
73 ArrayList<SequenceI> rna=new ArrayList<SequenceI>();
76 boolean modelFlag = false;
77 boolean terFlag = false;
81 String atomnam = null;
84 while ((line = nextLine()) != null)
86 if (line.indexOf("HEADER") == 0)
88 if (line.length() > 62)
91 if (line.length() > 67)
93 tid = line.substring(62, 67).trim();
97 tid = line.substring(62).trim();
106 // Were we to do anything with SEQRES - we start it here
107 if (line.indexOf("SEQRES") == 0)
111 if (line.indexOf("MODEL") == 0)
116 if (line.indexOf("TER") == 0)
121 if (modelFlag && line.indexOf("ENDMDL") == 0)
125 if (line.indexOf("ATOM") == 0
126 || (line.indexOf("HETATM") == 0 && !terFlag))
130 // Jalview is only interested in CA bonds????
131 atomnam = line.substring(12, 15).trim();
132 if (!atomnam.equals("CA") && !atomnam.equals("P"))
137 Atom tmpatom = new Atom(line);
138 tmpchain = findChain(tmpatom.chain);
139 if (tmpchain != null)
141 if (tmpatom.resNumIns.trim().equals(lastID))
143 // phosphorylated protein - seen both CA and P..
146 tmpchain.atoms.addElement(tmpatom);
150 tmpchain = new PDBChain(id, tmpatom.chain);
151 chains.addElement(tmpchain);
152 tmpchain.atoms.addElement(tmpatom);
154 lastID = tmpatom.resNumIns.trim();
164 id = inFile.getName();
166 for (int i = 0; i < chains.size(); i++)
168 SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
169 dataset.setName(id + "|" + dataset.getName());
170 PDBEntry entry = new PDBEntry();
172 entry.setProperty(new Hashtable());
173 entry.getProperty().put("CHAIN", ((PDBChain)chains.elementAt(i)).id);
176 entry.setFile(inFile.getAbsolutePath());
180 // TODO: decide if we should dump the datasource to disk
181 entry.setFile(getDataName());
183 dataset.addPDBId(entry);
184 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
185 // maintain reference to
187 seqs.addElement(chainseq);
188 if(isRNA(chainseq)==true)
193 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
195 if (chainannot != null)
197 for (int ai = 0; ai < chainannot.length; ai++)
200 chainannot[ai].visible = VisibleChainAnnotation;
201 annotations.addElement(chainannot[ai]);
207 String path =inFile.getPath();
208 System.out.println("this is a PDB format and RNA sequence");
209 Annotate3D an3d = new Annotate3D();
210 AlignmentI al = an3d.getRNAMLFor(new FileParse(getDataName(),type));
211 if (al!=null && al.getHeight()>0)
213 ArrayList<SequenceI> matches=new ArrayList<SequenceI>();
214 ArrayList<AlignSeq> aligns=new ArrayList<AlignSeq>();
215 for (SequenceI sq:rna)
217 SequenceI bestm=null;
218 AlignSeq bestaseq=null;
220 for (SequenceI msq:al.getSequences())
222 AlignSeq aseq = AlignSeq.doGlobalNWAlignment(msq, sq, AlignSeq.DNA);
223 if (bestm==null || aseq.getMaxScore()>bestscore)
225 bestscore=aseq.getMaxScore();
230 System.out.println("Best Score for "+(matches.size()+1)+" :"+bestscore);
232 aligns.add(bestaseq);
233 al.deleteSequence(bestm);
235 for (int p=0,pSize=seqs.size();p<pSize;p++)
237 SequenceI sq,sp=seqs.get(p);
239 if ((q=rna.indexOf(sp))>-1)
241 seqs.set(p, sq=matches.get(q));
242 sq.setName(sp.getName());
243 sq.setDescription(sp.getDescription());
244 sq.transferAnnotation(sp, aligns.get(q).getMappingFromS1(false));
245 //sq.setSequenceFeatures(sp.getSequenceFeatures());
247 for (int ap=0;ap<annotations.size();)
249 if (((AlignmentAnnotation)annotations.get(ap)).sequenceRef==sp) {
254 annotations.remove(ap);
259 annotations.addAll(inspos, Arrays.asList(sq.getAnnotation()));
263 } catch (Exception x)
265 System.err.println("Exceptions when dealing with RNA in pdb file");
269 } catch (OutOfMemoryError er)
271 System.out.println("OUT OF MEMORY LOADING PDB FILE");
272 throw new IOException("Out of memory loading PDB File");
273 } catch (NumberFormatException ex)
277 System.err.println("Couldn't read number from line:");
278 System.err.println(line);
284 * make a friendly ID string.
287 * @return truncated dataName to after last '/'
289 private String safeName(String dataName)
292 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
294 dataName = dataName.substring(p + 1);
299 public void makeResidueList()
301 for (int i = 0; i < chains.size(); i++)
303 ((PDBChain) chains.elementAt(i)).makeResidueList();
307 public void makeCaBondList()
309 for (int i = 0; i < chains.size(); i++)
311 ((PDBChain) chains.elementAt(i)).makeCaBondList();
315 public PDBChain findChain(String id)
317 for (int i = 0; i < chains.size(); i++)
319 if (((PDBChain) chains.elementAt(i)).id.equals(id))
321 return (PDBChain) chains.elementAt(i);
328 public void setChargeColours()
330 for (int i = 0; i < chains.size(); i++)
332 ((PDBChain) chains.elementAt(i)).setChargeColours();
336 public void setColours(jalview.schemes.ColourSchemeI cs)
338 for (int i = 0; i < chains.size(); i++)
340 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
344 public void setChainColours()
346 for (int i = 0; i < chains.size(); i++)
348 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
349 1.0f / (float) i, .4f, 1.0f));
352 public boolean isRNA(SequenceI seqs)
354 for (int i=0;i<seqs.getLength();i++){
355 if((seqs.getCharAt(i)!='A') &&(seqs.getCharAt(i)!='C')&&(seqs.getCharAt(i)!='G')&&(seqs.getCharAt(i)!='U'))