diff --git a/.test_suite.sh b/.test_suite.sh index 3e81dea6..6ee2cf6a 100755 --- a/.test_suite.sh +++ b/.test_suite.sh @@ -57,4 +57,14 @@ testCramView() { assertEquals 0 $? } +testIndexUsage() { + rm *.bai && rm c1_* + ./build/sambamba view -S htslib/test/c1\#pad2.sam -T htslib/test/c1.fa -f cram -o c1_pad2.cram && + ./build/sambamba index -C c1_pad2.cram && test -e c1_pad2.cram.crai + ./build/sambamba index -C c1_pad2.cram c1_cram_index && test -e c1_cram_index.crai + ./build/sambamba index ex1_header.sorted.bam && test -e ex1_header.sorted.bam.bai + ./build/sambamba index ex1_header.sorted.bam ex1_header.sorted.bai && test -e ex1_header.sorted.bai + assertEquals 0 $? +} + . shunit2-2.0.3/src/shell/shunit2 diff --git a/cram/reader.d b/cram/reader.d index 8e035df8..3b47a612 100644 --- a/cram/reader.d +++ b/cram/reader.d @@ -113,8 +113,8 @@ class CramReader : IBamSamReader { return inputRangeObject(reads()); } - void createIndex() { - int ret = cram_index_build(_fd, toStringz(_fn)); + void createIndex(string fn_prefix=null) { + int ret = cram_index_build(_fd, toStringz(fn_prefix is null ? _fn : fn_prefix)); if (ret != 0) { throw new Exception("failed to build index for CRAM file " ~ _fn); } diff --git a/sambamba/index.d b/sambamba/index.d index 080ff851..bfc29657 100644 --- a/sambamba/index.d +++ b/sambamba/index.d @@ -1,6 +1,6 @@ /* This file is part of Sambamba. - Copyright (C) 2012-2013 Artem Tarasov + Copyright (C) 2012-2015 Artem Tarasov Sambamba is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -32,7 +32,7 @@ import bio.bam.bai.indexing; import bio.bam.reader; void printUsage() { - stderr.writeln("Usage: sambamba-index [OPTIONS] "); + stderr.writeln("Usage: sambamba-index [OPTIONS] [output_file]"); stderr.writeln(); stderr.writeln("\tCreates index for a BAM or CRAM file"); stderr.writeln(); @@ -58,6 +58,7 @@ int index_main(string[] args) { bool check_bins; uint n_threads = totalCPUs; bool is_cram; + string out_filename = null; getopt(args, std.getopt.config.caseSensitive, @@ -67,14 +68,16 @@ int index_main(string[] args) { "cram-input|C", &is_cram); try { - string out_filename = null; - if (args.length != 2) { + if (args.length < 2 || args.length > 3) { printUsage(); return 0; } if (!is_cram) { - out_filename = args[1] ~ ".bai"; + if (args.length > 2) + out_filename = args[2]; + else + out_filename = args[1] ~ ".bai"; // default taskPool uses only totalCPUs-1 threads, // but in case of indexing the most time is spent @@ -103,7 +106,7 @@ int index_main(string[] args) { stderr.writeln("[info] progressbar is unavailable for CRAM input"); defaultPoolThreads = 0; // decompression not needed for CRAM auto cram = new CramReader(args[1], taskPool); - cram.createIndex(); + cram.createIndex(args[$-1]); } } catch (Throwable e) { stderr.writeln("sambamba-index: ", e.msg);