34 cout <<
"DEBUG readPDB: filename=" << filename << endl;
37 ifstream fpdb(filename.
c_str());
39 cerr <<
"Error: File " << filename <<
" not opened!" << endl;
43 int readflag, linenum(0);
44 int i, nmol(0), natom(0), nsec(0);
45 int resnum, resnum2, prev_resnum = -10000;
46 int atomnum, ba, helixclass;
47 char chain,
insert, prev_insert =
' ';
49 char label[12], restype[12], atomtype[12], satomnum[8];
63 memset(al, 0, 100000*
sizeof(
Batom*));
68 while ( !fpdb.eof() ) {
73 cout <<
"DEBUG readPDB: " << aline << endl;
74 if ( strncmp(aline,
"TER", 3) == 0 || strncmp(aline,
"END", 3) == 0 ) chain =
'%';
75 if ( strncmp(aline,
"CRYST1", 6) == 0 ) {
76 i = sscanf(aline,
"%6c%9lf%9lf%9lf%7lf%7lf%7lf%11c%4d",
77 label, &unitcell[0], &unitcell[1], &unitcell[2],
78 &unitcell[3], &unitcell[4], &unitcell[5],
84 if ( i < 9 ) molgroup->
sgstring =
"P 1";
86 if ( ( strncmp(aline,
"ATOM", 4) == 0 ) || ( strncmp(aline,
"HETATM", 6) == 0 ) ) {
88 if ( strstr(aline,
"ATOM") ) readflag = 1;
90 if ( strstr(aline,
"HETATM") ) readflag = 1;
92 if ( strncmp(&aline[13],
"CA", 2) == 0 ) readflag = 1;
94 if ( strstr(aline,
"nan") || strstr(aline,
"NAN") ) {
95 cerr <<
"Warning: NAN at line number " << linenum << endl;
98 if ( aline[16] ==
'B' ) readflag = 0;
100 cout <<
"DEBUG readPDB: readflag=" << readflag << endl;
103 if ( aline[21] != chain ) {
107 if ( !molgroup->
mol ) molgroup->
mol = mol;
110 prev_resnum = -10000;
112 cout <<
"DEBUG readPDB: chain=" << chain <<
" nmol=" << nmol << endl;
114 memset(satomnum, 0, 8);
116 if ( atomnum < 1 ) atomnum = natom + 1;
117 strncpy(atomtype, aline + 12, 4);
118 strncpy(restype, aline + 17, 3);
124 if ( natom < 1 || resnum != prev_resnum ||
insert != prev_insert ) {
126 cout <<
"DEBUG readPDB: resnum=" << resnum <<
" restype=" << restype << endl;
128 error_show(
"No molecule structure allocated!", __FILE__, __LINE__);
132 if ( !mol->
res ) mol->
res = res;
135 prev_resnum = resnum;
140 cout <<
"DEBUG readPDB: atomnum=" << atomnum <<
" atomtype=" << atomtype << endl;
151 cout <<
"DEBUG readPDB: " << atom->
num <<
" " << res->
num <<
" " << atom->
coord <<
" " << atom->
chrg << endl;
153 if ( strncmp(aline,
"HETATM", 6) == 0 ) atom->
sel = 2;
155 if ( natom <= atomnum ) natom = atomnum + 1;
158 if ( strncmp(aline,
"CONECT", 6) == 0 ) {
160 if ( atomnum < natom )
for ( i=0, aptr=aline+11; ba && strlen(aptr) > 4; aptr+=5, i++ ) {
162 if ( ba <= natom && ba > atomnum && al[atomnum] && al[ba] ) {
164 printf (
"DEBUG readPDB: Atom %d bound to %d\n", atomnum, ba);
168 bond =
bond_add(&bond, atom, atom2, d, 1);
169 if ( !molgroup->
bond ) molgroup->
bond = bond;
182 fpdb.seekg(0, ios::beg);
188 while ( !fpdb.eof() ) {
190 if ( strncmp(aline,
"HELIX", 5) == 0 ) {
195 for ( mol = molgroup->
mol; mol && chain != mol->
id[0]; mol = mol->
next ) ;
200 strncpy(sec->
id, aline + 11, 3);
202 switch ( helixclass ) {
216 for ( res=mol->
res; res; res=res->
next ) {
217 if ( res->
num == resnum ) sec->
first = res;
218 if ( res->
num == resnum2 ) sec->
last = res;
222 cerr <<
"Warning: First residue " << resnum <<
" not found!" << endl;
224 cerr <<
"Warning: Last residue " << resnum2 <<
" not found!" << endl;
227 if ( ( strncmp(aline,
"ATOM", 4) == 0 ) || ( strncmp(aline,
"HETATM", 6) == 0 ) )
233 fpdb.seekg(0, ios::beg);
239 while ( !fpdb.eof() ) {
241 if ( strncmp(aline,
"SHEET", 5) == 0 ) {
246 for ( mol = molgroup->
mol; mol && chain != mol->
id[0]; mol = mol->
next ) ;
251 strncpy(sec->
id, aline + 11, 3);
257 for ( res=mol->
res; res; res=res->
next ) {
258 if ( res->
num == resnum ) sec->
first = res;
259 if ( res->
num == resnum2 ) sec->
last = res;
263 cerr <<
"Warning: First residue " << resnum <<
" not found!" << endl;
265 cerr <<
"Warning: Last residue " << resnum2 <<
" not found!" << endl;
268 if ( ( strncmp(aline,
"ATOM", 4) == 0 ) || ( strncmp(aline,
"HETATM", 6) == 0 ) )
274 fpdb.seekg(0, ios::beg);
280 while ( !fpdb.eof() ) {
282 if ( strncmp(aline,
"TURN", 4) == 0 ) {
287 for ( mol = molgroup->
mol; mol && chain != mol->
id[0]; mol = mol->
next ) ;
292 strncpy(sec->
id, aline + 11, 3);
296 for ( res=mol->
res; res; res=res->
next ) {
297 if ( res->
num == resnum ) sec->
first = res;
298 if ( res->
num == resnum2 ) sec->
last = res;
303 if ( ( strncmp(aline,
"ATOM", 4) == 0 ) || ( strncmp(aline,
"HETATM", 6) == 0 ) )
310 cout <<
"DEBUG readPDB: Number of molecules = " << nmol << endl;
311 cout <<
"DEBUG readPDB: Secondary structures = " << nsec << endl;
333 cout <<
"DEBUG writePDB: filename=" << filename << endl;
335 if ( !molgroup )
return -1;
337 ofstream fpdb(filename.
c_str());
339 cerr <<
"Error: File " << filename <<
" not opened!" << endl;
343 int i, j, nw(0), nmol(0), a1, a2, pnum;
344 char atomtag[8], atomtype[12];
345 char alt[4] =
" ", sid[8] =
" ", esm[4] =
" ", chain;
348 cout <<
"DEBUG writePDB: New file name: " << filename << endl;
353 cout <<
"DEBUG writePDB: File " << filename <<
" opened" << endl;
362 int* al =
new int[1000000];
363 for ( i=0; i<1000000; i++ ) al[i] = 0;
365 time_t t = time(NULL);
366 tm* ltm = localtime(&t);
368 strftime(stm,
sizeof(stm),
"%d-%b-%y", ltm);
373 fpdb << left << setw(50) <<
"HEADER Written by Bsoft" << stm << endl;
374 fpdb <<
"TITLE " << title << endl;
379 cout <<
"DEBUG writePDB: remarks done" << endl;
381 for ( mol = molgroup->
mol; mol; mol = mol->
next ) {
383 for ( sec = mol->
sec; sec; sec = sec->
next ) {
386 fpdb <<
"HELIX " << right << setw(4) << sec->
num <<
" " <<
387 left << setw(4) << sec->
id <<
389 right << setw(5) << sec->
first->
num <<
391 setw(4) << sec->
last->
type << chain <<
392 right << setw(5) << sec->
last->
num <<
394 right << setw(2) << sec->
type << endl;
400 cout <<
"DEBUG writePDB: helices done" << endl;
402 for ( mol = molgroup->
mol; mol; mol = mol->
next ) {
404 for ( sec = mol->
sec; sec; sec = sec->
next ) {
407 fpdb <<
"SHEET " << right << setw(4) << sec->
num <<
" " <<
408 left << setw(3) << sec->
id <<
409 right << setw(2) << sec->
nstrands <<
" " <<
410 left << setw(4) << sec->
first->
type << chain <<
411 right << setw(4) << sec->
first->
num <<
413 setw(4) << sec->
last->
type << chain <<
414 right << setw(4) << sec->
last->
num <<
416 right << setw(2) << sec->
sense << endl;
422 cout <<
"DEBUG writePDB: sheets done" << endl;
424 for ( mol = molgroup->
mol; mol; mol = mol->
next ) {
426 for ( sec = mol->
sec; sec; sec = sec->
next ) {
429 fpdb <<
"TURN " << right << setw(4) << sec->
num <<
" " <<
430 left << setw(4) << sec->
id <<
432 right << setw(4) << sec->
first->
num <<
434 setw(4) << sec->
last->
type << chain <<
435 right << setw(4) << sec->
last->
num <<
442 cout <<
"DEBUG writePDB: turns done" << endl;
445 fpdb <<
"CRYST1" << fixed << setprecision(3) << setw(9) << unitcell.
a() << setw(9) <<
446 unitcell.
b() << setw(9) << unitcell.
c() << setprecision(2) << setw(7) <<
448 setw(7) << unitcell.
gamma()*180.0/
M_PI << setw(11) <<
452 cout <<
"DEBUG writePDB: crystal parameters done" << endl;
457 for ( mol = molgroup->
mol; mol; mol = mol->
next ) {
460 for ( res = mol->
res; res; res = res->
next ) {
461 for ( atom = res->
atom; atom; atom = atom->
next ) {
462 if ( atom->
num <= pnum ) atom->
num = pnum + 1;
463 if ( atom->
num > 99999 ) atom->
num = 1;
466 if ( atom->
sel < 2 ) strncpy(atomtag,
"ATOM ", 6);
467 else strncpy(atomtag,
"HETATM", 6);
469 if ( strlen(atom->
type) < 4 )
470 snprintf(atomtype, 12,
" %s", atom->
type);
472 snprintf(atomtype, 12,
"%s", atom->
type);
474 strncpy(esm, atom->
el, 2);
476 if ( strlen(esm) < 2 ) esm[1] =
' ';
479 atomtag << right << setw(5) << atom->
num <<
" " << left << setw(4) << atomtype << alt <<
480 setw(3) << res->
type <<
" " << chain << right << setw(4) << res->
num << res->
insert <<
" " <<
481 setprecision(3) << setw(8) << atom->
coord[0] << setw(8) <<
482 atom->
coord[1] << setw(8) << atom->
coord[2] <<
483 setprecision(2) << setw(6) << atom->
q << setw(6) << atom->
b <<
" " <<
484 setw(4) << sid << setw(2) << esm << setprecision(3) << setw(6) << atom->
chrg << endl;
491 fpdb <<
"TER" << endl;
496 cout <<
"DEBUG writePDB: atoms done" << endl;
498 for ( bond = molgroup->
bond; bond; bond = bond->
next ) {
501 for ( i=0; i<10 && al[a1+i]; i++ ) ;
502 if ( i<10 ) al[a1+i] = bond->
atom2->
num;
503 for ( i=0; i<10 && al[a2+i]; i++ ) ;
504 if ( i<10 ) al[a2+i] = bond->
atom1->
num;
507 for ( i=1; i<100000; i++ )
if ( al[i*10] ) {
508 fpdb <<
"CONECT" << setw(5) << i;
509 for ( j=0; j<10 && al[i*10+j]; j++ ) {
510 if ( j == 4 ) fpdb << endl <<
"CONECT" << setw(5) << i;
511 fpdb << setw(5) << al[i*10+j];
519 cout <<
"DEBUG writePDB: bonds done" << endl;
521 fpdb <<
"END" << endl;
526 cout <<
"DEBUG writePDB: File " << filename <<
" closed" << endl;