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