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
|
/*
* whirligig.c
*
* Find and combine rotation series
*
* Copyright © 2012-2014 Deutsches Elektronen-Synchrotron DESY,
* a research centre of the Helmholtz Association.
*
* Authors:
* 2012-2014 Thomas White <taw@physics.org>
*
* This file is part of CrystFEL.
*
* CrystFEL 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 3 of the License, or
* (at your option) any later version.
*
* CrystFEL 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 CrystFEL. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <assert.h>
#include <image.h>
#include <utils.h>
#include <stream.h>
#include "version.h"
#include "cell-utils.h"
#include "integer_matrix.h"
static RefList *transform_reflections(RefList *in, IntegerMatrix *m)
{
}
static void process_series(struct image *images, signed int *ser,
IntegerMatrix **mat, int len)
{
int i;
RefList *list;
printf("\n");
STATUS("Found a rotation series of %i views\n", len);
for ( i=0; i<len; i++ ) {
Crystal *cr = images[i].crystals[ser[i]];
RefList *p = transform_reflections(crystal_get_reflections(cr),
mat[i]);
reflist_free(p);
}
}
static double moduli_check(double ax, double ay, double az,
double bx, double by, double bz)
{
double ma = modulus(ax, ay, az);
double mb = modulus(bx, by, bz);
return fabs(ma-mb)/ma;
}
static int cells_are_similar(UnitCell *cell1, UnitCell *cell2)
{
double asx1, asy1, asz1, bsx1, bsy1, bsz1, csx1, csy1, csz1;
double asx2, asy2, asz2, bsx2, bsy2, bsz2, csx2, csy2, csz2;
UnitCell *pcell1, *pcell2;
const double atl = deg2rad(5.0);
const double ltl = 0.1;
/* Compare primitive cells, not centered */
pcell1 = uncenter_cell(cell1, NULL);
pcell2 = uncenter_cell(cell2, NULL);
cell_get_reciprocal(pcell1, &asx1, &asy1, &asz1,
&bsx1, &bsy1, &bsz1,
&csx1, &csy1, &csz1);
cell_get_reciprocal(pcell2, &asx2, &asy2, &asz2,
&bsx2, &bsy2, &bsz2,
&csx2, &csy2, &csz2);
cell_free(pcell1);
cell_free(pcell2);
if ( angle_between(asx1, asy1, asz1, asx2, asy2, asz2) > atl ) return 0;
if ( angle_between(bsx1, bsy1, bsz1, bsx2, bsy2, bsz2) > atl ) return 0;
if ( angle_between(csx1, csy1, csz1, csx2, csy2, csz2) > atl ) return 0;
if ( moduli_check(asx1, asy1, asz1, asx2, asy2, asz2) > ltl ) return 0;
if ( moduli_check(bsx1, bsy1, bsz1, bsx2, bsy2, bsz2) > ltl ) return 0;
if ( moduli_check(csx1, csy1, csz1, csx2, csy2, csz2) > ltl ) return 0;
return 1;
}
static int gatinator(UnitCell *a, UnitCell *b, IntegerMatrix **pmb)
{
IntegerMatrix *m;
int i[9];
m = intmat_new(3, 3);
for ( i[0]=-1; i[0]<=+1; i[0]++ ) {
for ( i[1]=-1; i[1]<=+1; i[1]++ ) {
for ( i[2]=-1; i[2]<=+1; i[2]++ ) {
for ( i[3]=-1; i[3]<=+1; i[3]++ ) {
for ( i[4]=-1; i[4]<=+1; i[4]++ ) {
for ( i[5]=-1; i[5]<=+1; i[5]++ ) {
for ( i[6]=-1; i[6]<=+1; i[6]++ ) {
for ( i[7]=-1; i[7]<=+1; i[7]++ ) {
for ( i[8]=-1; i[8]<=+1; i[8]++ ) {
UnitCellTransformation *tfn;
UnitCell *nc;
int j, k;
int l = 0;
for ( j=0; j<3; j++ )
for ( k=0; k<3; k++ )
intmat_set(m, j, k, i[l++]);
if ( intmat_det(m) != +1 ) continue;
tfn = tfn_from_intmat(m);
nc = cell_transform(b, tfn);
if ( cells_are_similar(a, nc) ) {
*pmb = m;
tfn_free(tfn);
cell_free(nc);
return 1;
}
tfn_free(tfn);
cell_free(nc);
}
}
}
}
}
}
}
}
}
intmat_free(m);
return 0;
}
/* Try all combinations of crystals from the two patterns, in the hope of
* finding the start of a rotation series */
static int try_all(struct image *a, struct image *b, int *c1, int *c2,
IntegerMatrix **m2)
{
int i, j;
for ( i=0; i<a->n_crystals; i++ ) {
for ( j=0; j<b->n_crystals; j++ ) {
if ( gatinator(crystal_get_cell(a->crystals[i]),
crystal_get_cell(b->crystals[j]), m2) )
{
*c1 = i;
*c2 = j;
return 1;
}
}
}
return 0;
}
/* Try to continue the rotation series from crystal c1 in image a, using any
* crystal from image b */
static int try_cont(struct image *a, struct image *b, int c1, int *c2,
IntegerMatrix **m2)
{
int j;
for ( j=0; j<b->n_crystals; j++ ) {
if ( gatinator(crystal_get_cell(a->crystals[c1]),
crystal_get_cell(b->crystals[j]), m2) )
{
*c2 = j;
return 1;
}
}
return 0;
}
static void dump(struct image *win, signed int *ser, IntegerMatrix **mat,
int window_len, int pos)
{
int i;
for ( i=0; i<pos; i++ ) {
free_all_crystals(&win[i]);
intmat_free(mat[i]);
free(win[i].filename);
}
memmove(win, &win[pos], (window_len-pos)*sizeof(struct image *));
memmove(ser, &ser[pos], (window_len-pos)*sizeof(signed int));
memmove(mat, &mat[pos], (window_len-pos)*sizeof(IntegerMatrix *));
}
static void show_help(const char *s)
{
printf("Syntax: %s <input.stream> [options]\n\n", s);
printf(
"Find and combine rotation series.\n"
"\n"
" -h, --help Display this help message.\n"
" --version Print CrystFEL version number and exit.\n"
" --no-polarisation Disable polarisation correction.\n");
}
int main(int argc, char *argv[])
{
int c;
Stream *st;
int polarisation = 1;
int pos = 0;
struct image *win;
signed int *ser;
IntegerMatrix **mat;
int window_len = 64;
/* Long options */
const struct option longopts[] = {
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 3 },
{"no-polarisation", 0, &polarisation, 0},
{"no-polarization", 0, &polarisation, 0},
{"polarisation", 0, &polarisation, 1}, /* compat */
{"polarization", 0, &polarisation, 1}, /* compat */
{0, 0, NULL, 0}
};
/* Short options */
while ((c = getopt_long(argc, argv, "h",
longopts, NULL)) != -1)
{
switch (c) {
case 'h' :
show_help(argv[0]);
return 0;
case 0 :
break;
case '?' :
break;
default :
ERROR("Unhandled option '%c'\n", c);
break;
}
}
if ( argc != (optind+1) ) {
ERROR("Please provide exactly one stream to process.\n");
return 1;
}
st = open_stream_for_read(argv[optind++]);
if ( st == NULL ) {
ERROR("Failed to open input stream '%s'\n", argv[optind-1]);
return 1;
}
win = calloc(window_len, sizeof(struct image));
ser = calloc(window_len, sizeof(int));
mat = calloc(window_len, sizeof(IntegerMatrix *));
if ( (win == NULL) || (ser == NULL) || (mat == NULL) ) {
ERROR("Failed to allocate series buffers\n");
return 1;
}
pos = 0;
do {
struct image *cur;
int c1, c2;
cur = &win[pos];
cur->div = NAN;
cur->bw = NAN;
cur->det = NULL;
if ( read_chunk_2(st, cur, STREAM_READ_REFLECTIONS
| STREAM_READ_UNITCELL) != 0 ) {
break;
}
if ( isnan(cur->div) || isnan(cur->bw) ) {
ERROR("Chunk doesn't contain beam parameters.\n");
return 1;
}
/* Need at least two images to compare */
if ( pos == 0 ) {
ser[0] = -1;
pos++;
continue;
}
if ( ser[pos-1] == -1 ) {
if ( try_all(&win[pos-1], cur, &c1, &c2, &mat[pos]) ) {
ser[pos-1] = c1;
ser[pos] = c2;
} else {
ser[pos] = -1;
mat[pos] = NULL;
}
} else {
if ( try_cont(&win[pos-1], cur, ser[pos-1], &c2,
&mat[pos]) )
{
ser[pos] = c2;
} else {
ser[pos] = -1;
mat[pos] = NULL;
}
}
if ( ser[pos-1] != -1 ) {
printf("*");
} else {
printf("-");
}
fflush(stdout);
if ( ser[0] == -1 ) {
dump(win, ser, mat, window_len, 1);
pos--;
}
if ( (pos > 0) && (ser[pos] == -1) && (ser[pos-1] == -1) ) {
/* Series ready to process */
process_series(win, ser, mat, pos-1);
dump(win, ser, mat, window_len, pos);
pos = 0;
}
pos++;
if ( pos == window_len ) {
window_len *= 2;
win = realloc(win, window_len*sizeof(struct image));
ser = realloc(ser, window_len*sizeof(signed int));
mat = realloc(mat, window_len*sizeof(IntegerMatrix *));
if ( (win == NULL) || (ser == NULL) || (mat == NULL) ) {
ERROR("Failed to expand series buffers\n");
return 1;
}
}
} while ( 1 );
printf("\n");
close_stream(st);
/* Final series to process? */
if ( pos > 0 ) {
process_series(win, ser, mat, pos);
dump(win, ser, mat, window_len, 1);
}
free(win);
free(ser);
free(mat);
return 0;
}
|