compare_hkl.c 3.33 KB
Newer Older
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
/*
 * compare_hkl.c
 *
 * Compare reflection lists
 *
 * (c) 2006-2010 Thomas White <taw@physics.org>
 *
 * 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"
Thomas White's avatar
Thomas White committed
27
#include "statistics.h"
28
29
30
31


static void show_help(const char *s)
{
32
	printf("Syntax: %s [options] <file1.hkl> <file2.hkl>\n\n", s);
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
	printf(
"Compare intensity lists.\n"
"\n"
"  -h, --help                 Display this help message.\n"
"  -o, --output=<filename>    Specify output filename for correction factor.\n"
"\n");
}


int main(int argc, char *argv[])
{
	int c;
	double *ref1;
	double *ref2;
	double *out;
48
	UnitCell *cell;
49
50
51
52
	char *outfile = NULL;
	char *afile = NULL;
	char *bfile = NULL;
	signed int h, k, l;
53
	double scale, R2, Rmerge, pearson;
Thomas White's avatar
Thomas White committed
54
55
56
57
	unsigned int *c1;
	unsigned int *c2;
	int i;
	int nc1, nc2, ncom;
58
	unsigned int *outcounts;
59
60
61
62
63
64
65
66
67

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{"output",             1, NULL,               'o'},
		{0, 0, NULL, 0}
	};

	/* Short options */
68
	while ((c = getopt_long(argc, argv, "ho:", longopts, NULL)) != -1) {
69
70

		switch (c) {
Thomas White's avatar
Thomas White committed
71
		case 'h' :
72
73
74
			show_help(argv[0]);
			return 0;

Thomas White's avatar
Thomas White committed
75
		case 'o' :
76
77
78
			outfile = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
79
		case 0 :
80
81
			break;

Thomas White's avatar
Thomas White committed
82
		default :
83
84
85
86
87
			return 1;
		}

	}

88
89
90
91
92
93
94
95
	if ( argc != (optind+2) ) {
		ERROR("Please provide exactly two HKL files to compare.\n");
		return 1;
	}

	afile = strdup(argv[optind++]);
	bfile = strdup(argv[optind]);

96
	cell = load_cell_from_pdb("molecule.pdb");
Thomas White's avatar
Thomas White committed
97
	c1 = new_list_count();
98
	ref1 = read_reflections(afile, c1, NULL);
99
100
101
102
	if ( ref1 == NULL ) {
		ERROR("Couldn't open file '%s'\n", afile);
		return 1;
	}
Thomas White's avatar
Thomas White committed
103
	c2 = new_list_count();
104
	ref2 = read_reflections(bfile, c2, NULL);
105
106
107
108
109
	if ( ref2 == NULL ) {
		ERROR("Couldn't open file '%s'\n", bfile);
		return 1;
	}
	out = new_list_intensity();
110
	outcounts = new_list_count();
111

Thomas White's avatar
Thomas White committed
112
	/* Knock out the zero beam, in case it's present */
Thomas White's avatar
Thomas White committed
113
114
115
	set_count(c1, 0, 0, 0, 0);
	set_count(c2, 0, 0, 0, 0);

116
117
118
119
	/* Divide by number of counts, since we're not interested in them */
	divide_down(ref1, c1);
	divide_down(ref2, c2);

120
121
122
123
124
	for ( h=-INDMAX; h<INDMAX; h++ ) {
	for ( k=-INDMAX; k<INDMAX; k++ ) {
	for ( l=-INDMAX; l<INDMAX; l++ ) {

		double i1, i2;
125
126
127
128
		unsigned int c1s, c2s;

		c1s = lookup_count(c1, h, k, l);
		c2s = lookup_count(c2, h, k, l);
129
130
131
132

		i1 = lookup_intensity(ref1, h, k, l);
		i2 = lookup_intensity(ref2, h, k, l);

133
134
135
136
137
138
		if ( c1s && c2s ) {
			if ( (i1 != 0.0) && (i2 != 0.0) ) {
				set_intensity(out, h, k, l,
				              (i1/(double)c1s)/i2/(double)c2s);
				set_count(outcounts, h, k, l, 1);
			}
139
140
141
142
143
144
		}

	}
	}
	}

Thomas White's avatar
Thomas White committed
145
146
147
148
149
150
	nc1 = 0;
	nc2 = 0;
	ncom = 0;
	for ( i=0; i<IDIM*IDIM*IDIM; i++ ) {
		nc1 += c1[i];
		nc2 += c2[i];
151
		ncom += c1[i] && c2[i];
Thomas White's avatar
Thomas White committed
152
153
	}
	STATUS("%i,%i reflections: %i in common\n", nc1, nc2, ncom);
154
	R2 = stat_r2(ref1, c1, ref2, c2, &scale);
155
	STATUS("R2 = %5.4f %% (scale=%5.2e)\n", R2*100.0, scale);
156
	Rmerge = stat_rmerge(ref1, c1, ref2, c2, &scale);
157
	STATUS("Rmerge = %5.4f %% (scale=%5.2e)\n", Rmerge*100.0, scale);
158
159
	pearson = stat_pearson(ref1, c1, ref2, c2);
	STATUS("Pearson r = %5.4f\n", pearson);
Thomas White's avatar
Thomas White committed
160

161
	if ( outfile != NULL ) {
162
		write_reflections(outfile, outcounts, out, NULL, 0, cell, 1);
163
	}
164
165
166

	return 0;
}