annotate pyPRADA_1.2/tools/bwa-0.5.7-mh/bwaseqio.c @ 3:f17965495ec9 draft default tip

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