-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsePDB.m
276 lines (216 loc) · 7.54 KB
/
parsePDB.m
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
% Read PDB file
%
% Input:
% filename PDB file name
% System structure with field
% Output:
% Coordinates Nx3 array of coordinates (meters)
% Type cell array of character string with isotope information
% UnitCell
% Connected cell array of vectors containing connectivity information
% Indices_nonWater list of nuclei that are not part of a water molecule
% pbdID pdb ID
% numberH [nProtons nDeuterons nHydrogensTotal]
function pdbData = parsePDB(filename,System)
% Open pdb file.
fh = fopen(filename);
% Load in content of pdb file.
allLines = textscan(fh,'%s','whitespace','','delimiter','\n');
% Close pdb file.
fclose(fh);
% Remove extra lat=yer of cell arrays.
allLines = allLines{1};
% Count number of lines in pdb.
nLines = numel(allLines);
% Initialize variables.
Connected = {};
isSolvent = false(1,nLines);
isWater = false(1,nLines);
Coordinates = zeros(nLines,3);
pdbID = zeros(1,nLines);
MoleculeID = zeros(1,nLines);
Type = cell(1,nLines);
Exchangeable = false(1,nLines);
VanDerWaalsRadii = zeros(1,nLines);
UnitCell.isUnitCell = false;
numberH = [0,0,0];
iNucleus = 0;
% Loop over lines of the pdb.
for iline = 1:nLines
% Get the ith line.
line_ = allLines{iline};
% Check if line is a atom specifier.
if strncmp(line_,'ATOM',4) || strncmp(line_,'HETATM',6)
% Parse information about atom.
% Update count.
iNucleus = iNucleus + 1;
% Get pdb ID #.
pdbID(iNucleus) = sscanf(line_(7:12),'%f');
% Get molucule number.
MoleculeID(iNucleus) = sscanf(line_(23:27),'%f');
% Get coordinates in meters.
Coordinates(iNucleus,:) = sscanf(line_(31:54),'%f %f %f')*System.angstrom; % angstrom -> m
% Get name.
ResidueName_ = line_(18:20);
% Get element.
Element_ = strtrim(line_(77:78));
VanDerWaalsRadii(iNucleus) = getVanDerWaalsRadius(Element_);
% Determine if atom is not part of the solvent.
if ~strcmp(ResidueName_,'WAT') && ~strcmp(ResidueName_,'SOL') ...
&& ~strcmp(ResidueName_,'MGLY') && ~strcmp(ResidueName_,'MGL') ...
&& ~strcmp(ResidueName_,'TIP') && ~strcmp(ResidueName_,'IP3') ...
&& ~strcmp(ResidueName_,'DOP') && ~strcmp(ResidueName_,'OPC')
isSolvent(iNucleus) = false;
else
isSolvent(iNucleus) = true;
end
% Determine if atom is not part of water.
if ~strcmp(ResidueName_,'WAT') && ~strcmp(ResidueName_,'SOL')
isWater(iNucleus) = false;
else
isWater(iNucleus) = true;
end
% Determine if the atom is hydrogen.
if strcmp(Element_,'H')
% Replace H with D depending on options.
if isSolvent(iNucleus)
if System.D2O, Element_ = 'D'; end
else
if System.deuterateProtein, Element_ = 'D'; end
if System.solventOnly, Element_ = 'null'; end
end
end
% Count hydrons.
if strcmp(Element_,'H')
numberH(1) = numberH(1) + 1;
numberH(3) = numberH(3) + 1;
elseif strcmp(Element_,'D')
numberH(2) = numberH(2) + 1;
numberH(3) = numberH(3) + 1;
end
% Record elemental type.
Type{iNucleus} = Element_;
% Check if line contains connection data.
elseif strncmp(line_,'CONECT',6)
% Initialize connetion array.
if isempty(Connected)
Connected = cell(1,iNucleus);
end
if any(line_(7:end)=='*')
continue
end
l = strtrim(line_);
l = reshape(l(7:end),5,[]).';
numbers = str2num(l).';
if isempty(numbers), continue; end
referenceNucleus = numbers(1);
ConnectedNuclei = numbers(2:end);
Connected{referenceNucleus} = unique(...
[Connected{referenceNucleus},ConnectedNuclei]);
for index_ = Connected{referenceNucleus}
if index_ == referenceNucleus
continue
end
if index_>nLines
disp(['CONECT data is not readable:',...
'check that whitspaces separate each number.']);
continue
end
if ~isempty(Connected{index_})
Connected{index_} = [Connected{index_},referenceNucleus];
else
Connected{index_} = referenceNucleus;
end
Connected{index_} = unique(Connected{index_});
end
if strcmp(Type{referenceNucleus},'H') || strcmp(Type{referenceNucleus},'D')
exchangable_ = [];
for jjNuc = Connected{referenceNucleus}
switch Type{jjNuc}
case {'O','M'}
Exchangeable(referenceNucleus) = true;
case 'C'
Exchangeable(referenceNucleus) = false;
otherwise
Exchangeable(referenceNucleus) = System.defaultExchangability;
end
if ~isempty(exchangable_) && exchangable_~=...
Exchangeable(referenceNucleus)
fprintf('Inconsistant exchangability for nucleus %d.\n', ...
referenceNucleus);
disp(' Using System.defaultExchangability.')
break;
end
exchangable_ = Exchangeable(referenceNucleus);
end
end
elseif strncmp(line_,'CRYST1',6)
% Parse information about unit cell
UnitCell.isUnitCell = true;
try
values_ = sscanf(line_(7:66),'%f %f %f %f %f %f %s %i');
catch
values_ = sscanf(line_(7:end),'%f %f %f %f %f %f');
end
UnitCell.ABC = values_(1:3)*System.angstrom; % angstrom -> m
UnitCell.Angles = values_(4:6)*pi/180; % degree -> rad
if length(values_)>=8
UnitCell.spaceGroup = values_(8);
else
UnitCell.spaceGroup = 1;
end
end
end
% Removed unused array slots.
isSolvent(iNucleus+1:end) = [];
Coordinates(iNucleus+1:end,:) = [];
pdbID(iNucleus+1:end) = [];
Type(iNucleus+1:end) = [];
Exchangeable(iNucleus+1:end) = [];
Indices_nonSolvent = find(~isSolvent);
isWater(iNucleus+1:end) = [];
VanDerWaalsRadii(iNucleus+1:end) = [];
Connected(iNucleus+1:end) = [];
if UnitCell.isUnitCell && UnitCell.spaceGroup == -1
[Coordinates,Type,Connected, Indices_nonSolvent, ...
pdbID,MoleculeID,numberH, isSolvent,isWater,Exchangeable,VanDerWaalsRadii]...
= mirrorPDB(Coordinates,Type,Connected, Indices_nonSolvent, ...
pdbID,MoleculeID,numberH, isSolvent,isWater,Exchangeable,VanDerWaalsRadii);
end
% [Coordinates,Type,UnitCell,Connected, Indices_nonSolvent, ...
% pdbID,MoleculeID,numberH, isSolvent,isWater,Exchangeable,VanDerWaalsRadii]
pdbData.Coordinates = Coordinates;
% pdbData.pdbCoordinates = pdbCoordinates;
pdbData. Type = Type;
pdbData. UnitCell = UnitCell;
pdbData. Connected = Connected;
pdbData. Indices_nonSolvent = Indices_nonSolvent;
pdbData. pdbID = pdbID;
pdbData. MoleculeID = MoleculeID;
pdbData. numberH = numberH;
pdbData. isSolvent = isSolvent;
pdbData. isWater = isWater;
pdbData.Exchangeable = Exchangeable;
pdbData.VanDerWaalsRadii = VanDerWaalsRadii;
end
function [Coordinates,Type,Connected, Indices_nonSolvent, ...
pdbID,MoleculeID,numberH, isSolvent,isWater,Exchangeable,VanDerWaalsRadii]...
= mirrorPDB(Coordinates,Type,Connected, Indices_nonSolvent, ...
pdbID,MoleculeID,numberH, isSolvent,isWater,Exchangeable,VanDerWaalsRadii)
Coordinates_ = -Coordinates;
Coordinates = [Coordinates;Coordinates_];
Type = [Type(:);Type(:)]';
N = length(pdbID);
pdbID = [pdbID,pdbID(end)+1:pdbID(end)+N];
numConnect = size(Connected,2);
for ii = 1:numConnect
Connected{ii+N} = Connected{ii} + N;
end
Indices_nonSolvent = [Indices_nonSolvent,Indices_nonSolvent+N];
MoleculeID = [MoleculeID,MoleculeID+MoleculeID(end)];
numberH = 2*numberH;
isSolvent = [isSolvent,isSolvent];
isWater = [isWater,isWater];
Exchangeable = [Exchangeable,Exchangeable];
VanDerWaalsRadii = [VanDerWaalsRadii,VanDerWaalsRadii];
end