fix modeller generated PDB file reading bug (missing HEADER line with PDB code)
[jalview.git] / src / MCview / PDBfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package MCview;
20
21 import java.io.*;
22 import java.util.*;
23
24 import java.awt.*;
25
26 import jalview.datamodel.*;
27
28 public class PDBfile
29     extends jalview.io.AlignFile
30 {
31   public Vector chains;
32   public String id;
33
34   /**
35    * set to true to add chain alignment annotation as visible annotation.
36    */
37   boolean VisibleChainAnnotation=false;
38   public PDBfile(String inFile, String inType)
39       throws IOException
40   {
41     super(inFile, inType);
42   }
43
44   public String print()
45   {
46     return null;
47   }
48
49   public void parse()
50       throws IOException
51   {
52     // TODO set the filename sensibly 
53     id = (inFile==null) ? "PDBFILE" : inFile.getName();
54     try
55     {
56       chains = new Vector();
57
58       PDBChain tmpchain;
59       String line;
60       boolean modelFlag = false;
61       boolean terFlag = false;
62
63       int index = 0;
64       while ( (line = nextLine()) != null)
65       {
66         if (line.indexOf("HEADER") == 0)
67         {
68           id = line.substring(62, 67).trim();
69           continue;
70         }
71         // Were we to do anything with SEQRES - we start it here
72         if (line.indexOf("SEQRES") == 0)
73         {
74         }
75
76         if (line.indexOf("MODEL") == 0)
77         {
78           modelFlag = true;
79         }
80
81         if (line.indexOf("TER") == 0)
82         {
83           terFlag = true;
84         }
85
86         if (modelFlag && line.indexOf("ENDMDL") == 0)
87         {
88           break;
89         }
90         if (line.indexOf("ATOM") == 0
91             || (line.indexOf("HETATM") == 0 && !terFlag)
92             )
93         {
94           terFlag = false;
95
96           //Jalview is only interested in CA bonds????
97           if (!line.substring(12, 15).trim().equals("CA"))
98           {
99             continue;
100           }
101
102           Atom tmpatom = new Atom(line);
103           tmpchain = findChain(tmpatom.chain);
104           if (tmpchain != null)
105           {
106             tmpchain.atoms.addElement(tmpatom);
107           }
108           else
109           {
110             tmpchain = new PDBChain(id, tmpatom.chain);
111             chains.addElement(tmpchain);
112             tmpchain.atoms.addElement(tmpatom);
113           }
114         }
115         index++;
116       }
117
118       makeResidueList();
119       makeCaBondList();
120
121       if (id == null)
122       {
123         id = inFile.getName();
124       }
125       for (int i = 0; i < chains.size(); i++)
126       {
127         SequenceI dataset = ( (PDBChain) chains.elementAt(i)).
128             sequence;
129         dataset.setName(id + "|" + dataset.getName());
130         PDBEntry entry = new PDBEntry();
131         entry.setId(id);
132         if (inFile != null)
133         {
134           entry.setFile(inFile.getAbsolutePath());
135         }
136         dataset.addPDBId(entry);
137         SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects maintain reference to dataset
138         seqs.addElement(chainseq);
139         AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
140         if (chainannot!=null)
141         {
142           for (int ai=0; ai<chainannot.length; ai++)
143           {
144             chainannot[ai].visible=VisibleChainAnnotation;
145             annotations.addElement(chainannot[ai]);
146           }
147         }
148       }
149     }
150     catch (OutOfMemoryError er)
151     {
152       System.out.println("OUT OF MEMORY LOADING PDB FILE");
153       throw new IOException("Out of memory loading PDB File");
154     }
155   }
156
157   public void makeResidueList()
158   {
159     for (int i = 0; i < chains.size(); i++)
160     {
161       ( (PDBChain) chains.elementAt(i)).makeResidueList();
162     }
163   }
164
165   public void makeCaBondList()
166   {
167     for (int i = 0; i < chains.size(); i++)
168     {
169       ( (PDBChain) chains.elementAt(i)).makeCaBondList();
170     }
171   }
172
173   public PDBChain findChain(String id)
174   {
175     for (int i = 0; i < chains.size(); i++)
176     {
177       if ( ( (PDBChain) chains.elementAt(i)).id.equals(id))
178       {
179         return (PDBChain) chains.elementAt(i);
180       }
181     }
182
183     return null;
184   }
185
186   public void setChargeColours()
187   {
188     for (int i = 0; i < chains.size(); i++)
189     {
190       ( (PDBChain) chains.elementAt(i)).setChargeColours();
191     }
192   }
193
194   public void setColours(jalview.schemes.ColourSchemeI cs)
195   {
196     for (int i = 0; i < chains.size(); i++)
197     {
198       ( (PDBChain) chains.elementAt(i)).setChainColours(cs);
199     }
200   }
201
202   public void setChainColours()
203   {
204     for (int i = 0; i < chains.size(); i++)
205     {
206       ( (PDBChain) chains.elementAt(i)).setChainColours(
207           Color.getHSBColor(1.0f / (float) i, .4f, 1.0f)
208           );
209     }
210   }
211 }