forked from 3dem/relion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctf.cpp
313 lines (265 loc) · 10.3 KB
/
ctf.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/***************************************************************************
*
* Author: "Sjors H.W. Scheres"
* MRC Laboratory of Molecular Biology
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* This complete copyright notice must be included in any revised version of the
* source code. Additional authorship citations may be added, but existing
* author citations must be preserved.
***************************************************************************/
/***************************************************************************
*
* Authors: Carlos Oscar S. Sorzano ([email protected])
*
* Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
* All comments concerning this program package may be sent to the
* e-mail address '[email protected]'
***************************************************************************/
#include "src/ctf.h"
#include "src/args.h"
#include "src/fftw.h"
#include "src/metadata_table.h"
/* Read -------------------------------------------------------------------- */
void CTF::read(const MetaDataTable &MD1, const MetaDataTable &MD2, long int objectID)
{
if (!MD1.getValue(EMDL_CTF_VOLTAGE, kV, objectID))
if (!MD2.getValue(EMDL_CTF_VOLTAGE, kV, objectID))
kV=200;
if (!MD1.getValue(EMDL_CTF_DEFOCUSU, DeltafU, objectID))
if (!MD2.getValue(EMDL_CTF_DEFOCUSU, DeltafU, objectID))
DeltafU=0;
if (!MD1.getValue(EMDL_CTF_DEFOCUSV, DeltafV, objectID))
if (!MD2.getValue(EMDL_CTF_DEFOCUSV, DeltafV, objectID))
DeltafV=DeltafU;
if (!MD1.getValue(EMDL_CTF_DEFOCUS_ANGLE, azimuthal_angle, objectID))
if (!MD2.getValue(EMDL_CTF_DEFOCUS_ANGLE, azimuthal_angle, objectID))
azimuthal_angle=0;
if (!MD1.getValue(EMDL_CTF_CS, Cs, objectID))
if (!MD2.getValue(EMDL_CTF_CS, Cs, objectID))
Cs=0;
if (!MD1.getValue(EMDL_CTF_BFACTOR, Bfac, objectID))
if (!MD2.getValue(EMDL_CTF_BFACTOR, Bfac, objectID))
Bfac=0;
if (!MD1.getValue(EMDL_CTF_SCALEFACTOR, scale, objectID))
if (!MD2.getValue(EMDL_CTF_SCALEFACTOR, scale, objectID))
scale=1;
if (!MD1.getValue(EMDL_CTF_Q0, Q0, objectID))
if (!MD2.getValue(EMDL_CTF_Q0, Q0, objectID))
Q0=0;
if (!MD1.getValue(EMDL_CTF_PHASESHIFT, phase_shift, objectID))
if (!MD2.getValue(EMDL_CTF_PHASESHIFT, phase_shift, objectID))
phase_shift=0;
initialise();
}
void CTF::setValues(RFLOAT _defU, RFLOAT _defV, RFLOAT _defAng, RFLOAT _voltage,
RFLOAT _Cs, RFLOAT _Q0, RFLOAT _Bfac, RFLOAT _scale, RFLOAT _phase_shift)
{
kV = _voltage;
DeltafU = _defU;
DeltafV = _defV;
azimuthal_angle = _defAng;
Cs = _Cs;
Bfac = _Bfac;
scale = _scale;
Q0 = _Q0;
phase_shift = _phase_shift;
initialise();
}
/* Read from 1 MetaDataTable ----------------------------------------------- */
void CTF::read(const MetaDataTable &MD)
{
MetaDataTable MDempty;
MDempty.addObject(); // add one empty object
read(MD, MDempty);
}
/** Write to an existing object in a MetaDataTable. */
void CTF::write(MetaDataTable &MD)
{
MD.setValue(EMDL_CTF_VOLTAGE, kV);
MD.setValue(EMDL_CTF_DEFOCUSU, DeltafU);
MD.setValue(EMDL_CTF_DEFOCUSV, DeltafV);
MD.setValue(EMDL_CTF_DEFOCUS_ANGLE, azimuthal_angle);
MD.setValue(EMDL_CTF_CS, Cs);
MD.setValue(EMDL_CTF_BFACTOR, Bfac);
MD.setValue(EMDL_CTF_SCALEFACTOR, scale);
MD.setValue(EMDL_CTF_PHASESHIFT, phase_shift);
MD.setValue(EMDL_CTF_Q0, Q0);
}
/* Write ------------------------------------------------------------------- */
void CTF::write(std::ostream &out)
{
MetaDataTable MD;
MD.addObject();
write(MD);
MD.write(out);
}
/* Default values ---------------------------------------------------------- */
void CTF::clear()
{
kV = 200;
DeltafU = DeltafV = azimuthal_angle = phase_shift = 0;
Cs = Bfac = 0;
Q0 = 0;
scale = 1;
}
/* Initialise the CTF ------------------------------------------------------ */
void CTF::initialise()
{
// Change units
RFLOAT local_Cs = Cs * 1e7;
RFLOAT local_kV = kV * 1e3;
rad_azimuth = DEG2RAD(azimuthal_angle);
// Average focus and deviation
defocus_average = -(DeltafU + DeltafV) * 0.5;
defocus_deviation = -(DeltafU - DeltafV) * 0.5;
// lambda=h/sqrt(2*m*e*kV)
// h: Planck constant
// m: electron mass
// e: electron charge
// lambda=0.387832/sqrt(kV*(1.+0.000978466*kV)); // Hewz: Angstroms
// lambda=h/sqrt(2*m*e*kV)
lambda=12.2643247 / sqrt(local_kV * (1. + local_kV * 0.978466e-6)); // See http://en.wikipedia.org/wiki/Electron_diffraction
// Helpful constants
// ICE: X(u)=-PI/2*deltaf(u)*lambda*u^2+PI/2*Cs*lambda^3*u^4
// = K1*deltaf(u)*u^2 +K2*u^4
K1 = PI / 2 * 2 * lambda;
K2 = PI / 2 * local_Cs * lambda * lambda * lambda;
K3 = atan(Q0/sqrt(1-Q0*Q0));
K4 = -Bfac / 4.;
// Phase shift in radian
K5 = DEG2RAD(phase_shift);
if (Q0 < 0. || Q0 > 1.)
REPORT_ERROR("CTF::initialise ERROR: AmplitudeContrast Q0 cannot be smaller than zero or larger than one!");
if (ABS(DeltafU) < 1e-6 && ABS(DeltafV) < 1e-6 && ABS(Q0) < 1e-6 && ABS(Cs) < 1e-6)
REPORT_ERROR("CTF::initialise: ERROR: CTF initialises to all-zero values. Was a correct STAR file provided?");
}
double CTF::getGamma(double X, double Y)
{
RFLOAT u2 = X * X + Y * Y;
RFLOAT u4 = u2 * u2;
RFLOAT deltaf = getDeltaF(X, Y);
return K1 * deltaf * u2 + K2 * u4 - K5 - K3;
}
RFLOAT CTF::getCtfFreq(RFLOAT X, RFLOAT Y)
{
RFLOAT u2 = X * X + Y * Y;
RFLOAT u = sqrt(u2);
RFLOAT deltaf = getDeltaF(X, Y);
return 2.0 * K1 * deltaf * u + 4.0 * K2 * u * u * u;
}
/* Generate a complete CTF Image ------------------------------------------------------ */
void CTF::getFftwImage(MultidimArray<RFLOAT> &result, int orixdim, int oriydim, RFLOAT angpix,
bool do_abs, bool do_only_flip_phases, bool do_intact_until_first_peak, bool do_damping)
{
RFLOAT xs = (RFLOAT)orixdim * angpix;
RFLOAT ys = (RFLOAT)oriydim * angpix;
FOR_ALL_ELEMENTS_IN_FFTW_TRANSFORM2D(result)
{
RFLOAT x = (RFLOAT)jp / xs;
RFLOAT y = (RFLOAT)ip / ys;
DIRECT_A2D_ELEM(result, i, j) = getCTF(x, y, do_abs, do_only_flip_phases, do_intact_until_first_peak, do_damping);
}
}
/* Generate a complete CTFP (complex) image (with sector along angle) ------------------------------------------------------ */
void CTF::getCTFPImage(MultidimArray<Complex> &result, int orixdim, int oriydim, RFLOAT angpix,
bool is_positive, float angle)
{
if (angle < 0 || angle >= 360.)
REPORT_ERROR("CTF::getCTFPImage: angle should be in [0,360>");
// Angles larger than 180, are the inverse of the other half!
if (angle >= 180.)
{
angle -= 180.;
is_positive = !is_positive;
}
float anglerad = DEG2RAD(angle);
RFLOAT xs = (RFLOAT)orixdim * angpix;
RFLOAT ys = (RFLOAT)oriydim * angpix;
FOR_ALL_ELEMENTS_IN_FFTW_TRANSFORM2D(result)
{
RFLOAT x = (RFLOAT)jp / xs;
RFLOAT y = (RFLOAT)ip / ys;
RFLOAT myangle = (x*x+y*y > 0) ? acos(y/sqrt(x*x+y*y)) : 0; // dot-product with Y-axis: (0,1)
if (myangle >= anglerad)
DIRECT_A2D_ELEM(result, i, j) = getCTFP(x, y, is_positive);
else
DIRECT_A2D_ELEM(result, i, j) = getCTFP(x, y, !is_positive);
}
// Special line along the vertical Y-axis, where FFTW stores both Friedel mates and Friedel symmetry needs to remain
if (angle == 0.)
{
int dim = YSIZE(result);
int hdim = dim/2;
for (int i = hdim + 1; i < dim; i++)
DIRECT_A2D_ELEM(result, i, 0) = conj(DIRECT_A2D_ELEM(result, dim-i, 0));
}
}
void CTF::getCenteredImage(MultidimArray<RFLOAT> &result, RFLOAT Tm,
bool do_abs, bool do_only_flip_phases, bool do_intact_until_first_peak, bool do_damping)
{
result.setXmippOrigin();
RFLOAT xs = (RFLOAT)XSIZE(result) * Tm;
RFLOAT ys = (RFLOAT)YSIZE(result) * Tm;
FOR_ALL_ELEMENTS_IN_ARRAY2D(result)
{
RFLOAT x = (RFLOAT)j / xs;
RFLOAT y = (RFLOAT)i / ys;
A2D_ELEM(result, i, j) = getCTF(x, y, do_abs, do_only_flip_phases, do_intact_until_first_peak, do_damping);
}
}
void CTF::get1DProfile(MultidimArray < RFLOAT > &result, RFLOAT angle, RFLOAT Tm,
bool do_abs, bool do_only_flip_phases, bool do_intact_until_first_peak, bool do_damping)
{
result.setXmippOrigin();
RFLOAT xs = (RFLOAT)XSIZE(result) * Tm; // assuming result is at the image size!
FOR_ALL_ELEMENTS_IN_ARRAY1D(result)
{
RFLOAT x = (COSD(angle) * (RFLOAT)i) / xs;
RFLOAT y = (SIND(angle) * (RFLOAT)i) / xs;
A1D_ELEM(result, i) = getCTF(x, y, do_abs, do_only_flip_phases, do_intact_until_first_peak, do_damping);
}
}
void CTF::applyWeightEwaldSphereCurvature(MultidimArray < RFLOAT > &result, int orixdim, int oriydim,
RFLOAT angpix, RFLOAT particle_diameter)
{
RFLOAT xs = (RFLOAT)orixdim * angpix;
RFLOAT ys = (RFLOAT)oriydim * angpix;
FOR_ALL_ELEMENTS_IN_FFTW_TRANSFORM2D(result)
{
RFLOAT x = (RFLOAT)jp / xs;
RFLOAT y = (RFLOAT)ip / ys;
RFLOAT deltaf = fabs(getDeltaF(x, y));
RFLOAT inv_d = sqrt(x*x + y*y);
RFLOAT aux = (2.*deltaf*lambda*inv_d)/(particle_diameter);
RFLOAT A = (aux > 1.) ? 0. : (2./PI) * (acos(aux) - aux * sin(acos(aux)));
DIRECT_A2D_ELEM(result, i, j) = 1. + A * (2.*fabs(getCTF(x, y)) - 1.);
// Keep everything on the same scale inside RELION, where we use sin(chi), not 2sin(chi)
DIRECT_A2D_ELEM(result, i, j) *= 0.5;
}
}