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