| 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 |
|
| 27 |
#include <config.h> |
| 28 |
|
| 29 |
#include <string.h> |
| 30 |
#include <assert.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include <inttypes.h> |
| 33 |
#include <htslib/kstring.h> |
| 34 |
#include <htslib/sam.h> |
| 35 |
#include <htslib/faidx.h> |
| 36 |
#include "sam_opts.h" |
| 37 |
#include "samtools.h" |
| 38 |
|
| 39 |
#define bam_reg2bin(b,e) hts_reg2bin((b),(e), 14, 5) |
| 40 |
|
| 41 |
static int replace_cigar(bam1_t *b, uint32_t n, uint32_t *cigar) |
| 42 |
{ |
| 43 |
int diff = 0; |
| 44 |
if (n != b->core.n_cigar) { |
| 45 |
int o = b->core.l_qname + b->core.n_cigar * 4; |
| 46 |
if (n > b->core.n_cigar) { |
| 47 |
diff = (n - b->core.n_cigar) * 4; |
| 48 |
if ((INT_MAX - b->l_data)/4 < (n - b->core.n_cigar)) { |
| 49 |
fprintf(stderr, "[depad] ERROR: BAM record too big\n"); |
| 50 |
return -1; |
| 51 |
} |
| 52 |
if (b->l_data + diff > b->m_data) { |
| 53 |
b->m_data = b->l_data + diff; |
| 54 |
kroundup32(b->m_data); |
| 55 |
uint8_t *tmp = (uint8_t*)realloc(b->data, b->m_data); |
| 56 |
if (!tmp) { |
| 57 |
fprintf(stderr, "[depad] ERROR: Memory allocation failure.\n"); |
| 58 |
return -1; |
| 59 |
} |
| 60 |
b->data = tmp; |
| 61 |
} |
| 62 |
} else { |
| 63 |
diff = -(int)((b->core.n_cigar - n) * 4); |
| 64 |
} |
| 65 |
memmove(b->data + b->core.l_qname + n * 4, b->data + o, b->l_data - o); |
| 66 |
b->core.n_cigar = n; |
| 67 |
} |
| 68 |
|
| 69 |
memcpy(b->data + b->core.l_qname, cigar, n * 4); |
| 70 |
b->l_data += diff; |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
#define write_cigar(_c, _n, _m, _v) do { \ |
| 76 |
if (_n == _m) { \ |
| 77 |
_m = _m? _m<<1 : 4; \ |
| 78 |
_c = (uint32_t*)realloc(_c, _m * 4); \ |
| 79 |
if (!(_c)) { \ |
| 80 |
fprintf(stderr, "[depad] ERROR: Memory allocation failure.\n"); \ |
| 81 |
return -1; \ |
| 82 |
} \ |
| 83 |
} \ |
| 84 |
_c[_n++] = (_v); \ |
| 85 |
} while (0) |
| 86 |
|
| 87 |
static int unpad_seq(bam1_t *b, kstring_t *s) |
| 88 |
{ |
| 89 |
|
| 90 |
int k, j, i; |
| 91 |
int length; |
| 92 |
int cigar_n_warning = 0; |
| 93 |
uint32_t *cigar = bam_get_cigar(b); |
| 94 |
uint8_t *seq = bam_get_seq(b); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
length = bam_cigar2rlen(b->core.n_cigar, cigar); |
| 100 |
ks_resize(s, length); |
| 101 |
for (k = 0, s->l = 0, j = 0; k < b->core.n_cigar; ++k) { |
| 102 |
int op, ol; |
| 103 |
op = bam_cigar_op(cigar[k]); |
| 104 |
ol = bam_cigar_oplen(cigar[k]); |
| 105 |
if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) { |
| 106 |
for (i = 0; i < ol; ++i, ++j) s->s[s->l++] = bam_seqi(seq, j); |
| 107 |
} else if (op == BAM_CSOFT_CLIP) { |
| 108 |
j += ol; |
| 109 |
} else if (op == BAM_CHARD_CLIP) { |
| 110 |
|
| 111 |
} else if (op == BAM_CDEL) { |
| 112 |
for (i = 0; i < ol; ++i) s->s[s->l++] = 0; |
| 113 |
} else if (op == BAM_CREF_SKIP) { |
| 114 |
|
| 115 |
for (i = 0; i < ol; ++i) s->s[s->l++] = 0; |
| 116 |
if (0 == cigar_n_warning) { |
| 117 |
cigar_n_warning = -1; |
| 118 |
fprintf(stderr, "[depad] WARNING: CIGAR op N treated as op D in read %s\n", bam_get_qname(b)); |
| 119 |
} |
| 120 |
} else { |
| 121 |
fprintf(stderr, "[depad] ERROR: Didn't expect CIGAR op %c in read %s\n", BAM_CIGAR_STR[op], bam_get_qname(b)); |
| 122 |
return -1; |
| 123 |
} |
| 124 |
} |
| 125 |
return length != s->l; |
| 126 |
} |
| 127 |
|
| 128 |
int load_unpadded_ref(faidx_t *fai, const char *ref_name, hts_pos_t ref_len, kstring_t *seq) |
| 129 |
{ |
| 130 |
char base; |
| 131 |
char *fai_ref = 0; |
| 132 |
hts_pos_t fai_ref_len = 0, k; |
| 133 |
|
| 134 |
fai_ref = fai_fetch64(fai, ref_name, &fai_ref_len); |
| 135 |
if (fai_ref_len != ref_len) { |
| 136 |
fprintf(stderr, "[depad] ERROR: FASTA sequence %s length %"PRIhts_pos", expected %"PRIhts_pos"\n", ref_name, fai_ref_len, ref_len); |
| 137 |
free(fai_ref); |
| 138 |
return -1; |
| 139 |
} |
| 140 |
ks_resize(seq, ref_len); |
| 141 |
seq->l = 0; |
| 142 |
for (k = 0; k < ref_len; ++k) { |
| 143 |
base = fai_ref[k]; |
| 144 |
if (base == '-' || base == '*') { |
| 145 |
|
| 146 |
seq->s[seq->l++] = 0; |
| 147 |
} else { |
| 148 |
int i = seq_nt16_table[(int)base]; |
| 149 |
if (i == 0 || i==16) { |
| 150 |
fprintf(stderr, "[depad] ERROR: Invalid character %c (ASCII %i) in FASTA sequence %s\n", base, (int)base, ref_name); |
| 151 |
free(fai_ref); |
| 152 |
return -1; |
| 153 |
} |
| 154 |
seq->s[seq->l++] = i; |
| 155 |
} |
| 156 |
} |
| 157 |
assert(ref_len == seq->l); |
| 158 |
free(fai_ref); |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
hts_pos_t get_unpadded_len(faidx_t *fai, const char *ref_name, hts_pos_t padded_len) |
| 163 |
{ |
| 164 |
char base; |
| 165 |
char *fai_ref = 0; |
| 166 |
hts_pos_t fai_ref_len = 0, k; |
| 167 |
hts_pos_t bases=0, gaps=0; |
| 168 |
|
| 169 |
fai_ref = fai_fetch64(fai, ref_name, &fai_ref_len); |
| 170 |
if (fai_ref_len != padded_len) { |
| 171 |
fprintf(stderr, "[depad] ERROR: FASTA sequence '%s' length %"PRIhts_pos", expected %"PRIhts_pos"\n", ref_name, fai_ref_len, padded_len); |
| 172 |
free(fai_ref); |
| 173 |
return -1; |
| 174 |
} |
| 175 |
for (k = 0; k < padded_len; ++k) { |
| 176 |
|
| 177 |
base = fai_ref[k]; |
| 178 |
if (base == '-' || base == '*') { |
| 179 |
gaps += 1; |
| 180 |
} else { |
| 181 |
int i = seq_nt16_table[(int)base]; |
| 182 |
if (i == 0 || i==16) { |
| 183 |
fprintf(stderr, "[depad] ERROR: Invalid character %c (ASCII %i) in FASTA sequence '%s'\n", base, (int)base, ref_name); |
| 184 |
free(fai_ref); |
| 185 |
return -1; |
| 186 |
} |
| 187 |
bases += 1; |
| 188 |
} |
| 189 |
} |
| 190 |
free(fai_ref); |
| 191 |
assert (padded_len == bases + gaps); |
| 192 |
return bases; |
| 193 |
} |
| 194 |
|
| 195 |
static inline int * update_posmap(int *posmap, kstring_t ref) |
| 196 |
{ |
| 197 |
int i, k; |
| 198 |
posmap = realloc(posmap, ref.m * sizeof(int)); |
| 199 |
for (i = k = 0; i < ref.l; ++i) { |
| 200 |
posmap[i] = k; |
| 201 |
if (ref.s[i]) ++k; |
| 202 |
} |
| 203 |
return posmap; |
| 204 |
} |
| 205 |
|
| 206 |
int bam_pad2unpad(samFile *in, samFile *out, sam_hdr_t *h, faidx_t *fai) |
| 207 |
{ |
| 208 |
bam1_t *b = 0; |
| 209 |
kstring_t r, q; |
| 210 |
int r_tid = -1; |
| 211 |
uint32_t *cigar2 = 0; |
| 212 |
int ret = 0, *posmap = 0; |
| 213 |
uint32_t n2 = 0, m2 = 0; |
| 214 |
|
| 215 |
b = bam_init1(); |
| 216 |
if (!b) { |
| 217 |
fprintf(stderr, "[depad] Couldn't allocate bam struct\n"); |
| 218 |
return -1; |
| 219 |
} |
| 220 |
r.l = r.m = q.l = q.m = 0; r.s = q.s = 0; |
| 221 |
int read_ret; |
| 222 |
while ((read_ret = sam_read1(in, h, b)) >= 0) { |
| 223 |
|
| 224 |
if (b->core.flag & BAM_FUNMAP) |
| 225 |
goto next_seq; |
| 226 |
|
| 227 |
uint32_t *cigar = bam_get_cigar(b); |
| 228 |
n2 = 0; |
| 229 |
if (b->core.pos == 0 && b->core.tid >= 0 && strcmp(bam_get_qname(b), sam_hdr_tid2name(h, b->core.tid)) == 0) { |
| 230 |
|
| 231 |
r_tid = b->core.tid; |
| 232 |
if (0!=unpad_seq(b, &r)) { |
| 233 |
fprintf(stderr, "[depad] ERROR: Problem parsing SEQ and/or CIGAR in reference %s\n", bam_get_qname(b)); |
| 234 |
return -1; |
| 235 |
}; |
| 236 |
if (sam_hdr_tid2len(h, r_tid) != r.l) { |
| 237 |
fprintf(stderr, "[depad] ERROR: (Padded) length of '%s' is %"PRId64" in BAM header, but %zu in embedded reference\n", bam_get_qname(b), (int64_t) sam_hdr_tid2len(h, r_tid), r.l); |
| 238 |
return -1; |
| 239 |
} |
| 240 |
if (fai) { |
| 241 |
|
| 242 |
if (load_unpadded_ref(fai, sam_hdr_tid2name(h, b->core.tid), sam_hdr_tid2len(h, b->core.tid), &q)) { |
| 243 |
fprintf(stderr, "[depad] ERROR: Failed to load embedded reference '%s' from FASTA\n", sam_hdr_tid2name(h, b->core.tid)); |
| 244 |
return -1; |
| 245 |
} |
| 246 |
assert(r.l == q.l); |
| 247 |
int i; |
| 248 |
for (i = 0; i < r.l; ++i) { |
| 249 |
if (r.s[i] != q.s[i]) { |
| 250 |
|
| 251 |
fprintf(stderr, "[depad] ERROR: Embedded sequence and reference FASTA don't match for %s base %i, '%c' vs '%c'\n", |
| 252 |
sam_hdr_tid2name(h, b->core.tid), i+1, |
| 253 |
r.s[i] ? seq_nt16_str[(int)r.s[i]] : 45, |
| 254 |
q.s[i] ? seq_nt16_str[(int)q.s[i]] : 45); |
| 255 |
return -1; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
write_cigar(cigar2, n2, m2, bam_cigar_gen(b->core.l_qseq, BAM_CMATCH)); |
| 260 |
if (replace_cigar(b, n2, cigar2) < 0) |
| 261 |
return -1; |
| 262 |
posmap = update_posmap(posmap, r); |
| 263 |
} else if (b->core.n_cigar > 0) { |
| 264 |
int i, k, op; |
| 265 |
if (b->core.tid < 0) { |
| 266 |
fprintf(stderr, "[depad] ERROR: Read '%s' has CIGAR but no RNAME\n", bam_get_qname(b)); |
| 267 |
return -1; |
| 268 |
} else if (b->core.tid == r_tid) { |
| 269 |
; |
| 270 |
|
| 271 |
} else if (fai) { |
| 272 |
if (load_unpadded_ref(fai, sam_hdr_tid2name(h, b->core.tid), sam_hdr_tid2len(h, b->core.tid), &r)) { |
| 273 |
fprintf(stderr, "[depad] ERROR: Failed to load '%s' from reference FASTA\n", sam_hdr_tid2name(h, b->core.tid)); |
| 274 |
return -1; |
| 275 |
} |
| 276 |
posmap = update_posmap(posmap, r); |
| 277 |
r_tid = b->core.tid; |
| 278 |
|
| 279 |
} else { |
| 280 |
fprintf(stderr, "[depad] ERROR: Missing %s embedded reference sequence (and no FASTA file)\n", sam_hdr_tid2name(h, b->core.tid)); |
| 281 |
return -1; |
| 282 |
} |
| 283 |
if (0!=unpad_seq(b, &q)) { |
| 284 |
fprintf(stderr, "[depad] ERROR: Problem parsing SEQ and/or CIGAR in read %s\n", bam_get_qname(b)); |
| 285 |
return -1; |
| 286 |
}; |
| 287 |
if (bam_cigar_op(cigar[0]) == BAM_CSOFT_CLIP) { |
| 288 |
write_cigar(cigar2, n2, m2, cigar[0]); |
| 289 |
} else if (bam_cigar_op(cigar[0]) == BAM_CHARD_CLIP) { |
| 290 |
write_cigar(cigar2, n2, m2, cigar[0]); |
| 291 |
if (b->core.n_cigar > 2 && bam_cigar_op(cigar[1]) == BAM_CSOFT_CLIP) { |
| 292 |
write_cigar(cigar2, n2, m2, cigar[1]); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
for (i = 0, k = b->core.pos; i < q.l; ++i, ++k) |
| 297 |
q.s[i] = q.s[i]? (r.s[k]? BAM_CMATCH : BAM_CINS) : (r.s[k]? BAM_CDEL : BAM_CPAD); |
| 298 |
|
| 299 |
if (q.s[0] == BAM_CINS) { |
| 300 |
for (k = 0; k+1 < b->core.pos && !r.s[b->core.pos - k - 1]; ++k); |
| 301 |
if (k) write_cigar(cigar2, n2, m2, bam_cigar_gen(k, BAM_CPAD)); |
| 302 |
k = 0; |
| 303 |
} else if (q.s[0] == BAM_CPAD) { |
| 304 |
|
| 305 |
for (k = 0; k+1 < b->core.pos && !r.s[b->core.pos - k - 1]; ++k); |
| 306 |
} else { |
| 307 |
k = 0; |
| 308 |
} |
| 309 |
|
| 310 |
for (i = 1, k++, op = q.s[0]; i < q.l; ++i) { |
| 311 |
if (op != q.s[i]) { |
| 312 |
write_cigar(cigar2, n2, m2, bam_cigar_gen(k, op)); |
| 313 |
op = q.s[i]; k = 1; |
| 314 |
} else ++k; |
| 315 |
} |
| 316 |
write_cigar(cigar2, n2, m2, bam_cigar_gen(k, op)); |
| 317 |
if (bam_cigar_op(cigar[b->core.n_cigar-1]) == BAM_CSOFT_CLIP) { |
| 318 |
write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-1]); |
| 319 |
} else if (bam_cigar_op(cigar[b->core.n_cigar-1]) == BAM_CHARD_CLIP) { |
| 320 |
if (b->core.n_cigar > 2 && bam_cigar_op(cigar[b->core.n_cigar-2]) == BAM_CSOFT_CLIP) { |
| 321 |
write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-2]); |
| 322 |
} |
| 323 |
write_cigar(cigar2, n2, m2, cigar[b->core.n_cigar-1]); |
| 324 |
} |
| 325 |
|
| 326 |
int pre_op, post_op; |
| 327 |
for (i = 2; i < n2; ++i) |
| 328 |
if (bam_cigar_op(cigar2[i-1]) == BAM_CPAD) { |
| 329 |
pre_op = bam_cigar_op(cigar2[i-2]); |
| 330 |
post_op = bam_cigar_op(cigar2[i]); |
| 331 |
|
| 332 |
if ((pre_op == BAM_CMATCH || pre_op == BAM_CDEL) && (post_op == BAM_CMATCH || post_op == BAM_CDEL)) { |
| 333 |
|
| 334 |
cigar2[i-1] = 0; |
| 335 |
|
| 336 |
if (pre_op == post_op) { |
| 337 |
|
| 338 |
cigar2[i] = bam_cigar_gen(bam_cigar_oplen(cigar2[i-2]) + bam_cigar_oplen(cigar2[i]), post_op); |
| 339 |
cigar2[i-2] = 0; |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
for (i = k = 0; i < n2; ++i) |
| 345 |
if (cigar2[i]) cigar2[k++] = cigar2[i]; |
| 346 |
n2 = k; |
| 347 |
if (replace_cigar(b, n2, cigar2) < 0) |
| 348 |
return -1; |
| 349 |
} |
| 350 |
|
| 351 |
if (b->core.pos != -1) b->core.pos = posmap[b->core.pos]; |
| 352 |
if (b->core.mtid < 0 || b->core.mpos < 0) { |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
b->core.mtid = -1; |
| 358 |
b->core.mpos = -1; |
| 359 |
} else if (b->core.mtid == b->core.tid) { |
| 360 |
|
| 361 |
|
| 362 |
b->core.mpos = posmap[b->core.mpos]; |
| 363 |
} else { |
| 364 |
|
| 365 |
|
| 366 |
if (!fai) { |
| 367 |
fprintf(stderr, "[depad] ERROR: Needed reference %s sequence for mate (and no FASTA file)\n", sam_hdr_tid2name(h, b->core.mtid)); |
| 368 |
return -1; |
| 369 |
} |
| 370 |
|
| 371 |
if (load_unpadded_ref(fai, sam_hdr_tid2name(h, b->core.mtid), sam_hdr_tid2len(h, b->core.mtid), &r)) { |
| 372 |
fprintf(stderr, "[depad] ERROR: Failed to load '%s' from reference FASTA\n", sam_hdr_tid2name(h, b->core.mtid)); |
| 373 |
return -1; |
| 374 |
} |
| 375 |
posmap = update_posmap(posmap, r); |
| 376 |
b->core.mpos = posmap[b->core.mpos]; |
| 377 |
|
| 378 |
if (load_unpadded_ref(fai, sam_hdr_tid2name(h, b->core.tid), sam_hdr_tid2len(h, b->core.tid), &r)) { |
| 379 |
fprintf(stderr, "[depad] ERROR: Failed to load '%s' from reference FASTA\n", sam_hdr_tid2name(h, b->core.tid)); |
| 380 |
return -1; |
| 381 |
} |
| 382 |
posmap = update_posmap(posmap, r); |
| 383 |
} |
| 384 |
|
| 385 |
b->core.bin = bam_reg2bin(b->core.pos, bam_endpos(b)); |
| 386 |
|
| 387 |
next_seq: |
| 388 |
if (sam_write1(out, h, b) < 0) { |
| 389 |
print_error_errno("depad", "error writing to output"); |
| 390 |
return -1; |
| 391 |
} |
| 392 |
} |
| 393 |
if (read_ret < -1) { |
| 394 |
fprintf(stderr, "[depad] truncated file.\n"); |
| 395 |
ret = 1; |
| 396 |
} |
| 397 |
free(r.s); free(q.s); free(posmap); |
| 398 |
free(cigar2); |
| 399 |
bam_destroy1(b); |
| 400 |
return ret; |
| 401 |
} |
| 402 |
|
| 403 |
sam_hdr_t * fix_header(sam_hdr_t *old, faidx_t *fai) |
| 404 |
{ |
| 405 |
int i = 0, ret = 0; |
| 406 |
hts_pos_t unpadded_len = 0; |
| 407 |
sam_hdr_t *header = sam_hdr_dup(old); |
| 408 |
if (!header) |
| 409 |
return NULL; |
| 410 |
|
| 411 |
int nref = sam_hdr_nref(old); |
| 412 |
char len_buf[64]; |
| 413 |
|
| 414 |
for (i = 0; i < nref; ++i) { |
| 415 |
unpadded_len = get_unpadded_len(fai, sam_hdr_tid2name(old, i), sam_hdr_tid2len(old, i)); |
| 416 |
if (unpadded_len < 0) { |
| 417 |
fprintf(stderr, "[depad] ERROR getting unpadded length of '%s', padded length %"PRIhts_pos"\n", sam_hdr_tid2name(old, i), (hts_pos_t) sam_hdr_tid2len(old, i)); |
| 418 |
} else if (unpadded_len > sam_hdr_tid2len(old, i)) { |
| 419 |
fprintf(stderr, "[depad] New unpadded length of '%s' is larger than the padded length (%"PRIhts_pos" > %"PRIhts_pos")\n", |
| 420 |
sam_hdr_tid2name(old, i), unpadded_len, |
| 421 |
(hts_pos_t) sam_hdr_tid2len(old, i)); |
| 422 |
ret = 1; |
| 423 |
} else { |
| 424 |
sprintf(len_buf, "%"PRIhts_pos"", unpadded_len); |
| 425 |
if ((ret |= sam_hdr_update_line(header, "SQ", "SN", sam_hdr_tid2name(header, i), "LN", len_buf, NULL))) |
| 426 |
fprintf(stderr, "[depad] Error updating length of '%s' from %"PRIhts_pos" to %"PRIhts_pos"\n", |
| 427 |
sam_hdr_tid2name(header, i), |
| 428 |
(hts_pos_t) sam_hdr_tid2len(header, i), |
| 429 |
unpadded_len); |
| 430 |
|
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
if (ret) { |
| 435 |
sam_hdr_destroy(header); |
| 436 |
return NULL; |
| 437 |
} |
| 438 |
|
| 439 |
return header; |
| 440 |
} |
| 441 |
|
| 442 |
static int usage(int is_long_help); |
| 443 |
|
| 444 |
int main_pad2unpad(int argc, char *argv[]) |
| 445 |
{ |
| 446 |
samFile *in = 0, *out = 0; |
| 447 |
sam_hdr_t *h = 0, *h_fix = 0; |
| 448 |
faidx_t *fai = 0; |
| 449 |
int c, compress_level = -1, is_long_help = 0, no_pg = 0; |
| 450 |
char in_mode[5], out_mode[6], *fn_out = 0, *fn_fai = 0, *fn_out_idx = NULL; |
| 451 |
int ret=0; |
| 452 |
char *arg_list = NULL; |
| 453 |
sam_global_args ga = SAM_GLOBAL_ARGS_INIT; |
| 454 |
|
| 455 |
static const struct option lopts[] = { |
| 456 |
SAM_OPT_GLOBAL_OPTIONS('-', 0, 0, 0, 'T', '-'), |
| 457 |
{"no-PG", no_argument, NULL, 1}, |
| 458 |
{ NULL, 0, NULL, 0 } |
| 459 |
}; |
| 460 |
|
| 461 |
|
| 462 |
strcpy(in_mode, "r"); strcpy(out_mode, "w"); |
| 463 |
while ((c = getopt_long(argc, argv, "SCso:u1T:?", lopts, NULL)) >= 0) { |
| 464 |
switch (c) { |
| 465 |
case 'S': break; |
| 466 |
case 'C': hts_parse_format(&ga.out, "cram"); break; |
| 467 |
case 's': assert(compress_level == -1); hts_parse_format(&ga.out, "sam"); break; |
| 468 |
case 'o': fn_out = strdup(optarg); break; |
| 469 |
case 'u': |
| 470 |
compress_level = 0; |
| 471 |
if (ga.out.format == unknown_format) |
| 472 |
hts_parse_format(&ga.out, "bam"); |
| 473 |
break; |
| 474 |
case '1': |
| 475 |
compress_level = 1; |
| 476 |
if (ga.out.format == unknown_format) |
| 477 |
hts_parse_format(&ga.out, "bam"); |
| 478 |
break; |
| 479 |
case 1: no_pg = 1; break; |
| 480 |
case '?': is_long_help = 1; break; |
| 481 |
default: if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break; |
| 482 |
fprintf(stderr, "[bam_fillmd] unrecognized option '-%c'\n\n", c); |
| 483 |
return usage(is_long_help); |
| 484 |
} |
| 485 |
} |
| 486 |
if (argc == optind) return usage(is_long_help); |
| 487 |
|
| 488 |
strcat(out_mode, "h"); |
| 489 |
if (compress_level >= 0) { |
| 490 |
char tmp[2]; |
| 491 |
tmp[0] = compress_level + '0'; tmp[1] = '\0'; |
| 492 |
strcat(out_mode, tmp); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
if (ga.reference) { |
| 497 |
fn_fai = fai_path(ga.reference); |
| 498 |
fai = fai_load3(ga.reference, fn_fai, NULL, FAI_CREATE); |
| 499 |
} |
| 500 |
|
| 501 |
if ((in = sam_open_format(argv[optind], in_mode, &ga.in)) == 0) { |
| 502 |
print_error_errno("depad", "failed to open \"%s\" for reading", argv[optind]); |
| 503 |
ret = 1; |
| 504 |
goto depad_end; |
| 505 |
} |
| 506 |
if (fn_fai && hts_set_fai_filename(in, fn_fai) != 0) { |
| 507 |
fprintf(stderr, "[depad] failed to load reference file \"%s\".\n", fn_fai); |
| 508 |
ret = 1; |
| 509 |
goto depad_end; |
| 510 |
} |
| 511 |
if ((h = sam_hdr_read(in)) == 0) { |
| 512 |
fprintf(stderr, "[depad] failed to read the header from \"%s\".\n", argv[optind]); |
| 513 |
ret = 1; |
| 514 |
goto depad_end; |
| 515 |
} |
| 516 |
if (fai) { |
| 517 |
if (!(h_fix = fix_header(h, fai))){ |
| 518 |
fprintf(stderr, "[depad] failed to fix the header from\n"); |
| 519 |
ret = 1; |
| 520 |
goto depad_end; |
| 521 |
} |
| 522 |
} else { |
| 523 |
fprintf(stderr, "[depad] Warning - reference lengths will not be corrected without FASTA reference\n"); |
| 524 |
h_fix = h; |
| 525 |
} |
| 526 |
char wmode[2]; |
| 527 |
strcat(out_mode, sam_open_mode(wmode, fn_out, NULL)==0 ? wmode : "b"); |
| 528 |
if ((out = sam_open_format(fn_out? fn_out : "-", out_mode, &ga.out)) == 0) { |
| 529 |
print_error_errno("depad", "failed to open \"%s\" for writing", fn_out? fn_out : "standard output"); |
| 530 |
ret = 1; |
| 531 |
goto depad_end; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
if (ga.out.format == cram) |
| 537 |
hts_set_opt(out, CRAM_OPT_NO_REF, 1); |
| 538 |
|
| 539 |
if (!no_pg) { |
| 540 |
if(!(arg_list = stringify_argv(argc+1, argv-1))) { |
| 541 |
fprintf(stderr, "[depad] failed to create arg_list\n"); |
| 542 |
ret = 1; |
| 543 |
goto depad_end; |
| 544 |
} |
| 545 |
|
| 546 |
if (sam_hdr_add_pg(h_fix, "samtools", |
| 547 |
"VN", samtools_version(), |
| 548 |
arg_list ? "CL": NULL, |
| 549 |
arg_list ? arg_list : NULL, |
| 550 |
NULL)) { |
| 551 |
fprintf(stderr, "[depad] failed to add PG line to header\n"); |
| 552 |
ret = 1; |
| 553 |
goto depad_end; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
if (sam_hdr_write(out, h_fix) != 0) { |
| 558 |
fprintf(stderr, "[depad] failed to write header.\n"); |
| 559 |
ret = 1; |
| 560 |
goto depad_end; |
| 561 |
} |
| 562 |
if (ga.write_index) { |
| 563 |
if (!(fn_out_idx = auto_index(out, fn_out, h_fix))) { |
| 564 |
ret = 1; |
| 565 |
goto depad_end; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
if (bam_pad2unpad(in, out, h, fai) != 0) ret = 1; |
| 571 |
|
| 572 |
if (ga.write_index) { |
| 573 |
if (sam_idx_save(out) < 0) { |
| 574 |
print_error_errno("depad", "writing index failed"); |
| 575 |
ret = 1; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
depad_end: |
| 580 |
|
| 581 |
free(arg_list); |
| 582 |
if (fai) fai_destroy(fai); |
| 583 |
if (h) sam_hdr_destroy(h); |
| 584 |
if (h_fix && h_fix != h) sam_hdr_destroy(h_fix); |
| 585 |
if (in) sam_close(in); |
| 586 |
if (out && sam_close(out) < 0) { |
| 587 |
fprintf(stderr, "[depad] error on closing output file.\n"); |
| 588 |
ret = 1; |
| 589 |
} |
| 590 |
free(fn_fai); free(fn_out); |
| 591 |
if (fn_out_idx) |
| 592 |
free(fn_out_idx); |
| 593 |
sam_global_args_free(&ga); |
| 594 |
return ret; |
| 595 |
} |
| 596 |
|
| 597 |
static int usage(int is_long_help) |
| 598 |
{ |
| 599 |
fprintf(stderr, "\n"); |
| 600 |
fprintf(stderr, "Usage: samtools depad <in.bam>\n\n"); |
| 601 |
fprintf(stderr, "Options:\n"); |
| 602 |
fprintf(stderr, " -s Output is SAM (default is BAM)\n"); |
| 603 |
fprintf(stderr, " -S Input is SAM (default is BAM)\n"); |
| 604 |
fprintf(stderr, " -u Uncompressed BAM output (can't use with -s)\n"); |
| 605 |
fprintf(stderr, " -1 Fast compression BAM output (can't use with -s)\n"); |
| 606 |
fprintf(stderr, " -T, --reference FILE\n"); |
| 607 |
fprintf(stderr, " Padded reference sequence file [null]\n"); |
| 608 |
fprintf(stderr, " -o FILE Output file name [stdout]\n"); |
| 609 |
fprintf(stderr, " --no-PG do not add a PG line\n"); |
| 610 |
fprintf(stderr, " -? Longer help\n"); |
| 611 |
sam_global_opt_help(stderr, "-...--.."); |
| 612 |
|
| 613 |
if (is_long_help) |
| 614 |
fprintf(stderr, |
| 615 |
"Notes:\n" |
| 616 |
"\n" |
| 617 |
"1. Requires embedded reference sequences (before the reads for that reference),\n" |
| 618 |
" or ideally a FASTA file of the padded reference sequences (via a -T option).\n" |
| 619 |
"\n" |
| 620 |
"2. Input padded alignment reads' CIGAR strings must not use P or I operators.\n" |
| 621 |
"\n"); |
| 622 |
return 1; |
| 623 |
} |