#include <stdio.h>
#include <unistd.h>
#include <math.h>
+#include <inttypes.h>
#include "sam_header.h"
#include "sam.h"
#include "faidx.h"
// data passed to the bam_fetch callback is encapsulated in this struct.
typedef struct {
bam_header_t *header;
- int *count;
+ int64_t *count; // int does overflow for very big BAMs
} count_func_data_t;
typedef khash_t(rg) *rghash_t;
// FIXME: we'd better use no global variables...
static rghash_t g_rghash = 0;
-static int g_min_mapQ = 0, g_flag_on = 0, g_flag_off = 0, g_qual_scale = 0;
+static int g_min_mapQ = 0, g_flag_on = 0, g_flag_off = 0, g_qual_scale = 0, g_min_qlen = 0;
static uint32_t g_subsam_seed = 0;
static double g_subsam_frac = -1.;
static char *g_library, *g_rg;
qual[i] = c < 93? c : 93;
}
}
+ if (g_min_qlen > 0) {
+ int k, qlen = 0;
+ uint32_t *cigar = bam1_cigar(b);
+ for (k = 0; k < b->core.n_cigar; ++k)
+ if ((bam_cigar_type(bam_cigar_op(cigar[k]))&1) || bam_cigar_op(cigar[k]) == BAM_CHARD_CLIP)
+ qlen += bam_cigar_oplen(cigar[k]);
+ if (qlen < g_min_qlen) return 1;
+ }
if (b->core.qual < g_min_mapQ || ((b->core.flag & g_flag_on) != g_flag_on) || (b->core.flag & g_flag_off))
return 1;
if (g_bed && b->core.tid >= 0 && !bed_overlap(g_bed, h->target_name[b->core.tid], b->core.pos, bam_calend(&b->core, bam1_cigar(b))))
{
int c, is_header = 0, is_header_only = 0, is_bamin = 1, ret = 0, compress_level = -1, is_bamout = 0, is_count = 0;
int of_type = BAM_OFDEC, is_long_help = 0, n_threads = 0;
- int count = 0;
+ int64_t count = 0;
samfile_t *in = 0, *out = 0;
char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0, *fn_ref = 0, *fn_rg = 0, *q;
/* parse command-line options */
strcpy(in_mode, "r"); strcpy(out_mode, "w");
- while ((c = getopt(argc, argv, "SbBct:h1Ho:q:f:F:ul:r:xX?T:R:L:s:Q:@:")) >= 0) {
+ while ((c = getopt(argc, argv, "SbBct:h1Ho:q:f:F:ul:r:xX?T:R:L:s:Q:@:m:")) >= 0) {
switch (c) {
case 's':
if ((g_subsam_seed = strtol(optarg, &q, 10)) != 0) {
}
g_subsam_frac = strtod(q, &q);
break;
+ case 'm': g_min_qlen = atoi(optarg); break;
case 'c': is_count = 1; break;
case 'S': is_bamin = 0; break;
case 'b': is_bamout = 1; break;
}
view_end:
- if (is_count && ret == 0) {
- printf("%d\n", count);
- }
+ if (is_count && ret == 0)
+ printf("%" PRId64 "\n", count);
+
// close files, free and return
free(fn_list); free(fn_ref); free(fn_out); free(g_library); free(g_rg); free(fn_rg);
if (g_bed) bed_destroy(g_bed);
bam_header_t *h;
bam1_t *b;
int8_t *buf;
- int max_buf;
+ int max_buf, c, no12 = 0;
+ while ((c = getopt(argc, argv, "n")) > 0)
+ if (c == 'n') no12 = 1;
if (argc == 1) {
fprintf(stderr, "Usage: samtools bam2fq <in.bam>\n");
return 1;
}
- fp = strcmp(argv[1], "-")? bam_open(argv[1], "r") : bam_dopen(fileno(stdin), "r");
+ fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
if (fp == 0) return 1;
h = bam_header_read(fp);
b = bam_init1();
int i, qlen = b->core.l_qseq;
uint8_t *seq;
putchar('@'); fputs(bam1_qname(b), stdout);
- if ((b->core.flag & 0x40) && !(b->core.flag & 0x80)) puts("/1");
- else if ((b->core.flag & 0x80) && !(b->core.flag & 0x40)) puts("/2");
- else putchar('\n');
+ if (no12) putchar('\n');
+ else {
+ if ((b->core.flag & 0x40) && !(b->core.flag & 0x80)) puts("/1");
+ else if ((b->core.flag & 0x80) && !(b->core.flag & 0x40)) puts("/2");
+ else putchar('\n');
+ }
if (max_buf < qlen + 1) {
max_buf = qlen + 1;
kroundup32(max_buf);