-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathgval.c
213 lines (172 loc) · 4.32 KB
/
gval.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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <inttypes.h>
#include <nicklib.h>
#include "admutils.h"
#include "mcio.h"
#include "gval.h"
static SNP **xxsnps = NULL;
static Indiv **xindivmarkers = NULL;
static int xnrows, xncols;
static int xnumindivs;
static int *xxindex = NULL;
static double *xmean, *xfancy;
static double **gtable = NULL;
int getcolxz (double *xcol, SNP * cupt, int *xindex, int *xtypes,
int nrows, int col, double *xmean, double *xfancy, int *n0,
int *n1);
void
setgval (SNP ** xsnps, int nrows, Indiv ** indivmarkers, int numindivs,
int *xindex, int *xtypes, int ncols)
{
double *cc;
int t, n0, n1, i, k, col;
SNP *cupt;
double mean, y;
unsetgval ();
xxsnps = xsnps;
xnrows = nrows;
xncols = ncols;
xindivmarkers = indivmarkers;
xnumindivs = numindivs;
xxindex = xindex;
for (i = 1; i < nrows; i++) {
if (xxindex[i] < xxindex[i - 1]) {
fprintf (stderr, "xindex not sorted\n");
exit (1);
}
}
ZALLOC (cc, nrows, double);
ZALLOC (xmean, ncols, double);
ZALLOC (xfancy, ncols, double);
vclear (xfancy, 1.0, ncols);
gtable = initarray_2Ddouble (ncols, 4, 0);
for (i = 0; i < ncols; ++i) {
col = i;
cupt = xsnps[i];
/**
if (i>=0) {
printf("zz: %d %s\n", cupt -> ID) ; fflush(stdout) ;
}
*/
getcolxz (cc, cupt, xindex, xtypes, nrows, i, xmean, xfancy, &n0, &n1);
mean = xmean[col] / xfancy[col];
for (k = 0; k < 3; ++k) {
y = ((double) k) - mean;
y *= xfancy[col];
gtable[col][k] = y / sqrt (2.0);
}
gtable[col][3] = 0;
t = MIN (n0, n1);
if (t == 0)
cupt->ignore = YES; // side-effect
}
free (cc);
}
void
unsetgval ()
{
if (xxsnps == NULL)
return;
xxsnps = NULL;
xindivmarkers = NULL;
xxindex = NULL;
free2D (>able, xncols);
gtable = NULL;
free (xmean);
free (xfancy);
}
int
getgval (int row, int col, double *val)
{
/**
if (row>=xnrows) fatalx("row index overflow\n") ;
if (col>=xncols) fatalx("col index overflow\n") ;
*/
return getggval (xxindex[row], col, val);
}
int
getggval (int indindx, int col, double *val)
// indindex is index in full array
{
SNP *cupt;
int t, z;
double y, mean;
if (gtable == NULL) fatalx("(gval) bug\n") ;
*val = 0;
if (xindivmarkers[indindx]->ignore)
return -1;
cupt = xxsnps[col];
t = getgtypes (cupt, indindx);
if (t < 0)
return t;
*val = gtable[col][t];
return t;
}
// Unpack lookup table
// macro to unpack a single byte
#define U(n) { ((n) >> 6) & 3, ((n) >> 4) & 3, ((n) >> 2) & 3, (n) & 3 }
// macros to build the u(n)packi(n)g table
#define U1(n) U(n), U((n) + 1), U((n) + 2), U((n) + 3)
#define U2(n) U1(n), U1((n) + 4), U1((n) + 8), U1((n) + 12)
#define U3(n) U2(n), U2((n) + 16), U2((n) + 32), U2((n) + 48)
// the unpacking table
static const uint8_t UL[256][4] = { U3 (0), U3 (64), U3 (128), U3 (192) };
size_t
get_nrows ()
{
return (xnrows);
}
size_t
get_ncols ()
{
return (xncols);
}
/**
* Unpacks a SNP column
* @param snp_index
<
5679
div id="LC170" class="react-file-line html-div" data-testid="code-cell" data-line-number="170" style="position:relative"> * @param *y arrayref to store data
*/
void
kjg_geno_get_normalized_row (const size_t snp_index, double *y)
{
uint8_t *packed = xxsnps[snp_index]->pbuff;
double *norm_lookup = gtable[snp_index];
size_t i = 0, j = xxindex[i];
while (1) {
size_t k = j / 4; // packed location
size_t jf = (k + 1) * 4; // last index in packed location
uint8_t p = packed[k]; // packed data
const uint8_t *u = UL[p]; // unpacked data
while (j < jf) {
size_t o = j % 4; // offset in packed data
size_t t = u[o]; // unpacked data
y[i] = norm_lookup[t]; // normalized data
if (++i == xnrows) // move onto next entry
return; // break if we are done with SNP
j = xxindex[i]; // perform the lookup
}
}
}
/**
* Unpacks several SNP coluns
* @param snp_index index of the SNP
* @param *unpacked arrayref to store data
*/
size_t
kjg_geno_get_normalized_rows (const size_t i, const size_t r, double *Y)
{
size_t j;
for (j = i; j < i + r && j < xncols; j++) {
kjg_geno_get_normalized_row (j, Y);
Y += xnrows;
}
return (j - i);
}