 +  blkrd_work_t * const w = (blkrd_work_t*)data;
 +  mt_blkrd_t * const mt = w->mt;
 +  const int myid = w - w->mt->w;
-+  samFile *fp = mt->fp;
++
 +  int partial_line_len = 0;
 +  char partial_line[MAX_LINE_LENGTH];
 +  uint32_t block_seqid = 0;
-+
-+  const int format = mt->fp->format.format;
-+  switch (format) {
-+  case bam: 
-+  case cram:
-+    // enqueue bam read
-+    //TODO:
-+    fprintf(stderr, "BAM/CRAM input is not supported yet\n");
-+    exit(-1);
-+    //mt_hash_enqueue(p_hash, b, g_bamid, 0, qnameid);
-+    break;
-+  case sam:
-+    break;
-+  default:
-+    abort();
-+  }
++static double total_open = 0.0;
++int n_open = 0;
++
++int fn_i;
++for (fn_i=0; fn_i < mt->input_files->n; fn_i++) {
++  char * const fn = mt->input_files->fn[fn_i];
++
++struct timeval tm_open;
++gettimeofday(&tm_open, NULL);
++
++  samFile *const fp = sam_open(fn, "r");
++  if (fp == NULL) {
++    // in case that the target file was removed after its header was checked
++    fprintf(stderr, "(E) fail to open file %s\n", fn);
++    return NULL;
++  }
++  mt->fps->fp[fn_i] = fp;
++  // skip the header
++  bam_hdr_t * const tmp_hdr = sam_hdr_read(fp);
++  if (0 != fn_i) {
++    bam_hdr_t * const header = mt->linesplit->samparse->header;
++    // compare the current header to the header for the 1st SAM file 
++    if (header->l_text == tmp_hdr->l_text &&
++        0 == memcmp(header->text, tmp_hdr->text, header->l_text)) {
++      // the same header
++    } else {
++      fprintf(stderr, "\n\n(E) %s has the header (len %d) that is different from the first file's %s (len %d)\n", mt->input_files->fn[fn_i], tmp_hdr->l_text, mt->input_files->fn[0], header->l_text);
++      char *p1 = header->text;
++      char *p2 = tmp_hdr->text;
++      int i=0;
++      while(1) {
++        i++;
++        char * const ln1 = strsep(&p1, "\n");
++        char * const ln2 = strsep(&p2, "\n");
++        if (0 != strcmp(ln1, ln2)) {
++          fprintf(stderr, "(E)     The first difference between the first and current files appears at line %d:\n", i);
++          fprintf(stderr, "(E)     current file: %s\n", mt->input_files->fn[fn_i]);
++          fprintf(stderr, "(E)         %s\n", ln2);
++          fprintf(stderr, "(E)     first file: %s\n", mt->input_files->fn[0]);
++          fprintf(stderr, "(E)         %s\n", ln1);
++          break;
++        }
++        if (p1 == header->text + header->l_text
++         && p2 != tmp_hdr->text + tmp_hdr->l_text) {
++          fprintf(stderr, "(E)     The current file has the following line at line %d, but the first file does not have it at the same position\n", i);
++          fprintf(stderr, "(E)         %s\n", p2);
++          break;
++        } else if (p1 != header->text + header->l_text
++         && p2 == tmp_hdr->text + tmp_hdr->l_text) {
++          fprintf(stderr, "(E)     The first file has the following line at line %d, but the current file does not have it at the same position\n", i);
++          fprintf(stderr, "(E)         %s\n", p1);
++          break;
++        } else if (p1 == header->text + header->l_text
++         && p2 == tmp_hdr->text + tmp_hdr->l_text) {
++          break;
++        }
++      }
++      exit(-1);
++    }
++  }
++  bam_hdr_destroy(tmp_hdr);
++struct timeval tm_open2;
++gettimeofday(&tm_open2, NULL);
++double t = tm_open2.tv_sec-tm_open.tv_sec+(tm_open2.tv_usec-tm_open.tv_usec)/1000000.0;
++total_open += t;
++n_open++;
 +
 +/*
 +  After sam_hdr_read(), some data were already read in the internal buffers:

+static void *mt_drop_cache(void *data) {
+  mt_drop_cache_t * const mt = (mt_drop_cache_t*)data;
+  static off_t last = 0;
+  const int fd = mt->fd;
+
+  while(1) {
+    off_t const curr = lseek(fd, 0, SEEK_CUR);
+    if (-1 == curr) {
+      perror("mt_drop_cache: lseek ");
+      exit(-1);
+    }
+    posix_fadvise(fd, last, curr - last, POSIX_FADV_DONTNEED);
+    last = curr;
+
+    mt->count++;

+}
+}
+
+/*!
+  @abstract Convert SAM to compressed BAM
+
+  @param  fn       name of the file to be sorted
+  @param  prefix   prefix of the temporary files (prefix.NNNN.bam are written)
+  @param  fnout    name of the final output file to be written
