| 1 |
#include "PDBReader.hpp" |
| 2 |
|
| 3 |
PDBReader::PDBReader(void) { |
| 4 |
} |
| 5 |
|
| 6 |
PDBReader::~PDBReader(void) { |
| 7 |
gzclose(PDBfile); |
| 8 |
} |
| 9 |
|
| 10 |
|
| 11 |
void PDBReader::setPDBfile(const char *fname) { |
| 12 |
PDBfile = gzopen(fname, "r"); |
| 13 |
if (PDBfile == Z_NULL) { |
| 14 |
printf("Could not open PDB file %s\n", fname); |
| 15 |
exit(-1); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
vector<VDWAtom*> PDBReader::getAtomList() { |
| 20 |
|
| 21 |
char numstr[9]; |
| 22 |
char resIDstr[6]; |
| 23 |
char buf[150]; |
| 24 |
double pos[3]; |
| 25 |
char aType[5]; |
| 26 |
char aBase[2]; |
| 27 |
char resName[3]; |
| 28 |
int id, resID, ta; |
| 29 |
VDWAtom* atom; |
| 30 |
|
| 31 |
ta = getTotAtoms(); |
| 32 |
theAtoms.reserve(ta); |
| 33 |
|
| 34 |
gzrewind(PDBfile); |
| 35 |
|
| 36 |
while (!gzeof(PDBfile)) { |
| 37 |
gzgets(PDBfile, buf, 150); |
| 38 |
if (!strcmp(buf, "END") || |
| 39 |
!strncmp(buf, "END ", 4) || |
| 40 |
!strncmp(buf, "TER ", 4) || |
| 41 |
!strncmp(buf, "ENDMDL", 6)) break; |
| 42 |
if (!strncmp(buf, "ATOM ", 6) || !strncmp(buf, "HETATM", 6)) { |
| 43 |
|
| 44 |
atom = new VDWAtom(); |
| 45 |
|
| 46 |
memset(numstr, 0, 9 * sizeof(char)); |
| 47 |
strncpy(numstr, buf + 30, 8); |
| 48 |
pos[0] = atof(numstr); |
| 49 |
|
| 50 |
memset(numstr, 0, 9 * sizeof(char)); |
| 51 |
strncpy(numstr, buf + 38, 8); |
| 52 |
pos[1] = atof(numstr); |
| 53 |
|
| 54 |
memset(numstr, 0, 9 * sizeof(char)); |
| 55 |
strncpy(numstr, buf + 46, 8); |
| 56 |
pos[2] = atof(numstr); |
| 57 |
|
| 58 |
atom->setPos(pos); |
| 59 |
|
| 60 |
memset(aType, 0, 5 * sizeof(char)); |
| 61 |
strncpy(aType, buf + 12, 4); |
| 62 |
memset(aBase, 0, 2 * sizeof(char)); |
| 63 |
strncpy(aBase, buf + 13, 1); |
| 64 |
|
| 65 |
atom->setType(TrimSpaces(aType)); |
| 66 |
atom->setBase(TrimSpaces(aBase)); |
| 67 |
|
| 68 |
memset(resName, 0, 3 * sizeof(char)); |
| 69 |
strncpy(resName, buf + 17, 2); |
| 70 |
atom->setResName(resName); |
| 71 |
|
| 72 |
memset(resIDstr, 0, 6 * sizeof(char)); |
| 73 |
strncpy(resIDstr, buf + 22, 4); |
| 74 |
resID = atoi(resIDstr); |
| 75 |
atom->setResID(resID); |
| 76 |
|
| 77 |
theAtoms.push_back(atom); |
| 78 |
|
| 79 |
} |
| 80 |
} // while |
| 81 |
return theAtoms; |
| 82 |
} |
| 83 |
|
| 84 |
unsigned int PDBReader::getTotAtoms(void) { |
| 85 |
char buf[150]; |
| 86 |
unsigned int ta = 0; |
| 87 |
gzrewind(PDBfile); |
| 88 |
while (!gzeof(PDBfile)) { |
| 89 |
gzgets(PDBfile,buf,150); |
| 90 |
if (!strcmp(buf, "END") || |
| 91 |
!strncmp(buf, "END ", 4) || |
| 92 |
!strncmp(buf, "TER ", 4) || |
| 93 |
!strncmp(buf, "ENDMDL", 6)) break; |
| 94 |
if (!strncmp(buf, "ATOM ", 6) || !strncmp(buf, "HETATM", 6)) |
| 95 |
ta++; |
| 96 |
} |
| 97 |
return ta; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Removes left and right spaces from a string |
| 102 |
* |
| 103 |
* @param str String to trim |
| 104 |
* |
| 105 |
* @return char* to the trimed string |
| 106 |
*/ |
| 107 |
char * PDBReader::TrimSpaces(char *str) { |
| 108 |
size_t len; |
| 109 |
char *right, *left; |
| 110 |
|
| 111 |
/* Trim whitespace from left side */ |
| 112 |
for (left = str; isspace(*left); left++); |
| 113 |
|
| 114 |
/* Trim whitespace from right side */ |
| 115 |
if ((len = strlen(left))) |
| 116 |
{ |
| 117 |
right = left + (len - 1); |
| 118 |
|
| 119 |
while (isspace(*right)) |
| 120 |
{ |
| 121 |
*right = '\0'; |
| 122 |
right--; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/* Only do the str copy if their was spaces to the left */ |
| 127 |
if (left != str) |
| 128 |
strcpy(str, left); |
| 129 |
|
| 130 |
return (str); |
| 131 |
} |