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.FileParse;
37 import jalview.ws.jws1.Annotate3D;
39 public class PDBfile extends jalview.io.AlignFile
46 * set to true to add chain alignment annotation as visible annotation.
48 boolean VisibleChainAnnotation = false;
50 public PDBfile(String inFile, String inType) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
52 super(inFile, inType);
55 public PDBfile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException, ExceptionUnmatchedClosingParentheses
65 public void parse() throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
67 // TODO set the filename sensibly - try using data source name.
68 id = safeName(getDataName());
70 chains = new Vector();
71 ArrayList<SequenceI> rna=new ArrayList<SequenceI>();
74 boolean modelFlag = false;
75 boolean terFlag = false;
79 String atomnam = null;
82 while ((line = nextLine()) != null)
84 if (line.indexOf("HEADER") == 0)
86 if (line.length() > 62)
89 if (line.length() > 67)
91 tid = line.substring(62, 67).trim();
95 tid = line.substring(62).trim();
104 // Were we to do anything with SEQRES - we start it here
105 if (line.indexOf("SEQRES") == 0)
109 if (line.indexOf("MODEL") == 0)
114 if (line.indexOf("TER") == 0)
119 if (modelFlag && line.indexOf("ENDMDL") == 0)
123 if (line.indexOf("ATOM") == 0
124 || (line.indexOf("HETATM") == 0 && !terFlag))
128 // Jalview is only interested in CA bonds????
129 atomnam = line.substring(12, 15).trim();
130 if (!atomnam.equals("CA") && !atomnam.equals("P"))
135 Atom tmpatom = new Atom(line);
136 tmpchain = findChain(tmpatom.chain);
137 if (tmpchain != null)
139 if (tmpatom.resNumIns.trim().equals(lastID))
141 // phosphorylated protein - seen both CA and P..
144 tmpchain.atoms.addElement(tmpatom);
148 tmpchain = new PDBChain(id, tmpatom.chain);
149 chains.addElement(tmpchain);
150 tmpchain.atoms.addElement(tmpatom);
152 lastID = tmpatom.resNumIns.trim();
162 id = inFile.getName();
164 for (int i = 0; i < chains.size(); i++)
166 SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
167 dataset.setName(id + "|" + dataset.getName());
168 PDBEntry entry = new PDBEntry();
170 entry.setProperty(new Hashtable());
171 entry.getProperty().put("CHAIN", ((PDBChain)chains.elementAt(i)).id);
174 entry.setFile(inFile.getAbsolutePath());
178 // TODO: decide if we should dump the datasource to disk
179 entry.setFile(getDataName());
181 dataset.addPDBId(entry);
182 SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
183 // maintain reference to
185 seqs.addElement(chainseq);
186 if(isRNA(chainseq)==true)
191 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
193 if (chainannot != null)
195 for (int ai = 0; ai < chainannot.length; ai++)
198 chainannot[ai].visible = VisibleChainAnnotation;
199 annotations.addElement(chainannot[ai]);
205 processPdbFileWithAnnotate3d(rna);
206 } catch (Exception x)
208 System.err.println("Exceptions when dealing with RNA in pdb file");
212 } catch (OutOfMemoryError er)
214 System.out.println("OUT OF MEMORY LOADING PDB FILE");
215 throw new IOException("Out of memory loading PDB File");
216 } catch (NumberFormatException ex)
220 System.err.println("Couldn't read number from line:");
221 System.err.println(line);
226 private void processPdbFileWithAnnotate3d(ArrayList<SequenceI> rna) throws Exception {
227 // System.out.println("this is a PDB format and RNA sequence");
228 Annotate3D an3d = new Annotate3D();
229 AlignmentI al = an3d.getRNAMLFor(new FileParse(getDataName(),type));
230 replaceMatchingSeqsWith(rna, al, AlignSeq.DNA);
232 private void replaceMatchingSeqsWith(ArrayList<SequenceI> ochains, AlignmentI al, String dnaOrProtein)
234 if (al!=null && al.getHeight()>0)
236 ArrayList<SequenceI> matches=new ArrayList<SequenceI>();
237 ArrayList<AlignSeq> aligns=new ArrayList<AlignSeq>();
239 for (SequenceI sq:ochains)
241 SequenceI bestm=null;
242 AlignSeq bestaseq=null;
244 for (SequenceI msq:al.getSequences())
246 AlignSeq aseq = AlignSeq.doGlobalNWAlignment(msq, sq, dnaOrProtein);
247 if (bestm==null || aseq.getMaxScore()>bestscore)
249 bestscore=aseq.getMaxScore();
254 System.out.println("Best Score for "+(matches.size()+1)+" :"+bestscore);
256 aligns.add(bestaseq);
257 al.deleteSequence(bestm);
259 for (int p=0,pSize=seqs.size();p<pSize;p++)
261 SequenceI sq,sp=seqs.get(p);
263 if ((q=ochains.indexOf(sp))>-1)
265 seqs.set(p, sq=matches.get(q));
266 sq.setName(sp.getName());
267 sq.setDescription(sp.getDescription());
268 sq.transferAnnotation(sp, aligns.get(q).getMappingFromS1(false));
270 for (int ap=0;ap<annotations.size();)
272 if (((AlignmentAnnotation)annotations.get(ap)).sequenceRef==sp) {
277 annotations.remove(ap);
282 annotations.addAll(inspos, Arrays.asList(sq.getAnnotation()));
288 * make a friendly ID string.
291 * @return truncated dataName to after last '/'
293 private String safeName(String dataName)
296 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
298 dataName = dataName.substring(p + 1);
303 public void makeResidueList()
305 for (int i = 0; i < chains.size(); i++)
307 ((PDBChain) chains.elementAt(i)).makeResidueList();
311 public void makeCaBondList()
313 for (int i = 0; i < chains.size(); i++)
315 ((PDBChain) chains.elementAt(i)).makeCaBondList();
319 public PDBChain findChain(String id)
321 for (int i = 0; i < chains.size(); i++)
323 if (((PDBChain) chains.elementAt(i)).id.equals(id))
325 return (PDBChain) chains.elementAt(i);
332 public void setChargeColours()
334 for (int i = 0; i < chains.size(); i++)
336 ((PDBChain) chains.elementAt(i)).setChargeColours();
340 public void setColours(jalview.schemes.ColourSchemeI cs)
342 for (int i = 0; i < chains.size(); i++)
344 ((PDBChain) chains.elementAt(i)).setChainColours(cs);
348 public void setChainColours()
350 for (int i = 0; i < chains.size(); i++)
352 ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
353 1.0f / (float) i, .4f, 1.0f));
356 public boolean isRNA(SequenceI seqs)
358 for (int i=0;i<seqs.getLength();i++){
359 if((seqs.getCharAt(i)!='A') &&(seqs.getCharAt(i)!='C')&&(seqs.getCharAt(i)!='G')&&(seqs.getCharAt(i)!='U'))