1 #include "PDBReader.h" 2 3 #include "stringutilities.h" 4 #include "Report.h" 5 #include "mathutilities.h" 6 7 using std::string; 8 using std::vector; 9 using std::endl; 10 using std::find; 11 using std::stringstream; 12 using namespace ProtoMol::Report; 13 14 //#define DEBUG_PDB 15 16 namespace ProtoMol { 17 //_________________________________________________________________PDBReader 18 PDBReader()19 PDBReader::PDBReader(): 20 Reader(), 21 myCoords(NULL),myAtoms(NULL){} 22 PDBReader(const std::string & filename)23 PDBReader::PDBReader(const std::string& filename): 24 Reader(filename), 25 myCoords(NULL),myAtoms(NULL){} 26 27 ~PDBReader()28 PDBReader::~PDBReader(){ 29 if(myCoords != NULL) 30 delete myCoords; 31 if(myAtoms != NULL) 32 delete myAtoms; 33 } 34 tryFormat()35 bool PDBReader::tryFormat(){ 36 if(!open()) 37 return false; 38 do { 39 string record,str; 40 record = getline(); 41 stringstream ss(record); 42 ss >> str; 43 if("ATOM" == str){ 44 close(); 45 return true; 46 } 47 } while (!myFile.eof()); 48 myFile.setstate(std::ios::failbit); 49 close(); 50 return false; 51 } 52 read()53 bool PDBReader::read() { 54 if(myCoords == NULL) 55 myCoords = new Vector3DBlock(); 56 if(myAtoms == NULL) 57 myAtoms = new std::vector<PDB::PDBAtom>(); 58 return read(*myCoords,*myAtoms); 59 } 60 read(PDB & pdb)61 bool PDBReader::read(PDB& pdb){ 62 return read(pdb.coords,pdb.atoms); 63 } 64 read(Vector3DBlock & coords,std::vector<PDB::PDBAtom> & atoms)65 bool PDBReader::read(Vector3DBlock& coords, std::vector<PDB::PDBAtom>& atoms) { 66 if(!tryFormat()) 67 return false; 68 if(!open()) 69 return false; 70 coords.cleaR(); 71 atoms.clear(); 72 73 // Now we want to read data in until the record name is "END", then stop. 74 int big = 0; 75 int toBig = 0; 76 myComment = ""; 77 do{ 78 string record(getline()); 79 if(equalStart("END",record)) 80 break; 81 if(equalStart("REMARK",record) || record.empty()){ 82 record = removeBeginEndBlanks(record); 83 if(!record.empty()) 84 myComment += (myComment.empty()?"":"\n")+record; 85 continue; 86 } 87 if(equalStart("ATOM",record)){ 88 record = getRightFill(record,80); 89 string str = removeBeginEndBlanks(record.substr(PDB::PDBAtom::S_RES_SEQ,PDB::PDBAtom::L_RES_SEQ)); 90 int resSeq = toInt(str); 91 if(!isInt(str)){ 92 if(str.size() == 4 && isInt(string(&str[1],&str[4])) && str[0] >= 'A' && str[0] <= 'Z'){ 93 resSeq = (str[0] - 'A') * 1000 + 10000 + toInt(string(&str[1],&str[4])); 94 ++big; 95 } 96 else { 97 resSeq = -1; 98 ++toBig; 99 } 100 } 101 102 atoms.push_back(PDB::PDBAtom(removeBeginEndBlanks(record.substr(PDB::PDBAtom::S_RECORD_NAME,PDB::PDBAtom::L_RECORD_NAME)), 103 toInt(record.substr(PDB::PDBAtom::S_SERIAL,PDB::PDBAtom::L_SERIAL)), 104 removeBeginEndBlanks(record.substr(PDB::PDBAtom::S_ATOM_NAME,PDB::PDBAtom::L_ATOM_NAME)), 105 removeBeginEndBlanks(record.substr(PDB::PDBAtom::S_ALT_LOC,PDB::PDBAtom::L_ALT_LOC)), 106 removeBeginEndBlanks(record.substr(PDB::PDBAtom::S_RES_NAME,PDB::PDBAtom::L_RES_NAME)), 107 removeBeginEndBlanks(record.substr(PDB::PDBAtom::S_CHAIN_ID,PDB::PDBAtom::L_CHAIN_ID)), 108 resSeq, 109 removeBeginEndBlanks(record.substr(PDB::PDBAtom::S_I_CODE,PDB::PDBAtom::L_I_CODE)), 110 toReal(record.substr(PDB::PDBAtom::S_OCCUP,PDB::PDBAtom::L_OCCUP)), 111 toReal(record.substr(PDB::PDBAtom::S_TEMP,PDB::PDBAtom::L_TEMP)), 112 toInt(record.substr(PDB::PDBAtom::S_FOOT_NOTE,PDB::PDBAtom::L_FOOT_NOTE)), 113 removeBeginEndBlanks(record.substr(PDB::PDBAtom::S_SEG_ID,PDB::PDBAtom::L_SEG_ID)), 114 removeBeginEndBlanks(record.substr(PDB::PDBAtom::S_ELEMENT_SYMBOL,PDB::PDBAtom::L_ELEMENT_SYMBOL)), 115 removeBeginEndBlanks(record.substr(PDB::PDBAtom::S_CHARGE,PDB::PDBAtom::L_CHARGE)), 116 0)); 117 coords.push_back(Vector3D(toReal(record.substr(PDB::PDBAtom::S_X,PDB::PDBAtom::L_X)), 118 toReal(record.substr(PDB::PDBAtom::S_Y,PDB::PDBAtom::L_Y)), 119 toReal(record.substr(PDB::PDBAtom::S_Z,PDB::PDBAtom::L_Z)))); 120 } 121 else{ 122 report << recoverable << "[PDB::read] Record unknow:\'"<<record<<"\'."<<endr; 123 } 124 } 125 while (!(myFile.eof())); 126 127 if(big > 0) 128 report << hint << "[PDB::read] Found "<<big<<" X-Plor residue number(s) starting with a character."<<endr; 129 if(toBig > 0){ 130 report << recoverable << "[PDB::read] Found "<<toBig<<" non interger/X-Plor residue number(s)."<<endr; 131 } 132 133 close(); 134 return !myFile.fail(); 135 } 136 orphanCoords()137 Vector3DBlock* PDBReader::orphanCoords(){ 138 Vector3DBlock* tmp = myCoords; 139 myCoords = NULL; 140 return tmp; 141 } 142 orphanAtoms()143 std::vector<PDB::PDBAtom>* PDBReader::orphanAtoms(){ 144 std::vector<PDB::PDBAtom>* tmp = myAtoms; 145 myAtoms = NULL; 146 return tmp; 147 } 148 getPDB() const149 PDB PDBReader::getPDB() const{ 150 PDB res; 151 if(myCoords != NULL) 152 res.coords = (*myCoords); 153 if(myAtoms != NULL) 154 res.atoms = (*myAtoms); 155 return res; 156 } 157 operator >>(PDBReader & pdbReader,PDB & pdb)158 PDBReader& operator>>(PDBReader& pdbReader, PDB& pdb){ 159 pdbReader.read(pdb.coords,pdb.atoms); 160 return pdbReader; 161 } 162 operator >>(PDBReader & pdbReader,Vector3DBlock & coords)163 PDBReader& operator>>(PDBReader& pdbReader, Vector3DBlock& coords){ 164 if(pdbReader.myAtoms == NULL) 165 pdbReader.myAtoms = new std::vector<PDB::PDBAtom>(); 166 pdbReader.read(coords,*pdbReader.myAtoms); 167 return pdbReader; 168 } 169 operator >>(PDBReader & pdbReader,XYZ & xyz)170 PDBReader& operator>>(PDBReader& pdbReader, XYZ& xyz){ 171 if(pdbReader.myAtoms == NULL) 172 pdbReader.myAtoms = new std::vector<PDB::PDBAtom>(); 173 if(pdbReader.read(xyz.coords,*pdbReader.myAtoms)){ 174 xyz.names.resize(xyz.coords.size()); 175 for(unsigned int i=0;i<xyz.coords.size();++i) 176 xyz.names[i] = (*pdbReader.myAtoms)[i].elementName; 177 } 178 return pdbReader; 179 } 180 181 } 182 183