X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=padding.c;h=88945e2ca5c54c1a592a45899358996ddd810ece;hb=0b3418cf166ce4a58cedf0d9a2df5ec3dd4cc5fa;hp=23d1b23c9ac68dd8dd6a5d1c7847d714e3e837ed;hpb=2b4d9b14c3cdd96b9b1df2e9f2c83ceef4a76593;p=samtools.git diff --git a/padding.c b/padding.c index 23d1b23..88945e2 100644 --- a/padding.c +++ b/padding.c @@ -170,7 +170,7 @@ int bam_pad2unpad(samfile_t *in, samfile_t *out, faidx_t *fai) r_tid = b->core.tid; unpad_seq(b, &r); if (h->target_len[r_tid] != r.l) { - fprintf(stderr, "[depad] ERROR: (Padded) length of '%s' is %i in BAM header, but %id in embedded reference\n", bam1_qname(b), h->target_len[r_tid], r.l); + fprintf(stderr, "[depad] ERROR: (Padded) length of '%s' is %d in BAM header, but %ld in embedded reference\n", bam1_qname(b), h->target_len[r_tid], r.l); return -1; } if (fai) { @@ -186,8 +186,8 @@ int bam_pad2unpad(samfile_t *in, samfile_t *out, faidx_t *fai) // Show gaps as ASCII 45 fprintf(stderr, "[depad] ERROR: Embedded sequence and reference FASTA don't match for %s base %i, '%c' vs '%c'\n", h->target_name[b->core.tid], i+1, - r.s[i] ? bam_nt16_rev_table[r.s[i]] : 45, - q.s[i] ? bam_nt16_rev_table[q.s[i]] : 45); + r.s[i] ? bam_nt16_rev_table[(int)r.s[i]] : 45, + q.s[i] ? bam_nt16_rev_table[(int)q.s[i]] : 45); return -1; } } @@ -295,10 +295,48 @@ bam_header_t * fix_header(bam_header_t *old, faidx_t *fai) fprintf(stderr, "[depad] ERROR getting unpadded length of '%s', padded length %i\n", old->target_name[i], old->target_len[i]); } else { header->target_len[i] = unpadded_len; - fprintf(stderr, "[depad] Recalculating '%s' length %i -> %i\n", old->target_name[i], old->target_len[i], header->target_len[i]); + //fprintf(stderr, "[depad] Recalculating '%s' length %i -> %i\n", old->target_name[i], old->target_len[i], header->target_len[i]); } } - /* TODO - Correct the @SQ reference lengths from padded to unpadded */ + /* Duplicating the header allocated new buffer for header string */ + /* After modifying the @SQ lines it will only get smaller, since */ + /* the LN entries will be the same or shorter, and we'll remove */ + /* any MD entries (MD5 checksums). */ + assert(strlen(old->text) == strlen(header->text)); + assert (0==strcmp(old->text, header->text)); + const char *text; + text = old->text; + header->text[0] = '\0'; /* Resuse the allocated buffer */ + char * newtext = header->text; + char * end=NULL; + while (text[0]=='@') { + end = strchr(text, '\n'); + assert(end != 0); + if (text[1]=='S' && text[2]=='Q' && text[3]=='\t') { + /* TODO - edit the @SQ line here to remove MD and fix LN. */ + /* For now just remove the @SQ line, and samtools will */ + /* automatically generate a minimal replacement with LN. */ + /* However, that discards any other tags like AS, SP, UR. */ + //fprintf(stderr, "[depad] Removing @SQ line\n"); + } else { + /* Copy this line to the new header */ + strncat(newtext, text, end - text + 1); + } + text = end + 1; + } + assert (text[0]=='\0'); + /* Check we didn't overflow the buffer */ + assert (strlen(header->text) <= strlen(old->text)); + if (strlen(header->text) < header->l_text) { + //fprintf(stderr, "[depad] Reallocating header buffer\n"); + assert (newtext == header->text); + newtext = calloc(strlen(header->text) + 1, 1); + strcpy(newtext, header->text); + free(header->text); + header->text = newtext; + header->l_text = strlen(newtext); + } + //fprintf(stderr, "[depad] Here is the new header (pending @SQ lines),\n\n%s\n(end)\n", header->text); return header; }