Skip to content

Commit

Permalink
* samtools-0.1.4-7 (r337)
Browse files Browse the repository at this point in the history
 * bgzf.c: support mode string "wu": uncompressed output
 * "samtools view" support "-u" command-line option
  • Loading branch information
Heng Li committed Jun 12, 2009
1 parent 13f55c1 commit 431e646
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bamtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "bam.h"

#ifndef PACKAGE_VERSION
#define PACKAGE_VERSION "0.1.4-6 (r334)"
#define PACKAGE_VERSION "0.1.4-7 (r337)"
#endif

int bam_taf2baf(int argc, char *argv[]);
Expand Down
24 changes: 13 additions & 11 deletions bgzf.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* or functionality.
*/

/* 2009-06-12 by lh3: support a mode string like "wu" where 'u' for uncompressed output */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -91,7 +93,7 @@ open_read(int fd)
fp = malloc(sizeof(BGZF));
fp->file_descriptor = fd;
fp->open_mode = 'r';
fp->owned_file = 0;
fp->owned_file = 0; fp->is_uncompressed = 0;
fp->file = file;
fp->uncompressed_block_size = MAX_BLOCK_SIZE;
fp->uncompressed_block = malloc(MAX_BLOCK_SIZE);
Expand All @@ -106,15 +108,15 @@ open_read(int fd)

static
BGZF*
open_write(int fd)
open_write(int fd, bool is_uncompressed)
{
FILE* file = fdopen(fd, "w");
BGZF* fp;
if (file == 0) return 0;
fp = malloc(sizeof(BGZF));
fp->file_descriptor = fd;
fp->open_mode = 'w';
fp->owned_file = 0;
fp->owned_file = 0; fp->is_uncompressed = is_uncompressed;
fp->file = file;
fp->uncompressed_block_size = DEFAULT_BLOCK_SIZE;
fp->uncompressed_block = NULL;
Expand All @@ -131,16 +133,16 @@ BGZF*
bgzf_open(const char* __restrict path, const char* __restrict mode)
{
BGZF* fp = NULL;
if (strcasecmp(mode, "r") == 0) {
if (mode[0] == 'r' || mode[0] == 'R') { /* The reading mode is preferred. */
int oflag = O_RDONLY;
int fd = open(path, oflag);
if (fd == -1) return 0;
fp = open_read(fd);
} else if (strcasecmp(mode, "w") == 0) {
} else if (mode[0] == 'w' || mode[0] == 'W') {
int oflag = O_WRONLY | O_CREAT | O_TRUNC;
int fd = open(path, oflag, 0644);
if (fd == -1) return 0;
fp = open_write(fd);
fp = open_write(fd, strstr(mode, "u")? 1 : 0);
}
if (fp != NULL) {
fp->owned_file = 1;
Expand All @@ -152,10 +154,10 @@ BGZF*
bgzf_fdopen(int fd, const char * __restrict mode)
{
if (fd == -1) return 0;
if (strcasecmp(mode, "r") == 0) {
if (mode[0] == 'r' || mode[0] == 'R') {
return open_read(fd);
} else if (strcasecmp(mode, "w") == 0) {
return open_write(fd);
} else if (mode[0] == 'w' || mode[0] == 'W') {
return open_write(fd, strstr(mode, "u")? 1 : 0);
} else {
return NULL;
}
Expand Down Expand Up @@ -195,7 +197,7 @@ deflate_block(BGZF* fp, int block_length)
int input_length = block_length;
int compressed_length = 0;
while (1) {

int compress_level = fp->is_uncompressed? 0 : Z_DEFAULT_COMPRESSION;
z_stream zs;
zs.zalloc = NULL;
zs.zfree = NULL;
Expand All @@ -204,7 +206,7 @@ deflate_block(BGZF* fp, int block_length)
zs.next_out = (void*)&buffer[BLOCK_HEADER_LENGTH];
zs.avail_out = buffer_size - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH;

int status = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
int status = deflateInit2(&zs, compress_level, Z_DEFLATED,
GZIP_WINDOW_BITS, Z_DEFAULT_MEM_LEVEL, Z_DEFAULT_STRATEGY);
if (status != Z_OK) {
report_error(fp, "deflate init failed");
Expand Down
5 changes: 2 additions & 3 deletions bgzf.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@

#include <stdint.h>
#include <stdio.h>
#include "zlib.h"
#include <stdbool.h>
//#include "zutil.h"
#include <zlib.h>

//typedef int8_t bool;

typedef struct {
int file_descriptor;
char open_mode; // 'r' or 'w'
bool owned_file;
bool owned_file, is_uncompressed;
FILE* file;
int uncompressed_block_size;
int compressed_block_size;
Expand Down
4 changes: 3 additions & 1 deletion sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ samfile_t *samopen(const char *fn, const char *mode, const void *aux)
} 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
Expand Down
13 changes: 9 additions & 4 deletions sam_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,33 @@ static int usage(void);

int main_samview(int argc, char *argv[])
{
int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0;
int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0, is_uncompressed = 0, is_bamout = 0;
samfile_t *in = 0, *out = 0;
char in_mode[4], out_mode[4], *fn_out = 0, *fn_list = 0;
char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0;

/* parse command-line options */
strcpy(in_mode, "r"); strcpy(out_mode, "w");
while ((c = getopt(argc, argv, "Sbt:hHo:q:f:F:")) >= 0) {
while ((c = getopt(argc, argv, "Sbt:hHo:q:f:F:u")) >= 0) {
switch (c) {
case 'S': is_bamin = 0; break;
case 'b': strcat(out_mode, "b"); break;
case 'b': is_bamout = 1; break;
case 't': fn_list = strdup(optarg); is_bamin = 0; break;
case 'h': is_header = 1; break;
case 'H': is_header_only = 1; break;
case 'o': fn_out = strdup(optarg); break;
case 'f': g_flag_on = strtol(optarg, 0, 0); break;
case 'F': g_flag_off = strtol(optarg, 0, 0); break;
case 'q': g_min_mapQ = atoi(optarg); break;
case 'u': is_uncompressed = 1; break;
default: return usage();
}
}
if (is_uncompressed) is_bamout = 1;
if (is_header_only) is_header = 1;
if (is_bamout) strcat(out_mode, "b");
if (is_bamin) strcat(in_mode, "b");
if (is_header) strcat(out_mode, "h");
if (is_uncompressed) strcat(out_mode, "u");
if (argc == optind) return usage();

// open file handlers
Expand Down Expand Up @@ -101,6 +105,7 @@ static int usage()
fprintf(stderr, " -h print header for the SAM output\n");
fprintf(stderr, " -H print header only (no alignments)\n");
fprintf(stderr, " -S input is SAM\n");
fprintf(stderr, " -u uncompressed BAM output (force -b)\n");
fprintf(stderr, " -t FILE list of reference names and lengths (force -S) [null]\n");
fprintf(stderr, " -o FILE output file name [stdout]\n");
fprintf(stderr, " -f INT required flag, 0 for unset [0]\n");
Expand Down

0 comments on commit 431e646

Please sign in to comment.