isRNAcirc / src / CG_to_allatom_main.cpp
// CG_to_allatom_main.cpp — 独立 CG_to_allatom 工具
// 用法: CG_to_allatom input_cg.pdb output_aa.pdb [coeffDIR]
// coeffDIR 默认: ../Data/data/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;
#include "./header/int_to_string.h"
#include "./header/double_to_string.h"
#include "./header/char_to_string.h"
#include "./header/string_to_vector_string.h"
#include "./header/pdb_details.h"
#include "./header/measure_distance.h"
#include "./header/get_nucleic.h"
#include "./header/get_residue_info.h"
#include "./header/FIT_RMSD.h"
#include "./header/CG_to_allatom.h"
using namespace std;
// 从 PDB 生成 CG 格式行: "atomID chainID resID resname atomtype x y z"
vector<string> pdb_to_cg_lines(const string &pdb_path) {
vector<string> cg_lines;
ifstream fin(pdb_path);
string line;
int atom_id = 0;
while(getline(fin, line)) {
if(line.find("ATOM")!=0 && line.find("HETATM")!=0) continue;
// 解析 PDB: atomname, resname, chain, resid, x, y, z
string atomname = line.substr(12, 4);
// 去空格
string aname;
for(char c : atomname) if(c!=' ') aname+=c;
string resname = line.substr(17, 3);
string chain = line.substr(21, 1);
string resid_s = line.substr(22, 4);
int resid = stoi(resid_s);
double x = stod(line.substr(30, 8));
double y = stod(line.substr(38, 8));
double z = stod(line.substr(46, 8));
// 映射原子名到 atomtype
int atomtype = -1;
if(aname=="P") atomtype=1;
else if(aname=="C1'") atomtype=2;
else if(aname=="C4'") atomtype=3;
else if(aname=="N9"||aname=="N1") atomtype=4;
else if(aname=="C8"||aname=="C2") atomtype=5;
else if(aname=="N7"||aname=="N3") atomtype=6;
else if(aname=="C5"||aname=="C4") atomtype=7;
else if(aname=="C6"||aname=="C5") atomtype=8;
else if(aname=="N1"||aname=="C6") atomtype=9;
else if(aname=="C2"||aname=="N4") atomtype=10;
else if(aname=="N3"||aname=="O2") atomtype=11;
else if(aname=="O5'") atomtype=2;
else if(aname=="C5'") atomtype=3;
else if(aname=="O4'") atomtype=4;
else if(aname=="C3'") atomtype=5;
else if(aname=="C2'") atomtype=6;
else if(aname=="O2'") atomtype=7;
else if(aname=="O3'") atomtype=8;
if(atomtype < 1) continue;
char buf[256];
sprintf(buf, "%d %s %d %s %d %.3f %.3f %.3f",
++atom_id, chain.c_str(), resid, resname.c_str(),
atomtype, x, y, z);
// DEBUG: print first few
if(atom_id <= 5) fprintf(stderr, "CG line: '%s'\n", buf);
cg_lines.push_back(string(buf));
}
return cg_lines;
}
int main(int argc, char *argv[]) {
if(argc < 3) {
cerr << "Usage: CG_to_allatom input_cg.pdb output_aa.pdb [coeffDIR]" << endl;
return 1;
}
string input_pdb = argv[1];
string output_pdb = argv[2];
string coeffDIR = (argc >= 4) ? argv[3] : "../Data/data/";
// 确保 coeffDIR 以 / 结尾
if(coeffDIR.back() != '/') coeffDIR += "/";
// 读 PDB → CG 格式
vector<string> cg_lines = pdb_to_cg_lines(input_pdb);
if(cg_lines.empty()) {
cerr << "Error: no CG atoms found in " << input_pdb << endl;
return 1;
}
// 调用 cg_to_allatom
vector<string> outAA;
int ret = cg_to_allatom(cg_lines, outAA, coeffDIR, 1);
if(ret != 0) {
cerr << "Error: cg_to_allatom failed (ret=" << ret << ")" << endl;
return 1;
}
// 写 PDB
ofstream fout(output_pdb);
fout << "HEADER CG to allatom reconstruction" << endl;
for(const string &line : outAA) {
fout << line << endl;
}
fout << "END" << endl;
fout.close();
cout << "OK: " << outAA.size() << " atoms written to " << output_pdb << endl;
return 0;
}