-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnphwe_test.c
75 lines (73 loc) · 2.19 KB
/
snphwe_test.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
double SNPHWE2(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, uint32_t midp);
int32_t SNPHWE_t(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, double thresh);
int32_t SNPHWE_midp_t(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, double thresh);
int main(int argc, char** argv) {
uint32_t midp = 0;
char name[100];
FILE* test_file;
int32_t hets;
int32_t homs1;
int32_t homs2;
double p_value;
uint32_t uii;
if (!strcmp(argv[argc - 1], "midp")) {
midp = 1;
argc--;
}
if ((argc == 4) || (argc == 5)) {
hets = atoi(argv[1]);
homs1 = atoi(argv[2]);
homs2 = atoi(argv[3]);
if ((hets < 0) || (homs1 < 0) || (homs2 < 0)) {
printf("Error: Negative parameter.\n");
return 3;
}
if (argc == 4) {
p_value = SNPHWE2(hets, homs1, homs2, midp);
printf("P-value: %lg\n", p_value);
} else {
if (sscanf(argv[4], "%lg", &p_value) != 1) {
printf("Error: Invalid p-value '%s'.\n", argv[4]);
return 3;
}
if (midp) {
uii = SNPHWE_midp_t(hets, homs1, homs2, p_value);
} else {
uii = SNPHWE_t(hets, homs1, homs2, p_value);
}
if (uii) {
printf("Test failed (p-value below threshold).\n");
} else {
printf("Test passed (p-value above threshold).\n");
}
}
return 0;
} else if (argc != 2) {
printf(
"SNPHWE2 demo http://www.sph.umich.edu/csg/abecasis/Exact/\n"
" https://github.com/chrchang/stats\n\n"
" snphwe_test [het count] [hom count 1] [hom count 2] {threshold} <midp>\n"
" snphwe_test [filename] <midp>\n\n"
"If a filename is provided, the file is expected to contain marker names in the\n"
"first column, heterozygote counts in the second, and homozygote counts in the\n"
"third and fourth.\n");
return 1;
}
test_file = fopen(argv[1], "r");
if (!test_file) {
printf("Error: Unable to open file.\n");
return 2;
}
while (!feof(test_file)) {
fscanf(test_file, "%s %d %d %d\n", name, &hets, &homs1, &homs2);
p_value = SNPHWE2(hets, homs1, homs2, midp);
printf("P-value for %s: %lg\n", name, p_value);
}
fclose(test_file);
return 0;
}