EMAN2
pdbreader.cpp
Go to the documentation of this file.
1/*
2 * Author: Muthu Alagappan, 08/11/2004 (m.alagappan901@gmail.com)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#include "pdbreader.h"
33#include <vector>
34#include <cstring>
35#include <string>
36#include <stdio.h>
37#include <iostream>
38
39using namespace EMAN;
40
41
42PDBReader::PDBReader()
43{
44 points = 0;
45 n = 0;
46}
47
49{
50 n = nn;
51 points = (double *) calloc(4 * n, sizeof(double));
52 ter_stop = 0;
53}
54
56{
57 if( points )
58 {
59 free(points);
60 points = 0;
61 }
62}
63
65{
66 memset((void *) points, 0, 4 * n * sizeof(double));
67}
68
69
70//copies all relevant information that exists into a new PDBReader object.
72{
73 PDBReader *pa2 = new PDBReader();
75 double *pa2data = pa2->get_points_array();
76 memcpy(pa2data, get_points_array(), sizeof(double) * 4 * get_number_points());
77 pa2->pWords = pWords;
78 pa2->atomName = atomName;
80 pa2->chainId = chainId;
82 pa2->tail = tail;
83 pa2->head = head;
84 pa2->pointInfo = pointInfo;
85 pa2->lines = lines;
86 return pa2;
87}
88
89
91{
92 if (this != &pa) {
94 memcpy(get_points_array(), pa.get_points_array(), sizeof(double) * 4 * get_number_points());
95 }
96 return *this;
97}
98
100{
101 return n;
102}
103
104//reallocates the number of points
106{
107 if (n != nn) {
108 n = nn;
109 points = (double *) realloc(points, 4 * n * sizeof(double));
110 }
111}
112
114{
115 return points;
116}
117
119{
120 points = p;
121}
122
123
124
125// adds purely the x values to a vector
126vector<float> PDBReader::get_x() {
127 if (count_stop == 0) {count_stop = atomName.size();}
128 for (int i=0; i<count_stop; i++) {
129 x.push_back((float)points[4*i]);
130 y.push_back((float)points[4*i + 1]);
131 z.push_back((float)points[4*i + 2]);
132 resNum.push_back(pointInfo[2*i+1]);
133 }
134
135 return x;
136}
137
138// adds purely the y values to a vector
139vector<float> PDBReader::get_y() {
140 //cout << y.size() << endl;
141 return y;
142}
143
144// adds purely the z values to a vector
145vector<float> PDBReader::get_z() {
146 return z;
147}
148
149vector<string> PDBReader::get_atomName() {
150 return atomName;
151}
152
153vector<string> PDBReader::get_resName() {
154 return residueName;
155}
156
158 return resNum;
159}
160
161
162//Accurately parses a pdb file for all information
163bool PDBReader::read_from_pdb(const char *file)
164{
165
166 pWords.clear();
167 atomName.clear();
168 residueName.clear();
169 chainId.clear();
170 elementSym.clear();
171 tail.clear();
172 head.clear();
173 lines.clear();
174 pointInfo.clear();
175 ter_stop = 0;
176 count_stop = 0;
177
178 struct stat filestat;
179 stat(file, &filestat);
180 set_number_points(( int)(filestat.st_size / 80 + 1)); //80 bytes per line
181#ifdef DEBUG
182 printf("PDBReader::read_pdb(): try %4lu atoms first\n", get_number_points());
183#endif
184
185 FILE *fp = fopen(file, "r");
186 if(!fp) {
187 fprintf(stderr,"ERROR in PDBReader::read_pdb(): cannot open file %s\n",file);
188 throw;
189 }
190 char s[200];
191 size_t count = 0;
192
193 while ((fgets(s, 200, fp) != NULL)) {
194 lines.push_back(s);
195 if ((strncmp(s, "ENDMDL", 6) == 0) && (ter_stop==0)){
196 ter_stop =1;
197 count_stop = count;
198 }
199 if (strncmp(s, "END", 6) == 0){
200 break;
201 }
202 //if ((strncmp(s, "TER", 3) == 0) && (ter_stop ==0)){
203 //if (count !=0) {ter_stop = 1;}
204 //count_stop = count;
205 //}
206 if (strncmp(s, "ATOM", 4) != 0) {
207 if (count == 0) {head.push_back(s);}
208 else {tail.push_back(s);}
209 }
210 else {
211 pWords.push_back(s);
212 atomName.push_back(pWords[count].substr(12,4));
213 residueName.push_back(pWords[count].substr(17,3));
214 chainId.push_back(pWords[count].substr(21,1));
215 if (pWords[count].size()>78)
216 elementSym.push_back(pWords[count].substr(76,2));
217 else
218 elementSym.push_back(string("Null"));
219
220 float x, y, z, tf;
221 int an, sn;
222 sscanf(&s[6], "%d", &an);
223 sscanf(&s[23], "%d %f %f %f %*f %f", &sn, &x, &y, &z, &tf);
224
225 if (count + 1 > get_number_points()) {
226 set_number_points(2 * (count + 1));
227 } //makes sure point array is big enough
228
229 points[4 * count] = x;
230 points[4 * count + 1] = y;
231 points[4 * count + 2] = z;
232 points[4 * count + 3] = tf;
233 pointInfo.push_back(an);
234 pointInfo.push_back(sn);
235 count++;
236 }
237 }
238
239 fclose(fp);
240 set_number_points(count);
241 return true;
242}
243
244
245//Accurately writes a pdb file once the information is alread stored.
246/*
247void PDBReader::save_to_pdb(const char *file) const {
248 FILE *fp = fopen(file, "w");
249 for (int i = 0; i <head.size(); i++) {
250 char head2 [200];
251 strcpy(head2, head[i].c_str());
252 fprintf (fp, head2);
253 }
254 int m = 0;
255 for ( size_t i = 0; i < get_number_points(); i++) {
256 string curr = pWords[m];
257 string mid, final;
258 mid = curr.substr(12, 10);
259 final = curr.substr(76,2);
260 char mid2 [12];
261 strcpy(mid2, mid.c_str());
262 char final2 [4];
263 strcpy(final2, final.c_str());
264 m++;
265 fprintf(fp, "ATOM %5d %10s%4d %8.3f%8.3f%8.3f 1.00%6.2f %2s\n", pointInfo[2*i], mid2, pointInfo[2*i +1], points[4 * i], points[4 * i + 1], points[4 * i + 2], points[4*i + 3], final2);
266 }
267 for (int i = 0; i <tail.size(); i++) {
268 char tail2 [200];
269 strcpy(tail2, tail[i].c_str());
270 fprintf (fp, tail2);
271 }
272 fprintf (fp, "END");
273 fclose(fp);
274
275}
276*/
277
278
279//Accurately writes a pdb file once the information is alread stored.
280void PDBReader::save_to_pdb(const char *file) const {
281 FILE *fp = fopen(file, "w");
282 int m = 0;
283 for (size_t i =0; i< lines.size(); i++) {
284 char liner [200];
285 strcpy (liner, lines[i].c_str());
286 if (strncmp(liner, "ATOM", 4) != 0) {
287 fprintf (fp, "%s", liner);
288 }
289 else {
290 string curr = pWords[m];
291 string mid, final;
292 mid = curr.substr(12, 10);
293 final = curr.substr(76,2);
294 char mid2 [12];
295 strcpy(mid2, mid.c_str());
296 char final2 [4];
297 strcpy(final2, final.c_str());
298 fprintf(fp, "ATOM %5d %10s%4d %8.3f%8.3f%8.3f 1.00%6.2f %2s\n", pointInfo[2*m], mid2, pointInfo[2*m +1], points[4 * m], points[4 * m + 1], points[4 * m + 2], points[4*m + 3], final2);
299 m++;
300 }
301 }
302 fclose(fp);
303
304}
305
306
307//returns a vector of the x,y,z values
308vector<float> PDBReader::get_points() {
309vector<float> ret;
310for (unsigned int i=0; i<n; i++) {
311 ret.push_back((float)points[i*4]);
312 ret.push_back((float)points[i*4+1]);
313 ret.push_back((float)points[i*4+2]);
314}
315
316return ret;
317}
318
319//performs a right transform of all x,y,z points given a Transform object
320void PDBReader::right_transform(const Transform& transform) {
321 for ( unsigned int i = 0; i < 4 * n; i += 4) {
322 Transform s = transform.transpose();
323 Vec3f v((float)points[i],(float)points[i+1],(float)points[i+2]);
324 v= s*v;
325 points[i] =v[0];
326 points[i+1]=v[1];
327 points[i+2]=v[2];
328 }
329}
330
332 PointArray* pArray = new PointArray;
333 p.save_to_pdb("thisFile3.txt");
334 pArray->read_from_pdb("thisFile3.txt");
335 remove("thisFile3.txt");
336
337 return pArray;
338}
339
340
PointArray defines a double array of points with values in a 3D space.
Definition: pdbreader.h:55
bool read_from_pdb(const char *file)
Reads and parses all information from file.
Definition: pdbreader.cpp:163
vector< string > elementSym
Definition: pdbreader.h:123
vector< int > get_resNum()
Definition: pdbreader.cpp:157
PDBReader * copy()
Definition: pdbreader.cpp:71
vector< float > z
Definition: pdbreader.h:133
vector< string > chainId
Definition: pdbreader.h:122
vector< float > get_y()
Definition: pdbreader.cpp:139
void save_to_pdb(const char *file) const
Saves all atom information into a pdb in the official format.
Definition: pdbreader.cpp:280
vector< string > head
Definition: pdbreader.h:125
vector< float > x
Definition: pdbreader.h:131
vector< float > get_z()
Definition: pdbreader.cpp:145
vector< float > y
Definition: pdbreader.h:132
vector< string > lines
Definition: pdbreader.h:126
vector< string > pWords
Definition: pdbreader.h:119
PointArray * makePointArray(const PDBReader &p)
Definition: pdbreader.cpp:331
vector< string > atomName
Definition: pdbreader.h:120
void set_number_points(size_t nn)
Definition: pdbreader.cpp:105
vector< string > tail
Definition: pdbreader.h:124
void right_transform(const Transform &transform)
Does Transform*v as opposed to v*Transform (as in the transform function)
Definition: pdbreader.cpp:320
double * points
Definition: pdbreader.h:117
vector< float > get_points()
Returns all x,y,z triplets packed into a vector<float>
Definition: pdbreader.cpp:308
void set_points_array(double *p)
Allows the user to set the double array of points.
Definition: pdbreader.cpp:118
vector< int > resNum
Definition: pdbreader.h:134
vector< string > get_resName()
Definition: pdbreader.cpp:153
double * get_points_array()
Returns the double array of points.
Definition: pdbreader.cpp:113
PDBReader & operator=(PDBReader &pa)
Definition: pdbreader.cpp:90
vector< string > residueName
Definition: pdbreader.h:121
vector< int > pointInfo
Definition: pdbreader.h:118
size_t get_number_points() const
Definition: pdbreader.cpp:99
vector< string > get_atomName()
Definition: pdbreader.cpp:149
vector< float > get_x()
Definition: pdbreader.cpp:126
PointArray defines a double array of points with values in a 3D space.
Definition: pointarray.h:52
bool read_from_pdb(const char *file, const vector< int > &lines=vector< int >())
Definition: pointarray.cpp:329
A Transform object is a somewhat specialized object designed specifically for EMAN2/Sparx storage of ...
Definition: transform.h:75
Transform transpose() const
Get the transpose of this transformation matrix.
Definition: transform.cpp:1346
E2Exception class.
Definition: aligner.h:40