aboutsummaryrefslogtreecommitdiff
path: root/src/gui_export.c
blob: 0daf8ae50e26176749417bf9d35c2c78f175f16a (plain)
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
/*
 * gui_export.c
 *
 * Export data from CrystFEL GUI
 *
 * Copyright © 2021 Deutsches Elektronen-Synchrotron DESY,
 *                  a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2021 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 <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <assert.h>

#include <utils.h>
#include <reflist-utils.h>
#include <cell-utils.h>
#include <symmetry.h>

#include "gui_project.h"
#include "crystfel_gui.h"
#include "gtk-util-routines.h"


struct export_window
{
	struct crystfelproject *proj;
	GtkWidget *cell_chooser;
	GtkWidget *limit_res;
	GtkWidget *min_res;
	GtkWidget *max_res;
	GtkWidget *dataset;
	GtkWidget *format;
};


static int export_to_mtz(struct gui_merge_result *result,
                         const char *filename, UnitCell *cell,
                         double min_res, double max_res)
{
	return 1;
}


static int export_to_mtz_bij(struct gui_merge_result *result,
                             const char *filename, UnitCell *cell,
                             double min_res, double max_res)
{
	return 1;
}


struct point_group_conversion
{
	char centering;
	const char *crystfel;
	int friedel;

	int xds_spgnum;

	const char *ccp4;
};


struct point_group_conversion pg_conversions[] = {

	/* Triclinic */
	{'P', "1",       0,     1,      "P 1"},

	/* Monoclinic */
	{'P', "2_uaa",   0,     0,      "P211"},
	{'P', "2/m_uaa", 1,     0,      "P211"},
	{'P', "2_uab",   0,     3,      "P121"},
	{'P', "2/m_uab", 1,     3,      "P121"},
	{'P', "2_uac",   0,     0,      "P112"},
	{'P', "2/m_uac", 1,     0,      "P112"},
	{'P', "2",       0,     0,      "P121"}, /* unique axis c */
	{'P', "2/m",     1,     0,      "P121"}, /* unique axis c */

	{'C', "2_uaa",   0,     0,      "C211"},
	{'C', "2/m_uaa", 1,     0,      "C211"},
	{'C', "2_uab",   0,     5,      "C121"},
	{'C', "2/m_uab", 1,     5,      "C121"},
	{'C', "2_uac",   0,     0,      "C112"},
	{'C', "2/m_uac", 1,     0,      "C112"},
	{'C', "2",       0,     0,      "C121"}, /* unique axis c */
	{'C', "2/m",     1,     0,      "C121"}, /* unique axis c */

	/* Orthorhombic */
	{'P', "222",     0,     16,     "P222"},
	{'P', "mmm",     1,     16,     "P222"},
	{'C', "222",     0,     21,     "C222"},
	{'C', "mmm",     1,     21,     "C222"},

	/* FIXME: Complete this list.  Ugh. */

	{'*', NULL,  0, 0, NULL}
};


static int space_group_for_xds(const char *sym_str, char cen)
{
	int i = 0;
	do {
		if ( (pg_conversions[i].centering == cen)
		  && (strcmp(sym_str, pg_conversions[i].crystfel) == 0) )
		{
			return pg_conversions[i].xds_spgnum;
		}
		i++;
	} while (pg_conversions[i].centering != '*');

	ERROR("Couldn't derive XDS representation of symmetry.\n");
	return 0;
}


static int export_to_xds(struct gui_merge_result *result,
                         const char *filename, UnitCell *cell,
                         double min_res, double max_res)
{
	FILE *fh;
	RefList *reflist;
	RefListIterator *iter;
	Reflection *refl;
	double a, b, c, al, be,ga;
	char *sym_str;
	SymOpList *sym;
	int spg;

	fh = fopen(filename, "w");
	if ( fh == NULL ) return 1;

	reflist = read_reflections_2(result->hkl, &sym_str);
	if ( reflist == NULL ) return 1;
	if ( sym_str == NULL ) return 1;

	sym = get_pointgroup(sym_str);
	if ( sym == NULL ) return 1;

	cell_get_parameters(cell, &a, &b, &c, &al, &be, &ga);

	spg = space_group_for_xds(sym_str, cell_get_centering(cell));
	if ( spg == 0 ) return 1;

	fprintf(fh, "!FORMAT=XDS_ASCII MERGE=TRUE FRIEDEL'S_LAW=%s\n",
	        is_centrosymmetric(sym) ? "TRUE" : "FALSE");
	fprintf(fh, "!SPACE_GROUP_NUMBER=%i\n", spg);
	fprintf(fh, "!UNIT_CELL_CONSTANT= %.2f %.2f %.2f %.2f %.2f %.2f\n",
	        a*1e10, b*1e10, c*1e10, rad2deg(al), rad2deg(be), rad2deg(ga));
	fprintf(fh, "!NUMBER_OF_ITEMS_IN_EACH_DATA_RECORD=5\n");
	fprintf(fh, "!ITEM_H=1\n");
	fprintf(fh, "!ITEM_K=2\n");
	fprintf(fh, "!ITEM_L=3\n");
	fprintf(fh, "!ITEM_IOBS=4\n");
	fprintf(fh, "!ITEM_SIGMA(IOBS)=5\n");
	fprintf(fh, "!END_OF_HEADER\n");

	for ( refl = first_refl(reflist, &iter);
	      refl != NULL;
	      refl = next_refl(refl, iter) )
	{
		signed int h, k, l;
		double one_over_d;

		get_indices(refl, &h, &k, &l);

		one_over_d = 2.0*resolution(cell, h, k, l);
		if ( (one_over_d > min_res) && (one_over_d < max_res) ) {

			fprintf(fh, "%6i %6i %6i %9.2f %9.2f\n",
			        h, k, l,
			        get_intensity(refl),
			        get_esd_intensity(refl));

		}
	}

	fprintf(fh, "!END_OF_DATA\n");
	free_symoplist(sym);
	free(sym_str);
	reflist_free(reflist);

	fclose(fh);
	return 0;
}


static int export_data(struct export_window *win, char *filename)
{
	gchar *cell_filename;
	const char *dataset;
	const char *format;
	struct gui_merge_result *result;
	UnitCell *cell;
	int r = 0;
	double min_res = 0;
	double max_res = +INFINITY;

	dataset = gtk_combo_box_get_active_id(GTK_COMBO_BOX(win->dataset));
	if ( dataset == NULL ) {
		ERROR("Please select the dataset to export.\n");
		return 1;
	}

	format = gtk_combo_box_get_active_id(GTK_COMBO_BOX(win->format));
	if ( format == NULL ) {
		ERROR("Please select the data format to use.\n");
		return 1;
	}

	cell_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(win->cell_chooser));
	if ( cell_filename == NULL ) {
		ERROR("Please choose the unit cell file.\n");
		return 1;
	}

	cell = load_cell_from_file(cell_filename);
	if ( cell == NULL ) {
		ERROR("Failed to load unit cell file %s\n", cell_filename);
		return 1;
	}

	if ( get_bool(win->limit_res) ) {
		min_res = 1e10/get_float(win->min_res);
		max_res = 1e10/get_float(win->max_res);
	}

	result = find_merge_result_by_name(win->proj, dataset);
	if ( result == NULL ) {
		ERROR("Couldn't find merged dataset '%s'\n", dataset);
		return 1;
	}

	STATUS("Exporting dataset %s to %s, in format %s, using unit cell %s,"
	       "%f to %f m^-1\n", dataset, filename, format, cell_filename,
	       min_res, max_res);

	if ( strcmp(format, "mtz") == 0 ) {
		r = export_to_mtz(result, filename, cell, min_res, max_res);
	} else if ( strcmp(format, "mtz-bij") == 0 ) {
		r = export_to_mtz_bij(result, filename, cell, min_res, max_res);
	} else if ( strcmp(format, "xds") == 0 ) {
		r = export_to_xds(result, filename, cell, min_res, max_res);
	} else {
		ERROR("Unrecognised export format '%s'\n", format);
		return 1;
	}

	if ( r ) {
		ERROR("Export failed\n");
	}

	g_free(cell_filename);

	return 0;
}


static void export_response_sig(GtkWidget *dialog, gint resp,
                                struct export_window *win)
{
	int r = 0;

	if ( resp == GTK_RESPONSE_ACCEPT ) {
		gchar *filename;
		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
		r = export_data(win, filename);
		g_free(filename);
	}

	if ( !r ) gtk_widget_destroy(dialog);
}


gint export_sig(GtkWidget *widget, struct crystfelproject *proj)
{
	GtkWidget *dialog;
	GtkWidget *vbox;
	GtkWidget *hbox;
	GtkWidget *label;
	char tmp[64];
	struct export_window *win;
	int i;

	win = malloc(sizeof(struct export_window));
	if ( win == NULL ) return 0;

	win->proj = proj;

	dialog = gtk_file_chooser_dialog_new("Export data",
	                                     GTK_WINDOW(proj->window),
	                                     GTK_FILE_CHOOSER_ACTION_SAVE,
	                                     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
	                                     GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
	                                     NULL);
	gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog),
	                                               TRUE);

	g_signal_connect(G_OBJECT(dialog), "response",
	                 G_CALLBACK(export_response_sig),
	                 win);

	vbox = gtk_vbox_new(FALSE, 0.0);
	gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog),
	                                  GTK_WIDGET(vbox));
	gtk_container_set_border_width(GTK_CONTAINER(vbox), 4);

	hbox = gtk_hbox_new(FALSE, 0.0);
	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 4.0);
	label = gtk_label_new("Results to export:");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 4.0);
	win->dataset = gtk_combo_box_text_new();
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(win->dataset),
	                   FALSE, FALSE, 4.0);
	for ( i=0; i<proj->n_merge_results; i++ ) {
		gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(win->dataset),
		                          proj->merge_results[i].name,
		                          proj->merge_results[i].name);
	}
	label = gtk_label_new("Format");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 4.0);
	win->format = gtk_combo_box_text_new();
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(win->format),
	                   FALSE, FALSE, 4.0);
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(win->format), "mtz",
	                          "MTZ, plain");
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(win->format), "mtz-bij",
	                          "MTZ, Bijvoet pairs together");
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(win->format), "xds",
	                          "XDS ASCII");
	gtk_combo_box_set_active(GTK_COMBO_BOX(win->format), 0);

	label = gtk_label_new("Unit cell file:");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 4.0);
	win->cell_chooser = gtk_file_chooser_button_new("Unit cell file",
	                                              GTK_FILE_CHOOSER_ACTION_OPEN);
	gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(win->cell_chooser),
	                                TRUE);
	/* Use the "FoM" cell file because there should only be one
	 * point of truth for the "final" cell parameters.  Eventually, I hope
	 * to determine this automatically. */
	gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(win->cell_chooser),
	                              proj->fom_cell_filename);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(win->cell_chooser),
	                   FALSE, FALSE, 4.0);

	hbox = gtk_hbox_new(FALSE, 0.0);
	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbox),
	                   FALSE, FALSE, 4.0);
	win->limit_res = gtk_check_button_new_with_label("Restrict resolution range:");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(win->limit_res),
	                   FALSE, FALSE, 4.0);
	win->min_res = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(win->min_res), 4);
	snprintf(tmp, 64, "%.2f", proj->export_res_min);
	gtk_entry_set_text(GTK_ENTRY(win->min_res), tmp);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(win->min_res),
	                   FALSE, FALSE, 4.0);
	label = gtk_label_new("to");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 4.0);
	win->max_res = gtk_entry_new();
	gtk_entry_set_width_chars(GTK_ENTRY(win->max_res), 4);
	snprintf(tmp, 64, "%.2f", proj->export_res_max);
	gtk_entry_set_text(GTK_ENTRY(win->max_res), tmp);
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(win->max_res),
	                   FALSE, FALSE, 4.0);
	label = gtk_label_new("Å");
	gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(label),
	                   FALSE, FALSE, 4.0);
	g_signal_connect(G_OBJECT(win->limit_res), "toggled",
	                 G_CALLBACK(i_maybe_disable), win->min_res);
	g_signal_connect(G_OBJECT(win->limit_res), "toggled",
	                 G_CALLBACK(i_maybe_disable), win->max_res);
	gtk_widget_set_sensitive(win->min_res, FALSE);
	gtk_widget_set_sensitive(win->max_res, FALSE);

	gtk_dialog_set_default_response(GTK_DIALOG(dialog),
	                                GTK_RESPONSE_CLOSE);
	gtk_widget_show_all(dialog);

	return FALSE;
}