forked from plumed/plumed2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDB.cpp
235 lines (207 loc) · 7.22 KB
/
PDB.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright (c) 2013 The plumed team
(see the PEOPLE file at the root of the distribution for a list of names)
See http://www.plumed-code.org for more information.
This file is part of plumed, version 2.0.
plumed is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
plumed is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with plumed. If not, see <http://www.gnu.org/licenses/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#include "PDB.h"
#include "Tools.h"
#include <cstdio>
#include <iostream>
using namespace std;
namespace PLMD{
const std::vector<Vector> & PDB::getPositions()const{
return positions;
}
const std::vector<double> & PDB::getOccupancy()const{
return occupancy;
}
const std::vector<double> & PDB::getBeta()const{
return beta;
}
const std::vector<std::string> & PDB::getRemark()const{
return remark;
}
const std::vector<AtomNumber> & PDB::getAtomNumbers()const{
return numbers;
}
std::string PDB::getAtomName(AtomNumber a)const{
std::map<AtomNumber,unsigned>::const_iterator p;
p=number2index.find(a);
if(p==number2index.end()) return "";
else return atomsymb[p->second];
}
unsigned PDB::getResidueNumber(AtomNumber a)const{
std::map<AtomNumber,unsigned>::const_iterator p;
p=number2index.find(a);
if(p==number2index.end()) return 0;
else return residue[p->second];
}
std::string PDB::getResidueName(AtomNumber a) const{
std::map<AtomNumber,unsigned>::const_iterator p;
p=number2index.find(a);
if(p==number2index.end()) return "";
else return residuenames[p->second];
}
unsigned PDB::size()const{
return positions.size();
}
bool PDB::readFromFilepointer(FILE *fp,bool naturalUnits,double scale){
//cerr<<file<<endl;
bool file_is_alive=false;
if(naturalUnits) scale=1.0;
string line;
fpos_t pos;
while(Tools::getline(fp,line)){
//cerr<<line<<"\n";
fgetpos (fp,&pos);
while(line.length()<80) line.push_back(' ');
string record=line.substr(0,6);
string serial=line.substr(6,5);
string atomname=line.substr(12,4);
string residuename=line.substr(17,3);
string chainID=line.substr(21,1);
string resnum=line.substr(22,4);
string x=line.substr(30,8);
string y=line.substr(38,8);
string z=line.substr(46,8);
string occ=line.substr(54,6);
string bet=line.substr(60,6);
Tools::trim(record);
if(record=="TER"){file_is_alive=true; break;}
if(record=="END"){file_is_alive=true; break;}
if(record=="ENDMDL"){file_is_alive=true; break;}
if(record=="REMARK"){
vector<string> v1; v1=Tools::getWords(line.substr(6));
remark.insert(remark.begin(),v1.begin(),v1.end());
}
if(record=="ATOM" || record=="HETATM"){
AtomNumber a; unsigned resno;
double o,b;
Vector p;
Tools::convert(serial,a);
Tools::convert(resnum,resno);
Tools::convert(occ,o);
Tools::convert(bet,b);
Tools::convert(x,p[0]);
Tools::convert(y,p[1]);
Tools::convert(z,p[2]);
// scale into nm
p*=scale;
numbers.push_back(a);
number2index[a]=positions.size();
std::size_t startpos=atomname.find_first_not_of(" \t");
std::size_t endpos=atomname.find_last_not_of(" \t");
atomsymb.push_back( atomname.substr(startpos, endpos-startpos+1) );
residue.push_back(resno);
chain.push_back(chainID);
occupancy.push_back(o);
beta.push_back(b);
positions.push_back(p);
residuenames.push_back(residuename);
}
}
return file_is_alive;
}
void PDB::setArgKeyword( const std::string& new_args ){
bool replaced=false;
for(unsigned i=0;i<remark.size();++i){
if( remark[i].find("ARG=")!=std::string::npos){
remark[i]=new_args; replaced=true;
}
}
plumed_assert( replaced );
}
bool PDB::read(const std::string&file,bool naturalUnits,double scale){
FILE* fp=fopen(file.c_str(),"r");
if(!fp) return false;
readFromFilepointer(fp,naturalUnits,scale);
fclose(fp);
return true;
}
void PDB::getChainNames( std::vector<std::string>& chains ) const {
chains.resize(0);
chains.push_back( chain[0] );
for(unsigned i=1;i<size();++i){
if( chains[chains.size()-1]!=chain[i] ) chains.push_back( chain[i] );
}
}
void PDB::getResidueRange( const std::string& chainname, unsigned& res_start, unsigned& res_end, std::string& errmsg ) const {
bool inres=false, foundchain=false;
for(unsigned i=0;i<size();++i){
if( chain[i]==chainname ){
if(!inres){
if(foundchain) errmsg="found second start of chain named " + chainname;
res_start=residue[i];
}
inres=true; foundchain=true;
} else if( inres && chain[i]!=chainname ){
inres=false;
res_end=residue[i-1];
}
}
if(inres) res_end=residue[size()-1];
}
void PDB::getAtomRange( const std::string& chainname, AtomNumber& a_start, AtomNumber& a_end, std::string& errmsg ) const {
bool inres=false, foundchain=false;
for(unsigned i=0;i<size();++i){
if( chain[i]==chainname ){
if(!inres){
if(foundchain) errmsg="found second start of chain named " + chainname;
a_start=numbers[i];
}
inres=true; foundchain=true;
} else if( inres && chain[i]!=chainname ){
inres=false;
a_end=numbers[i-1];
}
}
if(inres) a_end=numbers[size()-1];
}
bool PDB::getBackbone( const unsigned& resnumber, const std::vector<std::string>& backnames, std::vector<AtomNumber>& backnumbers ) const {
if( backnames.size()!=backnumbers.size() ) plumed_merror("mismatch between number of backbone names and number of backbone numbers");
bool foundresidue=false; std::vector<bool> found( backnames.size(), false );
for(unsigned i=0;i<size();++i){
if( residue[i]==resnumber ){
foundresidue=true;
for(unsigned bno=0;bno<backnames.size();++bno){
if( backnames[bno]==atomsymb[i] ){ backnumbers[bno]=numbers[i]; found[bno]=true; }
}
}
}
if(!foundresidue){ return false; }
for(unsigned i=0;i<found.size();++i){
if( !found[i] ) return false;
}
return true;
}
std::string PDB::getChainID(const unsigned& resnumber) const {
for(unsigned i=0;i<size();++i){
if(resnumber==residue[i]) return chain[i];
}
plumed_merror("Not enough residues in pdb input file");
}
void PDB::renameAtoms( const std::string& old_name, const std::string& new_name ){
for(unsigned i=0;i<size();++i){
if( atomsymb[i]==old_name ) atomsymb[i]=new_name;
}
}
Log& operator<<(Log& ostr, const PDB& pdb){
char buffer[1000];
for(unsigned i=0;i<pdb.positions.size();i++){
sprintf(buffer,"ATOM %3d %8.3f %8.3f %8.3f\n",pdb.numbers[i].serial(),pdb.positions[i][0],pdb.positions[i][1],pdb.positions[i][2]);
ostr<<buffer;
}
return ostr;
}
}