Skip to content

Commit

Permalink
[index] optional output filename; closes #138
Browse files Browse the repository at this point in the history
  • Loading branch information
lomereiter committed May 16, 2015
1 parent 511fe52 commit eafcf81
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
10 changes: 10 additions & 0 deletions .test_suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions cram/reader.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
15 changes: 9 additions & 6 deletions sambamba/index.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This file is part of Sambamba.
Copyright (C) 2012-2013 Artem Tarasov <lomereiter@gmail.com>
Copyright (C) 2012-2015 Artem Tarasov <lomereiter@gmail.com>
Sambamba is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -32,7 +32,7 @@ import bio.bam.bai.indexing;
import bio.bam.reader;

void printUsage() {
stderr.writeln("Usage: sambamba-index [OPTIONS] <input.bam|input.cram>");
stderr.writeln("Usage: sambamba-index [OPTIONS] <input.bam|input.cram> [output_file]");
stderr.writeln();
stderr.writeln("\tCreates index for a BAM or CRAM file");
stderr.writeln();
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit eafcf81

Please sign in to comment.