c671ec2bb67085691c306d579fb2c009ff9aa17b
[jalview.git] / src / MCview / PDBfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 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 import jalview.io.FileParse;
28
29 public class PDBfile extends jalview.io.AlignFile
30 {
31   public Vector chains;
32
33   public String id;
34
35   /**
36    * set to true to add chain alignment annotation as visible annotation.
37    */
38   boolean VisibleChainAnnotation = false;
39
40   public PDBfile(String inFile, String inType) throws IOException
41   {
42     super(inFile, inType);
43   }
44
45   public PDBfile(FileParse source) throws IOException
46   {
47     super(source);
48   }
49
50   public String print()
51   {
52     return null;
53   }
54
55   public void parse() throws IOException
56   {
57     // TODO set the filename sensibly
58     id = (inFile == null) ? "PDBFILE" : inFile.getName();
59     try
60     {
61       chains = new Vector();
62
63       PDBChain tmpchain;
64       String line;
65       boolean modelFlag = false;
66       boolean terFlag = false;
67
68       int index = 0;
69       while ((line = nextLine()) != null)
70       {
71         if (line.indexOf("HEADER") == 0)
72         {
73           if (line.length()>62)
74           {
75             if (line.length()>67) {
76               id = line.substring(62, 67).trim();
77             } else {
78               id=line.substring(62).trim();
79             }
80             continue;
81           }
82         }
83         // Were we to do anything with SEQRES - we start it here
84         if (line.indexOf("SEQRES") == 0)
85         {
86         }
87
88         if (line.indexOf("MODEL") == 0)
89         {
90           modelFlag = true;
91         }
92
93         if (line.indexOf("TER") == 0)
94         {
95           terFlag = true;
96         }
97
98         if (modelFlag && line.indexOf("ENDMDL") == 0)
99         {
100           break;
101         }
102         if (line.indexOf("ATOM") == 0
103                 || (line.indexOf("HETATM") == 0 && !terFlag))
104         {
105           terFlag = false;
106
107           // Jalview is only interested in CA bonds????
108           if (!line.substring(12, 15).trim().equals("CA"))
109           {
110             continue;
111           }
112
113           Atom tmpatom = new Atom(line);
114           tmpchain = findChain(tmpatom.chain);
115           if (tmpchain != null)
116           {
117             tmpchain.atoms.addElement(tmpatom);
118           }
119           else
120           {
121             tmpchain = new PDBChain(id, tmpatom.chain);
122             chains.addElement(tmpchain);
123             tmpchain.atoms.addElement(tmpatom);
124           }
125         }
126         index++;
127       }
128
129       makeResidueList();
130       makeCaBondList();
131
132       if (id == null)
133       {
134         id = inFile.getName();
135       }
136       for (int i = 0; i < chains.size(); i++)
137       {
138         SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
139         dataset.setName(id + "|" + dataset.getName());
140         PDBEntry entry = new PDBEntry();
141         entry.setId(id);
142         if (inFile != null)
143         {
144           entry.setFile(inFile.getAbsolutePath());
145         }
146         dataset.addPDBId(entry);
147         SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
148                                                         // maintain reference to
149                                                         // dataset
150         seqs.addElement(chainseq);
151         AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
152         if (chainannot != null)
153         {
154           for (int ai = 0; ai < chainannot.length; ai++)
155           {
156             chainannot[ai].visible = VisibleChainAnnotation;
157             annotations.addElement(chainannot[ai]);
158           }
159         }
160       }
161     } catch (OutOfMemoryError er)
162     {
163       System.out.println("OUT OF MEMORY LOADING PDB FILE");
164       throw new IOException("Out of memory loading PDB File");
165     }
166   }
167
168   public void makeResidueList()
169   {
170     for (int i = 0; i < chains.size(); i++)
171     {
172       ((PDBChain) chains.elementAt(i)).makeResidueList();
173     }
174   }
175
176   public void makeCaBondList()
177   {
178     for (int i = 0; i < chains.size(); i++)
179     {
180       ((PDBChain) chains.elementAt(i)).makeCaBondList();
181     }
182   }
183
184   public PDBChain findChain(String id)
185   {
186     for (int i = 0; i < chains.size(); i++)
187     {
188       if (((PDBChain) chains.elementAt(i)).id.equals(id))
189       {
190         return (PDBChain) chains.elementAt(i);
191       }
192     }
193
194     return null;
195   }
196
197   public void setChargeColours()
198   {
199     for (int i = 0; i < chains.size(); i++)
200     {
201       ((PDBChain) chains.elementAt(i)).setChargeColours();
202     }
203   }
204
205   public void setColours(jalview.schemes.ColourSchemeI cs)
206   {
207     for (int i = 0; i < chains.size(); i++)
208     {
209       ((PDBChain) chains.elementAt(i)).setChainColours(cs);
210     }
211   }
212
213   public void setChainColours()
214   {
215     for (int i = 0; i < chains.size(); i++)
216     {
217       ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
218               1.0f / (float) i, .4f, 1.0f));
219     }
220   }
221 }