formatting
[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   public PDBfile(String inFile, String inType)
35       throws IOException
36   {
37     super(inFile, inType);
38   }
39
40   public String print()
41   {
42     return null;
43   }
44
45   public void parse()
46       throws IOException
47   {
48     try
49     {
50       chains = new Vector();
51
52       PDBChain tmpchain;
53       String line;
54       boolean modelFlag = false;
55       boolean terFlag = false;
56
57       int index = 0;
58       while ( (line = nextLine()) != null)
59       {
60         if (line.indexOf("HEADER") == 0)
61         {
62           id = line.substring(62, 67).trim();
63           continue;
64         }
65         // Were we to do anything with SEQRES - we start it here
66         if (line.indexOf("SEQRES") == 0)
67         {
68         }
69
70         if (line.indexOf("MODEL") == 0)
71         {
72           modelFlag = true;
73         }
74
75         if (line.indexOf("TER") == 0)
76         {
77           terFlag = true;
78         }
79
80         if (modelFlag && line.indexOf("ENDMDL") == 0)
81         {
82           break;
83         }
84         if (line.indexOf("ATOM") == 0
85             || (line.indexOf("HETATM") == 0 && !terFlag)
86             )
87         {
88           terFlag = false;
89
90           //Jalview is only interested in CA bonds????
91           if (!line.substring(12, 15).trim().equals("CA"))
92           {
93             continue;
94           }
95
96           Atom tmpatom = new Atom(line);
97           tmpchain = findChain(tmpatom.chain);
98           if (tmpchain != null)
99           {
100             tmpchain.atoms.addElement(tmpatom);
101           }
102           else
103           {
104             tmpchain = new PDBChain(id, tmpatom.chain);
105             chains.addElement(tmpchain);
106             tmpchain.atoms.addElement(tmpatom);
107           }
108         }
109         index++;
110       }
111
112       makeResidueList();
113       makeCaBondList();
114
115       if (id == null)
116       {
117         id = inFile.getName();
118       }
119       for (int i = 0; i < chains.size(); i++)
120       {
121         SequenceI dataset = ( (PDBChain) chains.elementAt(i)).
122             sequence;
123         dataset.setName(id + "|" + dataset.getName());
124         PDBEntry entry = new PDBEntry();
125         entry.setId(id);
126         if (inFile != null)
127         {
128           entry.setFile(inFile.getAbsolutePath());
129         }
130         dataset.addPDBId(entry);
131         getSeqs().addElement(dataset.deriveSequence()); // PDBChain objects maintain reference to dataset
132       }
133     }
134     catch (OutOfMemoryError er)
135     {
136       System.out.println("OUT OF MEMORY LOADING PDB FILE");
137       throw new IOException("Out of memory loading PDB File");
138     }
139   }
140
141   public void makeResidueList()
142   {
143     for (int i = 0; i < chains.size(); i++)
144     {
145       ( (PDBChain) chains.elementAt(i)).makeResidueList();
146     }
147   }
148
149   public void makeCaBondList()
150   {
151     for (int i = 0; i < chains.size(); i++)
152     {
153       ( (PDBChain) chains.elementAt(i)).makeCaBondList();
154     }
155   }
156
157   public PDBChain findChain(String id)
158   {
159     for (int i = 0; i < chains.size(); i++)
160     {
161       if ( ( (PDBChain) chains.elementAt(i)).id.equals(id))
162       {
163         return (PDBChain) chains.elementAt(i);
164       }
165     }
166
167     return null;
168   }
169
170   public void setChargeColours()
171   {
172     for (int i = 0; i < chains.size(); i++)
173     {
174       ( (PDBChain) chains.elementAt(i)).setChargeColours();
175     }
176   }
177
178   public void setColours(jalview.schemes.ColourSchemeI cs)
179   {
180     for (int i = 0; i < chains.size(); i++)
181     {
182       ( (PDBChain) chains.elementAt(i)).setChainColours(cs);
183     }
184   }
185
186   public void setChainColours()
187   {
188     for (int i = 0; i < chains.size(); i++)
189     {
190       ( (PDBChain) chains.elementAt(i)).setChainColours(
191           Color.getHSBColor(1.0f / (float) i, .4f, 1.0f)
192           );
193     }
194   }
195 }