1 #include <zlib.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 #define __STDC_LIMIT_MACROS
6 #include "bseq.h"
7 #include "kvec.h"
8 #include "kseq.h"
9 KSEQ_INIT2(, gzFile, gzread)
10
11 unsigned char seq_comp_table[256] = {
12 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
13 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
14 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
15 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
16 64, 'T', 'V', 'G', 'H', 'E', 'F', 'C', 'D', 'I', 'J', 'M', 'L', 'K', 'N', 'O',
17 'P', 'Q', 'Y', 'S', 'A', 'A', 'B', 'W', 'X', 'R', 'Z', 91, 92, 93, 94, 95,
18 96, 't', 'v', 'g', 'h', 'e', 'f', 'c', 'd', 'i', 'j', 'm', 'l', 'k', 'n', 'o',
19 'p', 'q', 'y', 's', 'a', 'a', 'b', 'w', 'x', 'r', 'z', 123, 124, 125, 126, 127,
20 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
21 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
22 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
23 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
24 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
25 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
26 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
27 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
28 };
29
30 #define CHECK_PAIR_THRES 1000000
31
32 struct mm_bseq_file_s {
33 gzFile fp;
34 kseq_t *ks;
35 mm_bseq1_t s;
36 };
37
mm_bseq_open(const char * fn)38 mm_bseq_file_t *mm_bseq_open(const char *fn)
39 {
40 mm_bseq_file_t *fp;
41 gzFile f;
42 f = fn && strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(0, "r");
43 if (f == 0) return 0;
44 fp = (mm_bseq_file_t*)calloc(1, sizeof(mm_bseq_file_t));
45 fp->fp = f;
46 fp->ks = kseq_init(fp->fp);
47 return fp;
48 }
49
mm_bseq_close(mm_bseq_file_t * fp)50 void mm_bseq_close(mm_bseq_file_t *fp)
51 {
52 kseq_destroy(fp->ks);
53 gzclose(fp->fp);
54 free(fp);
55 }
56
kstrdup(const kstring_t * s)57 static inline char *kstrdup(const kstring_t *s)
58 {
59 char *t;
60 t = (char*)malloc(s->l + 1);
61 memcpy(t, s->s, s->l + 1);
62 return t;
63 }
64
kseq2bseq(kseq_t * ks,mm_bseq1_t * s,int with_qual,int with_comment)65 static inline void kseq2bseq(kseq_t *ks, mm_bseq1_t *s, int with_qual, int with_comment)
66 {
67 int i;
68 if (ks->name.l == 0)
69 fprintf(stderr, "[WARNING]\033[1;31m empty sequence name in the input.\033[0m\n");
70 s->name = kstrdup(&ks->name);
71 s->seq = kstrdup(&ks->seq);
72 for (i = 0; i < (int)ks->seq.l; ++i) // convert U to T
73 if (s->seq[i] == 'u' || s->seq[i] == 'U')
74 --s->seq[i];
75 s->qual = with_qual && ks->qual.l? kstrdup(&ks->qual) : 0;
76 s->comment = with_comment && ks->comment.l? kstrdup(&ks->comment) : 0;
77 s->l_seq = ks->seq.l;
78 }
79
mm_bseq_read3(mm_bseq_file_t * fp,int64_t chunk_size,int with_qual,int with_comment,int frag_mode,int * n_)80 mm_bseq1_t *mm_bseq_read3(mm_bseq_file_t *fp, int64_t chunk_size, int with_qual, int with_comment, int frag_mode, int *n_)
81 {
82 int64_t size = 0;
83 int ret;
84 kvec_t(mm_bseq1_t) a = {0,0,0};
85 kseq_t *ks = fp->ks;
86 *n_ = 0;
87 if (fp->s.seq) {
88 kv_resize(mm_bseq1_t, 0, a, 256);
89 kv_push(mm_bseq1_t, 0, a, fp->s);
90 size = fp->s.l_seq;
91 memset(&fp->s, 0, sizeof(mm_bseq1_t));
92 }
93 while ((ret = kseq_read(ks)) >= 0) {
94 mm_bseq1_t *s;
95 assert(ks->seq.l <= INT32_MAX);
96 if (a.m == 0) kv_resize(mm_bseq1_t, 0, a, 256);
97 kv_pushp(mm_bseq1_t, 0, a, &s);
98 kseq2bseq(ks, s, with_qual, with_comment);
99 size += s->l_seq;
100 if (size >= chunk_size) {
101 if (frag_mode && a.a[a.n-1].l_seq < CHECK_PAIR_THRES) {
102 while ((ret = kseq_read(ks)) >= 0) {
103 kseq2bseq(ks, &fp->s, with_qual, with_comment);
104 if (mm_qname_same(fp->s.name, a.a[a.n-1].name)) {
105 kv_push(mm_bseq1_t, 0, a, fp->s);
106 memset(&fp->s, 0, sizeof(mm_bseq1_t));
107 } else break;
108 }
109 }
110 break;
111 }
112 }
113 if (ret < -1) {
114 if (a.n) fprintf(stderr, "[WARNING]\033[1;31m failed to parse the FASTA/FASTQ record next to '%s'. Continue anyway.\033[0m\n", a.a[a.n-1].name);
115 else fprintf(stderr, "[WARNING]\033[1;31m failed to parse the first FASTA/FASTQ record. Continue anyway.\033[0m\n");
116 }
117 *n_ = a.n;
118 return a.a;
119 }
120
mm_bseq_read2(mm_bseq_file_t * fp,int64_t chunk_size,int with_qual,int frag_mode,int * n_)121 mm_bseq1_t *mm_bseq_read2(mm_bseq_file_t *fp, int64_t chunk_size, int with_qual, int frag_mode, int *n_)
122 {
123 return mm_bseq_read3(fp, chunk_size, with_qual, 0, frag_mode, n_);
124 }
125
mm_bseq_read(mm_bseq_file_t * fp,int64_t chunk_size,int with_qual,int * n_)126 mm_bseq1_t *mm_bseq_read(mm_bseq_file_t *fp, int64_t chunk_size, int with_qual, int *n_)
127 {
128 return mm_bseq_read2(fp, chunk_size, with_qual, 0, n_);
129 }
130
mm_bseq_read_frag2(int n_fp,mm_bseq_file_t ** fp,int64_t chunk_size,int with_qual,int with_comment,int * n_)131 mm_bseq1_t *mm_bseq_read_frag2(int n_fp, mm_bseq_file_t **fp, int64_t chunk_size, int with_qual, int with_comment, int *n_)
132 {
133 int i;
134 int64_t size = 0;
135 kvec_t(mm_bseq1_t) a = {0,0,0};
136 *n_ = 0;
137 if (n_fp < 1) return 0;
138 while (1) {
139 int n_read = 0;
140 for (i = 0; i < n_fp; ++i)
141 if (kseq_read(fp[i]->ks) >= 0)
142 ++n_read;
143 if (n_read < n_fp) {
144 if (n_read > 0)
145 fprintf(stderr, "[W::%s]\033[1;31m query files have different number of records; extra records skipped.\033[0m\n", __func__);
146 break; // some file reaches the end
147 }
148 if (a.m == 0) kv_resize(mm_bseq1_t, 0, a, 256);
149 for (i = 0; i < n_fp; ++i) {
150 mm_bseq1_t *s;
151 kv_pushp(mm_bseq1_t, 0, a, &s);
152 kseq2bseq(fp[i]->ks, s, with_qual, with_comment);
153 size += s->l_seq;
154 }
155 if (size >= chunk_size) break;
156 }
157 *n_ = a.n;
158 return a.a;
159 }
160
mm_bseq_read_frag(int n_fp,mm_bseq_file_t ** fp,int64_t chunk_size,int with_qual,int * n_)161 mm_bseq1_t *mm_bseq_read_frag(int n_fp, mm_bseq_file_t **fp, int64_t chunk_size, int with_qual, int *n_)
162 {
163 return mm_bseq_read_frag2(n_fp, fp, chunk_size, with_qual, 0, n_);
164 }
165
mm_bseq_eof(mm_bseq_file_t * fp)166 int mm_bseq_eof(mm_bseq_file_t *fp)
167 {
168 return (ks_eof(fp->ks->f) && fp->s.seq == 0);
169 }
170