1 #include <stdio.h> // fprintf
4 #include <sys/mman.h> // madvise
10 KSEQ_INIT(gzFile
, zng_gzread
)
11 #define _GZ_OPEN_ zng_gzopen
12 #define _GZ_BUFFER_ zng_gzbuffer
13 #define _GZ_CLOSE_ zng_gzclose
17 #define _GZ_BUFFER_ gzbuffer
18 #else /* USE_LIBISAL */
21 #define _GZ_OPEN_ gzopen
22 #define _GZ_CLOSE_ gzclose
23 KSEQ_INIT(gzFile
, gzread
)
28 void fqReader_init(void) {
30 const char *pattern
= "R[0-9]{3}(:)?C[0-9]{3}";
31 if ((rc
= regcomp(&Parameters
.regex
, pattern
, REG_EXTENDED
)) != 0) {
32 regerror(rc
, &Parameters
.regex
, Parameters
.buffer
, PARAMETERS_BUFFER_SIZE
);
33 fprintf(stderr
, "[x]=%d=regcomp(\"%s\") failed with '%s'.\n", rc
, pattern
, Parameters
.buffer
);
36 Parameters
.ksfp
= _GZ_OPEN_(Parameters
.inFastqFilename
, "r");
37 if (unlikely(Parameters
.ksfp
== NULL
)) {
38 fprintf(stderr
, "[x]gzopen error on opening [%s]: %s.\n", Parameters
.inFastqFilename
, strerror(errno
));
41 #if defined(_GZ_BUFFER_)
42 rc
= _GZ_BUFFER_(Parameters
.ksfp
, GZBUFSIZE
);
43 if (unlikely(rc
!= 0)) {
44 fprintf(stderr
, "[x]gzbuffer error: %s.\n", strerror(errno
));
48 Parameters
.kseq
= kseq_init(Parameters
.ksfp
);
49 Parameters
.ksflag
= 1;
50 // Parameters.worksQuene = (workerArray_t *)calloc(JOBQUEUESIZE, sizeof(workerArray_t));
51 size_t HPAGE_shift
= 21; // 1 << 21 = 2M
52 size_t HPAGE_alignment
= 1 << HPAGE_shift
;
53 size_t worksQuene_size
= JOBQUEUESIZE
* sizeof(workerArray_t
);
54 size_t worksQuene_aligned_cnt
= worksQuene_size
>> HPAGE_shift
;
55 if (worksQuene_size
% HPAGE_alignment
!= 0) {
56 worksQuene_aligned_cnt
++;
58 size_t worksQuene_aligned_size
= HPAGE_alignment
* worksQuene_aligned_cnt
;
59 fprintf(stderr
, "[!]aligned_alloc(%zu, %zu) for [%zu].\n", HPAGE_alignment
, worksQuene_aligned_size
, worksQuene_size
);
60 Parameters
.worksQuene
= (workerArray_t
*)aligned_alloc(HPAGE_alignment
, worksQuene_aligned_size
);
61 if (Parameters
.worksQuene
!= NULL
) {
63 madvise(Parameters
.worksQuene
, worksQuene_aligned_size
, MADV_HUGEPAGE
);
65 memset(Parameters
.worksQuene
, 0, worksQuene_aligned_size
);
67 perror("[x]aligned_alloc failed.");
72 void fqReader_destroy(void) {
73 regfree(&Parameters
.regex
);
74 kseq_destroy(Parameters
.kseq
);
75 _GZ_CLOSE_(Parameters
.ksfp
);
76 free(Parameters
.worksQuene
);
79 void fill_worker(int_least16_t worker_id
) {
80 kseq_t
*seq
= Parameters
.kseq
;
81 workerArray_t
*worker
= &Parameters
.worksQuene
[worker_id
];
82 // if (atomic_load(&worker->flag) == 1) return;
85 for (index
= 0; index
< JOBITEMSIZE
; index
++) {
86 fstBCdata_t
*fstBCdata_p
= &worker
->jobDatArray
[index
];
87 if ((kseq_ret
= kseq_read(seq
)) >= MINBARCODELEN
) {
88 // strncpy((char *)fstBCdata_p->name, seq->name.s, sizeof(fstBCdata_p->name));
89 STRcpyARRAY(fstBCdata_p
->name
, seq
->name
.s
);
90 /* seq->comment.s is discarded */
91 STRcpyARRAY(fstBCdata_p
->seq
, seq
->seq
.s
);
92 STRcpyARRAY(fstBCdata_p
->qual
, seq
->qual
.s
);
94 fprintf(stderr
, "- %llu -\n", index
);
95 ARRAYcpySTR(Parameters
.buffer
, fstBCdata_p
->name
);
96 // snprintf(Parameters.buffer, 1 + sizeof(fstBCdata_p->name), "%s", fstBCdata_p->name);
97 fprintf(stderr
, "->Name:[%s]\n", Parameters
.buffer
);
98 ARRAYcpySTR(Parameters
.buffer
, fstBCdata_p
->seq
);
99 fprintf(stderr
, "->Sequ:[%s]\n", Parameters
.buffer
);
100 ARRAYcpySTR(Parameters
.buffer
, fstBCdata_p
->qual
);
101 fprintf(stderr
, "->Qual:[%s]\n", Parameters
.buffer
);
104 fstBCdata_p
->name
[0] = '\0';
105 fstBCdata_p
->seq
[0] = '\0';
106 fstBCdata_p
->qual
[0] = '\0';
107 if (kseq_ret
< 0) { // -1 for FEOF
108 Parameters
.ksflag
= kseq_ret
;
113 // Update the workerArray's flag to indicate it is filled, thus ready for worker.
114 atomic_store(&worker
->flag
, 1);