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
|
/*
* ambigator.c
*
* Resolve indexing ambiguities
*
* Copyright © 2014 Deutsches Elektronen-Synchrotron DESY,
* a research centre of the Helmholtz Association.
* Copyright © 2014 Wolfgang Brehm
*
* Authors:
* 2014 Thomas White <taw@physics.org>
* 2014 Wolfgang Brehm <wolfgang.brehm@gmail.com>
*
* 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 <utils.h>
#include <hdf5-file.h>
#include <symmetry.h>
#include <stream.h>
#include <geometry.h>
#include <peaks.h>
#include <thread-pool.h>
#include <beam-parameters.h>
#include <reflist.h>
#include <reflist-utils.h>
#include "post-refinement.h"
#include "hrs-scaling.h"
#include "scaling-report.h"
static void show_help(const char *s)
{
printf("Syntax: %s [options]\n\n", s);
printf(
"Resolve indexing ambiguities.\n"
"\n"
" -h, --help Display this help message.\n"
"\n"
" -i, --input=<filename> Input stream.\n"
" -o, --output=<filename> Output stream.\n"
" -y, --symmetry=<sym> Apparent (\"source\") symmetry.\n"
" -e <sym> Actual (\"target\") symmetry.\n"
" -n, --iterations=<n> Iterate <n> times.\n"
);
}
static RefList *asymm_and_merge(RefList *in, const SymOpList *sym)
{
Reflection *refl;
RefListIterator *iter;
RefList *asym;
asym = reflist_new();
if ( asym == NULL ) return NULL;
for ( refl = first_refl(in, &iter);
refl != NULL;
refl = next_refl(refl, iter) )
{
signed int h, k, l;
signed int ha, ka, la;
Reflection *cr;
get_indices(refl, &h, &k, &l);
get_asymm(sym, h, k, l, &ha, &ka, &la);
cr = find_refl(asym, ha, ka, la);
if ( cr == NULL ) {
cr = add_refl(asym, ha, ka, la);
assert(cr != NULL);
copy_data(cr, refl);
} else {
const double i = get_intensity(cr);
const int r = get_redundancy(cr);
set_intensity(cr, (r*i + get_intensity(refl))/(r+1));
set_redundancy(cr, r+1);
}
}
return asym;
}
static float corr(Crystal *a, Crystal *b, int *pn)
{
Reflection *aref;
RefListIterator *iter;
float s_xy = 0.0;
float s_x = 0.0;
float s_y = 0.0;
float s_x2 = 0.0;
float s_y2 = 0.0;
int n = 0;
float t1, t2;
for ( aref = first_refl(crystal_get_reflections(a), &iter);
aref != NULL;
aref = next_refl(aref, iter) )
{
signed int h, k, l;
Reflection *bref;
float aint, bint;
get_indices(aref, &h, &k, &l);
bref = find_refl(crystal_get_reflections(b), h, k, l);
if ( bref == NULL ) continue;
aint = get_intensity(aref);
bint = get_intensity(bref);
s_xy += aint*bint;
s_x += aint;
s_y += bint;
s_x2 += aint*aint;
s_y2 += bint*bint;
n++;
}
*pn = n;
t1 = s_x2 - s_x*s_x / n;
t2 = s_y2 - s_y*s_y / n;
if ( (t1 < 0.0) || (t2 <= 0.0) ) return 0.0;
return ((s_xy - s_x*s_y)/n)/sqrt(t1*t2);
}
static void detwin(Crystal **crystals, int n_crystals, SymOpList *amb,
int *assignments)
{
int i;
int nch = 0;
for ( i=0; i<n_crystals; i++ ) {
int j;
float f = 0.0;
float g = 0.0;;
int p = 0;
int q = 0;
for ( j=0; j<n_crystals; j++ ) {
float cc;
int n;
cc = corr(crystals[i], crystals[j], &n);
if ( n < 3 ) continue;
if ( i == j ) continue;
if ( assignments[i] == assignments[j] ) {
f += cc;
p++;
} else {
g += cc;
q++;
}
}
f /= p;
g /= q;
if ( f > g ) {
assignments[i] = 1 - assignments[i];
nch++;
}
progress_bar(i, n_crystals-1, "Calculating");
}
STATUS("Changed %i assignments this time.\n", nch);
}
int main(int argc, char *argv[])
{
int c;
char *infile = NULL;
char *outfile = NULL;
char *s_sym_str = NULL;
SymOpList *s_sym;
char *e_sym_str = NULL;
SymOpList *e_sym;
SymOpList *amb;
int n_iter = 1;
int n_crystals, n_chunks, max_crystals;
int n_dif;
Crystal **crystals;
Stream *st;
int i;
int *assignments;
int *orig_assignments;
gsl_rng *rng;
/* Long options */
const struct option longopts[] = {
{"help", 0, NULL, 'h'},
{"input", 1, NULL, 'i'},
{"output", 1, NULL, 'o'},
{"symmetry", 1, NULL, 'y'},
{"iterations", 1, NULL, 'n'},
{0, 0, NULL, 0}
};
/* Short options */
while ((c = getopt_long(argc, argv, "hi:o:y:n:e:",
longopts, NULL)) != -1)
{
switch (c) {
case 'h' :
show_help(argv[0]);
return 0;
case 'i' :
infile = strdup(optarg);
break;
case 'o' :
outfile = strdup(optarg);
break;
case 'y' :
s_sym_str = strdup(optarg);
break;
case 'e' :
e_sym_str = strdup(optarg);
break;
case 'n' :
n_iter = atoi(optarg);
break;
case 0 :
break;
case '?' :
break;
default :
ERROR("Unhandled option '%c'\n", c);
break;
}
}
if ( infile == NULL ) {
infile = strdup("-");
}
st = open_stream_for_read(infile);
if ( st == NULL ) {
ERROR("Failed to open input stream '%s'\n", infile);
return 1;
}
free(infile);
/* Sanitise output filename */
if ( outfile == NULL ) {
outfile = strdup("partialator.hkl");
}
if ( s_sym_str == NULL ) {
ERROR("You must specify the input symmetry (with -y)\n");
return 1;
}
s_sym = get_pointgroup(s_sym_str);
free(s_sym_str);
if ( e_sym_str == NULL ) {
e_sym = NULL;
amb = NULL;
} else {
e_sym = get_pointgroup(e_sym_str);
free(e_sym_str);
if ( e_sym == NULL ) return 1;
amb = get_ambiguities(s_sym, e_sym);
if ( amb == NULL ) return 1;
STATUS("Ambiguity operations:\n");
describe_symmetry(amb);
}
crystals = NULL;
n_crystals = 0;
max_crystals = 0;
n_chunks = 0;
do {
struct image cur;
int i;
cur.det = NULL;
if ( read_chunk(st, &cur) != 0 ) {
break;
}
image_feature_list_free(cur.features);
for ( i=0; i<cur.n_crystals; i++ ) {
Crystal *cr;
cr = cur.crystals[i];
cell_free(crystal_get_cell(cr));
if ( n_crystals == max_crystals ) {
Crystal **crystals_new;
size_t nsz;
nsz = (max_crystals+1024)*sizeof(Crystal *);
crystals_new = realloc(crystals, nsz);
if ( crystals_new == NULL ) {
fprintf(stderr, "Failed to allocate "
"memory for crystals.\n");
break;
}
max_crystals += 1024;
crystals = crystals_new;
}
crystals[n_crystals] = cr;
n_crystals++;
}
fprintf(stderr, "Loaded %i crystals from %i chunks\r",
n_crystals, ++n_chunks);
} while ( 1 );
fprintf(stderr, "\n");
close_stream(st);
for ( i=0; i<n_crystals; i++ ) {
RefList *list, *merged;
list = crystal_get_reflections(crystals[i]);
merged = asymm_and_merge(list, s_sym);
reflist_free(list);
crystal_set_reflections(crystals[i], merged);
}
assignments = malloc(n_crystals*sizeof(int));
if ( assignments == NULL ) {
ERROR("Couldn't allocate memory for assignments.\n");
return 1;
}
orig_assignments = malloc(n_crystals*sizeof(int));
if ( orig_assignments == NULL ) {
ERROR("Couldn't allocate memory for original assignments.\n");
return 1;
}
rng = gsl_rng_alloc(gsl_rng_mt19937);
for ( i=0; i<n_crystals/2; i++ ) {
assignments[i] = (random_flat(rng, 1.0) > 0.5);
orig_assignments[i] = assignments[i];
}
for ( i=0; i<n_iter; i++ ) {
detwin(crystals, n_crystals, amb, assignments);
}
n_dif = 0;
for ( i=0; i<n_crystals/2; i++ ) {
if ( orig_assignments[i] != assignments[i] ) n_dif++;
}
STATUS("%i assignments are different from their starting values.\n",
n_dif);
free(assignments);
free(crystals);
gsl_rng_free(rng);
return 0;
}
|