-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathaffy2vcf.c
2758 lines (2540 loc) · 116 KB
/
affy2vcf.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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* The MIT License
Copyright (c) 2018-2024 Giulio Genovese
Author: Giulio Genovese <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <getopt.h>
#include <errno.h>
#include <wchar.h>
#include <sys/resource.h>
#include <arpa/inet.h>
#include <htslib/vcf.h>
#include <htslib/kseq.h>
#include <htslib/khash_str2int.h>
#include "bcftools.h"
#include "gtc2vcf.h"
#define AFFY2VCF_VERSION "2024-12-13"
#define TAG_LIST_DFLT "GT,CONF,BAF,LRR,NORMX,NORMY,DELTA,SIZE"
#define GC_WIN_DFLT "200"
#define VERBOSE (1 << 0)
#define LOAD_CEL (1 << 1)
#define PROBESET_IDS_LOADED (1 << 2)
#define CALLS_LOADED (1 << 3)
#define CONFIDENCES_LOADED (1 << 4)
#define SUMMARY_LOADED (1 << 5)
#define SNP_LOADED (1 << 6)
#define ADJUST_CLUSTERS (1 << 7)
#define NO_INFO_GC (1 << 8)
#define FORMAT_GT (1 << 9)
#define FORMAT_CONF (1 << 10)
#define FORMAT_BAF (1 << 11)
#define FORMAT_LRR (1 << 12)
#define FORMAT_NORMX (1 << 13)
#define FORMAT_NORMY (1 << 14)
#define FORMAT_DELTA (1 << 15)
#define FORMAT_SIZE (1 << 16)
// #%affymetrix—algorithm—param—apt—opt—use—copynumber—call—codes=0
// #%call—code-1=NoCall:-1:2
// #%call—code-2=AA:0:2
// #%call—code-3=AB:1:2
// #%call—code-4=BB:2:2
#define GT_NC -1
#define GT_AA 0
#define GT_AB 1
#define GT_BB 2
// #%max—alleles=4
// #%max—cn—states=2
// #%call—code-1=OTV_1:-4:1
// #%call—code-2=NoCall_1:-3:1
// #%call—code-3=OTV:-2:2
// #%call—code-4=NoCall:-1:2
// #%call—code-5=AA:0:2
// #%call—code-6=AB:1:2
// #%call—code-7=BB:2:2
// #%call—code-8=ZeroCN:3:0
// #%call—code-9=A:4:1
// #%call—code-10=B:5:1
// #%call—code-11=C:6:1
// #%call—code-12=AC:7:2
// #%call—code-13=BC:8:2
// #%call—code-14=CC:9:2
// #%call—code-15=D:10:1
// #%call—code-16=AD:11:2
// #%call—code-17=BD:12:2
// #%call—code-18=CD:13:2
// #%call—code-19=DD:14:2
static const int txt_gt[19] = {GT_NC, GT_NC, GT_NC, GT_NC, GT_AA, GT_AB, GT_BB, GT_NC, GT_AA, GT_BB,
GT_NC, GT_NC, GT_NC, GT_NC, GT_NC, GT_NC, GT_NC, GT_NC, GT_NC};
static const int chp_gt[16] = {-1, -1, -1, -1, -1, -1, GT_AA, GT_BB, GT_AB, -1, -1, GT_NC, -1, -1, -1, -1};
/****************************************
* hFILE READING FUNCTIONS *
****************************************/
// read long in network order
static inline uint32_t read_long(hFILE *hfile) {
uint32_t value;
read_bytes(hfile, (void *)&value, sizeof(uint32_t));
value = ntohl(value);
return value;
}
// read float in network order
static inline float read_float(hFILE *hfile) {
union {
uint32_t u;
float f;
} convert;
read_bytes(hfile, (void *)&convert.u, sizeof(uint32_t));
convert.u = ntohl(convert.u);
return convert.f;
}
// read string in network order
static inline int32_t read_string8(hFILE *hfile, char **buffer) {
int32_t len = (int32_t)read_long(hfile);
if (len) {
*buffer = (char *)malloc((1 + len) * sizeof(char));
read_bytes(hfile, (void *)*buffer, len * sizeof(char));
(*buffer)[len] = '\0';
} else {
*buffer = NULL;
}
return len;
}
// read wide-character string in network order
static inline int32_t read_string16(hFILE *hfile, wchar_t **buffer) {
int32_t len = (int32_t)read_long(hfile);
if (len) {
*buffer = (wchar_t *)malloc((1 + len) * sizeof(wchar_t));
int i;
for (i = 0; i < len; i++) {
uint16_t cvalue;
read_bytes(hfile, (void *)&cvalue, sizeof(unsigned short));
(*buffer)[i] = (wchar_t)ntohs(cvalue);
}
(*buffer)[len] = L'\0';
} else {
*buffer = NULL;
}
return len;
}
/****************************************
* CEL FILE IMPLEMENTATION *
****************************************/
// http://www.affymetrix.com/support/developer/powertools/changelog/gcos-agcc/index.html
typedef struct {
float mean __attribute__((packed));
float dev __attribute__((packed));
int16_t N;
} Cell;
typedef struct {
int16_t x;
int16_t y;
} Entry;
typedef struct {
int32_t row;
int32_t col;
float upper_left_x;
float upper_left_y;
float upper_right_x;
float upper_right_y;
float lower_left_x;
float lower_left_y;
float lower_right_x;
float lower_right_y;
int32_t left_cell;
int32_t top_cell;
int32_t right_cell;
int32_t bottom_cell;
} SubGrid;
typedef struct {
char *fn;
hFILE *hfile;
int32_t version;
int32_t num_rows;
int32_t num_cols;
int32_t num_cells;
int32_t n_header;
char *header;
int32_t n_algorithm;
char *algorithm;
int32_t n_parameters;
char *parameters;
int32_t cell_margin;
uint32_t num_outlier_cells;
uint32_t num_masked_cells;
int32_t num_sub_grids;
Cell *cells;
Entry *masked_entries;
Entry *outlier_entries;
SubGrid *sub_grids;
} xda_cel_t;
static xda_cel_t *xda_cel_init(const char *fn, hFILE *hfile, int flags) {
xda_cel_t *xda_cel = (xda_cel_t *)calloc(1, sizeof(xda_cel_t));
xda_cel->fn = strdup(fn);
xda_cel->hfile = hfile;
int32_t magic;
read_bytes(xda_cel->hfile, (void *)&magic, sizeof(int32_t));
if (magic != 64) error("XDA CEL file %s magic number is %d while it should be 64\n", xda_cel->fn, magic);
read_bytes(xda_cel->hfile, (void *)&xda_cel->version, sizeof(int32_t));
if (xda_cel->version != 4)
error("Cannot read XDA CEL file %s. Unsupported XDA CEL file format version: %d\n", xda_cel->fn,
xda_cel->version);
read_bytes(xda_cel->hfile, (void *)&xda_cel->num_rows, sizeof(int32_t));
read_bytes(xda_cel->hfile, (void *)&xda_cel->num_cols, sizeof(int32_t));
read_bytes(xda_cel->hfile, (void *)&xda_cel->num_cells, sizeof(int32_t));
read_bytes(xda_cel->hfile, (void *)&xda_cel->n_header, sizeof(int32_t));
xda_cel->header = (char *)malloc((1 + xda_cel->n_header) * sizeof(char));
read_bytes(xda_cel->hfile, (void *)xda_cel->header, xda_cel->n_header * sizeof(char));
xda_cel->header[xda_cel->n_header] = '\0';
read_bytes(xda_cel->hfile, (void *)&xda_cel->n_algorithm, sizeof(int32_t));
xda_cel->algorithm = (char *)malloc((1 + xda_cel->n_algorithm) * sizeof(char));
read_bytes(xda_cel->hfile, (void *)xda_cel->algorithm, xda_cel->n_algorithm * sizeof(char));
xda_cel->algorithm[xda_cel->n_algorithm] = '\0';
read_bytes(xda_cel->hfile, (void *)&xda_cel->n_parameters, sizeof(int32_t));
xda_cel->parameters = (char *)malloc((1 + xda_cel->n_parameters) * sizeof(char));
read_bytes(xda_cel->hfile, (void *)xda_cel->parameters, xda_cel->n_parameters * sizeof(char));
xda_cel->parameters[xda_cel->n_parameters] = '\0';
read_bytes(xda_cel->hfile, (void *)&xda_cel->cell_margin, sizeof(int32_t));
read_bytes(xda_cel->hfile, (void *)&xda_cel->num_outlier_cells, sizeof(uint32_t));
read_bytes(xda_cel->hfile, (void *)&xda_cel->num_masked_cells, sizeof(uint32_t));
read_bytes(xda_cel->hfile, (void *)&xda_cel->num_sub_grids, sizeof(int32_t));
if (flags) return xda_cel;
xda_cel->cells = (Cell *)malloc(xda_cel->num_cells * sizeof(Cell));
read_bytes(xda_cel->hfile, (void *)xda_cel->cells, xda_cel->num_cells * sizeof(Cell));
xda_cel->masked_entries = (Entry *)malloc(xda_cel->num_masked_cells * sizeof(Entry));
read_bytes(xda_cel->hfile, (void *)xda_cel->masked_entries, xda_cel->num_masked_cells * sizeof(Entry));
xda_cel->outlier_entries = (Entry *)malloc(xda_cel->num_outlier_cells * sizeof(Entry));
read_bytes(xda_cel->hfile, (void *)xda_cel->outlier_entries, xda_cel->num_outlier_cells * sizeof(Entry));
xda_cel->sub_grids = (SubGrid *)malloc(xda_cel->num_sub_grids * sizeof(SubGrid));
read_bytes(xda_cel->hfile, (void *)xda_cel->sub_grids, xda_cel->num_sub_grids * sizeof(SubGrid));
if (!heof(xda_cel->hfile))
error("XDA CEL reader did not reach the end of file %s at position %ld\n", xda_cel->fn, htell(xda_cel->hfile));
return xda_cel;
}
static void xda_cel_destroy(xda_cel_t *xda_cel) {
if (!xda_cel) return;
free(xda_cel->fn);
if (hclose(xda_cel->hfile) < 0) error("Error closing XDA CEL file\n");
free(xda_cel->header);
free(xda_cel->algorithm);
free(xda_cel->parameters);
free(xda_cel->cells);
free(xda_cel->masked_entries);
free(xda_cel->outlier_entries);
free(xda_cel->sub_grids);
free(xda_cel);
}
static void xda_cel_print(const xda_cel_t *xda_cel, FILE *stream, int verbose) {
fprintf(stream, "[CEL]\n");
fprintf(stream, "Version=3\n");
fprintf(stream, "\n[HEADER]\n");
fprintf(stream, "%s", xda_cel->header);
fprintf(stream, "\n[INTENSITY]\n");
fprintf(stream, "NumberCells=%d\n", xda_cel->num_cells);
fprintf(stream, "CellHeader=X\tY\tMEAN\tSTDV\tNPIXELS\n");
int i;
if (!verbose)
fprintf(stream, "... use --verbose to visualize Cell Entries ...\n");
else
for (i = 0; i < xda_cel->num_cells; i++)
fprintf(stream, "%3d\t%3d\t%.1f\t%.1f\t%3d\n", i % xda_cel->num_cols, i / xda_cel->num_cols,
xda_cel->cells[i].mean, xda_cel->cells[i].dev, xda_cel->cells[i].N);
fprintf(stream, "\n[MASKS]\n");
fprintf(stream, "NumberCells=%d\n", xda_cel->num_masked_cells);
fprintf(stream, "CellHeader=X\tY\n");
if (!verbose)
fprintf(stream, "... use --verbose to visualize Masked Entries ...\n");
else
for (i = 0; i < xda_cel->num_masked_cells; i++)
fprintf(stream, "%d\t%d\n", xda_cel->masked_entries[i].x, xda_cel->masked_entries[i].y);
fprintf(stream, "\n[OUTLIERS]\n");
fprintf(stream, "NumberCells=%d\n", xda_cel->num_outlier_cells);
fprintf(stream, "CellHeader=X\tY\n");
if (!verbose)
fprintf(stream, "... use --verbose to visualize Outlier Entries ...\n");
else
for (i = 0; i < xda_cel->num_outlier_cells; i++)
fprintf(stream, "%d\t%d\n", xda_cel->outlier_entries[i].x, xda_cel->outlier_entries[i].y);
fprintf(stream, "\n[MODIFIED]\n");
fprintf(stream, "NumberCells=0\n");
fprintf(stream, "CellHeader=X\tY\tORIGMEAN\n");
}
/****************************************
* CHP FILE IMPLEMENTATION *
****************************************/
// http://www.affymetrix.com/support/developer/powertools/changelog/gcos-agcc/index.html
#define BYTE 0
#define UBYTE 1
#define SHORT 2
#define USHORT 3
#define INT 4
#define UINT 5
#define FLOAT 6
#define STRING 7
#define WSTRING 8
typedef struct {
wchar_t *name;
char *value;
wchar_t *mime_type;
int32_t n_value;
int8_t type;
} Parameter;
typedef struct DataHeader DataHeader;
struct DataHeader {
char *data_type_identifier;
char *guid;
wchar_t *datetime;
wchar_t *locale;
int32_t n_parameters;
Parameter *parameters;
int32_t n_parents;
DataHeader *parents;
};
typedef struct {
wchar_t *name;
int8_t type;
int32_t size;
} ColHeader;
typedef struct {
uint32_t pos_first_element;
uint32_t pos_next_data_set;
wchar_t *name;
int32_t n_parameters;
Parameter *parameters;
uint32_t n_cols;
ColHeader *col_headers;
uint32_t n_rows;
hFILE *hfile; // this should not be destroyed
uint32_t n_buffer;
uint32_t *col_offsets;
char *buffer;
} DataSet;
typedef struct {
uint32_t pos_next_data_group;
uint32_t pos_first_data_set;
int32_t num_data_sets;
wchar_t *name;
DataSet *data_sets;
} DataGroup;
typedef struct {
wchar_t *name;
int8_t type;
int32_t size;
} ColumnHeader;
typedef struct {
char *fn;
hFILE *hfile;
uint8_t magic;
uint8_t version;
int32_t num_data_groups;
uint32_t pos_first_data_group;
DataHeader data_header;
DataGroup *data_groups;
off_t size;
char *display_name;
} agcc_t;
static void agcc_read_parameters(Parameter *parameter, hFILE *hfile, int flags) {
read_string16(hfile, ¶meter->name);
parameter->n_value = read_string8(hfile, ¶meter->value);
read_string16(hfile, ¶meter->mime_type);
if (wcscmp(parameter->mime_type, L"text/x-calvin-integer-8") == 0)
parameter->type = BYTE;
else if (wcscmp(parameter->mime_type, L"text/x-calvin-unsigned-integer-8") == 0)
parameter->type = UBYTE;
else if (wcscmp(parameter->mime_type, L"text/x-calvin-integer-16") == 0)
parameter->type = SHORT;
else if (wcscmp(parameter->mime_type, L"text/x-calvin-unsigned-integer-16") == 0)
parameter->type = USHORT;
else if (wcscmp(parameter->mime_type, L"text/x-calvin-integer-32") == 0)
parameter->type = INT;
else if (wcscmp(parameter->mime_type, L"text/x-calvin-unsigned-integer-32") == 0)
parameter->type = UINT;
else if (wcscmp(parameter->mime_type, L"text/x-calvin-float") == 0)
parameter->type = FLOAT;
else if (wcscmp(parameter->mime_type, L"text/ascii") == 0)
parameter->type = STRING;
else if (wcscmp(parameter->mime_type, L"text/plain") == 0)
parameter->type = WSTRING;
else
error("MIME type %ls not allowed\n", parameter->mime_type);
// drop parameters that can increase the size of the header dramatically
if (flags && wcsncmp(parameter->name, L"affymetrix-algorithm-param-apt-opt-cel", 38) == 0) {
free(parameter->name);
parameter->name = NULL;
parameter->n_value = 0;
free(parameter->value);
parameter->value = NULL;
free(parameter->mime_type);
parameter->mime_type = NULL;
}
}
static void agcc_read_data_header(DataHeader *data_header, hFILE *hfile, int flags) {
int i;
read_string8(hfile, &data_header->data_type_identifier);
read_string8(hfile, &data_header->guid);
read_string16(hfile, &data_header->datetime);
read_string16(hfile, &data_header->locale);
data_header->n_parameters = (int32_t)read_long(hfile);
data_header->parameters = (Parameter *)malloc(data_header->n_parameters * sizeof(Parameter));
for (i = 0; i < data_header->n_parameters; i++) agcc_read_parameters(&data_header->parameters[i], hfile, flags);
data_header->n_parents = (int32_t)read_long(hfile);
data_header->parents = (DataHeader *)malloc(data_header->n_parents * sizeof(DataHeader));
for (i = 0; i < data_header->n_parents; i++) agcc_read_data_header(&data_header->parents[i], hfile, flags);
}
static void agcc_read_data_set(DataSet *data_set, hFILE *hfile, int flags) {
int i;
data_set->pos_first_element = read_long(hfile);
data_set->pos_next_data_set = read_long(hfile);
read_string16(hfile, &data_set->name);
data_set->n_parameters = (int32_t)read_long(hfile);
data_set->parameters = (Parameter *)malloc(data_set->n_parameters * sizeof(Parameter));
for (i = 0; i < data_set->n_parameters; i++) agcc_read_parameters(&data_set->parameters[i], hfile, flags);
data_set->n_cols = read_long(hfile);
data_set->col_headers = (ColHeader *)malloc(data_set->n_cols * sizeof(ColHeader));
for (i = 0; i < data_set->n_cols; i++) {
read_string16(hfile, &data_set->col_headers[i].name);
read_bytes(hfile, (void *)&data_set->col_headers[i].type, sizeof(int8_t));
data_set->col_headers[i].size = read_long(hfile);
}
data_set->n_rows = read_long(hfile);
data_set->hfile = hfile;
data_set->col_offsets = (uint32_t *)malloc(data_set->n_cols * sizeof(uint32_t *));
data_set->n_buffer = 0;
for (i = 0; i < data_set->n_cols; i++) {
data_set->col_offsets[i] = data_set->n_buffer;
data_set->n_buffer += data_set->col_headers[i].size;
}
data_set->buffer = (char *)malloc(data_set->n_buffer * sizeof(char));
if (data_set->pos_next_data_set)
if (hseek(hfile, data_set->pos_next_data_set, SEEK_SET) < 0)
error("Fail to seek to position %d in AGCC file\n", data_set->pos_next_data_set);
}
static void agcc_read_data_group(DataGroup *data_group, hFILE *hfile, int flags) {
int i;
data_group->pos_next_data_group = read_long(hfile);
data_group->pos_first_data_set = read_long(hfile);
data_group->num_data_sets = read_long(hfile);
read_string16(hfile, &data_group->name);
if (hseek(hfile, data_group->pos_first_data_set, SEEK_SET) < 0)
error("Fail to seek to position %d in AGCC file\n", data_group->pos_first_data_set);
data_group->data_sets = (DataSet *)malloc(data_group->num_data_sets * sizeof(DataSet));
for (i = 0; i < data_group->num_data_sets; i++) agcc_read_data_set(&data_group->data_sets[i], hfile, flags);
if (data_group->pos_next_data_group)
if (hseek(hfile, data_group->pos_next_data_group, SEEK_SET) < 0)
error("Fail to seek to position %d in AGCC file\n", data_group->pos_next_data_group);
}
static agcc_t *agcc_init(const char *fn, hFILE *hfile, int flags) {
int i;
agcc_t *agcc = (agcc_t *)calloc(1, sizeof(agcc_t));
agcc->fn = strdup(fn);
agcc->hfile = hfile;
// read File Header
read_bytes(agcc->hfile, (void *)&agcc->magic, sizeof(uint8_t));
if (agcc->magic != 59) error("AGCC file %s magic number is %d while it should be 59\n", agcc->fn, agcc->magic);
read_bytes(agcc->hfile, (void *)&agcc->version, sizeof(uint8_t));
if (agcc->version != 1)
error("Cannot read AGCC file %s. Unsupported AGCC file format version: %d\n", agcc->fn, agcc->version);
agcc->num_data_groups = (int32_t)read_long(agcc->hfile);
agcc->pos_first_data_group = read_long(agcc->hfile);
// read Generic Data Header
agcc_read_data_header(&agcc->data_header, agcc->hfile, flags);
// read Data Groups
if (hseek(agcc->hfile, agcc->pos_first_data_group, SEEK_SET) < 0)
error("Fail to seek to position %d in AGCC %s file\n", agcc->pos_first_data_group, agcc->fn);
agcc->data_groups = (DataGroup *)malloc(agcc->num_data_groups * sizeof(DataGroup));
for (i = 0; i < agcc->num_data_groups; i++) agcc_read_data_group(&agcc->data_groups[i], agcc->hfile, flags);
if (!heof(agcc->hfile))
error("AGCC reader did not reach the end of file %s at position %ld\n", agcc->fn, htell(agcc->hfile));
if (hseek(agcc->hfile, 0L, SEEK_END) < 0) error("Fail to seek to end of AGCC %s file\n", agcc->fn);
agcc->size = htell(agcc->hfile);
char *ptr = strrchr(agcc->fn, '/') ? strrchr(agcc->fn, '/') + 1 : agcc->fn;
agcc->display_name = strdup(ptr);
ptr = strrchr(agcc->display_name, '.');
if (ptr && strcmp(ptr + 1, "chp") == 0) {
*ptr = '\0';
ptr = strrchr(agcc->display_name, '.');
if (ptr && (strcmp(ptr + 1, "AxiomGT1") == 0 || strcmp(ptr + 1, "birdseed-v2") == 0)) *ptr = '\0';
}
return agcc;
}
static void agcc_destroy_parameters(Parameter *parameters, int32_t n_parameters) {
int i;
for (i = 0; i < n_parameters; i++) {
free(parameters[i].name);
free(parameters[i].value);
free(parameters[i].mime_type);
}
free(parameters);
}
static void agcc_destroy_data_header(DataHeader *data_header) {
int i;
free(data_header->data_type_identifier);
free(data_header->guid);
free(data_header->datetime);
free(data_header->locale);
agcc_destroy_parameters(data_header->parameters, data_header->n_parameters);
for (i = 0; i < data_header->n_parents; i++) agcc_destroy_data_header(&data_header->parents[i]);
free(data_header->parents);
}
static void agcc_destroy_data_set(DataSet *data_set) {
int i;
free(data_set->name);
agcc_destroy_parameters(data_set->parameters, data_set->n_parameters);
for (i = 0; i < data_set->n_cols; i++) free(data_set->col_headers[i].name);
free(data_set->col_headers);
free(data_set->col_offsets);
free(data_set->buffer);
}
static void agcc_destroy_data_group(DataGroup *data_group) {
int i;
free(data_group->name);
for (i = 0; i < data_group->num_data_sets; i++) agcc_destroy_data_set(&data_group->data_sets[i]);
free(data_group->data_sets);
}
static void agcc_destroy(agcc_t *agcc) {
if (!agcc) return;
int i;
free(agcc->fn);
if (hclose(agcc->hfile) < 0) error("Error closing AGCC file\n");
agcc_destroy_data_header(&agcc->data_header);
for (i = 0; i < agcc->num_data_groups; i++) agcc_destroy_data_group(&agcc->data_groups[i]);
free(agcc->data_groups);
free(agcc->display_name);
free(agcc);
}
static void buffer_string16(const uint16_t *value, int32_t n_value, size_t *m_buffer, wchar_t **buffer) {
int i;
hts_expand(wchar_t, n_value / 2 + 1, *m_buffer, *buffer);
for (i = 0; i < n_value / 2; i++) (*buffer)[i] = (wchar_t)ntohs(value[i]);
(*buffer)[n_value / 2] = L'\0';
}
static void agcc_print_parameters(const Parameter *parameters, int32_t n_parameters, FILE *stream) {
int i;
union {
uint32_t u;
float f;
} convert;
wchar_t *buffer = NULL;
size_t m_buffer = 0;
for (i = 0; i < n_parameters; i++) {
fprintf(stream, "#%%%ls=", parameters[i].name ? parameters[i].name : L"");
switch (parameters[i].type) {
case BYTE:
fprintf(stream, "%d\n", (int8_t)ntohl(*(uint32_t *)parameters[i].value));
break;
case UBYTE:
fprintf(stream, "%u\n", (uint8_t)ntohl(*(uint32_t *)parameters[i].value));
break;
case SHORT:
fprintf(stream, "%d\n", (int16_t)ntohl(*(uint32_t *)parameters[i].value));
break;
case USHORT:
fprintf(stream, "%u\n", (uint16_t)ntohl(*(uint32_t *)parameters[i].value));
break;
case INT:
fprintf(stream, "%d\n", (int32_t)ntohl(*(uint32_t *)parameters[i].value));
break;
case UINT:
fprintf(stream, "%u\n", ntohl(*(uint32_t *)parameters[i].value));
break;
case FLOAT:
convert.u = ntohl(*(uint32_t *)parameters[i].value);
fprintf(stream, "%f\n", convert.f);
break;
case STRING:
fprintf(stream, "%s\n", parameters[i].value);
break;
case WSTRING:
buffer_string16((uint16_t *)parameters[i].value, parameters[i].n_value, &m_buffer, &buffer);
fprintf(stream, "%ls\n", buffer);
break;
default:
break;
}
}
free(buffer);
}
static void agcc_print_data_header(const DataHeader *data_header, FILE *stream) {
int i;
if (data_header->guid) fprintf(stream, "#%%FileIdentifier=%s\n", data_header->guid);
fprintf(stream, "#%%FileTypeIdentifier=%s\n", data_header->data_type_identifier);
fprintf(stream, "#%%FileLocale=%ls\n", data_header->locale);
agcc_print_parameters(data_header->parameters, data_header->n_parameters, stream);
for (i = 0; i < data_header->n_parents; i++) agcc_print_data_header(&data_header->parents[i], stream);
}
typedef void (*col_print_t)(const char *, FILE *stream);
void agcc_print_probe_set_name(const char *s, FILE *stream) {
uint32_t size = ntohl(*(uint32_t *)s);
fwrite(s + 4, 1, size, stream);
}
void agcc_print_call(const char *s, FILE *stream) {
static const char a[16] = "......ABA..N....";
static const char b[16] = "......ABB..C....";
int c = s[0] & 0x0F;
fputc(a[c], stream);
fputc(b[c], stream);
}
void agcc_print_float(const char *s, FILE *stream) {
union {
uint32_t u;
float f;
} convert;
convert.u = ntohl(*(uint32_t *)s);
fprintf(stream, "%g", convert.f);
}
static void agcc_print_data_set(const DataSet *data_set, FILE *stream, int verbose) {
fprintf(stream, "#%%SetName=%ls\n", data_set->name);
fprintf(stream, "#%%Columns=%d\n", data_set->n_cols);
fprintf(stream, "#%%Rows=%d\n", data_set->n_rows);
int i, j;
agcc_print_parameters(data_set->parameters, data_set->n_parameters, stream);
for (i = 0; i < data_set->n_cols; i++)
fprintf(stream, "%ls%c", data_set->col_headers[i].name, i + 1 < data_set->n_cols ? '\t' : '\n');
if (data_set->n_rows == 0) return;
if (!verbose) {
fprintf(stream, "... use --verbose to visualize Data Set ...\n");
return;
}
if (wcscmp(data_set->name, L"Genotype") != 0) {
fprintf(stream, "... can only visualize Genotype Data Set ...\n");
return;
}
char *col_ends = (char *)malloc(data_set->n_cols * sizeof(char *));
col_print_t *col_prints = (col_print_t *)malloc(data_set->n_cols * sizeof(col_print_t *));
for (i = 0; i < data_set->n_cols; i++) {
col_ends[i] = i + 1 < data_set->n_cols ? '\t' : '\n';
if (wcscmp(data_set->col_headers[i].name, L"ProbeSetName") == 0)
col_prints[i] = agcc_print_probe_set_name;
else if (wcscmp(data_set->col_headers[i].name, L"Call") == 0)
col_prints[i] = agcc_print_call;
else if (wcscmp(data_set->col_headers[i].name, L"Confidence") == 0)
col_prints[i] = agcc_print_float;
else if (wcscmp(data_set->col_headers[i].name, L"Contrast") == 0)
col_prints[i] = agcc_print_float;
else if (wcscmp(data_set->col_headers[i].name, L"Log Ratio") == 0)
col_prints[i] = agcc_print_float;
else if (wcscmp(data_set->col_headers[i].name, L"Strength") == 0)
col_prints[i] = agcc_print_float;
else if (wcscmp(data_set->col_headers[i].name, L"Signal A") == 0)
col_prints[i] = agcc_print_float;
else if (wcscmp(data_set->col_headers[i].name, L"Signal B") == 0)
col_prints[i] = agcc_print_float;
else if (wcscmp(data_set->col_headers[i].name, L"Forced Call") == 0)
col_prints[i] = agcc_print_call;
else
error("Unknown column type %ls in AGCC file with type %d\n", data_set->col_headers[i].name,
data_set->col_headers[i].type);
}
if (hseek(data_set->hfile, data_set->pos_first_element, SEEK_SET) < 0)
error("Fail to seek to position %d in AGCC file\n", data_set->pos_first_element);
for (i = 0; i < data_set->n_rows; i++) {
read_bytes(data_set->hfile, (void *)data_set->buffer, data_set->n_buffer);
for (j = 0; j < data_set->n_cols; j++) {
col_prints[j](data_set->buffer + data_set->col_offsets[j], stream);
fputc(col_ends[j], stream);
}
}
free(col_ends);
free(col_prints);
}
static void agcc_print_data_group(const DataGroup *data_group, FILE *stream, int verbose) {
fprintf(stream, "#%%GroupName=%ls\n", data_group->name);
int i;
for (i = 0; i < data_group->num_data_sets; i++) agcc_print_data_set(&data_group->data_sets[i], stream, verbose);
}
static void agcc_print(const agcc_t *agcc, FILE *stream, int verbose) {
fprintf(stream, "#%%File=%s\n", agcc->fn);
fprintf(stream, "#%%FileSize=%ld\n", agcc->size);
fprintf(stream, "#%%Magic=%d\n", agcc->magic);
fprintf(stream, "#%%Version=%d\n", agcc->version);
int i;
agcc_print_data_header(&agcc->data_header, stream);
for (i = 0; i < agcc->num_data_groups; i++) agcc_print_data_group(&agcc->data_groups[i], stream, verbose);
}
static void chps_to_tsv(uint8_t *magic, agcc_t **agcc, int n, FILE *stream) {
int i, j, k;
// AxiomGT1 analysis has also cn-probe-chrXY-ratio_gender_meanX,
// cn-probe-chrXY-ratio_gender_meanY, cn-probe-chrXY-ratio_gender_ratio,
// cn-probe-chrXY-ratio_gender while BRLMM-P analysis has also em-cluster-chrX-het-contrast_gender
// em-cluster-chrX-het-contrast_gender_chrX_het_rate
// pm_mean
static const wchar_t *chipsummary[] = {L"computed_gender",
L"call_rate",
L"total_call_rate",
L"het_rate",
L"total_het_rate",
L"hom_rate",
L"total_hom_rate",
L"cluster_distance_mean",
L"cluster_distance_stdev",
L"allele_summarization_mean",
L"allele_summarization_stdev",
L"allele_deviation_mean",
L"allele_deviation_stdev",
L"allele_mad_residuals_mean",
L"allele_mad_residuals_stdev"};
fputs("chp", stream);
for (j = 0; j < 15; j++) fprintf(stream, "\t%ls", chipsummary[j]);
fputc('\n', stream);
for (i = 0; i < n; i++) {
if (magic[i] != 59) continue;
if (strcmp(agcc[i]->data_header.data_type_identifier, "affymetrix-multi-data-type-analysis") != 0) {
if (strcmp(agcc[i]->data_header.data_type_identifier, "affymetrix-calvin-intensity") == 0
|| strcmp(agcc[i]->data_header.data_type_identifier, "affymetrix-calvin-multi-intensity") == 0)
error(
"AGCC file %s contains calvin intensities rather multi data type analysis (use --cel to extract "
"metadata)\n",
agcc[i]->fn);
else
error("AGCC file %s does not contain multi data type analysis as data type identifier is %s\n",
agcc[i]->fn, agcc[i]->data_header.data_type_identifier);
}
fputs(strrchr(agcc[i]->fn, '/') ? strrchr(agcc[i]->fn, '/') + 1 : agcc[i]->fn, stream);
DataHeader *data_header = &agcc[i]->data_header;
for (j = 0, k = 0; j < 15; j++) {
fputc('\t', stream);
while (!data_header->parameters[k].name
|| wcsncmp(data_header->parameters[k].name, L"affymetrix-chipsummary-", 23) != 0
|| wcscmp(&data_header->parameters[k].name[23], chipsummary[j]) != 0) {
k++;
k %= data_header->n_parameters;
}
union {
uint32_t u;
float f;
} convert;
switch (data_header->parameters[k].type) {
case FLOAT:
convert.u = ntohl(*(uint32_t *)data_header->parameters[k].value);
fprintf(stream, "%.5f", convert.f);
break;
case STRING:
fputs(data_header->parameters[k].value, stream);
break;
default:
error("Unable to print parameter of type %d from %s AGCC file\n", data_header->parameters[k].type,
agcc[i]->fn);
break;
}
}
fputc('\n', stream);
}
}
/****************************************
* PRINT CEL SUMMARY *
****************************************/
// this function returns
// fusion-experiment-name
// pixel-cols
// pixel-rows
// XIN
// YIN
// VE
// temp
// power
// scan-date
// scanner-id
// scanner-type
// array-type
static void parse_dat_header(char *dat_header, char *str[12], int n_str[12]) {
char *ss = strchr(dat_header, ' ') + 2;
char *se = strchr(dat_header, '\0');
if (!se) goto fail;
se = strchr(ss, ':');
if (!se) goto fail;
str[0] = ss;
n_str[0] = se - ss;
ss = se + 5;
for (se = ss + 4; isspace(*se) && se >= ss; se--);
str[1] = ss;
n_str[1] = se - ss + 1;
ss = ss + 9;
for (se = ss + 4; isspace(*se) && se >= ss; se--);
str[2] = ss;
n_str[2] = se - ss + 1;
ss = ss + 9;
for (se = ss + 2; isspace(*se) && se >= ss; se--);
str[3] = ss;
n_str[3] = se - ss + 1;
ss = ss + 7;
for (se = ss + 2; isspace(*se) && se >= ss; se--);
str[4] = ss;
n_str[4] = se - ss + 1;
ss = ss + 6;
for (se = ss + 2; isspace(*se) && se >= ss; se--);
str[5] = ss;
n_str[5] = se - ss + 1;
ss = ss + 3;
for (se = ss + 6; isspace(*se) && se >= ss; se--);
str[6] = ss;
n_str[6] = se - ss + 1;
ss = ss + 7;
for (se = ss + 3; isspace(*se) && se >= ss; se--);
str[7] = ss;
n_str[7] = se - ss + 1;
ss = ss + 4;
for (se = ss + 17; isspace(*se) && se >= ss; se--);
str[8] = ss;
n_str[8] = se - ss + 1;
ss = ss + 18;
se = strchr(ss, ' ');
if (!se) goto fail;
str[9] = ss;
n_str[9] = se - ss;
ss = se + 2;
se = strstr(ss, "\x14 ");
if (!se) goto fail;
for (se--; isspace(*se) && se >= ss; se--);
str[10] = ss;
n_str[10] = se - ss + 1;
se = strstr(ss, "\x14 ");
if (!se) goto fail;
ss = se + 2;
se = strstr(ss, "\x14 ");
if (!se) goto fail;
ss = se + 2;
se = strstr(ss, ".1sq");
if (!se) goto fail;
str[11] = ss;
n_str[11] = se - ss;
return;
fail:
error("DAT header malformed\n");
}
// http://github.com/HenrikBengtsson/affxparser/blob/master/R/parseDatHeaderString.R
static void cels_to_tsv(uint8_t *magic, void **files, int n, FILE *stream) {
int i, j;
wchar_t *array_type = NULL; // affymetrix-array-type
wchar_t *scanner_type = NULL; // affymetrix-scanner-type
wchar_t *scanner_id = NULL; // affymetrix-scanner-id
wchar_t *scan_date = NULL; // affymetrix-scan-date
wchar_t *fusion_experiment_name = NULL; // affymetrix-fusion-experiment-name
size_t m_array_type = 0, m_scanner_type = 0, m_scanner_id = 0, m_scan_date = 0, m_fusion_experiment_name = 0;
int32_t pixel_rows = 0; // affymetrix-pixel-rows
int32_t pixel_cols = 0; // affymetrix-pixel-cols
char *str[12];
int n_str[12];
fprintf(stream,
"cel\tarray_type\tscanner_type\tscanner_id\tscan_date\tfusion_experiment_name\tpixel_rows\tpixel_cols\n");
for (i = 0; i < n; i++) {
char *ss, *se;
agcc_t *agcc = (agcc_t *)files[i];
xda_cel_t *xda_cel = (xda_cel_t *)files[i];
switch (magic[i]) {
case 59:
if (strcmp(agcc->data_header.data_type_identifier, "affymetrix-calvin-intensity") != 0
&& strcmp(agcc->data_header.data_type_identifier, "affymetrix-calvin-multi-intensity") != 0)
error("AGCC file %s does not contain calvin intensities as data type identifier is %s\n", agcc->fn,
agcc->data_header.data_type_identifier);
if (agcc->data_header.n_parents == 0
|| (strcmp(agcc->data_header.parents[0].data_type_identifier, "affymetrix-calvin-scan-acquisition") != 0
&& strcmp(agcc->data_header.parents[0].data_type_identifier,
"affymetrix-calvin-multi-scan-acquisition")
!= 0))
error("AGCC file %s is missing scan acquisition information as data type identifier is %s\n", agcc->fn,
agcc->data_header.parents[0].data_type_identifier);
const Parameter *parameter;
for (j = 0; j < agcc->data_header.parents[0].n_parameters; j++) {
parameter = &agcc->data_header.parents[0].parameters[j];
if (wcscmp(parameter->name, L"affymetrix-array-type") == 0 && parameter->type == WSTRING)
buffer_string16((uint16_t *)parameter->value, parameter->n_value, &m_array_type, &array_type);
else if (wcscmp(parameter->name, L"affymetrix-scanner-type") == 0 && parameter->type == WSTRING)
buffer_string16((uint16_t *)parameter->value, parameter->n_value, &m_scanner_type, &scanner_type);
else if (wcscmp(parameter->name, L"affymetrix-scanner-id") == 0 && parameter->type == WSTRING)
buffer_string16((uint16_t *)parameter->value, parameter->n_value, &m_scanner_id, &scanner_id);
else if (wcscmp(parameter->name, L"affymetrix-scan-date") == 0 && parameter->type == WSTRING)
buffer_string16((uint16_t *)parameter->value, parameter->n_value, &m_scan_date, &scan_date);
else if (wcscmp(parameter->name, L"affymetrix-fusion-experiment-name") == 0
&& parameter->type == WSTRING)
buffer_string16((uint16_t *)parameter->value, parameter->n_value, &m_fusion_experiment_name,
&fusion_experiment_name);
if (wcscmp(parameter->name, L"affymetrix-pixel-rows") == 0 && parameter->type == INT)
pixel_rows = (int32_t)ntohl(*(uint32_t *)parameter->value);
if (wcscmp(parameter->name, L"affymetrix-pixel-cols") == 0 && parameter->type == INT)
pixel_cols = (int32_t)ntohl(*(uint32_t *)parameter->value);
}
fputs(strrchr(agcc->fn, '/') ? strrchr(agcc->fn, '/') + 1 : agcc->fn, stream);
fputc('\t', stream);
if (array_type) {
fprintf(stream, "%ls", array_type);
array_type[0] = L'\0';
}
fputc('\t', stream);
if (scanner_type) {
fprintf(stream, "%ls", scanner_type);
scanner_type[0] = L'\0';
}
fputc('\t', stream);
if (scanner_id) {
fprintf(stream, "%ls", scanner_id);
scanner_id[0] = L'\0';
}
fputc('\t', stream);
if (scan_date) {
fprintf(stream, "%ls", scan_date);
scan_date[0] = L'\0';
}
fputc('\t', stream);
if (fusion_experiment_name) {
fprintf(stream, "%ls", fusion_experiment_name);
fusion_experiment_name[0] = L'\0';