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