-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
478 lines (446 loc) · 18.9 KB
/
main.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/***************************************************************************
*
* Authors: Yuxuan Chen
*
* Copyright (C) 2021 Pattern Recognition and Bioinformatics Group, Shanghai Jiao Tong University
*
* Licensed under the GNU General Public License v3.0 (see LICENSE for details)
*
* All comments concerning this program package may be sent to e-mail address 'yxchen11@sjtu.edu.cn'
***************************************************************************/
#include <iostream>
#include <map>
#include "mrc.h"
#include "image2D.h"
#include "align.h"
#include <ctime>
#include "matrix.h"
#include "xmippXMD.h"
#include "noise.h"
#include "alignEvaluation.h"
using namespace std;
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
bool getParams(int argc, char *argv[], map<string, string> ¶ms)
{
bool helpFlag = false;
for (int i = 1; i < argc; i += 1)
{
string str = argv[i];
if (str == "-h")
{
cout << BOLDRED << "USAGE: " << RESET << endl;
cout << "\tAlign a set of images." << endl;
cout << BOLDRED << "OPTIONS: " << RESET << endl;
cout << BLUE << "[-h]" << RESET << endl;
cout << "\t Show commandline instructions." << endl;
cout << BLUE << "[-i <string>]" << RESET << endl;
cout << "\t Path of images to be aligned." << endl;
cout << BLUE << "[-o <string>]" << RESET << endl;
cout << "\t Path of output aligned images." << endl;
cout << BLUE << R"([-m <"LM" or "MD" or "DA">])" << RESET << endl;
cout << "\t Method used for alignment. LM: Local Maximum. MD: Main Direction. DA: Direct Alignment" << endl;
cout << BLUE << R"([-e <"LM" or "MD">])" << RESET << endl;
cout
<< "\t Estimation of parameters of alignment method using all data. LM: Local Maximum. MD: Main Direction. Only mandatory parameters (wl for Main Direction method and wh for Local Maximum method) will be estimated."
<< endl;
cout << GREEN << R"([-r <string: "XMIPP" or "RFAA" or path>])" << RESET << endl;
cout
<< "\t Reference image for alignment. XMIPP for XMIPP's way of generating reference. RFAA for SPIDER's way of generating reference. Path for given path of reference image. If none, XMIPP's way will be used."
<< endl;
cout << GREEN << "[-wh <integer>]" << RESET << endl;
cout << "\t Radius for high-pass filter. Required by Local Maximum method and optional for Main Direction method. If none, automatic estimation using 1/10 data will be executed for Local Maximum method and value set to zero for Main Direction method." << endl;
cout << GREEN << "[-wl <integer>]" << RESET << endl;
cout << "\t Radius for low-pass filter. Required by Main Direction method. If none, automatic estimation using 1/10 data will be executed." << endl;
cout << GREEN << "[-th <float>]" << RESET << endl;
cout << "\t Threshold for power spectrum cutting. Used by Main Direction method. If none, default value will be used." << endl;
cout << GREEN << R"([-f <"True" or "False">])" << RESET << endl;
cout
<< "\t Whether to use fine search. Note fine search takes much more time so that it should only be used when normal ways doesn't work. If none, it will be set to False."
<< endl;
cout << GREEN << R"([-x <"True" or "False">])" << RESET << endl;
cout
<< "\t Whether to output XMIPP middle file. Set it to true if you would like to use this alignment method in NCEM, SPREAD or other program that makes use of XMIPP libraries. If none, it will be set to False."
<< endl;
cout << GREEN << R"([-u <"True" or "False">])" << RESET << endl;
cout
<< "\t Whether to output update reference during progress. Note it should be set to True if XMIPP's way of generating reference is used. If none, it will be set to True."
<< endl;
cout << BOLDRED << "EXAMPLES: " << RESET << endl;
cout << CYAN << "Estimate width: " << RESET << endl;
cout << "\t ./EMAF -i /path/to/input -o /path/to/output -e LM" << endl;
cout << CYAN << "Align using Local Maximum: " << RESET << endl;
cout << "\t ./EMAF -i /path/to/input -o /path/to/output -m LM -wh 6" << endl;
cout << CYAN << "Align using Main Direction: " << RESET << endl;
cout << "\t ./EMAF -i /path/to/input -o /path/to/output -m MD -wl 10" << endl;
helpFlag = true;
break;
}
else if (str == "-i")
{
params.insert(pair<string, string>("-i", argv[i + 1]));
i += 1;
}
else if (str == "-o")
{
params.insert(pair<string, string>("-o", argv[i + 1]));
i += 1;
}
else if (str == "-m")
{
params.insert(pair<string, string>("-m", argv[i + 1]));
i += 1;
}
else if (str == "-e")
{
params.insert(pair<string, string>("-e", argv[i + 1]));
i += 1;
}
else if (str == "-r")
{
params.insert(pair<string, string>("-r", argv[i + 1]));
i += 1;
}
else if (str == "-wl")
{
params.insert(pair<string, string>("-wl", argv[i + 1]));
i += 1;
}
else if (str == "-wh")
{
params.insert(pair<string, string>("-wh", argv[i + 1]));
i += 1;
}
else if (str == "-th")
{
params.insert(pair<string, string>("-th", argv[i + 1]));
i += 1;
}
else if (str == "-f")
{
params.insert(pair<string, string>("-f", argv[i + 1]));
i += 1;
}
else if (str == "-x")
{
params.insert(pair<string, string>("-x", argv[i + 1]));
i += 1;
}
else if (str == "-u")
{
params.insert(pair<string, string>("-u", argv[i + 1]));
i += 1;
}
else
throw baseException("Error: Unsupported input type! Use -h to see commandline instructions.");
}
if (!helpFlag)
{
if (params.count("-i") == 0)
throw baseException("Error: Input path is in need! Use -h to see commandline instructions.");
if (params.count("-o") == 0)
throw baseException("Error: Output path is in need!");
if (params.count("-m") == 0 && params.count("-e") == 0)
throw baseException("Error: One task should be specified, estimation or alignment! Use -h to see commandline instructions.");
if (params.count("-m") != 0 && params.count("-e") != 0)
throw baseException("Error: Only one task should be specified, estimation or alignment! Use -h to see commandline instructions.");
}
return helpFlag;
}
/// Please see ./EMAF -h for input format instructions.
/// Though some xmd files are supported, it is strongly suggested to convert input data into MRC format.
/// When estimation of filter's parameters is not accurate, manually fine tuning might help.
/// The source code is compiled into RELEASE version by default as specified in CMakeLists.txt. You may change it if DEBUG purpose is needed.
/// If you would like to look into the code and want to run the program with less RAM, you may change "stackReal" in code to "fileStack" which would make use of the disk and greatly reduce the demand for RAM. Such change also helps with the big data size problem.
/// Evaluation functions are provided in 'evaluation' folder with proper comments.
int main(int argc, char *argv[])
{
map<string, string> params;
/// As shown below, you may comment the below line and directly set params to avoid commandline inputs.
/**
* bool helpFlag = false;
* params.insert(pair<string, string>("-i", "/path/to/input"));
* params.insert(pair<string, string>("-o", "/path/to/output"));
* params.insert(pair<string, string>("-m", "MD"));
* **/
bool helpFlag = getParams(argc, argv, params);
if (helpFlag)
return 0;
else
{
bool XMIPP;
if (params.count("-m") != 0)
{
if (params.count("-x") == 0)
XMIPP = false;
else if (params["-x"] == "True")
XMIPP = true;
else if (params["-x"] == "False")
XMIPP = false;
else
throw baseException("Error: Unsupported XMIPP flag! Use -h to see commandline instructions.");
}
string pathIn = params["-i"];
string pathOut = params["-o"];
cout << "Input path: " << pathIn << endl;
cout << "Output path: " << pathOut << endl;
std::istringstream sin(pathIn);
std::vector<std::string> fields;
if (pathIn[0] == '.')
fields.emplace_back(".");
std::string field;
while (getline(sin, field, '.'))
{
fields.emplace_back(field);
}
std::istringstream sin2(pathOut);
std::vector<std::string> fields2;
if (pathOut[0] == '.')
fields2.emplace_back(".");
std::string field2;
while (getline(sin2, field2, '.'))
{
fields2.emplace_back(field2);
}
std::string name;
for (int i = 0; i < fields2.size() - 1; ++i)
name += fields2[i];
stackReal<double> data;
vector<string> info;
/// "xmd" file for certain XMIPP format. Again, MRC file is strongly recommended.
if (fields[fields.size() - 1] == "xmd")
{
data = readXMD(pathIn, info);
}
else if (fields[fields.size() - 1] == "mrcs" || fields[fields.size() - 1] == "mrc")
{
mrcFile mrc = mrcFile();
mrc.read(pathIn);
data = mrc.data;
if (XMIPP)
generateInfo(pathIn, data.shape[0], info);
}
else
{
throw baseException("Error: Unsupported file format! Only MRC or XMD files are supported.");
}
if (params.count("-m") == 0)
{
string nameMethod = params["-e"];
clock_t st, et;
st = clock();
if (nameMethod == "LM")
{
int width = circleEstimatePeak(data);
cout << "Estimated high-pass width for Local Maximum method: " << width << endl;
return 0;
}
else if (nameMethod == "MD")
{
int width = circleEstimateShape(data);
cout << "Estimated low-pass width for Main Direction method: " << width << endl;
return 0;
}
else
throw baseException("Error: Unsupported method type! Use -h to see commandline instructions.");
et = clock();
std::cout << "Estimation time:" << (double)(et - st) / CLOCKS_PER_SEC << std::endl;
}
else
{
string nameMethod = params["-m"];
clock_t st, et;
st = clock();
imageReal<double> ref;
bool fineFlag;
if (params.count("-f") == 0)
fineFlag = false;
else if (params["-f"] == "False")
fineFlag = false;
else if (params["-f"] == "True")
fineFlag = true;
else
throw baseException("Error: Unsupported fine search flag! Use -h to see commandline instructions.");
bool updateFlag;
if (params.count("-u") == 0)
updateFlag = true;
else if (params["-u"] == "False")
updateFlag = false;
else if (params["-u"] == "True")
updateFlag = true;
else
throw baseException("Error: Unsupported update flag! Use -h to see commandline instructions.");
if (nameMethod == "LM")
{
int width;
if (params.count("-wh") == 0)
{
stackReal<double> temp = data.pieces(0, int(data.shape[0] / 10));
width = circleEstimatePeak(temp, fineFlag);
cout << "Estimated high-pass width: " << width << endl;
}
else
{
width = std::stoi(params["-wh"]);
cout << "Input high-pass width: " << width << endl;
}
if (params.count("-r") == 0)
{
ref = initReference(data);
imageReal<int> mask = circularMask(ref.shape, ref.shape[0] / 2);
ref = ref * mask;
}
else if (params["-r"] == "XMIPP")
{
ref = initReference(data);
imageReal<int> mask = circularMask(ref.shape, ref.shape[0] / 2);
ref = ref * mask;
}
else if (params["-r"] == "RFAA")
{
ref = reference(data, alignPeak, width, fineFlag);
}
else
{
string pathRef = params["-r"];
mrcFile mrcRef = mrcFile();
mrcRef.read(pathIn);
ref = mrcRef.data.pieceGet(0);
}
stackReal<double> stkBackup;
if (XMIPP)
stkBackup = data;
for (int i = 0; i < 2; ++i)
refinement(data, ref, alignPeak, width, fineFlag, updateFlag);
ref = data.stackAvg();
if (XMIPP)
outInfo(stkBackup, ref, alignPeak, info, name, width, fineFlag);
}
else if (nameMethod == "MD")
{
int outWidth, inWidth;
double th;
if (params.count("-wh") == 0)
{
inWidth = -1;
}
else
{
inWidth = std::stoi(params["-wh"]);
cout << "Input high-pass width: " << inWidth << endl;
}
if (params.count("-th") == 0)
{
th = -1;
}
else
{
th = std::stof(params["-th"]);
cout << "Input threshold: " << th << endl;
}
if (params.count("-wl") == 0)
{
stackReal<double> temp = data.pieces(0, int(data.shape[0] / 10));
outWidth = circleEstimateShape(temp, inWidth, th, fineFlag);
cout << "Estimated low-pass width: " << outWidth << endl;
}
else
{
outWidth = std::stoi(params["-wl"]);
cout << "Input low-pass width: " << outWidth << endl;
}
if (params.count("-r") == 0)
{
ref = initReference(data);
imageReal<int> mask = circularMask(ref.shape, ref.shape[0] / 2);
ref = ref * mask;
}
else if (params["-r"] == "XMIPP")
{
ref = initReference(data);
imageReal<int> mask = circularMask(ref.shape, ref.shape[0] / 2);
ref = ref * mask;
}
else if (params["-r"] == "RFAA")
{
ref = reference(data, alignShape, outWidth, inWidth, th, fineFlag);
}
else
{
string pathRef = params["-r"];
mrcFile mrcRef = mrcFile();
mrcRef.read(pathIn);
ref = mrcRef.data.pieceGet(0);
}
stackReal<double> stkBackup;
if (XMIPP)
stkBackup = data;
for (int i = 0; i < 2; ++i)
refinement(data, ref, alignShape, outWidth, inWidth, th, fineFlag, updateFlag);
ref = data.stackAvg();
if (XMIPP)
outInfo(stkBackup, ref, alignShape, info, name, outWidth, inWidth, th, fineFlag);
}
else if (nameMethod == "DA")
{
if (params.count("-r") == 0)
{
ref = initReference(data);
imageReal<int> mask = circularMask(ref.shape, ref.shape[0] / 2);
ref = ref * mask;
}
else if (params["-r"] == "XMIPP")
{
ref = initReference(data);
imageReal<int> mask = circularMask(ref.shape, ref.shape[0] / 2);
ref = ref * mask;
}
else if (params["-r"] == "RFAA")
{
ref = reference(data, alignFFTX);
}
else
{
string pathRef = params["-r"];
mrcFile mrcRef = mrcFile();
mrcRef.read(pathIn);
ref = mrcRef.data.pieceGet(0);
}
stackReal<double> stkBackup;
if (XMIPP)
stkBackup = data;
for (int i = 0; i < 2; ++i)
refinement(data, ref, alignFFTX, updateFlag);
ref = data.stackAvg();
if (XMIPP)
outInfo(stkBackup, ref, alignFFTX, info, name);
}
else
throw baseException("Error: Unsupported method type! Use -h to see commandline instructions.");
mrcFile mrc_new = mrcFile();
stackReal<double> stk = image2Stack(ref);
mrc_new.setData(stk);
mrc_new.write(name + "_ref.mrcs");
mrc_new.setData(data);
mrc_new.write(pathOut);
et = clock();
std::cout << "Align time:" << (double)(et - st) / CLOCKS_PER_SEC << std::endl;
}
}
return 0;
}