]> git.donarmstrong.com Git - samtools.git/blobdiff - bam.c
Fixed a bug in knet_seek
[samtools.git] / bam.c
diff --git a/bam.c b/bam.c
index f6e05ff4285ef6743387e33942b91c91bf0e85fc..56c95db4e2c42c404627e41bd1ca2360a496af73 100644 (file)
--- a/bam.c
+++ b/bam.c
@@ -5,6 +5,7 @@
 #include "bam.h"
 #include "bam_endian.h"
 #include "kstring.h"
+#include "sam_header.h"
 
 int bam_is_be = 0;
 char *bam_flag2char_table = "pPuUrR12sfd\0\0\0\0\0";
@@ -13,28 +14,6 @@ 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)
-{
-       unsigned k;
-       int32_t x = c->pos, y = 0;
-       int state = 0;
-       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;
-               }
-       }
-       return state? 0 : -1;
-}
-
 uint32_t bam_calend(const bam1_core_t *c, const uint32_t *cigar)
 {
        uint32_t k, end;
@@ -81,10 +60,9 @@ void bam_header_destroy(bam_header_t *header)
                free(header->target_len);
        }
        free(header->text);
-#ifndef BAM_NO_HASH
-       if (header->rg2lib) bam_strmap_destroy(header->rg2lib);
+       if (header->dict) sam_header_free(header->dict);
+       if (header->rg2lib) sam_tbl_destroy(header->rg2lib);
        bam_destroy_header_hash(header);
-#endif
        free(header);
 }
 
@@ -290,7 +268,7 @@ char *bam_format1_core(const bam_header_t *header, const bam1_t *b, int of)
                ksprintf(&str, "\t%c%c:", key[0], key[1]);
                if (type == 'A') { ksprintf(&str, "A:%c", *s); ++s; }
                else if (type == 'C') { ksprintf(&str, "i:%u", *s); ++s; }
-               else if (type == 'c') { ksprintf(&str, "i:%d", *s); ++s; }
+               else if (type == 'c') { ksprintf(&str, "i:%d", *(int8_t*)s); ++s; }
                else if (type == 'S') { ksprintf(&str, "i:%u", *(uint16_t*)s); s += 2; }
                else if (type == 's') { ksprintf(&str, "i:%d", *(int16_t*)s); s += 2; }
                else if (type == 'I') { ksprintf(&str, "i:%u", *(uint32_t*)s); s += 4; }
@@ -313,3 +291,13 @@ void bam_view1(const bam_header_t *header, const bam1_t *b)
        printf("%s\n", s);
        free(s);
 }
+
+// FIXME: we should also check the LB tag associated with each alignment
+const char *bam_get_library(bam_header_t *h, const bam1_t *b)
+{
+       const uint8_t *rg;
+       if (h->dict == 0) h->dict = sam_header_parse2(h->text);
+       if (h->rg2lib == 0) h->rg2lib = sam_header2tbl(h->dict, "RG", "ID", "LB");
+       rg = bam_aux_get(b, "RG");
+       return (rg == 0)? 0 : sam_tbl_get(h->rg2lib, (const char*)(rg + 1));
+}