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