Deploy_0.9.0/isRNAcirc/src/header/allatom_to_CG.h

isRNAcirc / src / header / allatom_to_CG.h

isRNAcirc/src/header/allatom_to_CG.h · 9.7 KB

#ifndef ALLATOM_TO_CG_H
#define ALLATOM_TO_CG_H

#define MASS_O 15.999
#define MASS_C 12.016
#define MASS_N 14.007
#define NATOM_BASE_A 10
#define NATOM_BASE_G 11
#define NATOM_BASE_C 8
#define NATOM_BASE_U 8
#define DIS 4.0
#define DELTA 1.0

int allatom_to_cg(vector<string> inAA, vector<string> &outCG, map<string,string> &mapNTID, int sign)
{
  //outCG.push_back("atom_ID Chain_ID Res_ID Res_name atom_type x y z");
  //read residue xyz
  vector<vector<vector<string> > > RESxyzs;
  get_residue_info(inAA,RESxyzs,1,0);
  //check the sugar ring (C4',O4',C3',C2',C1') to identify a nucleotide
  string ATOMS[]={"C4'","O4'","C3'","C2'","C1'"};
  vector<vector<string> > RESxyz;
  for(int ii=0;ii<RESxyzs.size();ii++) 
    for(int jj=0;jj<RESxyzs[ii].size();jj++) {
      vector<string> residue=RESxyzs[ii][jj];
      vector<string> atomid;
      map<string,string> xyz_S;
      for(int kk=0;kk<residue.size();kk++) {
        vector<string> stoken=string_to_vector_string(residue[kk],0);
        string atomname=stoken[4];
        if(count(atomid.begin(), atomid.end(),atomname)==0)
          atomid.push_back(atomname);
        else
          continue;
        if(count(begin(ATOMS),end(ATOMS),atomname)==1) {
          string xyz=stoken[5]+" "+stoken[6]+" "+stoken[7];
          xyz_S.insert(pair<string,string>(atomname,xyz));
        } 
      }

      if(xyz_S.size()!=5) continue;
      //d1=dis(C4'-O4'), d2=dis(O4'-C1'), d3=dis(C1'-C2'), d4=dis(C2'-C3'), d5=dis(C3'-C4')
      vector<string> Vxyz;
      Vxyz.push_back(xyz_S["C4'"]);
      Vxyz.push_back(xyz_S["O4'"]);
      double d1=measure_vec(Vxyz,1);
      Vxyz[0]=xyz_S["C1'"];
      double d2=measure_vec(Vxyz,1);
      Vxyz[1]=xyz_S["C2'"];
      double d3=measure_vec(Vxyz,1);
      Vxyz[0]=xyz_S["C3'"];
      double d4=measure_vec(Vxyz,1);
      Vxyz[1]=xyz_S["C4'"];
      double d5=measure_vec(Vxyz,1);
      int checkS=0;
      if(fabs(d1-1.455)<=DELTA)
        checkS++;
      if(fabs(d2-1.414)<=DELTA)
        checkS++;
      if(fabs(d3-1.525)<=DELTA)
        checkS++;
      if(fabs(d4-1.519)<=DELTA)
        checkS++;
      if(fabs(d5-1.519)<=DELTA)
        checkS++;
      if(checkS==5)
        RESxyz.push_back(residue);
    }
  //check chain break(dis(O3'-P)>1.9); renumber chain_id and res_id; allatom to cg bead
  // 1: P; 2: C4'; 
  // 3: N9 C8 N7 C5 C4 N3; 4: C6 N6; 5: N1 C2; 6: C6 O6 N1; 7: C2 N2;
  // 8: N1 C5 C6; 9: C2 O2; 10: N3 C4 N4; 11: N3 C4 O4;
  string ATOM[]={"P","C4'","O3'","N9","C8","N7","C5","C4","N3","C6","N6",
                 "O6","N1","C2","N2","O2","N4","O4"};
  int index=1;
  int cid=0;
  string CID="";
  vector<double> xyz_O3p;
  int check_O3p;
  for(int ii=0;ii<RESxyz.size();ii++) {
    vector<string> residue=RESxyz[ii];
    string resname,chainid,resid;
    int checkP;
    map<string,vector<double> > atom_xyz;
    for(int jj=0;jj<residue.size();jj++) {
      vector<string> stoken=string_to_vector_string(residue[jj],0);
      chainid=stoken[1];
      resid=stoken[2];
      resname=stoken[3];
      string atomname=stoken[4];
      if(count(begin(ATOM),end(ATOM),atomname)==0) continue;
      vector<double> xyz;
      for(int kk=5;kk<8;kk++)
        xyz.push_back(atof(stoken[kk].c_str()));
      if(atom_xyz.count(atomname)==0)
        atom_xyz.insert(pair<string,vector<double> >(atomname,xyz));
    }
    if(atom_xyz.count("P")==0)
      checkP=0;
    else
      checkP=1;
    if(ii>0) {
      if(chainid!=CID||checkP==0||check_O3p==0) cid++;
      else {
        vector<double> xyz_P=atom_xyz["P"];
        double OPdis=0.0;
        for(int kk=0;kk<3;kk++)
          OPdis+=(xyz_O3p[kk]-xyz_P[kk])*(xyz_O3p[kk]-xyz_P[kk]);
        OPdis=sqrt(OPdis);
        //if(OPdis>DIS) cid++;
      }
    }
    if(atom_xyz.count("O3'")) {
      check_O3p=1;
      xyz_O3p=atom_xyz["O3'"];
      atom_xyz.erase("O3'");
    }
    else {
      check_O3p=0;
      cout<<"Atom O3' is missing in "<<chainid<<"/"<<resid<<"/"<<resname<<" --> Defaulted chain break."<<endl;
    }
    CID=chainid;

    string ntid=to_string(cid)+" "+to_string(ii+1)+" "+resname;
    string ntid_PDB=chainid+"/"+resid+"/"+resname;
    mapNTID.insert(pair<string,string>(ntid,ntid_PDB));
    if(checkP) {
      vector<double> xyz=atom_xyz["P"];
      string str=to_string(index)+" "+ntid+"  1 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);          
      outCG.push_back(str);
      index++;
      atom_xyz.erase("P");
    }
    if(atom_xyz.count("C4'")) {
      vector<double> xyz=atom_xyz["C4'"];
      string str=to_string(index)+" "+ntid+"  2 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]); 
      outCG.push_back(str);
      index++;
      atom_xyz.erase("C4'");
    }
    if(resname=="A") {
      if(atom_xyz.size()!=NATOM_BASE_A) {
        cout<<"Nt "<<ntid_PDB<<" skipped: Atoms missed in base --> "<<atom_xyz.size()<<endl;
        continue;
      }
      double xyz[3]={0.0};
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["N9"][kk]*MASS_N+atom_xyz["C8"][kk]*MASS_C+atom_xyz["N7"][kk]*MASS_N+atom_xyz["C5"][kk]*
                 MASS_C+atom_xyz["C4"][kk]*MASS_C+atom_xyz["N3"][kk]*MASS_N)/(MASS_N*3+MASS_C*3);
      string str=to_string(index)+" "+ntid+"  3 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["C6"][kk]*MASS_C+atom_xyz["N6"][kk]*MASS_N)/(MASS_C+MASS_N);
      str=to_string(index)+" "+ntid+"  4 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["N1"][kk]*MASS_N+atom_xyz["C2"][kk]*MASS_C)/(MASS_C+MASS_N);
      str=to_string(index)+" "+ntid+"  5 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;
    }
    else if(resname=="G") {
      if(atom_xyz.size()!=NATOM_BASE_G) {
        cout<<"Nt "<<ntid_PDB<<" skipped: Atoms missed in base --> "<<atom_xyz.size()<<endl;
        continue;
      }
      double xyz[3]={0.0};
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["N9"][kk]*MASS_N+atom_xyz["C8"][kk]*MASS_C+atom_xyz["N7"][kk]*MASS_N+atom_xyz["C5"][kk]*
                 MASS_C+atom_xyz["C4"][kk]*MASS_C+atom_xyz["N3"][kk]*MASS_N)/(MASS_N*3+MASS_C*3);
      string str=to_string(index)+" "+ntid+"  3 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["C6"][kk]*MASS_C+atom_xyz["O6"][kk]*MASS_O+atom_xyz["N1"][kk]*MASS_N)/(MASS_C+MASS_N+MASS_O);
      str=to_string(index)+" "+ntid+"  6 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["C2"][kk]*MASS_C+atom_xyz["N2"][kk]*MASS_N)/(MASS_C+MASS_N);
      str=to_string(index)+" "+ntid+"  7 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;
    }
    else if(resname=="C") {
      if(atom_xyz.size()!=NATOM_BASE_C) {
        cout<<"Nt "<<ntid_PDB<<" skipped: Atoms missed in base --> "<<atom_xyz.size()<<endl;
        continue;
      }
      double xyz[3]={0.0};
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["N1"][kk]*MASS_N+atom_xyz["C5"][kk]*MASS_C+atom_xyz["C6"][kk]*MASS_C)/(MASS_C*2+MASS_N);
      string str=to_string(index)+" "+ntid+"  8 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["C2"][kk]*MASS_C+atom_xyz["O2"][kk]*MASS_O)/(MASS_C+MASS_O);
      str=to_string(index)+" "+ntid+"  9 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;  
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["N3"][kk]*MASS_N+atom_xyz["C4"][kk]*MASS_C+atom_xyz["N4"][kk]*MASS_N)/(MASS_C+MASS_N*2);
      str=to_string(index)+" "+ntid+" 10 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;  
    }
    else if(resname=="U") {
      if(atom_xyz.size()!=NATOM_BASE_U) {
        cout<<"Nt "<<ntid_PDB<<" skipped: Atoms missed in base --> "<<atom_xyz.size()<<endl;
        continue;
      }
      double xyz[3]={0.0};
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["N1"][kk]*MASS_N+atom_xyz["C5"][kk]*MASS_C+atom_xyz["C6"][kk]*MASS_C)/(MASS_C*2+MASS_N);
      string str=to_string(index)+" "+ntid+"  8 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["C2"][kk]*MASS_C+atom_xyz["O2"][kk]*MASS_O)/(MASS_C+MASS_O);
      str=to_string(index)+" "+ntid+"  9 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;  
      for(int kk=0;kk<3;kk++)
        xyz[kk]=(atom_xyz["N3"][kk]*MASS_N+atom_xyz["C4"][kk]*MASS_C+atom_xyz["O4"][kk]*MASS_O)/(MASS_C+MASS_N+MASS_O);
      str=to_string(index)+" "+ntid+" 11 "+to_string(xyz[0])+" "+to_string(xyz[1])+" "+to_string(xyz[2]);
      outCG.push_back(str);
      index++;  
    }
    else
      continue;
  }

  string str="#Number-of-chains: "+to_string(cid+1)+" Number-of-nt: "+to_string(RESxyz.size());
  //outCG.insert(outCG.begin(),str);
    
  if(sign==1) {
    cout<<"Number of chains: "<<cid+1<<endl;
    cout<<"Number of nucleotides: "<<RESxyz.size()<<endl;
    cout<<"Number of CG beads: "<<outCG.size()-1<<endl;
    cout<<"Nucleotides ID map :"<<endl;
    for(map<string,string>::iterator it=mapNTID.begin(); it!=mapNTID.end(); it++)
      cout<<"  "<<it->first<<" --> "<<it->second<<endl;
    cout<<"atom_ID Chain_ID Res_ID Res_name atom_type x y z"<<endl;
    for(int jj=1;jj<outCG.size();jj++)
      cout<<outCG[jj]<<endl;
  }

  return 0;
}
#endif

← 返回文件树 · 在 GitHub 上查看