5fd3d10a476afc610935dff9ff56bf6ba3548f92
[jalview.git] / src / MCview / PDBfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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           id = line.substring(62, 67).trim();
74           continue;
75         }
76         // Were we to do anything with SEQRES - we start it here
77         if (line.indexOf("SEQRES") == 0)
78         {
79         }
80
81         if (line.indexOf("MODEL") == 0)
82         {
83           modelFlag = true;
84         }
85
86         if (line.indexOf("TER") == 0)
87         {
88           terFlag = true;
89         }
90
91         if (modelFlag && line.indexOf("ENDMDL") == 0)
92         {
93           break;
94         }
95         if (line.indexOf("ATOM") == 0
96                 || (line.indexOf("HETATM") == 0 && !terFlag))
97         {
98           terFlag = false;
99
100           // Jalview is only interested in CA bonds????
101           if (!line.substring(12, 15).trim().equals("CA"))
102           {
103             continue;
104           }
105
106           Atom tmpatom = new Atom(line);
107           tmpchain = findChain(tmpatom.chain);
108           if (tmpchain != null)
109           {
110             tmpchain.atoms.addElement(tmpatom);
111           }
112           else
113           {
114             tmpchain = new PDBChain(id, tmpatom.chain);
115             chains.addElement(tmpchain);
116             tmpchain.atoms.addElement(tmpatom);
117           }
118         }
119         index++;
120       }
121
122       makeResidueList();
123       makeCaBondList();
124
125       if (id == null)
126       {
127         id = inFile.getName();
128       }
129       for (int i = 0; i < chains.size(); i++)
130       {
131         SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
132         dataset.setName(id + "|" + dataset.getName());
133         PDBEntry entry = new PDBEntry();
134         entry.setId(id);
135         if (inFile != null)
136         {
137           entry.setFile(inFile.getAbsolutePath());
138         }
139         dataset.addPDBId(entry);
140         SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
141                                                         // maintain reference to
142                                                         // dataset
143         seqs.addElement(chainseq);
144         AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
145         if (chainannot != null)
146         {
147           for (int ai = 0; ai < chainannot.length; ai++)
148           {
149             chainannot[ai].visible = VisibleChainAnnotation;
150             annotations.addElement(chainannot[ai]);
151           }
152         }
153       }
154     } catch (OutOfMemoryError er)
155     {
156       System.out.println("OUT OF MEMORY LOADING PDB FILE");
157       throw new IOException("Out of memory loading PDB File");
158     }
159   }
160
161   public void makeResidueList()
162   {
163     for (int i = 0; i < chains.size(); i++)
164     {
165       ((PDBChain) chains.elementAt(i)).makeResidueList();
166     }
167   }
168
169   public void makeCaBondList()
170   {
171     for (int i = 0; i < chains.size(); i++)
172     {
173       ((PDBChain) chains.elementAt(i)).makeCaBondList();
174     }
175   }
176
177   public PDBChain findChain(String id)
178   {
179     for (int i = 0; i < chains.size(); i++)
180     {
181       if (((PDBChain) chains.elementAt(i)).id.equals(id))
182       {
183         return (PDBChain) chains.elementAt(i);
184       }
185     }
186
187     return null;
188   }
189
190   public void setChargeColours()
191   {
192     for (int i = 0; i < chains.size(); i++)
193     {
194       ((PDBChain) chains.elementAt(i)).setChargeColours();
195     }
196   }
197
198   public void setColours(jalview.schemes.ColourSchemeI cs)
199   {
200     for (int i = 0; i < chains.size(); i++)
201     {
202       ((PDBChain) chains.elementAt(i)).setChainColours(cs);
203     }
204   }
205
206   public void setChainColours()
207   {
208     for (int i = 0; i < chains.size(); i++)
209     {
210       ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
211               1.0f / (float) i, .4f, 1.0f));
212     }
213   }
214 }