]> git.donarmstrong.com Git - samtools.git/blobdiff - sam.c
* This revision is SERIOUSLY BUGGY. Please NOT use it.
[samtools.git] / sam.c
diff --git a/sam.c b/sam.c
index 9a6e201d75e24d366903ff4e937f80d71e45303c..07524c0213b30bcae3870f8424da38daef43ac49 100644 (file)
--- a/sam.c
+++ b/sam.c
@@ -1,4 +1,6 @@
 #include <string.h>
+#include <unistd.h>
+#include "faidx.h"
 #include "sam.h"
 
 #define TYPE_BAM  1
@@ -21,6 +23,18 @@ bam_header_t *bam_header_dup(const bam_header_t *h0)
        }
        return h;
 }
+static void append_header_text(bam_header_t *header, char* text, int len)
+{
+       int x = header->l_text + 1;
+       int y = header->l_text + len + 1; // 1 byte null
+       if (text == 0) return;
+       kroundup32(x); 
+       kroundup32(y);
+       if (x < y) header->text = (char*)realloc(header->text, y);
+       strncpy(header->text + header->l_text, text, len); // we cannot use strcpy() here.
+       header->l_text += len;
+       header->text[header->l_text] = 0;
+}
 
 samfile_t *samopen(const char *fn, const char *mode, const void *aux)
 {
@@ -39,24 +53,32 @@ samfile_t *samopen(const char *fn, const char *mode, const void *aux)
                        fp->header = sam_header_read(fp->x.tamr);
                        if (fp->header->n_targets == 0) { // no @SQ fields
                                if (aux) { // check if aux is present
-                                       bam_header_destroy(fp->header);
+                                       bam_header_t *textheader = fp->header;
                                        fp->header = sam_header_read2((const char*)aux);
+                                       append_header_text(fp->header, textheader->text, textheader->l_text);
+                                       bam_header_destroy(textheader);
                                }
                                if (fp->header->n_targets == 0)
                                        fprintf(stderr, "[samopen] no @SQ lines in the header.\n");
                        } else fprintf(stderr, "[samopen] SAM header is present: %d sequences.\n", fp->header->n_targets);
                }
+               sam_header_parse_rg(fp->header);
        } else if (mode[0] == 'w') { // write
                fp->header = bam_header_dup((const bam_header_t*)aux);
                if (mode[1] == 'b') { // binary
+                       char bmode[3];
+                       bmode[0] = 'w'; bmode[1] = strstr(mode, "u")? 'u' : 0; bmode[2] = 0;
                        fp->type |= TYPE_BAM;
-                       fp->x.bam = strcmp(fn, "-")? bam_open(fn, "w") : bam_dopen(fileno(stdout), "w");
+                       fp->x.bam = strcmp(fn, "-")? bam_open(fn, bmode) : bam_dopen(fileno(stdout), bmode);
                        if (fp->x.bam == 0) goto open_err_ret;
                        bam_header_write(fp->x.bam, fp->header);
                } else { // text
                        // open file
                        fp->x.tamw = strcmp(fn, "-")? fopen(fn, "w") : stdout;
                        if (fp->x.tamr == 0) goto open_err_ret;
+                       if (strstr(mode, "X")) fp->type |= BAM_OFSTR<<2;
+                       else if (strstr(mode, "x")) fp->type |= BAM_OFHEX<<2;
+                       else fp->type |= BAM_OFDEC<<2;
                        // write header
                        if (strstr(mode, "h")) {
                                int i;
@@ -108,7 +130,7 @@ int samwrite(samfile_t *fp, const bam1_t *b)
        if (fp == 0 || (fp->type & TYPE_READ)) return -1; // not open for writing
        if (fp->type & TYPE_BAM) return bam_write1(fp->x.bam, b);
        else {
-               char *s = bam_format1(fp->header, b);
+               char *s = bam_format1_core(fp->header, b, fp->type>>2&3);
                int l = strlen(s);
                fputs(s, fp->x.tamw); fputc('\n', fp->x.tamw);
                free(s);
@@ -116,7 +138,7 @@ int samwrite(samfile_t *fp, const bam1_t *b)
        }
 }
 
-int sampileup(samfile_t *fp, int mask, int min_mapQ, bam_pileup_f func, void *func_data)
+int sampileup(samfile_t *fp, int mask, bam_pileup_f func, void *func_data)
 {
        bam_plbuf_t *buf;
        int ret;
@@ -125,10 +147,29 @@ int sampileup(samfile_t *fp, int mask, int min_mapQ, bam_pileup_f func, void *fu
        buf = bam_plbuf_init(func, func_data);
        bam_plbuf_set_mask(buf, mask);
        while ((ret = samread(fp, b)) >= 0)
-               if (b->core.qual >= min_mapQ)
-                       bam_plbuf_push(b, buf);
+               bam_plbuf_push(b, buf);
        bam_plbuf_push(0, buf);
        bam_plbuf_destroy(buf);
        bam_destroy1(b);
        return 0;
 }
+
+char *samfaipath(const char *fn_ref)
+{
+       char *fn_list = 0;
+       if (fn_ref == 0) return 0;
+       fn_list = calloc(strlen(fn_ref) + 5, 1);
+       strcat(strcpy(fn_list, fn_ref), ".fai");
+       if (access(fn_list, R_OK) == -1) { // fn_list is unreadable
+               if (access(fn_ref, R_OK) == -1) {
+                       fprintf(stderr, "[samfaipath] fail to read file %s.\n", fn_ref);
+               } else {
+                       fprintf(stderr, "[samfaipath] build FASTA index...\n");
+                       if (fai_build(fn_ref) == -1) {
+                               fprintf(stderr, "[samfaipath] fail to build FASTA index.\n");
+                               free(fn_list); fn_list = 0;
+                       }
+               }
+       }
+       return fn_list;
+}