v2
[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            System.out.println("this is a PDB format and RNA sequence");
186            Annotate3D an3d = new Annotate3D(id);
187            System.out.println(id);
188            //BufferedWriter r = an3d.getReader();
189            
190            BufferedReader in = new BufferedReader(new FileReader("temp.rnaml"));
191            
192            String str;
193            while ((str = in.readLine()) != null) {
194                      System.out.println(str);
195                      System.out.println("toto");
196
197                      }
198            String type = "File";
199            RnamlFile rnaml =new RnamlFile("temp.rnaml",type);
200            System.out.println("Create rnamfile object");
201            //rnaml.parse("temp");
202            this.annotations =rnaml.getAnnot();
203            
204        }
205         
206         AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
207         
208         if (chainannot != null)
209         {
210           for (int ai = 0; ai < chainannot.length; ai++)
211           {
212         
213             chainannot[ai].visible = VisibleChainAnnotation;
214             annotations.addElement(chainannot[ai]);
215           }
216         }
217       }
218     } catch (OutOfMemoryError er)
219     {
220       System.out.println("OUT OF MEMORY LOADING PDB FILE");
221       throw new IOException("Out of memory loading PDB File");
222     }
223     catch (NumberFormatException ex)
224     {
225       if (line!=null) {
226         System.err.println("Couldn't read number from line:");
227         System.err.println(line);
228       }
229     }
230   }
231
232   /**
233    * make a friendly ID string.
234    * 
235    * @param dataName
236    * @return truncated dataName to after last '/'
237    */
238   private String safeName(String dataName)
239   {
240     int p = 0;
241     while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
242     {
243       dataName = dataName.substring(p + 1);
244     }
245     return dataName;
246   }
247
248   public void makeResidueList()
249   {
250     for (int i = 0; i < chains.size(); i++)
251     {
252       ((PDBChain) chains.elementAt(i)).makeResidueList();
253     }
254   }
255
256   public void makeCaBondList()
257   {
258     for (int i = 0; i < chains.size(); i++)
259     {
260       ((PDBChain) chains.elementAt(i)).makeCaBondList();
261     }
262   }
263
264   public PDBChain findChain(String id)
265   {
266     for (int i = 0; i < chains.size(); i++)
267     {
268       if (((PDBChain) chains.elementAt(i)).id.equals(id))
269       {
270         return (PDBChain) chains.elementAt(i);
271       }
272     }
273
274     return null;
275   }
276
277   public void setChargeColours()
278   {
279     for (int i = 0; i < chains.size(); i++)
280     {
281       ((PDBChain) chains.elementAt(i)).setChargeColours();
282     }
283   }
284
285   public void setColours(jalview.schemes.ColourSchemeI cs)
286   {
287     for (int i = 0; i < chains.size(); i++)
288     {
289       ((PDBChain) chains.elementAt(i)).setChainColours(cs);
290     }
291   }
292
293   public void setChainColours()
294   {
295     for (int i = 0; i < chains.size(); i++)
296     {
297       ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
298               1.0f / (float) i, .4f, 1.0f));
299     }
300   }
301   public boolean isRNA(SequenceI seqs)
302   {
303           for (int i=0;i<seqs.getLength();i++){
304                   if((seqs.getCharAt(i)!='A') &&(seqs.getCharAt(i)!='C')&&(seqs.getCharAt(i)!='G')&&(seqs.getCharAt(i)!='U'))
305                   {
306                           return false;
307                   }
308           }
309          
310                   return true;
311           
312           
313   }
314 }