+int load_unpadded_ref(faidx_t *fai, char *ref_name, int ref_len, kstring_t *seq)
+{
+ char base;
+ char *fai_ref = 0;
+ int fai_ref_len = 0, k;
+
+ fai_ref = fai_fetch(fai, ref_name, &fai_ref_len);
+ if (fai_ref_len != ref_len) {
+ fprintf(stderr, "[depad] ERROR: FASTA sequence %s length %i, expected %i\n", ref_name, fai_ref_len, ref_len);
+ free(fai_ref);
+ return -1;
+ }
+ ks_resize(seq, ref_len);
+ seq->l = 0;
+ for (k = 0; k < ref_len; ++k) {
+ base = fai_ref[k];
+ if (base == '-' || base == '*') {
+ // Map gaps to null to match unpad_seq function
+ seq->s[seq->l++] = 0;
+ } else {
+ int i = bam_nt16_table[(int)base];
+ if (i == 0 || i==16) { // Equals maps to 0, anything unexpected to 16
+ fprintf(stderr, "[depad] ERROR: Invalid character %c (ASCII %i) in FASTA sequence %s\n", base, (int)base, ref_name);
+ free(fai_ref);
+ return -1;
+ }
+ seq->s[seq->l++] = i;
+ }
+ }
+ assert(ref_len == seq->l);
+ free(fai_ref);
+ return 0;
+}
+
+inline int * update_posmap(int *posmap, kstring_t ref)
+{
+ int i, k;
+ posmap = realloc(posmap, ref.m * sizeof(int));
+ for (i = k = 0; i < ref.l; ++i) {
+ posmap[i] = k;
+ if (ref.s[i]) ++k;
+ }
+ return posmap;
+}
+
+int bam_pad2unpad(samfile_t *in, samfile_t *out, faidx_t *fai)