| 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 |
#include <config.h> |
| 27 |
|
| 28 |
#include <unistd.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include "htslib/hts.h" |
| 32 |
#include "htslib/sam.h" |
| 33 |
#include "htslib/faidx.h" |
| 34 |
#include "samtools.h" |
| 35 |
#include "sam_opts.h" |
| 36 |
|
| 37 |
#define ERR_DEP 0.83 |
| 38 |
|
| 39 |
typedef struct { |
| 40 |
int e[2][3], p[2][2]; |
| 41 |
} score_param_t; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static score_param_t g_param = { {{0,0,0},{-4,1,6}}, {{0,-14000}, {0,0}} }; |
| 47 |
|
| 48 |
typedef struct { |
| 49 |
int min_baseQ, tid, max_bases; |
| 50 |
uint16_t *bases; |
| 51 |
samFile *fp; |
| 52 |
sam_hdr_t *h; |
| 53 |
char *ref; |
| 54 |
hts_pos_t len; |
| 55 |
faidx_t *fai; |
| 56 |
errmod_t *em; |
| 57 |
} ct_t; |
| 58 |
|
| 59 |
static uint16_t gencns(ct_t *g, int n, const bam_pileup1_t *plp) |
| 60 |
{ |
| 61 |
int i, j, ret, tmp, k, sum[4], qual; |
| 62 |
float q[16]; |
| 63 |
if (n > g->max_bases) { |
| 64 |
g->max_bases = n; |
| 65 |
kroundup32(g->max_bases); |
| 66 |
g->bases = realloc(g->bases, (size_t) g->max_bases * 2); |
| 67 |
} |
| 68 |
for (i = k = 0; i < n; ++i) { |
| 69 |
const bam_pileup1_t *p = plp + i; |
| 70 |
uint8_t *seq; |
| 71 |
int q, baseQ, b; |
| 72 |
if (p->is_refskip || p->is_del) continue; |
| 73 |
baseQ = bam_get_qual(p->b)[p->qpos]; |
| 74 |
if (baseQ < g->min_baseQ) continue; |
| 75 |
seq = bam_get_seq(p->b); |
| 76 |
b = seq_nt16_int[bam_seqi(seq, p->qpos)]; |
| 77 |
if (b > 3) continue; |
| 78 |
q = baseQ < p->b->core.qual? baseQ : p->b->core.qual; |
| 79 |
if (q < 4) q = 4; |
| 80 |
if (q > 63) q = 63; |
| 81 |
g->bases[k++] = q<<5 | bam_is_rev(p->b)<<4 | b; |
| 82 |
} |
| 83 |
if (k == 0) return 0; |
| 84 |
errmod_cal(g->em, k, 4, g->bases, q); |
| 85 |
for (i = 0; i < 4; ++i) sum[i] = (int)(q[i<<2|i] + .499) << 2 | i; |
| 86 |
for (i = 1; i < 4; ++i) |
| 87 |
for (j = i; j > 0 && sum[j] < sum[j-1]; --j) |
| 88 |
tmp = sum[j], sum[j] = sum[j-1], sum[j-1] = tmp; |
| 89 |
qual = (sum[1]>>2) - (sum[0]>>2); |
| 90 |
k = k < 256? k : 255; |
| 91 |
ret = (qual < 63? qual : 63) << 2 | (sum[0]&3); |
| 92 |
return ret<<8|k; |
| 93 |
} |
| 94 |
|
| 95 |
static void process_cns(sam_hdr_t *h, int tid, hts_pos_t l, uint16_t *cns) |
| 96 |
{ |
| 97 |
int64_t i, s; |
| 98 |
int f[2][2], *prev, *curr, *swap_tmp; |
| 99 |
uint8_t *b; |
| 100 |
b = calloc(l, 1); |
| 101 |
f[0][0] = f[0][1] = 0; |
| 102 |
prev = f[0]; curr = f[1]; |
| 103 |
|
| 104 |
for (i = 0; i < l; ++i) { |
| 105 |
int c = (cns[i] == 0)? 0 : (cns[i]>>8 == 0)? 1 : 2; |
| 106 |
int tmp0, tmp1; |
| 107 |
|
| 108 |
tmp0 = prev[0] + g_param.e[0][c] + g_param.p[0][0]; |
| 109 |
tmp1 = prev[1] + g_param.e[0][c] + g_param.p[1][0]; |
| 110 |
if (tmp0 > tmp1) curr[0] = tmp0, b[i] = 0; |
| 111 |
else curr[0] = tmp1, b[i] = 1; |
| 112 |
|
| 113 |
tmp0 = prev[0] + g_param.e[1][c] + g_param.p[0][1]; |
| 114 |
tmp1 = prev[1] + g_param.e[1][c] + g_param.p[1][1]; |
| 115 |
if (tmp0 > tmp1) curr[1] = tmp0, b[i] |= 0<<1; |
| 116 |
else curr[1] = tmp1, b[i] |= 1<<1; |
| 117 |
|
| 118 |
swap_tmp = prev; prev = curr; curr = swap_tmp; |
| 119 |
} |
| 120 |
|
| 121 |
s = prev[0] > prev[1]? 0 : 1; |
| 122 |
for (i = l - 1; i > 0; --i) { |
| 123 |
b[i] |= s<<2; |
| 124 |
s = b[i]>>s&1; |
| 125 |
} |
| 126 |
|
| 127 |
for (i = 0, s = -1; i < INT64_MAX && i <= l; ++i) { |
| 128 |
if (i == l || ((b[i]>>2&3) == 0 && s >= 0)) { |
| 129 |
if (s >= 0) { |
| 130 |
int64_t j; |
| 131 |
printf("%s:%"PRId64"-%"PRId64"\t0\t%s\t%"PRId64"\t60\t%"PRId64"M\t*\t0\t0\t", sam_hdr_tid2name(h, tid), s+1, i, sam_hdr_tid2name(h, tid), s+1, i-s); |
| 132 |
for (j = s; j < i; ++j) { |
| 133 |
int c = cns[j]>>8; |
| 134 |
if (c == 0) putchar('N'); |
| 135 |
else putchar("ACGT"[c&3]); |
| 136 |
} |
| 137 |
putchar('\t'); |
| 138 |
for (j = s; j < i; ++j) |
| 139 |
putchar(33 + (cns[j]>>8>>2)); |
| 140 |
putchar('\n'); |
| 141 |
} |
| 142 |
|
| 143 |
s = -1; |
| 144 |
} else if ((b[i]>>2&3) && s < 0) s = i; |
| 145 |
} |
| 146 |
free(b); |
| 147 |
} |
| 148 |
|
| 149 |
static int read_aln(void *data, bam1_t *b) |
| 150 |
{ |
| 151 |
ct_t *g = (ct_t*)data; |
| 152 |
int ret; |
| 153 |
while (1) |
| 154 |
{ |
| 155 |
ret = sam_read1(g->fp, g->h, b); |
| 156 |
if ( ret<0 ) break; |
| 157 |
if ( b->core.flag & (BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP) ) continue; |
| 158 |
if ( g->fai && b->core.tid >= 0 ) { |
| 159 |
if (b->core.tid != g->tid) { |
| 160 |
free(g->ref); |
| 161 |
g->ref = fai_fetch64(g->fai, sam_hdr_tid2name(g->h, b->core.tid), &g->len); |
| 162 |
g->tid = b->core.tid; |
| 163 |
} |
| 164 |
sam_prob_realn(b, g->ref, g->len, 1<<1|1); |
| 165 |
} |
| 166 |
break; |
| 167 |
} |
| 168 |
return ret; |
| 169 |
} |
| 170 |
|
| 171 |
int main_cut_target(int argc, char *argv[]) |
| 172 |
{ |
| 173 |
int c, tid, pos, n, lasttid = -1, usage = 0, status = EXIT_SUCCESS; |
| 174 |
hts_pos_t l, max_l; |
| 175 |
const bam_pileup1_t *p; |
| 176 |
bam_plp_t plp; |
| 177 |
uint16_t *cns; |
| 178 |
ct_t g; |
| 179 |
|
| 180 |
sam_global_args ga = SAM_GLOBAL_ARGS_INIT; |
| 181 |
static const struct option lopts[] = { |
| 182 |
SAM_OPT_GLOBAL_OPTIONS('-', 0, '-', '-', 'f', '-'), |
| 183 |
{ NULL, 0, NULL, 0 } |
| 184 |
}; |
| 185 |
|
| 186 |
memset(&g, 0, sizeof(ct_t)); |
| 187 |
g.min_baseQ = 13; g.tid = -1; |
| 188 |
while ((c = getopt_long(argc, argv, "f:Q:i:o:0:1:2:", lopts, NULL)) >= 0) { |
| 189 |
switch (c) { |
| 190 |
case 'Q': g.min_baseQ = atoi(optarg); break; |
| 191 |
case 'i': g_param.p[0][1] = -atoi(optarg); break; |
| 192 |
case '0': g_param.e[1][0] = atoi(optarg); break; |
| 193 |
case '1': g_param.e[1][1] = atoi(optarg); break; |
| 194 |
case '2': g_param.e[1][2] = atoi(optarg); break; |
| 195 |
default: if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break; |
| 196 |
|
| 197 |
case '?': usage=1; break; |
| 198 |
} |
| 199 |
} |
| 200 |
if (ga.reference) { |
| 201 |
g.fai = fai_load(ga.reference); |
| 202 |
if (g.fai == 0) fprintf(stderr, "[%s] fail to load the fasta index.\n", __func__); |
| 203 |
} |
| 204 |
if (usage || argc == optind) { |
| 205 |
fprintf(stderr, "Usage: samtools targetcut [-Q minQ] [-i inPen] [-0 em0] [-1 em1] [-2 em2] <in.bam>\n"); |
| 206 |
sam_global_opt_help(stderr, "-.--f--."); |
| 207 |
return 1; |
| 208 |
} |
| 209 |
l = max_l = 0; cns = 0; |
| 210 |
g.fp = sam_open_format(argv[optind], "r", &ga.in); |
| 211 |
if (g.fp == NULL) { |
| 212 |
print_error_errno("targetcut", "can't open \"%s\"", argv[optind]); |
| 213 |
return 1; |
| 214 |
} |
| 215 |
|
| 216 |
g.h = sam_hdr_read(g.fp); |
| 217 |
if (g.h == NULL) { |
| 218 |
print_error("targetcut", "couldn't read header for \"%s\"", argv[optind]); |
| 219 |
sam_close(g.fp); |
| 220 |
return 1; |
| 221 |
} |
| 222 |
g.em = errmod_init(1. - ERR_DEP); |
| 223 |
plp = bam_plp_init(read_aln, &g); |
| 224 |
while ((p = bam_plp_auto(plp, &tid, &pos, &n)) != 0) { |
| 225 |
if (tid < 0) break; |
| 226 |
if (tid != lasttid) { |
| 227 |
if (cns) process_cns(g.h, lasttid, l, cns); |
| 228 |
if (max_l < sam_hdr_tid2len(g.h, tid)) { |
| 229 |
max_l = sam_hdr_tid2len(g.h, tid); |
| 230 |
kroundup32(max_l); |
| 231 |
cns = realloc(cns, max_l * 2); |
| 232 |
} |
| 233 |
l = sam_hdr_tid2len(g.h, tid); |
| 234 |
memset(cns, 0, max_l * 2); |
| 235 |
lasttid = tid; |
| 236 |
} |
| 237 |
cns[pos] = gencns(&g, n, p); |
| 238 |
} |
| 239 |
process_cns(g.h, lasttid, l, cns); |
| 240 |
|
| 241 |
if (n < 0) { |
| 242 |
print_error("targetcut", "error reading from \"%s\"", argv[optind]); |
| 243 |
status = EXIT_FAILURE; |
| 244 |
} |
| 245 |
|
| 246 |
free(cns); |
| 247 |
sam_hdr_destroy(g.h); |
| 248 |
bam_plp_destroy(plp); |
| 249 |
sam_close(g.fp); |
| 250 |
if (g.fai) { |
| 251 |
fai_destroy(g.fai); free(g.ref); |
| 252 |
} |
| 253 |
errmod_destroy(g.em); |
| 254 |
free(g.bases); |
| 255 |
sam_global_args_free(&ga); |
| 256 |
return status; |
| 257 |
} |