isRNAcirc / src / header / read_RNA_CG.h
// To read RNA chains
// remove P, OP1 OP2 O2'
// only first model if obtained by NMR
// if bases are modified, assign R(A,G),Y(C,U)
// 5.12.2015 by Xiaojun Xu
#ifndef _READ_RNA_CG_H_
#define _READ_RNA_CG_H_
//pdb_details.h"
//string_to_vector_string.h"
int read_RNA_CG(string inPDB, vector<string> &Vchain, vector<string> &Vseq, vector<string> &Vres, vector<vector<double> > &Vxyz, int sign) {
//cout << "Reading RNA ..." << endl;
ifstream inpdb (inPDB.c_str());
if(!inpdb.is_open()) {return 1; cout<<"ERROR: "<<inPDB<<" can not open for read!"<<endl;}
Vres.clear(); Vchain.clear(); Vxyz.clear();
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
map<string,int> Atom_index;
////backbone
//Atom_index.insert(pair<string,int>("P", 1));
//Atom_index.insert(pair<string,int>("OP1", 2));
//Atom_index.insert(pair<string,int>("OP2", 3));
//Atom_index.insert(pair<string,int>("O5'", 4));
Atom_index.insert(pair<string,int>("C5'", 5));
Atom_index.insert(pair<string,int>("C4'", 6));
Atom_index.insert(pair<string,int>("O4'", 7));
Atom_index.insert(pair<string,int>("C3'", 8));
Atom_index.insert(pair<string,int>("O3'", 9));
Atom_index.insert(pair<string,int>("C2'", 10));
//Atom_index.insert(pair<string,int>("O2'", 11));
Atom_index.insert(pair<string,int>("C1'", 12));
/// purine R (A & G)
/// Pyrimidine Y (C U & T)
Atom_index.insert(pair<string,int>("N9", 20));// R20 B-R1
Atom_index.insert(pair<string,int>("C8", 21));// R21 B-R2
Atom_index.insert(pair<string,int>("N7", 22));// R22
Atom_index.insert(pair<string,int>("C5", 23));// R23 Y22
Atom_index.insert(pair<string,int>("C6", 24));// R24 Y21 B-Y2
Atom_index.insert(pair<string,int>("N1", 25));// R25 Y20 B-Y1
Atom_index.insert(pair<string,int>("C2", 26));// R26 Y25 B-Y3
Atom_index.insert(pair<string,int>("N3", 27));// R27 Y24
Atom_index.insert(pair<string,int>("C4", 28));// R28 Y23 B-R3
/// additional atoms
//Atom_index.insert(pair<string,int>("N6", 30));// A
//Atom_index.insert(pair<string,int>("O6", 31));// G
//Atom_index.insert(pair<string,int>("N2", 32));// G
//Atom_index.insert(pair<string,int>("O2", 33));// C U and T
//Atom_index.insert(pair<string,int>("N4", 34));// C
//Atom_index.insert(pair<string,int>("O4", 35));// U and T
//Atom_index.insert(pair<string,int>("C7", 36));// T
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
string ID="";
string CHAIN, resSEQ, resNAME;
map<int,vector<double> > index_xyz;
string sline;
while(getline(inpdb,sline)) {
vector<string> stoken = string_to_vector_string(sline,0);
if(stoken[0]=="ENDMDL") break;//only first model included
if(!(stoken[0]=="ATOM" ||stoken[0]=="HETATM")) continue;
vector<string> key=pdb_details(sline, 0);
////////////////////////////
string altLoc=key[10]; if(!(altLoc==""||altLoc=="A")) {continue; cout<<sline<<endl; ppause();}
////////////////////////////
string Atom=key[2];
if(Atom.find("H")!=string::npos) continue;
if(Atom.find("*")!=string::npos) {for(int i=0;i<Atom.size();i++) if(Atom[i]=='*') Atom[i]='\'';}
if(Atom.find("O")!=string::npos && Atom.find("P")!=string::npos && Atom.find("1")!=string::npos) Atom="OP1";
if(Atom.find("O")!=string::npos && Atom.find("P")!=string::npos && Atom.find("2")!=string::npos) Atom="OP2";
if(Atom.find("P")!=string::npos && (Atom!="P" && Atom!="OP1" && Atom!="OP2")) continue;
if(Atom_index.count(Atom)==0) continue;
int index=Atom_index[Atom];
////////////////////////////
string resname=key[3];
if(resname=="HOH") continue;
if(resname=="SO4") continue;
if(resname=="MG") continue;
if(resname=="CA") continue;
if(resname=="K") continue;
if(resname=="GUA") resname="G";
if(resname=="URA") resname="U";
if(resname=="CYT") resname="C";
if(resname=="ADE") resname="A";
////////////////////////////
string resseq=key[5];
string chain=key[4];
string id=chain+" "+resseq;
////////////////////////////
if(ID=="") {ID=id; CHAIN=chain; resSEQ=resseq; resNAME=resname; index_xyz.clear();}
////////////////////////////
if(id!=ID) {
if(index_xyz.size()==13||index_xyz.size()==16) {//RNA
vector<double> NTxyz;
/// backbone
for(map<int,vector<double> >::iterator it=index_xyz.begin();it!=index_xyz.end();it++){
int iindex=it->first;
if(iindex>15) break;
vector<double> Axyz=it->second;
if(Axyz.size()!=3) {return 2; cout<<inPDB<<" "<<ID<<" Axyz.size()!=3"<<endl;}
for(int k=0;k<3;k++) NTxyz.push_back(Axyz[k]);
}//for-it
/// base
if(index_xyz.size()==16) {
vector<double> xyzB1=index_xyz[20]; //N9
for(int k=0;k<3;k++) NTxyz.push_back(xyzB1[k]);
vector<double> xyzB2=index_xyz[28]; //C4
for(int k=0;k<3;k++) NTxyz.push_back(xyzB2[k]);
vector<double> xyzB3=index_xyz[21]; //C8
for(int k=0;k<3;k++) NTxyz.push_back(xyzB3[k]);
}
if(index_xyz.size()==13) {
vector<double> xyzB1=index_xyz[25]; //N1
for(int k=0;k<3;k++) NTxyz.push_back(xyzB1[k]);
vector<double> xyzB2=index_xyz[26]; //C2
for(int k=0;k<3;k++) NTxyz.push_back(xyzB2[k]);
vector<double> xyzB3=index_xyz[24]; //C6
for(int k=0;k<3;k++) NTxyz.push_back(xyzB3[k]);
}
Vxyz.push_back(NTxyz);
Vchain.push_back(CHAIN);
Vseq.push_back(resSEQ);
if(!(resNAME=="A"||resNAME=="C"||resNAME=="G"||resNAME=="U")) {
if(index_xyz.size()==13) resNAME="Y";
if(index_xyz.size()==16) resNAME="R";
}
Vres.push_back(resNAME);
}
////////////////////////////
ID=id; CHAIN=chain; resSEQ=resseq; resNAME=resname; index_xyz.clear();
////////////////////////////
}//if(id!=ID)
////////////////////////////
vector<double> xyz;
for(int i=6;i<9;i++) xyz.push_back(atof(key[i].c_str()));
index_xyz.insert(pair<int,vector<double> >(index,xyz));
////////////////////////////
}//while
inpdb.close(); inpdb.clear();
///////////////////////////////////////////////////////////////////
if(index_xyz.size()==13||index_xyz.size()==16) {//RNA
vector<double> NTxyz;
/// backbone
for(map<int,vector<double> >::iterator it=index_xyz.begin();it!=index_xyz.end();it++){
int iindex=it->first;
if(iindex>15) break;
vector<double> Axyz=it->second;
if(Axyz.size()!=3) {return 2; cout<<inPDB<<" "<<ID<<" Axyz.size()!=3"<<endl;}
for(int k=0;k<3;k++) NTxyz.push_back(Axyz[k]);
}//for-it
/// base
if(index_xyz.size()==16) {
vector<double> xyzB1=index_xyz[20]; //N9
for(int k=0;k<3;k++) NTxyz.push_back(xyzB1[k]);
vector<double> xyzB2=index_xyz[28]; //C4
for(int k=0;k<3;k++) NTxyz.push_back(xyzB2[k]);
vector<double> xyzB3=index_xyz[21]; //C8
for(int k=0;k<3;k++) NTxyz.push_back(xyzB3[k]);
}
if(index_xyz.size()==13) {
vector<double> xyzB1=index_xyz[25]; //N1
for(int k=0;k<3;k++) NTxyz.push_back(xyzB1[k]);
vector<double> xyzB2=index_xyz[26]; //C2
for(int k=0;k<3;k++) NTxyz.push_back(xyzB2[k]);
vector<double> xyzB3=index_xyz[24]; //C6
for(int k=0;k<3;k++) NTxyz.push_back(xyzB3[k]);
}
Vxyz.push_back(NTxyz);
Vchain.push_back(CHAIN);
Vseq.push_back(resSEQ);
//cout<<resSEQ<<" "<<resNAME<<" "<<index_xyz.size()<<endl;
if(!(resNAME=="A"||resNAME=="C"||resNAME=="G"||resNAME=="U")) {
if(index_xyz.size()==13) resNAME="Y";
if(index_xyz.size()==16) resNAME="R";
}
Vres.push_back(resNAME);
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
if(sign==0) return 0;
cout<<inPDB<<endl;
for(int i=0;i<Vchain.size();i++) cout<<Vchain[i]<<" "<<Vseq[i]<<" "<<Vres[i]<<endl;
return 0;
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
}
#endif