forked from richarddurbin/pbwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbwtIO.c
595 lines (492 loc) · 20.1 KB
/
pbwtIO.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* File: pbwtIO.c
* Author: Richard Durbin ([email protected])
* Copyright (C) Genome Research Limited, 2013-
*-------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------
* Description: read/write functions for pbwt package
* Exported functions:
* HISTORY:
* Last edited: Dec 16 17:45 2013 (rd)
* Created: Thu Apr 4 11:42:08 2013 (rd)
*-------------------------------------------------------------------
*/
#include "pbwt.h"
int nCheckPoint = 0 ; /* if set non-zero write pbwt and sites files every n sites when parsing external files */
/* basic function to store packed PBWT */
void pbwtWrite (PBWT *p, FILE *fp) /* just writes compressed pbwt in yz */
{
if (!p || !p->yz) die ("pbwtWrite called without a valid pbwt") ;
if (!p->aFstart || !p->aFend) die ("pbwtWrite called without start and end indexes") ;
if (fwrite ("PBW2", 1, 4, fp) != 4) /* version 2, with start and end indexes */
die ("error writing PBWT in pbwtWrite") ;
if (fwrite (&p->M, sizeof(int), 1, fp) != 1)
die ("error writing M in pbwtWrite") ;
if (fwrite (&p->N, sizeof(int), 1, fp) != 1)
die ("error writing N in pbwtWrite") ;
if (fwrite (p->aFstart, sizeof(int), p->M, fp) != p->M)
die ("error writing aFstart in pbwtWrite") ;
if (fwrite (p->aFend, sizeof(int), p->M, fp) != p->M)
die ("error writing aFend in pbwtWrite") ;
int n = arrayMax(p->yz) ;
if (fwrite (&n, sizeof(int), 1, fp) != 1)
die ("error writing n in pbwtWrite") ;
if (fwrite (arrp(p->yz, 0, uchar), sizeof(uchar), arrayMax(p->yz), fp) != arrayMax(p->yz))
die ("error writing data in pbwtWrite") ;
fprintf (stderr, "written %d chars pbwt: M, N are %d, %d\n", arrayMax(p->yz), p->M, p->N) ;
}
void pbwtWriteSites (PBWT *p, FILE *fp)
{
if (!p || !p->sites) die ("pbwtWriteSites called without sites") ;
int i ;
for (i = 0 ; i < p->N ; ++i)
{ Site *s = arrp(p->sites, i, Site) ;
fprintf (fp, "%s\t%d", p->chrom ? p->chrom : ".", s->x) ;
fprintf (fp, "\t%s", dictName (variationDict, s->varD)) ;
fputc ('\n', fp) ;
}
if (ferror (fp)) die ("error writing sites file") ;
fprintf (stderr, "written %d sites from %d to %d\n", p->N,
arrp(p->sites, 0, Site)->x, arrp(p->sites, p->N-1, Site)->x) ;
}
void pbwtWriteSamples (PBWT *p, FILE *fp)
{
if (!p || !p->samples) die ("pbwtWriteSamples called without samples") ;
int i ;
for (i = 0 ; i < p->M ; i += 2) /* assume diploid for now */
fprintf (fp, "%s\n", sampleName (arr(p->samples,i,int))) ;
if (ferror (fp)) die ("error writing sites file") ;
fprintf (stderr, "written %d samples\n", p->M/2) ;
}
void pbwtWriteMissing (PBWT *p, FILE *fp)
{
if (!p || !p->zMissing) die ("pbwtWriteMissing called without data on missing") ;
int n = arrayMax(p->zMissing) ;
if (fwrite (&n, sizeof(int), 1, fp) != 1)
die ("error writing n in pbwtWriteMissing") ;
if (fwrite (arrp(p->zMissing, 0, uchar), sizeof(uchar), n, fp) != n)
die ("error writing zMissing in pbwtWriteMissing") ;
if (fwrite (arrp(p->missing, 0, int), sizeof(int), p->N, fp) != p->N)
die ("error writing missing in pbwtWriteMissing") ;
fprintf (stderr, "written %d chars compressed missing data\n", n) ;
}
void pbwtWriteReverse (PBWT *p, FILE *fp)
{
if (!p || !p->zz) die ("pbwtWriteReverse called without reverse pbwt") ;
Array tz = p->yz ; p->yz = p->zz ;
int* tstart = p->aFstart ; p->aFstart = p->aRstart ;
int* tend = p->aFend ; p->aFend = p->aRend ;
fprintf (stderr, "reverse: ") ; pbwtWrite (p, fp) ;
p->yz = tz ; p->aFstart = tstart ; p->aFend = tend ;
}
#define FOPEN_W(tag) strcpy (fileNameLeaf, tag) ; if (!(fp = fopen (fileName, "w"))) die ("failed to open %s", fileName)
void pbwtWriteAll (PBWT *p, char *fileNameRoot)
{
char *fileName = myalloc (strlen (fileNameRoot) + 32, char) ;
strcpy (fileName, fileNameRoot) ;
strcat (fileName, ".") ;
char *fileNameLeaf = fileName + strlen(fileName) ;
FILE *fp ;
FOPEN_W("pbwt") ; pbwtWrite (p, fp) ; fclose (fp) ;
if (p->sites) { FOPEN_W("sites") ; pbwtWriteSites (p, fp) ; fclose (fp) ; }
if (p->samples) { FOPEN_W("samples") ; pbwtWriteSamples (p, fp) ; fclose (fp) ; }
if (p->missing) { FOPEN_W("missing") ; pbwtWriteMissing (p, fp) ; fclose (fp) ; }
if (p->zz) { FOPEN_W("reverse") ; pbwtWriteReverse (p, fp) ; fclose (fp) ; }
free (fileName) ;
}
void pbwtCheckPoint (PBWT *p)
{
static BOOL isA = TRUE ;
char fileNameRoot[20] ;
sprintf (fileNameRoot, "check_%c", isA ? 'A' : 'B') ;
pbwtWriteAll (p, fileNameRoot) ;
isA = !isA ;
}
/*******************************/
PBWT *pbwtRead (FILE *fp)
{
int m, n ;
PBWT *p ;
static char tag[5] = "test" ;
int version ;
if (fread (tag, 1, 4, fp) != 4) die ("failed to read 4 char tag - is file readable?") ;
if (!strcmp (tag, "PBW2")) version = 2 ; /* current version */
else if (!strcmp (tag, "PBWT")) version = 1 ; /* without start, end indexes */
else if (!strcmp (tag, "GBWT")) version = 0 ; /* earliest version */
else die ("failed to recognise file type %s in pbwtRead - was it written by pbwt?", tag) ;
if (fread (&m, sizeof(int), 1, fp) != 1) die ("error reading m in pbwtRead") ;
if (fread (&n, sizeof(int), 1, fp) != 1) die ("error reading n in pbwtRead") ;
p = pbwtCreate (m) ;
p->N = n ;
if (version > 1) /* read aFstart and aFend */
{ p->aFstart = myalloc (m, int) ;
if (fread (p->aFstart, sizeof(int), m, fp) != m) die ("error reading aFstart in pbwtRead") ;
p->aFend = myalloc (m, int) ;
if (fread (p->aFend, sizeof(int), m, fp) != m) die ("error reading aFend in pbwtRead") ;
}
else /* set aFstart to 0..M-1, leave aFend empty */
{ p->aFstart = myalloc (m, int) ;
int i ; for (i = 0 ; i < m ; ++i) p->aFstart[i] = i ;
}
if (fread (&n, sizeof(int), 1, fp) != 1)
die ("error reading pbwt file") ;
p->yz = arrayCreate (n, uchar) ;
array(p->yz, n-1, uchar) = 0 ; /* sets arrayMax */
if (fread (arrp(p->yz, 0, uchar), sizeof(uchar), n, fp) != n)
die ("error reading data in pbwt file") ;
fprintf (stderr, "read pbwt %s file with %d bytes: M, N are %d, %d\n", tag, n, p->M, p->N) ;
return p ;
}
static BOOL readMatchChrom (char **pChrom, FILE *fp)
{
char *newChrom = fgetword (fp) ;
if (strcmp (newChrom, "."))
if (!*pChrom)
*pChrom = strdup (newChrom) ;
else if (strcmp (newChrom, *pChrom))
return FALSE ;
return TRUE ;
}
Array pbwtReadSitesFile (FILE *fp, char **chrom)
{
char c ;
Site *s ;
int line = 1 ;
Array varTextArray = arrayCreate (256, char) ;
Array sites = arrayCreate (4096, Site) ;
while (!feof(fp))
if (readMatchChrom (chrom, fp)) /* if p->chrom then match, else set if not "." */
{ if (feof(fp)) break ;
s = arrayp(sites, arrayMax(sites), Site) ;
s->x = 0 ; while (isdigit(c = fgetc(fp))) s->x = s->x*10 + (c-'0') ;
if (!feof(fp) && c != '\n')
{ if (!isspace (c)) die ("bad position line %d in sites file", line) ;
while (isspace(c = fgetc(fp)) && c != '\n') ;
if (c == '\n') die ("bad end of line at line %d in sites file", line) ;
int i = 0 ; array(varTextArray, i++, char) = c ;
while ((c = fgetc(fp)) && c != '\n')
array(varTextArray, i++, char) = c ;
array(varTextArray, i, char) = 0 ;
dictAdd (variationDict, arrayp(varTextArray,0,char), &s->varD) ;
while (c != '\n' && !feof(fp)) c = fgetc(fp) ;
}
++line ;
}
else if (!feof(fp))
die ("failed to match chromosome in sites file: line %d", line) ;
if (ferror (fp)) die ("error reading sites file") ;
fprintf (stderr, "read %d sites on chromosome %s from file\n", arrayMax(sites), *chrom) ;
arrayDestroy (varTextArray) ;
return sites ;
}
void pbwtReadSites (PBWT *p, FILE *fp)
{
if (!p) die ("pbwtReadSites called without a valid pbwt") ;
p->sites = pbwtReadSitesFile (fp, &p->chrom) ;
if (arrayMax(p->sites) != p->N)
die ("sites file contains %d sites not %d as in pbwt", arrayMax(p->sites), p->N) ;
}
Array pbwtReadSamplesFile (FILE *fp) /* for now assume all samples diploid */
/* should add code to this to read father and mother and population
propose to use IMPUTE2 format for this */
{
char *name, c ;
int line = 0 ;
Array nameArray = arrayCreate (1024, char) ;
Array samples = arrayCreate (1024, int) ;
while (!feof(fp))
{ int n = 0 ;
while ((c = getc(fp)) && !isspace(c) && !feof (fp)) array(nameArray, n++, char) = c ;
if (feof(fp)) break ;
if (!n) die ("no name line %d in samples file", arrayMax(samples)+1) ;
array(nameArray, n++, char) = 0 ;
/* next bit of code deals with header lines for IMPUTE2 samples file, so can read that */
if (!strcmp (arrp(nameArray,0,char), "ID_1") && !arrayMax(samples))
{ while ((c = getc(fp)) && c != '\n' && !feof(fp)) ; /* remove header line */
while ((c = getc(fp)) && c != '\n' && !feof(fp)) ; /* and next line of zeroes?? */
continue ;
}
array(samples,arrayMax(samples),int) = sampleAdd (arrp(nameArray,0,char), 0, 0, 0) ;
/* now remove the rest of the line, for now */
while (c != '\n' && !feof(fp)) c = getc(fp) ;
}
arrayDestroy (nameArray) ;
fprintf (stderr, "read %d sample names\n", arrayMax(samples)) ;
if (isCheck)
{ fprintf (stderr, "first five sample names are: ") ;
int i ; for (i = 0 ; i < 5 && i < arrayMax(samples) ; ++i)
fprintf (stderr, " %s", sampleName(arr(samples,i,int))) ;
putc ('\n', stderr) ;
}
return samples ;
}
void pbwtReadSamples (PBWT *p, FILE *fp)
{
Array samples = pbwtReadSamplesFile (fp) ;
if (arrayMax(samples) != p->M/2)
die ("wrong number of diploid samples: %d needed", p->M/2) ;
p->samples = arrayReCreate(p->samples, p->M, int) ;
int i ;
for (i = 0 ; i < arrayMax(samples) ; ++i)
{ array(p->samples, 2*i, int) = arr(samples, i, int) ;
array(p->samples, 2*i+1, int) = arr(samples, i, int) ;
}
arrayDestroy (samples) ;
}
void pbwtReadMissing (PBWT *p, FILE *fp)
{
if (!p) die ("pbwtReadSamples called without a valid pbwt") ;
int n ; if (fread (&n, sizeof(int), 1, fp) != 1)
die ("error reading n in pbwtReadMissing") ;
p->zMissing = arrayReCreate (p->zMissing, n, uchar) ;
if (fread (arrp(p->zMissing, 0, uchar), sizeof(uchar), n, fp) != n)
die ("error reading zMissing in pbwtReadMissing") ;
array(p->zMissing, n-1, uchar) ; /* forces setting max correctly - not sure if needed */
p->missing = arrayReCreate (p->missing, p->N, int) ;
if (fread (arrp(p->missing, 0, int), sizeof(int), p->N, fp) != p->N)
die ("error reading missing in pbwtReadMissing") ;
fprintf (stderr, "read %d chars compressed missing data\n", n) ;
}
void pbwtReadReverse (PBWT *p, FILE *fp)
{
if (!p) die ("pbwtReadReverse called without a valid pbwt") ;
PBWT *q = pbwtRead (fp) ;
if (q->M != p->M || q->N != p->N)
die ("M %d or N %d in reverse don't match %, %d in forward", q->M, q->N, p->M, p->N) ;
p->zz = q->yz ; q->yz = 0 ;
p->aRstart = q->aFstart ; q->aFstart = 0 ;
p->aRend = q->aFend ; q->aFend = 0 ;
pbwtDestroy (q) ;
}
#define FOPEN_R(tag) strcpy (fileNameLeaf, tag), (fp = fopen (fileName, "r"))
PBWT *pbwtReadAll (char *fileNameRoot)
{
PBWT *p ;
char *fileName = myalloc (strlen (fileNameRoot) + 32, char) ;
strcpy (fileName, fileNameRoot) ;
strcat (fileName, ".") ;
char *fileNameLeaf = fileName + strlen(fileName) ;
FILE *fp ;
if (FOPEN_R ("pbwt")) { p = pbwtRead (fp) ; fclose (fp) ; }
else die ("failed to open %s", fileName) ;
if (FOPEN_R("sites")) { pbwtReadSites (p, fp) ; fclose (fp) ; }
if (FOPEN_R("samples")) { pbwtReadSamples (p, fp) ; fclose (fp) ; }
if (FOPEN_R("missing")) { pbwtReadMissing (p, fp) ; fclose (fp) ; }
if (FOPEN_R("reverse")) { pbwtReadReverse (p, fp) ; fclose (fp) ; }
free (fileName) ;
return p ;
}
/****************** MaCS file parsing *****************/
static void parseMacsHeader (FILE *fp, int *M, double *L) /* parse MACS file header */
{
if (strcmp (fgetword (fp), "COMMAND:")) die ("MaCS COMMAND line not found") ;
fgetword (fp) ; /* the command */
*M = atoi(fgetword(fp)) ; if (!*M) die ("failed to get M") ;
*L = atof(fgetword(fp)) ; if (!*L) die ("failed to get L") ;
while (fgetc(fp) != '\n') ; /* ignore rest of line */
if (strcmp (fgetword (fp), "SEED:")) die ("SEED line not found") ;
while (fgetc(fp) != '\n') ; /* ignore rest of line */
}
static BOOL parseMacsSite (FILE *fp, Site *s, int M, double L, uchar *yp) /* parse MACS site line */
{
static uchar conv[256] ;
static BOOL isFirst = TRUE ;
int number ;
if (isFirst) { conv['0'] = 0 ; conv['1'] = 1 ; isFirst = FALSE ; }
if (feof (fp)) return FALSE ;
if (strcmp (fgetword (fp), "SITE:")) return FALSE;
number = atoi(fgetword(fp)) ; /* this is the site number */
s->x = (int) (L * atof(fgetword(fp))) ;
atof(fgetword(fp)) ; /* ignore the time */
while (M--)
*yp++ = conv[getc(fp)] ;
if (feof (fp)) return FALSE ;
if (getc(fp) != '\n') die ("end of line error for MaCS SITE %d", number) ;
return TRUE ;
}
PBWT *pbwtReadMacs (FILE *fp)
{
PBWT *p ;
int M ;
double L ;
int i,j, nxPack, nyPack, nUnpack, n0 ;
int nxTot = 0, nyTot = 0 ;
uchar *x ;
int *a ;
PbwtCursor *u ;
parseMacsHeader (fp, &M, &L) ;
p = pbwtCreate (M) ;
p->sites = arrayCreate(4096, Site) ;
p->yz = arrayCreate(4096*32, uchar) ;
p->N = 0 ;
u = pbwtCursorCreate (p, TRUE, TRUE) ;
x = myalloc (M+1, uchar) ; x[M] = Y_SENTINEL ; /* sentinel required for packing */
while (parseMacsSite (fp, arrayp(p->sites,p->N,Site), M, L, x))
{ for (j = 0 ; j < M ; ++j) u->y[j] = x[u->a[j]] ; /* next character in sort order: BWT */
pbwtCursorWriteForwards (u) ;
p->N++ ;
if (nCheckPoint && !(p->N % nCheckPoint))
pbwtCheckPoint (p) ;
}
p->aFend = myalloc (p->M, int) ; memcpy (p->aFend, u->a, p->M*sizeof(int)) ;
fprintf (stderr, "read MaCS file: M, N are\t%d\t%d\n", M, p->N) ;
if (isStats)
fprintf (stderr, " xtot, ytot are\t%d\t%d\n", nxTot, nyTot) ;
free(x) ; pbwtCursorDestroy (u) ;
return p ;
}
/************* read vcfq files, made with vcf query ... ***************/
static char* getVariation (FILE *fp)
{
static Array a = 0 ;
char c ;
int i = 0 ;
if (!a) a = arrayCreate (64, char) ;
while (!isspace(c = getc(fp)) && !feof(fp)) array(a,i++,char) = c ;
array(a,i++,char) = c ;
while (!isspace(c = getc(fp)) && !feof(fp)) array(a,i++,char) = c ;
array(a,i++,char) = 0 ;
return arrp(a,0,char) ;
}
static BOOL parseVcfqLine (PBWT **pp, FILE *fp, Array x)
{
char c, *chrom, *var ;
int pos, m, len ;
Site *s ;
PBWT *p = *pp ;
if (!p) /* first call on a file - don't know M yet */
chrom = strdup (fgetword (fp)) ;
else
while (!feof (fp) && !readMatchChrom (&p->chrom, fp))
while (!feof (fp) && (getc (fp) != '\n')) ; /* ignore rest of line */
if (feof (fp)) return 0 ;
pos = atoi (fgetword (fp)) ;
var = getVariation (fp) ;
m = 0 ;
while ((c = getc (fp)))
switch (c)
{ case EOF: return 0 ;
case '\n': goto endOfLine ; /* -1 is EOF */
case '0': array(x,m++,uchar) = 0 ; break ;
case '1': array(x,m++,uchar) = 1 ; break ;
case '|': case '/': case '\\': case '\t': break ; /* could check ploidy here */
default: die ("unexpected character %d in vcfq file genotype section") ;
}
endOfLine:
if (feof (fp)) return FALSE ;
if (p && m != p->M) die ("length mismatch reading vcfq line") ;
if (!*pp)
{ p = *pp = pbwtCreate (m) ;
if (strcmp (chrom, ".")) p->chrom = chrom ;
p->sites = arrayCreate(4096, Site) ;
array(x,p->M,uchar) = Y_SENTINEL ; /* sentinel required for packing */
}
s = arrayp(p->sites, arrayMax(p->sites), Site) ;
s->x = pos ;
dictAdd (variationDict, var, &s->varD) ;
++p->N ;
return TRUE ;
}
typedef BOOL (*ParseLineFunc)(PBWT** pp, FILE *f, Array a) ;
static PBWT *pbwtReadLineFile (FILE *fp, char* type, ParseLineFunc parseLine)
{
PBWT *p = 0 ;
int j ;
uchar *x ; /* original, sorted, compressed */
int *a ;
Array xArray = arrayCreate (10000, uchar) ;
PbwtCursor *u ;
while ((*parseLine) (&p, fp, xArray)) /* create p first time round */
{ if (!p->yz) /* first line; p was just made! */
{ p->yz = arrayCreate(4096*32, uchar) ;
u = pbwtCursorCreate (p, TRUE, TRUE) ;
}
x = arrp(xArray,0,uchar) ;
for (j = 0 ; j < p->M ; ++j) u->y[j] = x[u->a[j]] ;
pbwtCursorWriteForwards (u) ;
if (nCheckPoint && !(p->N % nCheckPoint)) pbwtCheckPoint (p) ;
}
p->aFend = myalloc (p->M, int) ; memcpy (p->aFend, u->a, p->M*sizeof(int)) ;
fprintf (stderr, "read %s file", type) ;
if (p->chrom) fprintf (stderr, " for chromosome %s", p->chrom) ;
fprintf (stderr, ": M, N are\t%d\t%d; yz length is %d\n", p->M, p->N, arrayMax(p->yz)) ;
arrayDestroy(xArray) ; pbwtCursorDestroy (u) ;
return p ;
}
PBWT *pbwtReadVcfq (FILE *fp) { return pbwtReadLineFile (fp, "vcfq", parseVcfqLine) ; }
/*************** read impute2 .gen format - contains sites not samples **********/
static long int nGenMissing ;
static BOOL parseGenLine (PBWT **pp, FILE *fp, Array x) /* based on parseVcfqLine() */
{
char c, *chrom, *var ;
int pos, m, len ;
Site *s ;
PBWT *p = *pp ;
if (!p) /* first call on a file - don't know M yet */
chrom = strdup (fgetword (fp)) ;
else if (!readMatchChrom (&p->chrom, fp))
if (feof (fp)) return FALSE ;
else die ("failed to match chromosome in parseGenLine") ;
pos = atoi (fgetword (fp)) ; fgetword (fp) ; /* ignore second position */
var = getVariation (fp) ; /* but need to change ' ' separator to '\t' */
char *cp = var ; while (*cp && *cp != ' ') ++cp ; if (*cp == ' ') *cp = '\t' ; else die ("missing separator in gen line") ;
m = 0 ; char term = ' ' ;
while (!feof(fp) && term != '\n')
{ char c1 = getc(fp) ; if (c1 == '\n') break ; /* needed to handle trailing blank */
c1 -= '0' ; if (getc(fp) != ' ') die ("error in gen file at %d line %d", m, p ? p->N : 0) ;
char c2 = getc(fp) - '0'; if (getc(fp) != ' ') die ("error in gen file at %d line %d", m, p ? p->N : 0) ;
char c3 = getc(fp) - '0' ; term = getc(fp) ;
if (c1 + c2 + c3 == 0) /* missing genotype */
{ c1 = 1 ; ++nGenMissing ; }
if (c1 + c2 + c3 != 1)
die ("inconsistent genotype in gen file: %d %d %d at %d line %d",
c1, c2, c3, m, p ? p->N : 0) ;
if (c1) { array(x,m++,uchar) = 0 ; array(x,m++,uchar) = 0 ; }
else if (c2) { array(x,m++,uchar) = 0 ; array(x,m++,uchar) = 1 ; }
else /*c3 == 1 */ { array(x,m++,uchar) = 1 ; array(x,m++,uchar) = 1 ; }
}
if (feof (fp)) return FALSE ;
if (p && m != p->M) die ("length mismatch reading vcfq line") ;
if (!*pp)
{ p = *pp = pbwtCreate (m) ;
if (strcmp (chrom, ".")) p->chrom = chrom ;
p->sites = arrayCreate(4096, Site) ;
array(x,p->M,uchar) = Y_SENTINEL ; /* sentinel required for packing */
}
s = arrayp(p->sites, arrayMax(p->sites), Site) ;
s->x = pos ;
dictAdd (variationDict, var, &s->varD) ;
++p->N ;
return TRUE ;
}
PBWT *pbwtReadGen (FILE *fp)
{
nGenMissing = 0 ;
PBWT *p = pbwtReadLineFile (fp, "gen", parseGenLine) ;
if (nGenMissing) fprintf (stderr, "%ld missing genotypes set to 00\n", nGenMissing) ;
return p ;
}
/*************** write haplotypes ******************/
void pbwtWriteHaplotypes (FILE *fp, PBWT *p)
{
int i, j, n = 0, M = p->M ;
uchar *hap = myalloc (M, uchar) ;
PbwtCursor *u = pbwtCursorCreate (p, TRUE, TRUE) ;
for (i = 0 ; i < p->N ; ++i)
{ for (j = 0 ; j < M ; ++j) hap[u->a[j]] = u->y[j] ;
for (j = 0 ; j < M ; ++j) putc (hap[j]?'1':'0', fp) ;
putc ('\n', fp) ; fflush (fp) ;
pbwtCursorForwardsRead (u) ;
}
free (hap) ; pbwtCursorDestroy (u) ;
fprintf (stderr, "written haplotype file: %d rows of %d\n", p->N, M) ;
}
/******************* end of file *******************/