From 8c3ff0ee312267f15f0d1645025d3a75476b1d9d Mon Sep 17 00:00:00 2001 From: Petr Danecek Date: Thu, 3 May 2012 10:26:18 +0100 Subject: [PATCH] Reverted to the previous behaviour, the unmapped reads are not removed. Added -r switch for backward compatibility. --- bam_mate.c | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/bam_mate.c b/bam_mate.c index 056c723..cfa2e75 100644 --- a/bam_mate.c +++ b/bam_mate.c @@ -30,7 +30,7 @@ void bam_template_cigar(bam1_t *b1, bam1_t *b2, kstring_t *str) } // currently, this function ONLY works if each read has one hit -void bam_mating_core(bamFile in, bamFile out) +void bam_mating_core(bamFile in, bamFile out, int remove_reads) { bam_header_t *header; bam1_t *b[2]; @@ -46,10 +46,18 @@ void bam_mating_core(bamFile in, bamFile out) curr = 0; has_prev = 0; while (bam_read1(in, b[curr]) >= 0) { bam1_t *cur = b[curr], *pre = b[1-curr]; - if (cur->core.tid < 0) continue; + if (cur->core.tid < 0) + { + if ( !remove_reads ) bam_write1(out, cur); + continue; + } cur_end = bam_calend(&cur->core, bam1_cigar(cur)); if (cur_end > (int)header->target_len[cur->core.tid]) cur->core.flag |= BAM_FUNMAP; - if (cur->core.flag & BAM_FSECONDARY) continue; // skip secondary alignments + if (cur->core.flag & BAM_FSECONDARY) + { + if ( !remove_reads ) bam_write1(out, cur); + continue; // skip secondary alignments + } if (has_prev) { if (strcmp(bam1_qname(cur), bam1_qname(pre)) == 0) { // identical pair name cur->core.mtid = pre->core.tid; cur->core.mpos = pre->core.pos; @@ -91,16 +99,29 @@ void bam_mating_core(bamFile in, bamFile out) free(str.s); } +void usage() +{ + fprintf(stderr,"Usage: samtools fixmate \n"); + fprintf(stderr,"Options:\n"); + fprintf(stderr," -r remove unmapped reads and secondary alignments\n"); + exit(1); +} + int bam_mating(int argc, char *argv[]) { bamFile in, out; - if (argc < 3) { - fprintf(stderr, "Usage: samtools fixmate \n"); - return 1; - } - in = (strcmp(argv[1], "-") == 0)? bam_dopen(fileno(stdin), "r") : bam_open(argv[1], "r"); - out = (strcmp(argv[2], "-") == 0)? bam_dopen(fileno(stdout), "w") : bam_open(argv[2], "w"); - bam_mating_core(in, out); + int c, remove_reads=0; + while ((c = getopt(argc, argv, "r")) >= 0) { + switch (c) { + case 'r': remove_reads=1; break; + } + } + if (optind+1 >= argc) usage(); + in = (strcmp(argv[optind], "-") == 0)? bam_dopen(fileno(stdin), "r") : bam_open(argv[optind], "r"); + out = (strcmp(argv[optind+1], "-") == 0)? bam_dopen(fileno(stdout), "w") : bam_open(argv[optind+1], "w"); + bam_mating_core(in, out, remove_reads); bam_close(in); bam_close(out); return 0; } + + -- 2.39.2