Skip to content

Commit

Permalink
Add option to output SAM header when querying BAM files
Browse files Browse the repository at this point in the history
  • Loading branch information
morispi committed Jun 23, 2021
1 parent 8198f56 commit 981d10c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/include/indexManagementBam.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ void saveBarcodesOffsetsIndex(BarcodesOffsetsIndex& index, string file);
*/
BarcodesOffsetsIndex loadBarcodesOffsetsIndex(string file);

#endif
#endif
2 changes: 1 addition & 1 deletion src/include/subcommands/help.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <string>

#define VERSION "LRez v.2.1.1"
#define VERSION "LRez v.2.1.2"

using namespace std;

Expand Down
1 change: 1 addition & 0 deletions src/subcommands/help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void subcommandHelp(std::string subcommand) {
printf("\t-q, --query\t Query barcode to search in the BAM / index\n");
printf("\t-l, --list\t File containing a list of barcodes to search in the BAM / index\n");
printf("\t-o, --output\t File where to output the extracted alignments (optional, default: stdout)\n");
printf("\t-H, --header\t Output SAM header (optional, default: false)\n");
} else if (subcommand == "index fastq") {
printf("%s\n", VERSION);
printf("Pierre Morisse <[email protected]>\n");
Expand Down
15 changes: 13 additions & 2 deletions src/subcommands/queryBam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ void subcommandQueryBam(int argc, char* argv[]) {
string outputFile;
BarcodesOffsetsIndex BarcodesOffsetsIndex;
ofstream out;
bool outputHeader = false;

const struct option longopts[] = {
{"bam", required_argument, 0, 'b'},
{"index", required_argument, 0, 'i'},
{"query", required_argument, 0, 'q'},
{"list", required_argument, 0, 'l'},
{"output", required_argument, 0, 'o'},
{"header", no_argument, 0, 'H'},
{0, 0, 0, 0},
};
int index;
int iarg = 0;

iarg = getopt_long(argc, argv, "b:i:q:l:o:", longopts, &index);
iarg = getopt_long(argc, argv, "b:i:q:l:o:H", longopts, &index);
if (iarg == -1) {
subcommandHelp("query bam");
}
Expand All @@ -45,11 +47,14 @@ void subcommandQueryBam(int argc, char* argv[]) {
case 'o':
outputFile = optarg;
break;
case 'H':
outputHeader = true;
break;
default:
subcommandHelp("query bam");
break;
}
iarg = getopt_long(argc, argv, "b:i:q:l:o:", longopts, &index);
iarg = getopt_long(argc, argv, "b:i:q:l:o:H", longopts, &index);
}

if (bamFile.empty()) {
Expand Down Expand Up @@ -96,11 +101,17 @@ void subcommandQueryBam(int argc, char* argv[]) {
fprintf(stderr, "Unable to open file %s.", outputFile.c_str());
exit(EXIT_FAILURE);
}
if (outputHeader) {
out << reader.GetHeaderText() << endl;
}
for (BamAlignment al : alignments) {
out << convertToSam(al, reader.GetReferenceData()) << endl;
}
out.close();
} else {
if (outputHeader) {
cout << reader.GetHeaderText() << endl;
}
for (BamAlignment al : alignments) {
cout << convertToSam(al, reader.GetReferenceData()) << endl;
}
Expand Down

0 comments on commit 981d10c

Please sign in to comment.