048f63925641ca44aec9fa0a6ecba89ea1395cb7
[jalview.git] / src / MCview / PDBfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  * 
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.
15  * 
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  */
18 package MCview;
19
20 import java.io.*;
21 import java.util.*;
22
23 import java.awt.*;
24
25 import javax.xml.parsers.ParserConfigurationException;
26
27 import org.xml.sax.SAXException;
28
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
33 import jalview.datamodel.*;
34 import jalview.io.FileParse;
35 import jalview.io.RnamlFile;
36 import jalview.ws.jws1.Annotate3D;
37
38 public class PDBfile extends jalview.io.AlignFile
39 {
40   public Vector chains;
41
42   public String id;
43
44   /**
45    * set to true to add chain alignment annotation as visible annotation.
46    */
47   boolean VisibleChainAnnotation = false;
48
49   public PDBfile(String inFile, String inType) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
50   {
51     super(inFile, inType);
52   }
53
54   public PDBfile(FileParse source) throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
55   {
56     super(source);
57   }
58
59   public String print()
60   {
61     return null;
62   }
63
64   public void parse() throws IOException, ExceptionFileFormatOrSyntax, ParserConfigurationException, SAXException, ExceptionPermissionDenied, ExceptionLoadingFailed, InterruptedException
65   {
66     // TODO set the filename sensibly - try using data source name.
67     id = safeName(getDataName());
68   
69       chains = new Vector();
70
71       PDBChain tmpchain;
72       String line=null;
73       boolean modelFlag = false;
74       boolean terFlag = false;
75       String lastID = "";
76
77       int index = 0;
78       String atomnam = null;
79       try
80       {
81       while ((line = nextLine()) != null)
82       {
83         if (line.indexOf("HEADER") == 0)
84         {
85           if (line.length() > 62)
86           {
87             String tid;
88             if (line.length() > 67)
89             {
90               tid = line.substring(62, 67).trim();
91             }
92             else
93             {
94               tid = line.substring(62).trim();
95             }
96             if (tid.length() > 0)
97             {
98               id = tid;
99             }
100             continue;
101           }
102         }
103         // Were we to do anything with SEQRES - we start it here
104         if (line.indexOf("SEQRES") == 0)
105         {
106         }
107
108         if (line.indexOf("MODEL") == 0)
109         {
110           modelFlag = true;
111         }
112
113         if (line.indexOf("TER") == 0)
114         {
115           terFlag = true;
116         }
117
118         if (modelFlag && line.indexOf("ENDMDL") == 0)
119         {
120           break;
121         }
122         if (line.indexOf("ATOM") == 0
123                 || (line.indexOf("HETATM") == 0 && !terFlag))
124         {
125           terFlag = false;
126
127           // Jalview is only interested in CA bonds????
128           atomnam = line.substring(12, 15).trim();
129           if (!atomnam.equals("CA") && !atomnam.equals("P"))
130           {
131             continue;
132           }
133
134           Atom tmpatom = new Atom(line);
135           tmpchain = findChain(tmpatom.chain);
136           if (tmpchain != null)
137           {
138             if (tmpatom.resNumIns.trim().equals(lastID))
139             {
140               // phosphorylated protein - seen both CA and P..
141               continue;
142             }
143             tmpchain.atoms.addElement(tmpatom);
144           }
145           else
146           {
147             tmpchain = new PDBChain(id, tmpatom.chain);
148             chains.addElement(tmpchain);
149             tmpchain.atoms.addElement(tmpatom);
150           }
151           lastID = tmpatom.resNumIns.trim();
152         }
153         index++;
154       }
155
156       makeResidueList();
157       makeCaBondList();
158
159       if (id == null)
160       {
161         id = inFile.getName();
162       }
163       for (int i = 0; i < chains.size(); i++)
164       {
165         SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
166         dataset.setName(id + "|" + dataset.getName());
167         PDBEntry entry = new PDBEntry();
168         entry.setId(id);
169         if (inFile != null)
170         {
171           entry.setFile(inFile.getAbsolutePath());
172         }
173         else
174         {
175           // TODO: decide if we should dump the datasource to disk
176           entry.setFile(getDataName());
177         }
178         dataset.addPDBId(entry);
179         SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
180         // maintain reference to
181         // dataset
182         seqs.addElement(chainseq);
183        if(isRNA(chainseq)==true)
184        {
185            String path =inFile.getPath();
186            System.out.println("this is a PDB format and RNA sequence");
187            Annotate3D an3d = new Annotate3D(path);
188            System.out.println(id);
189            //BufferedWriter r = an3d.getReader();
190            
191           // BufferedReader in = new BufferedReader(new FileReader("temp.rnaml"));
192            
193            //String str;
194           // while ((str = in.readLine()) != null) {
195                     // System.out.println(str);
196                     // System.out.println("toto");
197
198                     // }
199            //String type = "File";
200            //RnamlFile rnaml =new RnamlFile("temp.rnaml",type);
201            System.out.println("Create rnamfile object");
202            //rnaml.parse("temp");
203            //this.annotations =rnaml.getAnnot();
204            
205        }
206         
207         AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
208         
209         if (chainannot != null)
210         {
211           for (int ai = 0; ai < chainannot.length; ai++)
212           {
213         
214             chainannot[ai].visible = VisibleChainAnnotation;
215             annotations.addElement(chainannot[ai]);
216           }
217         }
218       }
219     } catch (OutOfMemoryError er)
220     {
221       System.out.println("OUT OF MEMORY LOADING PDB FILE");
222       throw new IOException("Out of memory loading PDB File");
223     }
224     catch (NumberFormatException ex)
225     {
226       if (line!=null) {
227         System.err.println("Couldn't read number from line:");
228         System.err.println(line);
229       }
230     }
231   }
232
233   /**
234    * make a friendly ID string.
235    * 
236    * @param dataName
237    * @return truncated dataName to after last '/'
238    */
239   private String safeName(String dataName)
240   {
241     int p = 0;
242     while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
243     {
244       dataName = dataName.substring(p + 1);
245     }
246     return dataName;
247   }
248
249   public void makeResidueList()
250   {
251     for (int i = 0; i < chains.size(); i++)
252     {
253       ((PDBChain) chains.elementAt(i)).makeResidueList();
254     }
255   }
256
257   public void makeCaBondList()
258   {
259     for (int i = 0; i < chains.size(); i++)
260     {
261       ((PDBChain) chains.elementAt(i)).makeCaBondList();
262     }
263   }
264
265   public PDBChain findChain(String id)
266   {
267     for (int i = 0; i < chains.size(); i++)
268     {
269       if (((PDBChain) chains.elementAt(i)).id.equals(id))
270       {
271         return (PDBChain) chains.elementAt(i);
272       }
273     }
274
275     return null;
276   }
277
278   public void setChargeColours()
279   {
280     for (int i = 0; i < chains.size(); i++)
281     {
282       ((PDBChain) chains.elementAt(i)).setChargeColours();
283     }
284   }
285
286   public void setColours(jalview.schemes.ColourSchemeI cs)
287   {
288     for (int i = 0; i < chains.size(); i++)
289     {
290       ((PDBChain) chains.elementAt(i)).setChainColours(cs);
291     }
292   }
293
294   public void setChainColours()
295   {
296     for (int i = 0; i < chains.size(); i++)
297     {
298       ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
299               1.0f / (float) i, .4f, 1.0f));
300     }
301   }
302   public boolean isRNA(SequenceI seqs)
303   {
304           for (int i=0;i<seqs.getLength();i++){
305                   if((seqs.getCharAt(i)!='A') &&(seqs.getCharAt(i)!='C')&&(seqs.getCharAt(i)!='G')&&(seqs.getCharAt(i)!='U'))
306                   {
307                           return false;
308                   }
309           }
310          
311                   return true;
312           
313           
314   }
315 }