ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/SHAPES/PDBReader.cpp
Revision: 1360
Committed: Tue Jul 20 20:02:15 2004 UTC (19 years, 11 months ago) by gezelter
File size: 2927 byte(s)
Log Message:
Changes for CGI and for gengetopts --unamed-opt="PDBFILE"

File Contents

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