]> git.donarmstrong.com Git - samtools.git/blobdiff - bam.c
Unfinished modification. Please do not use this revision...
[samtools.git] / bam.c
diff --git a/bam.c b/bam.c
index 619b46a6718a773aa073e7ecede13ef5cd10dee8..533250b95771d73d626a247ca58bc1c2a167fee6 100644 (file)
--- a/bam.c
+++ b/bam.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <ctype.h>
+#include <errno.h>
 #include <assert.h>
 #include "bam.h"
 #include "bam_endian.h"
@@ -12,26 +13,32 @@ char *bam_flag2char_table = "pPuUrR12sfd\0\0\0\0\0";
  * CIGAR related routines *
  **************************/
 
-int bam_segreg(int32_t pos, const bam1_core_t *c, const uint32_t *cigar, bam_segreg_t *reg)
+int bam_tpos2qpos(const bam1_core_t *c, const uint32_t *cigar, int32_t tpos, int32_t *_tpos)
 {
-       unsigned k;
-       int32_t x = c->pos, y = 0;
-       int state = 0;
+       int k, x = c->pos, y = 0, last_y = 0;
+       *_tpos = c->pos;
        for (k = 0; k < c->n_cigar; ++k) {
-               int op = cigar[k] & BAM_CIGAR_MASK; // operation
-               int l = cigar[k] >> BAM_CIGAR_SHIFT; // length
-               if (state == 0 && (op == BAM_CMATCH || op == BAM_CDEL || op == BAM_CINS) && x + l > pos) {
-                       reg->tbeg = x; reg->qbeg = y; reg->cbeg = k;
-                       state = 1;
-               }
-               if (op == BAM_CMATCH) { x += l; y += l; }
-               else if (op == BAM_CDEL || op == BAM_CREF_SKIP) x += l;
-               else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
-               if (state == 1 && (op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP || op == BAM_CREF_SKIP || k == c->n_cigar - 1)) {
-                       reg->tend = x; reg->qend = y; reg->cend = k;
+               int op = cigar[k] & BAM_CIGAR_MASK;
+               int l = cigar[k] >> BAM_CIGAR_SHIFT;
+               if (op == BAM_CMATCH) {
+                       if (c->pos > tpos) return y;
+                       if (x + l > tpos) {
+                               *_tpos = tpos;
+                               return y + (tpos - x);
+                       }
+                       x += l; y += l;
+                       last_y = y;
+               } else if (op == BAM_CINS || op == BAM_CSOFT_CLIP) y += l;
+               else if (op == BAM_CDEL || op == BAM_CREF_SKIP) {
+                       if (x + l > tpos) {
+                               *_tpos = x;
+                               return y;
+                       }
+                       x += l;
                }
        }
-       return state? 0 : -1;
+       *_tpos = x;
+       return last_y;
 }
 
 uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar)
@@ -94,7 +101,11 @@ bam_header_t *bam_header_read(bamFile fp)
        int32_t i = 1, name_len;
        // check EOF
        i = bgzf_check_EOF(fp);
-       if (i < 0) fprintf(stderr, "[bam_header_read] read from pipe; skip EOF checking.\n");
+       if (i < 0) {
+               // If the file is a pipe, checking the EOF marker will *always* fail
+               // with ESPIPE.  Suppress the error message in this case.
+               if (errno != ESPIPE) perror("[bam_header_read] bgzf_check_EOF");
+       }
        else if (i == 0) fprintf(stderr, "[bam_header_read] EOF marker is absent.\n");
        // read "BAM1"
        if (bam_read(fp, buf, 4) != 4) return 0;