]> git.donarmstrong.com Git - samtools.git/blobdiff - padding.c
Load FAI in 'samtools depad' (not used yet)
[samtools.git] / padding.c
index a10c08f315454a6b70d15362bc277e4a54494444..9f8bca7e0fe13992aba557b176090936a34cb34a 100644 (file)
--- a/padding.c
+++ b/padding.c
@@ -5,6 +5,7 @@
 #include "sam_header.h"
 #include "sam.h"
 #include "bam.h"
+#include "faidx.h"
 
 static void replace_cigar(bam1_t *b, int n, uint32_t *cigar)
 {
@@ -68,7 +69,7 @@ static void unpad_seq(bam1_t *b, kstring_t *s)
        assert(length == s->l);
 }
 
-int bam_pad2unpad(samfile_t *in, samfile_t *out)
+int bam_pad2unpad(samfile_t *in, samfile_t *out, faidx_t *fai)
 {
        bam_header_t *h;
        bam1_t *b;
@@ -185,19 +186,21 @@ static int usage(int is_long_help);
 int main_pad2unpad(int argc, char *argv[])
 {
        samfile_t *in = 0, *out = 0;
+       faidx_t *fai;
        int c, is_bamin = 1, compress_level = -1, is_bamout = 1, is_long_help = 0;
-       char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0;
+       char in_mode[5], out_mode[5], *fn_out = 0, *fn_list = 0, *fn_ref = 0;
         int ret=0;
 
        /* parse command-line options */
        strcpy(in_mode, "r"); strcpy(out_mode, "w");
-       while ((c = getopt(argc, argv, "Sso:u1?")) >= 0) {
+       while ((c = getopt(argc, argv, "Sso:u1T:?")) >= 0) {
                switch (c) {
                case 'S': is_bamin = 0; break;
                case 's': assert(compress_level == -1); is_bamout = 0; break;
                case 'o': fn_out = strdup(optarg); break;
                case 'u': assert(is_bamout == 1); compress_level = 0; break;
                case '1': assert(is_bamout == 1); compress_level = 1; break;
+               case 'T': fn_ref = strdup(optarg); is_bamin = 0; break;
                 case '?': is_long_help = 1; break;
                default: return usage(is_long_help);
                }
@@ -213,6 +216,11 @@ int main_pad2unpad(int argc, char *argv[])
                strcat(out_mode, tmp);
        }
 
+       // Load FASTA reference (also needed for SAM -> BAM if missing header)
+       if (fn_ref) {
+               fn_list = samfaipath(fn_ref);
+               fai = fai_load(fn_ref);
+       }
        // open file handlers
        if ((in = samopen(argv[optind], in_mode, fn_list)) == 0) {
                fprintf(stderr, "[depad] fail to open \"%s\" for reading.\n", argv[optind]);
@@ -232,13 +240,14 @@ int main_pad2unpad(int argc, char *argv[])
        }
 
        // Do the depad
-       ret = bam_pad2unpad(in, out);
+       ret = bam_pad2unpad(in, out, fai);
 
 depad_end:
        // close files, free and return
        free(fn_list); free(fn_out);
        samclose(in);
        samclose(out);
+       fai_destroy(fai);
        return ret;
 }
 
@@ -250,16 +259,15 @@ static int usage(int is_long_help)
        fprintf(stderr, "         -S       input is SAM (default is BAM)\n");
        fprintf(stderr, "         -u       uncompressed BAM output (can't use with -s)\n");
        fprintf(stderr, "         -1       fast compression BAM output (can't use with -s)\n");
-        //TODO - These are the arguments I think make sense to support:
-       //fprintf(stderr, "         -@ INT   number of BAM compression threads [0]\n");
-       //fprintf(stderr, "         -T FILE  reference sequence file (force -S) [null]\n");
+       fprintf(stderr, "         -T FILE  reference sequence file [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\
+  1. Requires embedded reference sequences (before the reads for that reference),\n\
+     with the future aim to also support a FASTA padded reference sequence file.\n\
 \n\
   2. The input padded alignment read's CIGAR strings must not use P or I operators.\n\
 \n");