]> git.donarmstrong.com Git - samtools.git/commitdiff
Reverted to the previous behaviour, the unmapped reads are not removed. Added -r...
authorPetr Danecek <pd3@sanger.ac.uk>
Thu, 3 May 2012 09:26:18 +0000 (10:26 +0100)
committerPetr Danecek <pd3@sanger.ac.uk>
Thu, 3 May 2012 09:26:18 +0000 (10:26 +0100)
bam_mate.c

index 056c723f21f80a93eb4f60657934d93ea29deed7..cfa2e758a2b550c24b887d992b6a9cbe2ce28b21 100644 (file)
@@ -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 <in.nameSrt.bam> <out.nameSrt.bam>\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 <in.nameSrt.bam> <out.nameSrt.bam>\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;
 }
+
+