Deploy_0.9.0/isRNAcirc/Data/header/read_chain.h

isRNAcirc / Data / header / read_chain.h

isRNAcirc/Data/header/read_chain.h · 28.2 KB

// To read RNA chains
// remove P, OP1 OP2 O2'
// only first model if obtained by NMR
// if bases are modified, assign M
// 11.26.2013  by Xiaojun Xu
#ifndef _READ_CHAIN_H_
#define _READ_CHAIN_H_


double dis_2vectors(vector<double> xyz, vector<double> XYZ) {
   //if(xyz.size() != XYZ.size()) {cout << "RMSD_vectors size() error!" << endl; return 100.0; }
   if(xyz.size() !=3 ||  XYZ.size() != 3) {cout <<"ERROR: distance_2vectors size() error!" << endl; return -100.0; }

   double dis = 0.0;  
   for(int i=0;i<3;i++) {
      dis=dis+ (xyz[i]-XYZ[i]) * (xyz[i]-XYZ[i]);
   }
   dis=sqrt(dis);
  return dis;
}
// N: A U G C -- rna-liked 
string check_resName(string resName, map<int,vector<double> > ntMxyz, map<int,string> ntMatom, vector<double> &NTXYZ){
   map<string,int> atomMind;
   int Nbb=0, NY=0, NR=0;
   for(map<int,string>::iterator it=ntMatom.begin();it!=ntMatom.end();it++) {
      string Atom=it->second;
      if(Atom=="C5'") Nbb++;
      if(Atom=="C4'") Nbb++;
      if(Atom=="O4'") Nbb++;
      if(Atom=="C3'") Nbb++;
      if(Atom=="O3'") Nbb++;
      if(Atom=="C2'") Nbb++;
      if(Atom=="C1'") Nbb++;

      if(Atom=="N9") NR++;//
      if(Atom=="C8") NR++;//
      if(Atom=="N7") NR++;
      if(Atom=="C5") NR++;
      if(Atom=="C6") NR++;
      if(Atom=="N1") NR++;
      if(Atom=="C2") NR++;
      if(Atom=="N3") NR++;
      if(Atom=="C4") NR++;//

      if(Atom=="N1") NY++;//
      if(Atom=="C6") NY++;//
      if(Atom=="C5") NY++;
      if(Atom=="C4") NY++;
      if(Atom=="N3") NY++;
      if(Atom=="C2") NY++;//
      atomMind.insert(pair<string,int>(Atom,it->first));
   }
   //if(resName=="4SU") {cout<<ntMxyz.size()<<" "<<Nbb<<" "<<NR<<" "<<NY<<endl; ppause("");}
   //cout<<resName<<"  "<<ntMxyz.size()<<" "<<Nbb<<" "<<NR<<" "<<NY<<endl;
   /////////////////////////////////////////////////////////////////////////////////
   string resname;
   if(ntMxyz.size()<13) resname="M";
   // >=13
   else if(Nbb!=7) resname="M";
   // >=13 Nbb=7
   else if(NY!=6) resname="M";
   // >=13 Nbb=7 NY==6
   else if(NY==6 && NR!=9) { //Y ( C, U)
      if(atomMind.size()==13) resname="Y";
      //>13
      else if(atomMind.count("O2")==0) resname="Y";
      else if(atomMind.size()==14) resname="Y";
      //if(resName=="4SU") {cout<<ntMxyz.size()<<" "<<Nbb<<" "<<NR<<" "<<NY<<endl; ppause("");}
      // contains O2
      else if(atomMind.count("N4")==1 && atomMind.size()==15){
         if(resName=="C") resname="C";
         else resname="c";
      }
      else if(atomMind.count("N4")==1 && atomMind.size()!=15) resname="Y";
      // no N4
      else if(atomMind.count("O4")==1 && atomMind.size()==15){
         if(resName=="U") resname="U";
         else resname="u";
      }
      else if(atomMind.count("O4")==1 && atomMind.size()!=15) resname="Y";
      // no O4
      else resName="Y";
   }//Y
   // >=13 Nbb=7 NY=6 NR=9
   else if(NY==6 && NR==9) { //R ( A, G)
      if(atomMind.size()==16) resname="R";
      // >16
      else if(atomMind.count("N6")==1 && atomMind.size()==17){
         if(resName=="A") resname="A";
         else resname="a";
      }
      else if(atomMind.count("N6")==1 && atomMind.size()!=17) resname="R";
      // no N6
      else if(atomMind.count("O6")==1 && atomMind.count("N2")==1 && atomMind.size()==18){
         if(resName=="G") resname="G";
         else resname="g";
      }
      else  resname="R";
   }//R
   else resname="M";
   /////////////////////////////////////////////////////////////////////////////////
   NTXYZ.clear();
   map<int,int> atom_index;
   if(resname=="N" || resname=="M") {
      int Natom=1;
      for(map<int,vector<double> >::iterator it=ntMxyz.begin();it!=ntMxyz.end();it++) {
         atom_index.insert(pair<int,int>(Natom,it->first)); Natom++;
      }
   }else if(resname=="A"||resname=="G"||resname=="a"||resname=="g"||resname=="R" ) {
      for(map<int,vector<double> >::iterator it=ntMxyz.begin();it!=ntMxyz.end();it++) {
         int Natom=100;
         string Atom=ntMatom[it->first];
         if(Atom=="C5'") Natom=1; 
         if(Atom=="C4'") Natom=2;
         if(Atom=="O4'") Natom=3;
         if(Atom=="C3'") Natom=4;
         if(Atom=="O3'") Natom=5;
         if(Atom=="C2'") Natom=6;
         if(Atom=="C1'") Natom=7;
         if(Atom=="N9") Natom=13;//
         if(Atom=="C8") Natom=14;//
         if(Atom=="N7") Natom=15;
         if(Atom=="C5") Natom=16;
         if(Atom=="C6") Natom=17;
         if(Atom=="N1") Natom=18;
         if(Atom=="C2") Natom=19;
         if(Atom=="N3") Natom=20;
         if(Atom=="C4") Natom=21;//
         if(Natom==100) continue;
         atom_index.insert(pair<int,int>(Natom,it->first));
      }
   }else if(resname=="C"||resname=="U"||resname=="c"||resname=="u"||resname=="Y" ) {
      for(map<int,vector<double> >::iterator it=ntMxyz.begin();it!=ntMxyz.end();it++) {
         int Natom=100;
         string Atom=ntMatom[it->first];
         if(Atom=="C5'") Natom=1; 
         if(Atom=="C4'") Natom=2;
         if(Atom=="O4'") Natom=3;
         if(Atom=="C3'") Natom=4;
         if(Atom=="O3'") Natom=5;
         if(Atom=="C2'") Natom=6;
         if(Atom=="C1'") Natom=7;
         if(Atom=="N1") Natom=13;//
         if(Atom=="C6") Natom=14;//
         if(Atom=="C5") Natom=15;
         if(Atom=="C4") Natom=16;
         if(Atom=="N3") Natom=17;
         if(Atom=="C2") Natom=18;//
         if(Natom==100) continue;
         atom_index.insert(pair<int,int>(Natom,it->first));
      }
   }
   //////
   for(map<int,int>::iterator it=atom_index.begin();it!=atom_index.end();it++) {
      int index=it->second;
      vector<double> atomxyz=ntMxyz[index];
      if(atomxyz.size()!=3)
      {cout<<"ERROR: "<<ntMatom[index]<<" atomxyz.size()!=3 "<< atomxyz.size()<< endl; return "";}
      for(int k=0;k<3;k++) NTXYZ.push_back(atomxyz[k]);
   }//for-map-atom_index
   ////////////////////////////////////
   return resname;
}

int read_chains(string inPDB, vector<string> &resSEQs, vector<vector<string> > &resIDs, vector<vector<vector<double> > > &resXYZs, int sign) {
   //cout<<inPDB<<endl;
   ifstream inpdb (inPDB.c_str());
   if(!inpdb.is_open()) {cout <<"ERROR: "<<inPDB<< " can not open for read!" << endl; return 1;}
   resXYZs.clear(); resSEQs.clear(); resIDs.clear();
   string sline;
   //////
   string resSEQ="";
   vector<vector<double> > resXYZ;
   vector<string> resID;

   string chainID="";
   string resName;
   string ntID="";
   map<int,vector<double> > ntXYZ;
   map<int,string> ntAtom;
   while(getline(inpdb,sline)) {
      //cout << sline << endl;
      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" ||stoken[0]=="TER")) continue;
      if(stoken[0]=="TER") {//end of a chain
         if(ntXYZ.size()>0) { 
            vector<double> resxyz;
            string resNAME=check_resName(resName, ntXYZ, ntAtom, resxyz);
            if(resNAME=="") {cout<<"ERROR: "<<inPDB<<" "<<ntID<<"  "<<ntAtom.size()<<" resNAME==null" << endl; return 1;}
            //cout<<inPDB<<" "<<ntID<<"  "<<ntAtom.size()<<" "<<resNAME << endl; //ppause("");
            resName=resNAME;
            if((resName=="A"||resName=="G"||resName=="a"||resName=="g"||resName=="R") && resxyz.size()!=16*3)
            {cout<<inPDB<<" "<<ntID<<" resxyz.size()!=16*3 " << endl; return 1;}
            if((resName=="C"||resName=="U"||resName=="c"||resName=="u"||resName=="Y") && resxyz.size()!=13*3)
            {cout<<inPDB<<" "<<ntID<<" resxyz.size()!=13*3 "<< resxyz.size()<<endl; return 1;}
            resSEQ+=resNAME;
            resID.push_back(ntID);
            resXYZ.push_back(resxyz);
         }
         //////////////////////////
         if(resSEQ.size()>0) {
            resXYZs.push_back(resXYZ);
            resSEQs.push_back(resSEQ);
            resIDs.push_back(resID);
         }
         //////////////////////////
         resXYZ.clear(); resSEQ=""; resID.clear();
         ntXYZ.clear(); ntAtom.clear();
         chainID=""; resName=""; ntID="";
         continue;
      }
      vector<string> key=pdb_details(sline, 0);
      string Atom=key[2];  if(Atom.find("H")!=string::npos) continue;
      if(Atom.find("*")!=string::npos) { Atom.replace(Atom.find("*"),1,"'"); }
      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.find("P")!=string::npos) continue;
      if(Atom=="O5'") continue;
      //cout << sline << endl;
      ////////
      string altLoc=key[10]; if(!(altLoc==""||altLoc=="A")) {continue; cout << sline << endl; ppause();}
      ////////
      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(key[3]=="GUA" || key[3]=="G") resname="G";
      if(key[3]=="URA" || key[3]=="U") resname="U";
      if(key[3]=="CYT" || key[3]=="C") resname="C";
      if(key[3]=="ADE" || key[3]=="A") resname="A";
      //if(resname!="G"&&resname!="C"&&resname!="A"&&resname!="U") resname="M"; //modified
      //if(resname!="G"&& resname!="C"&& resname!="A"&& resname!="U") continue;
      //if(resname=="M") continue; cout << sline <<endl;
      string chainid=key[4];
      string ntid=key[4]+" "+key[5];
      if(chainID=="") {chainID=chainid; ntID=ntid; resName=resname;}
      ////////
      if(chainid==chainID && ntid!=ntID) {   ///ntID changed
         //cout<< ntID<<"    "<<resName<<"   "<<ntXYZ.size()<<endl;
         if(ntXYZ.size()>0) { 
            //cout<< ntID<<" "<<resName<<" "<<ntXYZ.size()<<"    "<<ntid<<" "<<resname<<"   "<<endl; //ppause("");
            vector<double> resxyz;
            string resNAME=check_resName(resName, ntXYZ, ntAtom, resxyz);
            if(resNAME=="") {cout<<inPDB<<" "<<ntID<<"  "<<ntAtom.size()<<" resNAME==null" << endl; return 1;}
            //cout<<inPDB<<" "<<ntID<<"  "<<ntAtom.size()<<" "<<resNAME << endl; //ppause("");
            resName=resNAME;
            if((resName=="A"||resName=="G"||resName=="a"||resName=="g"||resName=="R") && resxyz.size()!=16*3)
            {cout<<inPDB<<" "<<ntID<<" resxyz.size()!=16*3 " << endl; return 1;}
            if((resName=="C"||resName=="U"||resName=="c"||resName=="u"||resName=="Y") && resxyz.size()!=13*3)
            {cout<<inPDB<<" "<<ntID<<" resxyz.size()!=13*3 "<< resxyz.size()<<endl; return 1;}
            resSEQ+=resNAME;
            resID.push_back(ntID);
            resXYZ.push_back(resxyz);
            //cout<< ntID<<" "<<resName<<" "<<resxyz.size()<< endl; //ppause("");
            if(resName!="M" && resname!="M") {
               vector<double> xyz_nt=ntXYZ[5];
               vector<double> xyz_c, xyz_n;
               for(int k=0;k<3;k++) xyz_c.push_back(xyz_nt[k]);
               for(int k=6;k<9;k++) xyz_n.push_back(atof(key[k].c_str()));
               double dis=dis_2vectors(xyz_c, xyz_n); //cout<<dis<<endl;
               if(dis>10.0 && resSEQ.size()>0) { //starting a new chain
                  resXYZs.push_back(resXYZ);
                  resSEQs.push_back(resSEQ);
                  resIDs.push_back(resID);
                  resXYZ.clear(); resSEQ=""; resID.clear();
               }//if-dis
            }//if-resName
            //////////////////////////
            ntXYZ.clear(); ntAtom.clear();
            resName=resname; ntID=ntid;
         }else{ //ntXYZ.size()==0
            //////////////////////////
            if(resSEQ.size()>0) {
               resXYZs.push_back(resXYZ);
               resSEQs.push_back(resSEQ);
               resIDs.push_back(resID);
            }
            //////////////////////////
            resXYZ.clear(); resSEQ=""; resID.clear();
            ntXYZ.clear();  ntAtom.clear();
            chainID=chainid; resName=resname; ntID=ntid;
         }
         //////////////////////////
      }//if-ntid
      ////////
      if(chainid!=chainID) {   ///chainID changed
         //cout<<ntXYZ.size()<<endl;
         //cout<< ntID<<"    "<<ntid<<endl; ppause("");
         if(ntXYZ.size()>0) { 
            vector<double> resxyz;
            string resNAME=check_resName(resName, ntXYZ, ntAtom, resxyz);
            if(resNAME=="") {cout<<inPDB<<" "<<ntID<<"  "<<ntAtom.size()<<" resNAME==null" << endl; return 1;}
            //cout<<inPDB<<" "<<ntID<<"  "<<ntAtom.size()<<" "<<resNAME << endl; //ppause("");
            resSEQ+=resNAME;
            resID.push_back(ntID);
            resXYZ.push_back(resxyz);
            resName=resNAME;
         }
         //////////////////////////
         if(resSEQ.size()>0) {
            resXYZs.push_back(resXYZ);
            resSEQs.push_back(resSEQ);
            resIDs.push_back(resID);
         }
         //////////////////////////
         resXYZ.clear(); resSEQ=""; resID.clear();
         ntXYZ.clear();  ntAtom.clear();
         chainID=chainid; resName=resname; ntID=ntid;
      }//if-chainid
      /////////////////////////////
      vector<double> xyz;
      for(int i=6;i<9;i++) xyz.push_back(atof(key[i].c_str()));
      int index=100;
      ////backbone
      //if(Atom=="P")   index=1;
      //if(Atom=="OP1") index=2;
      //if(Atom=="OP2") index=3;
      //if(Atom=="O5'") index=4;
      if(Atom=="C5'") index=5;
      if(Atom=="C4'") index=6;
      if(Atom=="O4'") index=7;
      if(Atom=="C3'") index=8;
      if(Atom=="O3'") index=9;
      if(Atom=="C2'") index=10;
      //if(Atom=="O2'") index=11;
      if(Atom=="C1'") index=12;
      /// purine (A & G)
      if(Atom=="N9") index=13;//
      if(Atom=="C8") index=14;//
      if(Atom=="N7") index=15;
      if(Atom=="C5") index=16;
      if(Atom=="C6") index=17;
      if(Atom=="N1") index=18;
      if(Atom=="C2") index=19;
      if(Atom=="N3") index=20;
      if(Atom=="C4") index=21;//
      /// Pyrimidine (C U & T)
      //if(Atom=="N1") index=13;//
      //if(Atom=="C6") index=14;//
      //if(Atom=="C5") index=15;
      //if(Atom=="C4") index=16;
      //if(Atom=="N3") index=17;
      //if(Atom=="C2") index=18;//
      /// additional atoms
      if(Atom=="N6") index=30;  //A
      if(Atom=="O6") index=31;  //G
      if(Atom=="N2") index=32;  //G
      if(Atom=="O2") index=33;  //C U and T
      if(Atom=="N4") index=34;  //C
      if(Atom=="O4") index=35;  //U and T
      if(Atom=="C7") index=36;  //T
      ///////////
      if(index==100) continue;
      //cout<<ntid<<" "<<Atom<<"   "<<index<< endl;
      ntXYZ.insert(pair<int,vector<double> >(index,xyz));
      ntAtom.insert(pair<int,string>(index,Atom));
   }//while
   inpdb.close(); inpdb.clear();  
   //last chain
   if(ntXYZ.size()>0) { 
      vector<double> resxyz;
      string resNAME=check_resName(resName, ntXYZ, ntAtom, resxyz);
      if(resNAME=="") {cout<<inPDB<<" "<<ntID<<"  "<<ntAtom.size()<<" resNAME==null" << endl; return 1;}
      //cout<<inPDB<<" "<<ntID<<"  "<<ntAtom.size()<<" "<<resNAME << endl; //ppause("");
      resName=resNAME;
      if((resName=="A"||resName=="G"||resName=="a"||resName=="g"||resName=="R") && resxyz.size()!=16*3)
      {cout<<inPDB<<" "<<ntID<<" resxyz.size()!=16*3 " << endl; return 1;}
      if((resName=="C"||resName=="U"||resName=="c"||resName=="u"||resName=="Y") && resxyz.size()!=13*3)
      {cout<<inPDB<<" "<<ntID<<" resxyz.size()!=13*3 "<< resxyz.size()<<endl; return 1;}
      resSEQ+=resNAME;
      resID.push_back(ntID);
      resXYZ.push_back(resxyz);
   }
   //////////////////////////
   if(resSEQ.size()>0) {
      resXYZs.push_back(resXYZ);
      resSEQs.push_back(resSEQ);
      resIDs.push_back(resID);
   }
   //////////////////////////
   if(resSEQs.size()!=resIDs.size() || resSEQs.size()!=resXYZs.size())
   {cout << "resSEQs.size()!=resIDs.size() || resSEQs.size()!=resXYZs.size()"<< endl; return 1;}
   // print out chains
   if(sign==0) return 0;
   cout << inPDB << endl;
   int Nchain= resIDs.size();
   cout << "chain number: " << Nchain << endl;
   for(int ii=0;ii<Nchain;ii++) {
      cout << resSEQs[ii] << "  -->> " << resSEQs[ii].size();
      if(resSEQs[ii].find("M")==string::npos) cout << "  RNA" << endl;
      else {cout << "  M"<< endl; }
      for(int nt=0;nt<resSEQs[ii].size();nt++)
      cout << ii<<" "<<nt<< " -  " << resSEQs[ii][nt] <<"  "<<resIDs[ii][nt]<< endl;
   }
   ppause();
   return 0;
}

/*
int read_chains (string inPDB, vector<string> &resSEQs, vector<vector<string> > &resIDs, vector<vector<vector<double> > > &resXYZs, int sign) {
   //cout<<inPDB<<endl;
   ifstream inpdb (inPDB.c_str());
   if(!inpdb.is_open()) {cout << inPDB<< " can not open for read!" << endl; return 1;}
   resXYZs.clear(); resSEQs.clear(); resIDs.clear();
   string sline;
   //////
   string resSEQ="";
   vector<vector<double> > resXYZ;
   vector<string> resID;

   string chainID="";
   string resName;
   string ntID="";
   map<int,vector<double> > ntXYZ;
   while(getline(inpdb,sline)) {
      //cout << sline << endl;
      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" ||stoken[0]=="TER")) continue;
      if(stoken[0]=="TER") {//end of a chain
         if(ntXYZ.size()>0) { 
            if((resName=="A"||resName=="a") && ntXYZ.size()!=16) resSEQ+="M";
            else if ((resName=="U"||resName=="u") && ntXYZ.size()!=13) resSEQ+="M";
            else if ((resName=="G"||resName=="g") && ntXYZ.size()!=16) resSEQ+="M";
            else if ((resName=="C"||resName=="c") && ntXYZ.size()!=13) resSEQ+="M";
            else resSEQ+=resName;
            resID.push_back(ntID); 
            vector<double> resxyz;
            for(map<int,vector<double> >::iterator it=ntXYZ.begin();it!=ntXYZ.end();it++) {
               vector<double> atomxyz=it->second;
               if(atomxyz.size()!=3)
               {cout<<inPDB<<" "<<ntID<<" atomxyz.size()!=3 "<< atomxyz.size()<< endl; return 1;}
               for(int i=0;i<atomxyz.size();i++) resxyz.push_back(atomxyz[i]);
            }
            resXYZ.push_back(resxyz);
         }
         //////////////////////////
         if(resSEQ.size()>0) {
            resXYZs.push_back(resXYZ);
            resSEQs.push_back(resSEQ);
            resIDs.push_back(resID);
         }
         //////////////////////////
         resXYZ.clear(); resSEQ=""; resID.clear();
         ntXYZ.clear();
         chainID=""; resName=""; ntID="";
         continue;
      }
      vector<string> key=pdb_details(sline, 0);
      string Atom=key[2];  if(Atom.find("H")!=string::npos) continue;
      if(Atom.find("*")!=string::npos) { Atom.replace(Atom.find("*"),1,"'"); }
      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.find("P")!=string::npos) continue;
      if(Atom=="O5'") continue;
      //cout << sline << endl;
      ////////
      string altLoc=key[10]; if(!(altLoc==""||altLoc=="A")) {continue; cout << sline << endl; ppause("");}
      ////////
      string resname=key[3];
      if(resname=="HOH") continue;
      if(resname=="MG") continue;
      if(resname=="CA") continue;
      if(resname=="K") continue;
      if(key[3]=="GUA" || key[3]=="G") resname="G";
      else { if(key[3].find("G")!=string::npos) resname="g";}
      if(key[3]=="URA" || key[3]=="U") resname="U";
      else { if(key[3].find("U")!=string::npos) resname="u";}
      if(key[3]=="CYT" || key[3]=="C") resname="C";
      else { if(key[3].find("C")!=string::npos) resname="c";}
      if(key[3]=="ADE" || key[3]=="A") resname="A";
      else { if(key[3].find("A")!=string::npos) resname="a";}
      if(resname!="G"&&resname!="C"&&resname!="A"&&resname!="U"&&resname!="g"&&resname!="c"&&resname!="a"&&resname!="u")
      resname="M"; //modified
      //if(resname!="G"&& resname!="C"&& resname!="A"&& resname!="U") continue;
      //if(resname=="M") continue; cout << sline <<endl;
      string chainid=key[4];
      string ntid=key[4]+" "+key[5];
      if(chainID=="") {chainID=chainid; ntID=ntid; resName=resname;}
      ////////
      if(chainid==chainID && ntid!=ntID) {   ///ntID changed
         //cout<< ntID<<"    "<<ntid<<"   "<<ntXYZ.size()<<endl; ppause("");
         if(ntXYZ.size()>0) { 
            if((resName=="A"||resName=="a") && ntXYZ.size()!=16) resSEQ+="M";
            else if ((resName=="U"||resName=="u") && ntXYZ.size()!=13) resSEQ+="M";
            else if ((resName=="G"||resName=="g") && ntXYZ.size()!=16) resSEQ+="M";
            else if ((resName=="C"||resName=="c") && ntXYZ.size()!=13) resSEQ+="M";
            else resSEQ+=resName; //cout<<resSEQ<<endl; ppause("");
            resID.push_back(ntID); 
            vector<double> resxyz;
            for(map<int,vector<double> >::iterator it=ntXYZ.begin();it!=ntXYZ.end();it++) {
               vector<double> atomxyz=it->second;
               if(atomxyz.size()!=3)
               {cout<<inPDB<<" "<<ntID<<" atomxyz.size()!=3 "<< atomxyz.size()<< endl; return 1;}
               for(int i=0;i<atomxyz.size();i++) resxyz.push_back(atomxyz[i]);
            }
            resXYZ.push_back(resxyz);
         }
         //////////////////////////
         //cout<< ntID<<"    "<<resName<<"   "<<ntXYZ.size()<<endl;
         if(resName!="M" && resname!="M") {
            vector<double> xyz_nt=ntXYZ[5];
            vector<double> xyz_c, xyz_n;
            for(int k=0;k<3;k++) xyz_c.push_back(xyz_nt[k]);
            for(int k=6;k<9;k++) xyz_n.push_back(atof(key[k].c_str()));
            double dis=dis_2vectors(xyz_c, xyz_n); //cout<<dis<<endl;
            if(dis>10.0 && resSEQ.size()>0) { //starting a new chain
               resXYZs.push_back(resXYZ);
               resSEQs.push_back(resSEQ);
               resIDs.push_back(resID);
               resXYZ.clear(); resSEQ=""; resID.clear();
            }//if-dis
         }//if-resName
         //////////////////////////

         ntXYZ.clear();
         resName=resname; ntID=ntid;
      }//if-ntid
      ////////
      if(chainid!=chainID) {   ///chainID changed
         //cout<<ntXYZ.size()<<endl;
         //cout<< ntID<<"    "<<ntid<<endl; ppause("");
         if(ntXYZ.size()>0) { 
            if((resName=="A"||resName=="a") && ntXYZ.size()!=16) resSEQ+="M";
            else if ((resName=="U"||resName=="u") && ntXYZ.size()!=13) resSEQ+="M";
            else if ((resName=="G"||resName=="g") && ntXYZ.size()!=16) resSEQ+="M";
            else if ((resName=="C"||resName=="c") && ntXYZ.size()!=13) resSEQ+="M";
            else resSEQ+=resName;
            resID.push_back(ntID); 
            vector<double> resxyz;
            for(map<int,vector<double> >::iterator it=ntXYZ.begin();it!=ntXYZ.end();it++) {
               vector<double> atomxyz=it->second;
               if(atomxyz.size()!=3)
               {cout<<inPDB<<" "<<ntID<<" atomxyz.size()!=3 "<< atomxyz.size()<< endl; return 1;}
               for(int i=0;i<atomxyz.size();i++) resxyz.push_back(atomxyz[i]);
            }
            resXYZ.push_back(resxyz);
         }
         //////////////////////////
         if(resSEQ.size()>0) {
            resXYZs.push_back(resXYZ);
            resSEQs.push_back(resSEQ);
            resIDs.push_back(resID);
         }
         //////////////////////////
         resXYZ.clear(); resSEQ=""; resID.clear();
         ntXYZ.clear();
         chainID=chainid; resName=resname; ntID=ntid;
      }//if-chainid
      /////////////////////////////
      vector<double> xyz;
      for(int i=6;i<9;i++) xyz.push_back(atof(key[i].c_str()));
      ////backbone
      int index=100;
      //if(resname!="M" && Atom=="P")   index=1;
      //if(resname!="M" && Atom=="OP1") index=2;
      //if(resname!="M" && Atom=="OP2") index=3;
      //if(resname!="M" && Atom=="O5'") index=4;
      if(resname!="M" && Atom=="C5'") index=5;
      if(resname!="M" && Atom=="C4'") index=6;
      if(resname!="M" && Atom=="O4'") index=7;
      if(resname!="M" && Atom=="C3'") index=8;
      if(resname!="M" && Atom=="O3'") index=9;
      if(resname!="M" && Atom=="C2'") index=10;
      //if(resname!="M" && Atom=="O2'") index=11;
      if(resname!="M" && Atom=="C1'") index=12;
      ///-A
      if((resname=="A"||resname=="a") && Atom=="N9") index=13;//
      if((resname=="A"||resname=="a") && Atom=="C8") index=14;//
      if((resname=="A"||resname=="a") && Atom=="N7") index=15;
      if((resname=="A"||resname=="a") && Atom=="C5") index=16;
      if((resname=="A"||resname=="a") && Atom=="C6") index=17;
      //if((resname=="A"||resname=="a") && Atom=="N6") index=18;
      if((resname=="A"||resname=="a") && Atom=="N1") index=19;
      if((resname=="A"||resname=="a") && Atom=="C2") index=20;
      if((resname=="A"||resname=="a") && Atom=="N3") index=21;
      if((resname=="A"||resname=="a") && Atom=="C4") index=22;//
      ///-U
      if((resname=="U"||resname=="u") && Atom=="N1") index=13;//
      if((resname=="U"||resname=="u") && Atom=="C6") index=14;//
      if((resname=="U"||resname=="u") && Atom=="C5") index=15;
      if((resname=="U"||resname=="u") && Atom=="C4") index=16;
      //if((resname=="U"||resname=="u") && Atom=="O4") index=17;
      if((resname=="U"||resname=="u") && Atom=="N3") index=18;
      if((resname=="U"||resname=="u") && Atom=="C2") index=19;//
      //if((resname=="U"||resname=="u") && Atom=="O2") index=20;
      ///-G
      if((resname=="G"||resname=="g") && Atom=="N9") index=13;//
      if((resname=="G"||resname=="g") && Atom=="C8") index=14;//
      if((resname=="G"||resname=="g") && Atom=="N7") index=15;
      if((resname=="G"||resname=="g") && Atom=="C5") index=16;
      if((resname=="G"||resname=="g") && Atom=="C6") index=17;
      //if((resname=="G"||resname=="g") && Atom=="O6") index=18;
      if((resname=="G"||resname=="g") && Atom=="N1") index=19;
      if((resname=="G"||resname=="g") && Atom=="C2") index=20;
      //if((resname=="G"||resname=="g") && Atom=="N2") index=21;
      if((resname=="G"||resname=="g") && Atom=="N3") index=22;
      if((resname=="G"||resname=="g") && Atom=="C4") index=23;//
      ///-C
      if((resname=="C"||resname=="c") && Atom=="N1") index=13;//
      if((resname=="C"||resname=="c") && Atom=="C6") index=14;//
      if((resname=="C"||resname=="c") && Atom=="C5") index=15;
      if((resname=="C"||resname=="c") && Atom=="C4") index=16;
      //if((resname=="C"||resname=="c") && Atom=="N4") index=17;
      if((resname=="C"||resname=="c") && Atom=="N3") index=18;
      if((resname=="C"||resname=="c") && Atom=="C2") index=19;//
      //if((resname=="C"||resname=="c") && Atom=="O2") index=20;
      ///-M
      if(resname=="M") index=atoi(key[1].c_str());
      ///////////
      if(ntXYZ.count(index)==1) index+=50;
      ///////////
      if(index!=100) ntXYZ.insert(pair<int,vector<double> >(index,xyz));
   }//while
   inpdb.close(); inpdb.clear();  
   //last chain
   if(ntXYZ.size()>0) { 
      //cout<<ntXYZ.size()<<endl;
      if((resName=="A"||resName=="a") && ntXYZ.size()!=16) resSEQ+="M";
      else if ((resName=="U"||resName=="u") && ntXYZ.size()!=13) resSEQ+="M";
      else if ((resName=="G"||resName=="g") && ntXYZ.size()!=16) resSEQ+="M";
      else if ((resName=="C"||resName=="c") && ntXYZ.size()!=13) resSEQ+="M";
      else resSEQ+=resName;
      resID.push_back(ntID); 
      vector<double> resxyz;
      for(map<int,vector<double> >::iterator it=ntXYZ.begin();it!=ntXYZ.end();it++) {
         vector<double> atomxyz=it->second;
         if(atomxyz.size()!=3)
         {cout<<inPDB<<" "<<ntID<<" atomxyz.size()!=3 "<< atomxyz.size()<< endl; return 1;}
         for(int i=0;i<atomxyz.size();i++) resxyz.push_back(atomxyz[i]);
      }
      resXYZ.push_back(resxyz);
   }
   //////////////////////////
   if(resSEQ.size()>0) {
      resXYZs.push_back(resXYZ);
      resSEQs.push_back(resSEQ);
      resIDs.push_back(resID);
   }
   //////////////////////////
   if(resSEQs.size()!=resIDs.size() || resSEQs.size()!=resXYZs.size())
   {cout << "resSEQs.size()!=resIDs.size() || resSEQs.size()!=resXYZs.size()"<< endl; return 1;}
   // print out chains
   if(sign==0) return 0;
   cout << inPDB << endl;
   int Nchain= resIDs.size();
   cout << "chain number: " << Nchain << endl;
   for(int ii=0;ii<Nchain;ii++) {
      cout << resSEQs[ii] << "  -->> " << resSEQs[ii].size();
      if(resSEQs[ii].find("M")==string::npos) cout << "  RNA" << endl;
      else {cout << "  M"<< endl; }
      for(int nt=0;nt<resSEQs[ii].size();nt++)
      cout << ii<<" "<<nt<< " -  " << resSEQs[ii][nt] <<"  "<<resIDs[ii][nt]<< endl;
   }
   ppause("");
   return 0;
}
*/
#endif

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