]> git.donarmstrong.com Git - samtools.git/blobdiff - padding.c
Minimal 'samtools depad' command line parsing (-? only so far)
[samtools.git] / padding.c
index b80463fe80cd7222507b081f56990be97a21e32e..9fbdb53b797a6381408c9d32feb12333bfd54db8 100644 (file)
--- a/padding.c
+++ b/padding.c
@@ -1,5 +1,6 @@
 #include <string.h>
 #include <assert.h>
+#include <unistd.h>
 #include "kstring.h"
 #include "bam.h"
 
@@ -30,9 +31,21 @@ static void replace_cigar(bam1_t *b, int n, uint32_t *cigar)
 static void unpad_seq(bam1_t *b, kstring_t *s)
 {
        int k, j, i;
+       int length;
        uint32_t *cigar = bam1_cigar(b);
        uint8_t *seq = bam1_seq(b);
-       ks_resize(s, b->core.l_qseq);
+       // b->core.l_qseq gives length of the SEQ entry (including soft clips, S)
+       // We need the padded length after alignment from the CIGAR (excluding
+       // soft clips S, but including pads from CIGAR D operations)
+       length = 0;
+       for (k = 0; k < b->core.n_cigar; ++k) {
+               int op, ol;
+               op= bam_cigar_op(cigar[k]);
+               ol = bam_cigar_oplen(cigar[k]);
+               if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF || op == BAM_CDEL)
+                       length += ol;
+       }
+       ks_resize(s, length);
        for (k = 0, s->l = 0, j = 0; k < b->core.n_cigar; ++k) {
                int op, ol;
                op = bam_cigar_op(cigar[k]);
@@ -43,13 +56,14 @@ static void unpad_seq(bam1_t *b, kstring_t *s)
                        j += ol;
                } else if (op == BAM_CHARD_CLIP) {
                        /* do nothing */
-               } else if (op == BAM_CDEL || op == BAM_CPAD) {
+               } else if (op == BAM_CDEL) {
                        for (i = 0; i < ol; ++i) s->s[s->l++] = 0;
                 } else {
                        fprintf(stderr, "[depad] ERROR: Didn't expect CIGAR op %c in read %s\n", BAM_CIGAR_STR[op], bam1_qname(b));
                         assert(-1);
                }
        }
+       assert(length == s->l);
 }
 
 int bam_pad2unpad(bamFile in, bamFile out)
@@ -136,7 +150,7 @@ int bam_pad2unpad(bamFile in, bamFile out)
                                        pre_op = bam_cigar_op(cigar2[i-2]);
                                        post_op = bam_cigar_op(cigar2[i]);
                                        /* Note don't need to check for X/= as code above will use M only */
-                                       if ((pre_op == BAM_CMATCH || pre_op == BAM_CDIFF) || (post_op == BAM_CMATCH || post_op == BAM_CDIFF)) {
+                                       if ((pre_op == BAM_CMATCH || pre_op == BAM_CDEL) && (post_op == BAM_CMATCH || post_op == BAM_CDEL)) {
                                                /* This is a redundant P operator */
                                                cigar2[i-1] = 0; // i.e. 0M
                                                /* If had same operator either side, combine them in post_op */
@@ -162,17 +176,52 @@ int bam_pad2unpad(bamFile in, bamFile out)
        return 0;
 }
 
+static int usage(int is_long_help);
+
 int main_pad2unpad(int argc, char *argv[])
 {
        bamFile in, out;
+       int c, is_long_help = 0;
         int result=0;
-       if (argc == 1) {
-               fprintf(stderr, "Usage: samtools depad <in.bam>\n");
-               return 1;
-       }
+
+       /* parse command-line options */
+       while ((c = getopt(argc, argv, "?")) >= 0) {
+               switch (c) {
+               case '?': is_long_help = 1; break;
+               default: return usage(is_long_help);
+               }
+        }
+       if (argc == optind) return usage(is_long_help);
+
        in = strcmp(argv[1], "-")? bam_open(argv[1], "r") : bam_dopen(fileno(stdin), "r");
        out = bam_dopen(fileno(stdout), "w");
        result = bam_pad2unpad(in, out);
        bam_close(in); bam_close(out);
        return result;
 }
+
+static int usage(int is_long_help)
+{
+       fprintf(stderr, "\n");
+       fprintf(stderr, "Usage:   samtools depad <in.bam>\n\n");
+       fprintf(stderr, "Options: -?       longer help\n");
+       //TODO - These are the arguments I think make sense to support:
+       //fprintf(stderr, "Usage:   samtools depad [options] <in.bam>|<in.sam>\n\n");
+       //fprintf(stderr, "Options: -b       output BAM\n");
+       //fprintf(stderr, "         -S       input is SAM\n");
+       //fprintf(stderr, "         -u       uncompressed BAM output (force -b)\n");
+       //fprintf(stderr, "         -1       fast compression (force -b)\n");
+       //fprintf(stderr, "         -@ INT   number of BAM compression threads [0]\n");
+       //fprintf(stderr, "         -T FILE  reference sequence file (force -S) [null]\n");
+       //fprintf(stderr, "         -o FILE  output file name [stdout]\n");
+       //fprintf(stderr, "         -?       longer help\n");
+       fprintf(stderr, "\n");
+       if (is_long_help)
+               fprintf(stderr, "Notes:\n\
+\n\
+  1. Requires embedded reference sequences (before the reads for that reference).\n\
+\n\
+  2. The input padded alignment read's CIGAR strings must not use P or I operators.\n\
+\n");
+        return 1;
+}