-
Notifications
You must be signed in to change notification settings - Fork 24
/
cif2pdb.cpp
553 lines (510 loc) · 18.6 KB
/
cif2pdb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>
#include <iomanip>
#include <map>
#include "pstream.h" // For reading gzip and bz2 compressed files
using namespace std;
void print_help()
{
cout <<
"Converting mmCIF file to PDB file(s)\n"
"\n"
"Usage: cif2pdb input.cif output.pdb\n"
"\n"
" -chain Specify auth chain ID to convert:\n"
" $ cif2pdb input.cif output.pdb -chain A\n"
"\n"
" -mol macromolecule type. default is all macromolecules.\n"
" 1: protein only\n"
" 2: RNA only\n"
" 4: DNA only\n"
"\n"
" -split Whether to split PDB file into multiple chains\n"
" 0: (default) do not split; output a single PDB\n"
" 1: output one PDB file per chain\n"
"\n"
" -het Whether to read residues marked as 'HETATM' in addition to 'ATOM '\n"
" 0: only 'ATOM ' residues\n"
" 1: (default) 'ATOM ' and 'HETATM' for MSE\n"
" 2: 'ATOM ' and all 'HETATM', excluding HOH\n"
" 3: 'ATOM ' and all 'HETATM', including HOH\n"
" If -het >=1, MSE will be converted to MET\n"
<<endl;
exit(EXIT_SUCCESS);
}
void PrintErrorAndQuit(const string sErrorString)
{
cout << sErrorString << endl;
exit(1);
}
/* strip white space at the begining or end of string */
string Trim(const string &inputString)
{
string result = inputString;
int idxBegin = inputString.find_first_not_of(" \n\r\t");
int idxEnd = inputString.find_last_not_of(" \n\r\t");
if (idxBegin >= 0 && idxEnd >= 0)
result = inputString.substr(idxBegin, idxEnd + 1 - idxBegin);
return result;
}
/* split a long string into vectors by whitespace
* line - input string
* line_vec - output vector
* delimiter - delimiter */
void split(const string &line, vector<string> &line_vec,
const char delimiter=' ')
{
bool within_word = false;
for (size_t pos=0;pos<line.size();pos++)
{
if (line[pos]==delimiter)
{
within_word = false;
continue;
}
if (!within_word)
{
within_word = true;
line_vec.push_back("");
}
line_vec.back()+=line[pos];
}
}
void write_mmcif_to_pdb(const string filename,
const vector<vector<string> >&PDB_lines,
const vector<string> &chainID_list, const int split_opt)
{
size_t c,r;
ofstream fout;
if (split_opt)
{
for (c=0;c<PDB_lines.size();c++)
{
if (PDB_lines[c].size()==0) continue;
if (filename=="-")
{
cout<<"REMARK cif2pdb "<<PDB_lines[c][0][21]<<" "<<chainID_list[c]<<endl;
for (r=0;r<PDB_lines[c].size();r++) cout<<PDB_lines[c][r];
cout<<"TER"<<endl;
continue;
}
cout<< filename+Trim(chainID_list[c])+".pdb"<<endl;
fout.open((filename+Trim(chainID_list[c])+".pdb").c_str());
fout<<"REMARK cif2pdb "<<PDB_lines[c][0][21]<<" "<<chainID_list[c]<<endl;
for (r=0;r<PDB_lines[c].size();r++) fout<<PDB_lines[c][r];
fout<<"TER"<<endl;
fout.close();
}
}
else if (filename=="-")
{
for (c=0;c<PDB_lines.size();c++)
{
if (PDB_lines[c].size()==0) continue;
cout<<"REMARK cif2pdb "<<PDB_lines[c][0][21]<<" "<<chainID_list[c]<<endl;
}
for (c=0;c<PDB_lines.size();c++)
{
if (PDB_lines[c].size()==0) continue;
for (r=0;r<PDB_lines[c].size();r++) cout<<PDB_lines[c][r];
cout<<"TER"<<endl;
}
cout<<"END"<<endl;
}
else
{
fout.open(filename.c_str());
for (c=0;c<PDB_lines.size();c++)
{
if (PDB_lines[c].size()==0) continue;
fout<<"REMARK cif2pdb "<<PDB_lines[c][0][21]<<" "<<chainID_list[c]<<endl;
}
for (c=0;c<PDB_lines.size();c++)
{
if (PDB_lines[c].size()==0) continue;
for (r=0;r<PDB_lines[c].size();r++) fout<<PDB_lines[c][r];
fout<<"TER"<<endl;
}
fout<<"END"<<endl;
fout.close();
}
return;
}
size_t resolve_chainID_for_mmcif(vector<vector<string> >&PDB_lines,
const vector<string> &chainID_list)
{
size_t changed_chains=0;
size_t c,r,i;
string chainID;
string chainID_string="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
vector<bool> chainID_taken(chainID_string.size(),false);
vector<bool> chainID_accept(chainID_list.size(),false);
/* accept all single character chain ID */
for (c=0;c<PDB_lines.size();c++)
{
if (PDB_lines[c].size()==0) continue;
chainID=PDB_lines[c][0][21];
if (chainID!=chainID_list[c]) continue;
chainID_accept[c]=true;
for (i=0;i<chainID_string.size();i++)
{
if (chainID_string[i]!=chainID[0]) continue;
if (chainID_taken[i]) chainID_accept[c]=false;
else chainID_taken[i]=true;
break;
}
}
/* accept all remaining non-conflicting chain ID */
for (c=0;c<PDB_lines.size();c++)
{
if (PDB_lines[c].size()==0 || chainID_accept[c]) continue;
chainID=PDB_lines[c][0][21];
chainID_accept[c]=true;
for (i=0;i<chainID_string.size();i++)
{
if (chainID_string[i]!=chainID[0]) continue;
if (chainID_taken[i]) chainID_accept[c]=false;
else chainID_taken[i]=true;
break;
}
}
/* resolve remaining chain ID */
for (c=0;c<PDB_lines.size();c++)
{
if (PDB_lines[c].size()==0 || chainID_accept[c]) continue;
chainID="";
for (i=0;i<chainID_taken.size();i++)
{
if (chainID_taken[i]) continue;
chainID=chainID_string[i];
chainID_taken[i]=true;
break;
}
if (chainID=="")
{
cerr<<"WARNING! Cannot parse "<<chainID_list[c]<<" with "
<<PDB_lines[c].size()<<" atoms due to chain ID conflict. "
<<"Please consider -split 1"<<endl;
vector<string>().swap(PDB_lines[c]);
}
else
{
for (r=0;r<PDB_lines[c].size();r++) PDB_lines[c][r]=
PDB_lines[c][r].substr(0,21)+chainID+PDB_lines[c][r].substr(22);
changed_chains++;
}
}
if (changed_chains)
cerr<<"WARNING! Changed "<<changed_chains<<" chain ID(s)"<<endl;
/* clean up*/
chainID.clear();
string().swap(chainID_string);
vector<bool>().swap(chainID_taken);
vector<bool>().swap(chainID_accept);
return changed_chains;
}
size_t get_all_mmcif_lines(const string filename, const string chain_opt,
vector<vector<string> >&PDB_lines, vector<string> &chainID_list,
const bool dna_opt, const bool rna_opt, const bool protein_opt,
const bool hoh_opt, const bool lig_opt, const bool mse_opt)
{
size_t a=0; // atom index
string line;
bool select_atom=false;
size_t model_idx=0;
vector<string> tmp_str_vec;
int compress_type=0; // uncompressed file
ifstream fin;
redi::ipstream fin_gz; // if file is compressed
if (filename.size()>=3 &&
filename.substr(filename.size()-3,3)==".gz")
{
fin_gz.open("gunzip -c '"+filename+"'");
compress_type=1;
}
else if (filename.size()>=4 &&
filename.substr(filename.size()-4,4)==".bz2")
{
fin_gz.open("bzcat '"+filename+"'");
compress_type=2;
}
else
{
if (filename=="-") compress_type=-1;
else fin.open(filename.c_str());
}
bool loop_ = false; // not reading following content
map<string,int> _atom_site;
int atom_site_pos;
vector<string> line_vec;
string group_PDB="ATOM ";
string alt_id=" "; // alternative location indicator
string asym_id="."; // this is similar to chainID, except that
// chainID is char while asym_id is a string
// with possibly multiple char
string prev_asym_id="";
string resn=""; // residue name
string resi="";
string atom="";
string model_index=""; // the same as model_idx but type is string
stringstream i8_stream;
map<string, string> alt_id_dict; // resi -> alt_id
string resi_chain;
while ((compress_type==-1)?cin.good():(compress_type?fin_gz.good():fin.good()))
{
if (compress_type==-1) getline(cin, line);
else if (compress_type) getline(fin_gz, line);
else getline(fin, line);
if (line.size()==0) continue;
if (loop_) loop_ = (line.size()>=2)?(line.compare(0,2,"# ")):(line.compare(0,1,"#"));
if (!loop_)
{
if (line.compare(0,5,"loop_")) continue;
while(1)
{
if (compress_type==-1)
{
if (cin.good()) getline(cin, line);
else PrintErrorAndQuit("ERROR! Unexpected end of "+filename);
}
else if (compress_type)
{
if (fin_gz.good()) getline(fin_gz, line);
else PrintErrorAndQuit("ERROR! Unexpected end of "+filename);
}
else
{
if (fin.good()) getline(fin, line);
else PrintErrorAndQuit("ERROR! Unexpected end of "+filename);
}
if (line.size()) break;
}
if (line.compare(0,11,"_atom_site.")) continue;
loop_=true;
_atom_site.clear();
atom_site_pos=0;
_atom_site[Trim(line.substr(11))]=atom_site_pos;
while(1)
{
if (compress_type==-1) getline(cin, line);
else if (compress_type) getline(fin_gz, line);
else getline(fin, line);
if (line.size()==0) continue;
if (line.compare(0,11,"_atom_site.")) break;
_atom_site[Trim(line.substr(11))]=++atom_site_pos;
}
if (_atom_site.count("group_PDB")*
_atom_site.count("label_atom_id")*
_atom_site.count("label_comp_id")*
(_atom_site.count("auth_asym_id")+
_atom_site.count("label_asym_id"))*
(_atom_site.count("auth_seq_id")+
_atom_site.count("label_seq_id"))*
_atom_site.count("Cartn_x")*
_atom_site.count("Cartn_y")*
_atom_site.count("Cartn_z")==0)
{
loop_ = false;
cerr<<"Warning! Missing one of the following _atom_site data items: group_PDB, label_atom_id, label_comp_id, auth_asym_id/label_asym_id, auth_seq_id/label_seq_id, Cartn_x, Cartn_y, Cartn_z"<<endl;
continue;
}
}
line_vec.clear();
split(line,line_vec);
atom =line_vec[_atom_site["label_atom_id"]];
resn =line_vec[_atom_site["label_comp_id"]];
group_PDB=line_vec[_atom_site["group_PDB"]];
if (group_PDB=="ATOM") group_PDB="ATOM ";
if (mse_opt && resn=="MSE")
{
group_PDB="ATOM ";
if (atom=="SE") atom="SD";
}
if (group_PDB=="HETATM")
{
if (!lig_opt) continue;
if (!hoh_opt && resn=="HOH") continue;
if (asym_id!=prev_asym_id && prev_asym_id.size())
asym_id=prev_asym_id; // no separate chain for ligand
}
else if (group_PDB!="ATOM ") continue;
//alt_id=".";
//if (_atom_site.count("label_alt_id")) // in 39.4 % of entries
//alt_id=line_vec[_atom_site["label_alt_id"]];
//if (alt_id!="." && alt_id!="A") continue;
if (resn.size()==1)
{
if (!rna_opt && group_PDB=="ATOM ") continue;
resn=" "+resn;
}
else if (resn.size()==2)
{
if (!dna_opt && resn[0]=='D' && group_PDB=="ATOM ") continue;
resn=" " +resn;
}
else if (resn.size()==3 && !protein_opt && group_PDB=="ATOM ") continue;
else if (resn.size()>=4) resn=resn.substr(0,3);
if (atom[0]=='"') atom=atom.substr(1);
if (atom.size() && atom[atom.size()-1]=='"')
atom=atom.substr(0,atom.size()-1);
if (atom.size()==0) continue;
else if (atom.size()==1) atom=" "+atom+" ";
else if (atom.size()==2) atom=" "+atom+" "; // wrong for sidechain H
else if (atom.size()==3) atom=" "+atom;
else if (atom.size()>=5) atom=atom.substr(0,4);
if (_atom_site.count("auth_seq_id"))
resi=line_vec[_atom_site["auth_seq_id"]];
else resi=line_vec[_atom_site["label_seq_id"]];
if (_atom_site.count("pdbx_PDB_ins_code") &&
line_vec[_atom_site["pdbx_PDB_ins_code"]]!="?")
resi+=line_vec[_atom_site["pdbx_PDB_ins_code"]][0];
else resi+=" ";
if (resi.size()>5)
{
cerr<<"WARNING! Cannot parse line due to long residue index\n"<<line<<endl;
continue;
}
if (_atom_site.count("auth_asym_id"))
asym_id=line_vec[_atom_site["auth_asym_id"]];
else asym_id=line_vec[_atom_site["label_asym_id"]];
if (asym_id==".") asym_id=" ";
if (chain_opt.size() && asym_id!=chain_opt &&
!(asym_id==" " && (chain_opt=="_" || chain_opt=="."))) continue;
if (_atom_site.count("pdbx_PDB_model_num") &&
model_index!=line_vec[_atom_site["pdbx_PDB_model_num"]])
{
if (PDB_lines.size()) break;
model_index=line_vec[_atom_site["pdbx_PDB_model_num"]];
map<string, string>().swap(alt_id_dict);
}
if (_atom_site.count("label_alt_id")) // in 39.4 % of entries
{
alt_id=line_vec[_atom_site["label_alt_id"]];
if (alt_id==".") alt_id=" ";
else
{
resi_chain=asym_id+resi;
if (alt_id_dict.count(resi_chain)==0)
alt_id_dict[resi_chain]=alt_id;
else if (alt_id_dict.count(resi_chain) &&
alt_id!=alt_id_dict[resi_chain]) continue;
//if (alt_id!="." && alt_id!="A") continue;
}
}
if (prev_asym_id!=asym_id)
{
PDB_lines.push_back(tmp_str_vec);
chainID_list.push_back(asym_id);
prev_asym_id=asym_id;
}
a++;
a%=100000;
i8_stream<<group_PDB
<<setw(5)<<a<<" "<<atom<<alt_id<<resn<<" "<<asym_id[asym_id.size()-1]
<<setw(5)<<resi<<" "
<<setw(8)<<line_vec[_atom_site["Cartn_x"]].substr(0,8)
<<setw(8)<<line_vec[_atom_site["Cartn_y"]].substr(0,8)
<<setw(8)<<line_vec[_atom_site["Cartn_z"]].substr(0,8);
if (_atom_site.count("B_iso_or_equiv"))
{
i8_stream<<" 1.00"<<setw(6)<<line_vec[_atom_site["B_iso_or_equiv"]].substr(0,6);
if (_atom_site.count("type_symbol"))
i8_stream<<setw(12)<<line_vec[_atom_site["type_symbol"]].substr(0,12);
}
i8_stream<<endl;
PDB_lines.back().push_back(i8_stream.str());
i8_stream.str(string());
}
group_PDB.clear();
_atom_site.clear();
line_vec.clear();
alt_id.clear();
asym_id.clear();
resn.clear();
map<string, string>().swap(alt_id_dict);
resi_chain.clear();
if (compress_type>=0)
{
if (compress_type) fin_gz.close();
else fin.close();
}
line.clear();
chainID_list.push_back("");
return PDB_lines.size();
}
int main(int argc, char *argv[])
{
if (argc < 2) print_help();
/**********************/
/* get argument */
/**********************/
string xname = "";
string yname = "";
int split_opt =0; // do not split chain
int het_opt =0; // do not read HETATM residues
int mol_opt =7; // auto-detect the molecule type as protein/RNA
string chain_opt =""; // read all chains
for(int i = 1; i < argc; i++)
{
if ( !strcmp(argv[i],"-split") && i < (argc-1) )
{
split_opt=atoi(argv[i + 1]); i++;
}
else if ( !strcmp(argv[i],"-mol") && i < (argc-1) )
{
mol_opt=atoi(argv[i + 1]); i++;
}
else if ( !strcmp(argv[i],"-chain") && i < (argc-1) )
{
chain_opt=argv[i + 1]; i++;
}
else if ( !strcmp(argv[i],"-het") && i < (argc-1) )
{
het_opt=atoi(argv[i + 1]); i++;
}
else if (xname.size() == 0) xname=argv[i];
else if (yname.size() == 0) yname=argv[i];
else PrintErrorAndQuit(string("ERROR! Undefined option ")+argv[i]);
}
if(yname.size()==0)
{
if (xname.size()==0)
PrintErrorAndQuit("Please provide input structures");
else if (yname.size()==0) yname="-";
}
bool dna_opt=(mol_opt>=4);
mol_opt %= 4;
bool rna_opt=(mol_opt>=2);
mol_opt %= 2;
bool protein_opt=(mol_opt>=1);
if (split_opt<0 || split_opt>1)
PrintErrorAndQuit("-split can only be 0 or 1");
if (het_opt<0 || het_opt>3)
PrintErrorAndQuit("-het can only be 0, 1, 2, or 3");
bool hoh_opt=(het_opt==3);
bool lig_opt=(het_opt>=2);
bool mse_opt=(het_opt>=1);
/* parse structure */
vector<vector<string> >PDB_lines;
vector<string> chainID_list;
get_all_mmcif_lines(xname, chain_opt, PDB_lines, chainID_list,
dna_opt, rna_opt, protein_opt, hoh_opt, lig_opt, mse_opt);
if (!split_opt) resolve_chainID_for_mmcif(PDB_lines,chainID_list);
write_mmcif_to_pdb(yname, PDB_lines, chainID_list, split_opt);
/* clean up */
vector<vector<string> >().swap(PDB_lines);
vector<string>().swap(chainID_list);
chain_opt.clear();
return 0;
}