0
|
1 #include <zlib.h>
|
|
2 #include "bwtaln.h"
|
|
3 #include "utils.h"
|
|
4
|
|
5 #include "kseq.h"
|
|
6 KSEQ_INIT(gzFile, gzread)
|
|
7
|
|
8 extern unsigned char nst_nt4_table[256];
|
|
9
|
|
10 struct __bwa_seqio_t {
|
|
11 kseq_t *ks;
|
|
12 };
|
|
13
|
|
14 bwa_seqio_t *bwa_seq_open(const char *fn)
|
|
15 {
|
|
16 gzFile fp;
|
|
17 bwa_seqio_t *bs;
|
|
18 bs = (bwa_seqio_t*)calloc(1, sizeof(bwa_seqio_t));
|
|
19 fp = xzopen(fn, "r");
|
|
20 bs->ks = kseq_init(fp);
|
|
21 return bs;
|
|
22 }
|
|
23
|
|
24 void bwa_seq_close(bwa_seqio_t *bs)
|
|
25 {
|
|
26 if (bs == 0) return;
|
|
27 gzclose(bs->ks->f->f);
|
|
28 kseq_destroy(bs->ks);
|
|
29 free(bs);
|
|
30 }
|
|
31
|
|
32 void seq_reverse(int len, ubyte_t *seq, int is_comp)
|
|
33 {
|
|
34 int i;
|
|
35 if (is_comp) {
|
|
36 for (i = 0; i < len>>1; ++i) {
|
|
37 char tmp = seq[len-1-i];
|
|
38 if (tmp < 4) tmp = 3 - tmp;
|
|
39 seq[len-1-i] = (seq[i] >= 4)? seq[i] : 3 - seq[i];
|
|
40 seq[i] = tmp;
|
|
41 }
|
|
42 if (len&1) seq[i] = (seq[i] >= 4)? seq[i] : 3 - seq[i];
|
|
43 } else {
|
|
44 for (i = 0; i < len>>1; ++i) {
|
|
45 char tmp = seq[len-1-i];
|
|
46 seq[len-1-i] = seq[i]; seq[i] = tmp;
|
|
47 }
|
|
48 }
|
|
49 }
|
|
50
|
|
51 int bwa_trim_read(int trim_qual, bwa_seq_t *p)
|
|
52 {
|
|
53 int s = 0, l, max = 0, max_l = p->len - 1;
|
|
54 if (trim_qual < 1 || p->qual == 0) return 0;
|
|
55 for (l = p->len - 1; l >= BWA_MIN_RDLEN - 1; --l) {
|
|
56 s += trim_qual - (p->qual[l] - 33);
|
|
57 if (s < 0) break;
|
|
58 if (s > max) {
|
|
59 max = s; max_l = l;
|
|
60 }
|
|
61 }
|
|
62 p->clip_len = p->len = max_l + 1;
|
|
63 return p->full_len - p->len;
|
|
64 }
|
|
65
|
|
66 bwa_seq_t *bwa_read_seq(bwa_seqio_t *bs, int n_needed, int *n, int is_comp, int trim_qual)
|
|
67 {
|
|
68 bwa_seq_t *seqs, *p;
|
|
69 kseq_t *seq = bs->ks;
|
|
70 int n_seqs, l, i;
|
|
71 long n_trimmed = 0, n_tot = 0;
|
|
72
|
|
73 n_seqs = 0;
|
|
74 seqs = (bwa_seq_t*)calloc(n_needed, sizeof(bwa_seq_t));
|
|
75 while ((l = kseq_read(seq)) >= 0) {
|
|
76 p = &seqs[n_seqs++];
|
|
77 p->tid = -1; // no assigned to a thread
|
|
78 p->qual = 0;
|
|
79 p->full_len = p->clip_len = p->len = l;
|
|
80 n_tot += p->full_len;
|
|
81 p->seq = (ubyte_t*)calloc(p->len, 1);
|
|
82 for (i = 0; i != p->full_len; ++i)
|
|
83 p->seq[i] = nst_nt4_table[(int)seq->seq.s[i]];
|
|
84 if (seq->qual.l) { // copy quality
|
|
85 p->qual = (ubyte_t*)strdup((char*)seq->qual.s);
|
|
86 if (trim_qual >= 1) n_trimmed += bwa_trim_read(trim_qual, p);
|
|
87 }
|
|
88 p->rseq = (ubyte_t*)calloc(p->full_len, 1);
|
|
89 memcpy(p->rseq, p->seq, p->len);
|
|
90 seq_reverse(p->len, p->seq, 0); // *IMPORTANT*: will be reversed back in bwa_refine_gapped()
|
|
91 seq_reverse(p->len, p->rseq, is_comp);
|
|
92 p->name = strdup((const char*)seq->name.s);
|
|
93 { // trim /[12]$
|
|
94 int t = strlen(p->name);
|
|
95 if (t > 2 && p->name[t-2] == '/' && (p->name[t-1] == '1' || p->name[t-1] == '2')) p->name[t-2] = '\0';
|
|
96 }
|
|
97 if (n_seqs == n_needed) break;
|
|
98 }
|
|
99 *n = n_seqs;
|
|
100 if (n_seqs && trim_qual >= 1)
|
|
101 fprintf(stderr, "[bwa_read_seq] %.1f%% bases are trimmed.\n", 100.0f * n_trimmed/n_tot);
|
|
102 if (n_seqs == 0) {
|
|
103 free(seqs);
|
|
104 return 0;
|
|
105 }
|
|
106 return seqs;
|
|
107 }
|
|
108
|
|
109 void bwa_free_read_seq(int n_seqs, bwa_seq_t *seqs)
|
|
110 {
|
|
111 int i, j;
|
|
112 for (i = 0; i != n_seqs; ++i) {
|
|
113 bwa_seq_t *p = seqs + i;
|
|
114 for (j = 0; j < p->n_multi; ++j)
|
|
115 if (p->multi[j].cigar) free(p->multi[j].cigar);
|
|
116 free(p->name);
|
|
117 free(p->seq); free(p->rseq); free(p->qual); free(p->aln); free(p->md); free(p->multi);
|
|
118 free(p->cigar);
|
|
119 }
|
|
120 free(seqs);
|
|
121 }
|