get_hkl.c 7.62 KB
Newer Older
1
2
3
4
5
/*
 * get_hkl.c
 *
 * Small program to write out a list of h,k,l,I values given a structure
 *
Thomas White's avatar
Thomas White committed
6
 * (c) 2006-2010 Thomas White <taw@physics.org>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */


#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 "utils.h"
#include "sfac.h"
#include "reflections.h"
27
#include "symmetry.h"
28
29
30
31
32
33


static void show_help(const char *s)
{
	printf("Syntax: %s [options]\n\n", s);
	printf(
34
"Create reflections lists.\n"
35
"\n"
Thomas White's avatar
Thomas White committed
36
37
"  -h, --help                 Display this help message.\n"
"\n"
Thomas White's avatar
Thomas White committed
38
39
"  -t, --template=<filename>  Only include reflections mentioned in file.\n"
"      --poisson              Simulate Poisson samples.\n"
40
"      --noise                Add 10%% random noise.\n"
41
42
43
"  -y, --symmetry=<sym>       The symmetry of the input file (-i).\n"
"  -w, --twin=<sym>           Generate twinned data according to the given\n"
"                              point group.\n"
44
45
"  -o, --output=<filename>    Output filename (default: stdout).\n"
"  -i, --intensities=<file>   Read intensities from file instead of\n"
46
47
48
"                              calculating them from scratch.  You might use\n"
"                              this if you need to apply noise or twinning.\n"
"  -p, --pdb=<file>           PDB file from which to get the structure.\n"
49
50
51
"      --no-phases            Do not try to use phases in the input file.\n"
"      --multiplicity         Multiply intensities by the number of\n"
"                              equivalent reflections.\n"
Thomas White's avatar
Thomas White committed
52
);
Thomas White's avatar
Thomas White committed
53
54
55
56
}


/* Apply Poisson noise to all reflections */
57
static void poisson_reflections(double *ref, ReflItemList *items)
Thomas White's avatar
Thomas White committed
58
{
59
60
	int i;
	const int n = num_items(items);
Thomas White's avatar
Thomas White committed
61

62
	for ( i=0; i<n; i++ ) {
Thomas White's avatar
Thomas White committed
63

64
		struct refl_item *it;
Thomas White's avatar
Thomas White committed
65
		double val;
Thomas White's avatar
Thomas White committed
66
		int c;
Thomas White's avatar
Thomas White committed
67

68
69
70
		it = get_item(items, i);

		val = lookup_intensity(ref, it->h, it->k, it->l);
Thomas White's avatar
Thomas White committed
71
		c = poisson_noise(val);
72
73
74
		set_intensity(ref, it->h, it->k, it->l, c);

		progress_bar(i, n-1, "Simulating noise");
Thomas White's avatar
Thomas White committed
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
}


/* Apply 10% uniform noise to all reflections */
static void noise_reflections(double *ref, ReflItemList *items)
{
	int i;
	const int n = num_items(items);

	for ( i=0; i<n; i++ ) {

		struct refl_item *it;
		double val;
		double r;

		it = get_item(items, i);

		val = lookup_intensity(ref, it->h, it->k, it->l);

		r = (double)random()/RAND_MAX;
		val += 0.1 * val * r;

		set_intensity(ref, it->h, it->k, it->l, val);

		progress_bar(i, n-1, "Simulating noise");

Thomas White's avatar
Thomas White committed
103
	}
104
105
106
}


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
static ReflItemList *twin_reflections(double *ref, ReflItemList *items,
                                      const char *holo, const char *mero)
{
	int i;
	ReflItemList *new;

	new = new_items();

	for ( i=0; i<num_items(items); i++ ) {

		double mean;
		struct refl_item *it;
		signed int h, k, l;
		int n, j;
		int skip;

		it = get_item(items, i);

125
126
127
128
		/* There is a many-to-one correspondence between reflections
		 * in the merohedral and holohedral unit cells.  Do the
		 * calculation only once for each reflection in the holohedral
		 * cell, which contains fewer reflections.
129
		 */
130
131
		get_asymm(it->h, it->k, it->l, &h, &k, &l, holo);
		if ( find_item(new, h, k, l) ) continue;
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

		n = num_equivs(h, k, l, holo);

		mean = 0.0;
		skip = 0;
		for ( j=0; j<n; j++ ) {

			signed int he, ke, le;
			signed int hu, ku, lu;

			get_equiv(h, k, l, &he, &ke, &le, holo, j);

			/* Do we have this reflection?
			 * We might not have the particular (merohedral)
			 * equivalent which belongs to our definition of the
			 * asymmetric unit cell, so check them all.
			 */
			if ( !find_unique_equiv(items, he, ke, le, mero,
			                        &hu, &ku, &lu) ) {
				/* Don't have this reflection, so bail out */
				ERROR("Twinning %i %i %i requires the %i %i %i "
				      "reflection (or an equivalent in %s), "
				      "which I don't have. %i %i %i won't "
				      "appear in the output\n",
				      h, k, l, he, ke, le, mero, h, k, l);
				skip = 1;
				break;
			}

			mean += lookup_intensity(ref, hu, ku, lu);

		}

		if ( !skip ) {

			mean /= (double)n;

			set_intensity(ref, h, k, l, mean);
			add_item(new, h, k, l);

		}

	}

	return new;
}


180
181
182
int main(int argc, char *argv[])
{
	int c;
Thomas White's avatar
Thomas White committed
183
	double *ideal_ref;
184
	double *phases;
185
	struct molecule *mol;
Thomas White's avatar
Thomas White committed
186
	char *template = NULL;
187
188
	int config_noise = 0;
	int config_poisson = 0;
189
190
	int config_nophase = 0;
	int config_multi = 0;
191
192
	char *holo = NULL;
	char *mero = NULL;
Thomas White's avatar
Thomas White committed
193
	char *output = NULL;
194
	char *input = NULL;
195
	char *filename = NULL;
Thomas White's avatar
Thomas White committed
196
197
	ReflItemList *input_items;
	ReflItemList *write_items;
198
	UnitCell *cell = NULL;
199
200
201
202

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
Thomas White's avatar
Thomas White committed
203
		{"template",           1, NULL,               't'},
204
205
		{"poisson",            0, &config_poisson,     1},
		{"noise",              0, &config_noise,       1},
Thomas White's avatar
Thomas White committed
206
		{"output",             1, NULL,               'o'},
207
208
		{"twin",               1, NULL,               'w'},
		{"symmetry",           1, NULL,               'y'},
209
		{"intensities",        1, NULL,               'i'},
210
		{"pdb",                1, NULL,               'p'},
211
212
		{"no-phases",          0, &config_nophase,     1},
		{"multiplicity",       0, &config_multi,       1},
213
214
215
216
		{0, 0, NULL, 0}
	};

	/* Short options */
217
	while ((c = getopt_long(argc, argv, "ht:o:i:p:w:y:", longopts, NULL)) != -1) {
218
219

		switch (c) {
Thomas White's avatar
Thomas White committed
220
		case 'h' :
221
222
223
			show_help(argv[0]);
			return 0;

Thomas White's avatar
Thomas White committed
224
		case 't' :
Thomas White's avatar
Thomas White committed
225
226
227
			template = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
228
		case 'o' :
Thomas White's avatar
Thomas White committed
229
230
231
			output = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
232
		case 'i' :
233
234
235
			input = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
236
		case 'p' :
237
238
239
			filename = strdup(optarg);
			break;

240
241
242
243
244
245
246
247
		case 'y' :
			mero = strdup(optarg);
			break;

		case 'w' :
			holo = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
248
		case 0 :
249
250
			break;

Thomas White's avatar
Thomas White committed
251
		default :
252
253
254
255
256
			return 1;
		}

	}

257
258
259
260
261
	if ( filename == NULL ) {
		filename = strdup("molecule.pdb");
	}

	mol = load_molecule(filename);
262
	cell = load_cell_from_pdb(filename);
263
264
265
266
267
	if ( !config_nophase ) {
		phases = new_list_phase();
	} else {
		phases = NULL;
	}
268
	if ( input == NULL ) {
Thomas White's avatar
Thomas White committed
269
		input_items = new_items();
270
		ideal_ref = get_reflections(mol, eV_to_J(1790.0), 1/(0.05e-9),
Thomas White's avatar
Thomas White committed
271
		                            phases, input_items);
272
	} else {
Thomas White's avatar
Thomas White committed
273
		ideal_ref = new_list_intensity();
274
275
		input_items = read_reflections(input, ideal_ref, phases,
		                               NULL, NULL);
276
277
		free(input);
	}
278

279
280
	if ( config_poisson ) poisson_reflections(ideal_ref, input_items);
	if ( config_noise ) noise_reflections(ideal_ref, input_items);
Thomas White's avatar
Thomas White committed
281

282
283
284
285
286
287
	if ( holo != NULL ) {
		ReflItemList *new;
		STATUS("Twinning from %s into %s\n", mero, holo);
		new = twin_reflections(ideal_ref, input_items, holo, mero);
		delete_items(input_items);
		input_items = new;
Thomas White's avatar
Thomas White committed
288
289
	}

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
	if ( config_multi ) {

		int i;

		for ( i=0; i<num_items(input_items); i++ ) {

			struct refl_item *it;
			double inty;

			it = get_item(input_items, i);
			inty = lookup_intensity(ideal_ref, it->h, it->k, it->l);
			inty *= num_equivs(it->h, it->k, it->l, mero);
			set_intensity(ideal_ref, it->h, it->k, it->l, inty);
			STATUS("%i %i %i %i\n", it->h, it->k, it->l,
			       num_equivs(it->h, it->k, it->l, mero));

		}
	}

Thomas White's avatar
Thomas White committed
309
310
311
312
	if ( template ) {
		/* Write out only reflections which are in the template
		 * (and which we have in the input) */
		ReflItemList *template_items;
313
314
		template_items = read_reflections(template,
		                                  NULL, NULL, NULL, NULL);
Thomas White's avatar
Thomas White committed
315
316
317
318
319
		write_items = intersection_items(input_items, template_items);
		delete_items(template_items);
	} else {
		/* Write out all reflections */
		write_items = new_items();
Thomas White's avatar
Thomas White committed
320
		/* (quick way of copying a list) */
Thomas White's avatar
Thomas White committed
321
322
323
		union_items(write_items, input_items);
	}

324
	write_reflections(output, write_items, ideal_ref, phases, NULL, cell);
Thomas White's avatar
Thomas White committed
325
326
327

	delete_items(input_items);
	delete_items(write_items);
328
329
330

	return 0;
}