]> git.donarmstrong.com Git - samtools.git/blobdiff - sam.c
* samtools-0.1.4-10 (r341)
[samtools.git] / sam.c
diff --git a/sam.c b/sam.c
index 671b5042bc17ec1948c83fe54093c03ff7065120..5b02abbc94daad6f5eb7b19f596fba6d9dc54109 100644 (file)
--- a/sam.c
+++ b/sam.c
@@ -19,6 +19,7 @@ bam_header_t *bam_header_dup(const bam_header_t *h0)
                h->target_len[i] = h0->target_len[i];
                h->target_name[i] = strdup(h0->target_name[i]);
        }
+       if (h0->rg2lib) h->rg2lib = bam_strmap_dup(h0->rg2lib);
        return h;
 }
 
@@ -43,14 +44,17 @@ samfile_t *samopen(const char *fn, const char *mode, const void *aux)
                                        fp->header = sam_header_read2((const char*)aux);
                                }
                                if (fp->header->n_targets == 0)
-                                       fprintf(stderr, "[samopen] empty header.\n");
+                                       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
@@ -67,10 +71,10 @@ samfile_t *samopen(const char *fn, const char *mode, const void *aux)
                                sam_header_parse(alt);
                                alt->l_text = 0; alt->text = 0;
                                // check if there are @SQ lines in the header
+                               fwrite(fp->header->text, 1, fp->header->l_text, fp->x.tamw);
                                if (alt->n_targets) { // then write the header text without dumping ->target_{name,len}
                                        if (alt->n_targets != fp->header->n_targets)
                                                fprintf(stderr, "[samopen] inconsistent number of target sequences.\n");
-                                       fwrite(fp->header->text, 1, fp->header->l_text, fp->x.tamw);
                                } else { // then dump ->target_{name,len}
                                        for (i = 0; i < fp->header->n_targets; ++i)
                                                fprintf(fp->x.tamw, "@SQ\tSN:%s\tLN:%d\n", fp->header->target_name[i], fp->header->target_len[i]);