Skip to content

Commit a049c6b

Browse files
authored
Merge pull request #4319 from vgteam/inject-alignment-emitter
Make vg inject use AlignmentEmitter and support GAF
2 parents 1f0e29d + af500c9 commit a049c6b

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/subcommand/inject_main.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "../alignment.hpp"
1515
#include "../vg.hpp"
1616
#include "../xg.hpp"
17+
#include "../hts_alignment_emitter.hpp"
1718
#include <vg/io/stream.hpp>
1819
#include <vg/io/vpkg.hpp>
1920
#include <bdsg/overlays/overlay_helper.hpp>
@@ -23,10 +24,11 @@ using namespace vg;
2324
using namespace vg::subcommand;
2425

2526
void help_inject(char** argv) {
26-
cerr << "usage: " << argv[0] << " inject [options] input.[bam|sam|cram] >output.gam" << endl
27+
cerr << "usage: " << argv[0] << " inject -x graph.xg [options] input.[bam|sam|cram] >output.gam" << endl
2728
<< endl
2829
<< "options:" << endl
2930
<< " -x, --xg-name FILE use this graph or xg index (required, non-XG formats also accepted)" << endl
31+
<< " -o, --output-format NAME output the alignments in NAME format (gam / gaf / json) [gam]" << endl
3032
<< " -t, --threads N number of threads to use" << endl;
3133
}
3234

@@ -37,6 +39,8 @@ int main_inject(int argc, char** argv) {
3739
}
3840

3941
string xg_name;
42+
string output_format = "GAM";
43+
std::set<std::string> output_formats = { "GAM", "GAF", "JSON" };
4044
int threads = get_thread_count();
4145

4246
int c;
@@ -46,12 +50,13 @@ int main_inject(int argc, char** argv) {
4650
{
4751
{"help", no_argument, 0, 'h'},
4852
{"xg-name", required_argument, 0, 'x'},
53+
{"output-format", required_argument, 0, 'o'},
4954
{"threads", required_argument, 0, 't'},
5055
{0, 0, 0, 0}
5156
};
5257

5358
int option_index = 0;
54-
c = getopt_long (argc, argv, "hx:t:",
59+
c = getopt_long (argc, argv, "hx:o:t:",
5560
long_options, &option_index);
5661

5762
// Detect the end of the options.
@@ -63,6 +68,19 @@ int main_inject(int argc, char** argv) {
6368
case 'x':
6469
xg_name = optarg;
6570
break;
71+
72+
case 'o':
73+
{
74+
output_format = optarg;
75+
for (char& c : output_format) {
76+
c = std::toupper(c);
77+
}
78+
if (output_formats.find(output_format) == output_formats.end()) {
79+
std::cerr << "error: [vg inject] Invalid output format: " << optarg << std::endl;
80+
std::exit(1);
81+
}
82+
}
83+
break;
6684

6785
case 't':
6886
threads = parse<int>(optarg);
@@ -90,14 +108,14 @@ int main_inject(int argc, char** argv) {
90108
}
91109
unique_ptr<PathHandleGraph> path_handle_graph = vg::io::VPKG::load_one<PathHandleGraph>(xg_name);
92110
bdsg::PathPositionOverlayHelper overlay_helper;
93-
PathPositionHandleGraph* xgidx = overlay_helper.apply(path_handle_graph.get());
111+
PathPositionHandleGraph* xgidx = overlay_helper.apply(path_handle_graph.get());
94112

95-
vg::io::ProtobufEmitter<Alignment> buf(cout);
96-
function<void(Alignment&)> lambda = [&buf](Alignment& aln) {
97-
#pragma omp critical (buf)
98-
{
99-
buf.write(std::move(aln));
100-
}
113+
// We don't do HTS output formats but we do need an empty paths collection to make an alignment emitter
114+
vector<tuple<path_handle_t, size_t, size_t>> paths;
115+
unique_ptr<vg::io::AlignmentEmitter> alignment_emitter = get_alignment_emitter("-", output_format, paths, threads, xgidx);
116+
117+
function<void(Alignment&)> lambda = [&](Alignment& aln) {
118+
alignment_emitter->emit_mapped_single({std::move(aln)});
101119
};
102120
if (threads > 1) {
103121
hts_for_each_parallel(file_name, lambda, xgidx);

test/t/39_vg_inject.t

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ BASH_TAP_ROOT=../deps/bash-tap
66
PATH=../bin:$PATH # for vg
77

88

9-
plan tests 12
9+
plan tests 13
1010

1111
vg construct -r small/x.fa > j.vg
1212
vg index -x j.xg j.vg
@@ -52,5 +52,7 @@ cat <(samtools view -H small/x.bam) <(printf "name\t4\t*\t0\t0\t*\t*\t0\t0\tACGT
5252
is "$(vg inject -x x.xg unmapped.sam | vg view -aj - | grep "path" | wc -l)" 0 "vg inject does not make an alignment for an umapped read"
5353
is "$(echo $?)" 0 "vg inject does not crash on an unmapped read"
5454

55+
is $(vg inject -x x.xg small/x.bam -o GAF | wc -l) \
56+
1000 "vg inject supports GAF output"
5557

5658
rm j.vg j.xg x.vg x.gcsa x.gcsa.lcp x.xg unmapped.sam

0 commit comments

Comments
 (0)