diff --git a/src/alignment.cpp b/src/alignment.cpp index b10e117b7f..185346632c 100644 --- a/src/alignment.cpp +++ b/src/alignment.cpp @@ -1798,7 +1798,7 @@ void normalize_alignment(Alignment& alignment) { if (doing_normalization) { // we found things we needed to normalize away, so we must have built the normalized // path, now replace the original with it - *alignment.mutable_path() = move(normalized); + *alignment.mutable_path() = std::move(normalized); } } @@ -1902,6 +1902,15 @@ void parse_bed_regions(istream& bedstream, const PathPositionHandleGraph* graph, vector* out_alignments) { out_alignments->clear(); + parse_bed_regions(bedstream, graph, [&](Alignment& aln) { + out_alignments->emplace_back(std::move(aln)); + }); +} + +void parse_bed_regions(istream& bedstream, + const PathPositionHandleGraph* graph, + const std::function& callback) { + if (!bedstream) { cerr << "Unable to open bed file." << endl; return; @@ -2147,8 +2156,7 @@ void parse_bed_regions(istream& bedstream, // Make the Alignment Alignment alignment = target_alignment(graph, path_handle, sbuf, ebuf, name, is_reverse); alignment.set_score(score); - - out_alignments->push_back(alignment); + callback(alignment); // if more subpaths need to be written, write them now while (!other_seqs.empty()){ @@ -2163,10 +2171,8 @@ void parse_bed_regions(istream& bedstream, path_handle = graph->get_path_handle(seq); alignment = target_alignment(graph, path_handle, sbuf, ebuf, name, is_reverse); alignment.set_score(score); - out_alignments->push_back(alignment); + callback(alignment); } - - vg::io::write_buffered(cout, *out_alignments, 1000); } } @@ -2174,6 +2180,16 @@ void parse_gff_regions(istream& gffstream, const PathPositionHandleGraph* graph, vector* out_alignments) { out_alignments->clear(); + parse_gff_regions(gffstream, graph, [&](Alignment& aln) { + out_alignments->emplace_back(std::move(aln)); + }); +} + +void parse_gff_regions(istream& gffstream, + const PathPositionHandleGraph* graph, + const std::function& callback) { + + if (!gffstream) { cerr << "Unable to open gff3/gtf file." << endl; return; @@ -2431,8 +2447,7 @@ void parse_gff_regions(istream& gffstream, } Alignment alignment = target_alignment(graph, graph->get_path_handle(seq), sbuf, ebuf, name, is_reverse); - - out_alignments->push_back(alignment); + callback(alignment); // if more subpaths need to be written, write them now while (!other_seqs.empty()){ @@ -2445,10 +2460,8 @@ void parse_gff_regions(istream& gffstream, other_ends.pop_back(); // get alignment alignment = target_alignment(graph, graph->get_path_handle(seq), sbuf, ebuf, name, is_reverse); - out_alignments->push_back(alignment); + callback(alignment); } - - vg::io::write_buffered(cout, *out_alignments, 1000); } } diff --git a/src/alignment.hpp b/src/alignment.hpp index a2a15a2597..2254b86246 100644 --- a/src/alignment.hpp +++ b/src/alignment.hpp @@ -296,10 +296,19 @@ void normalize_alignment(Alignment& alignment); // quality information; a kind of poor man's pileup map alignment_quality_per_node(const Alignment& aln); -/// Parse regions from the given BED file into Alignments in a vector. +/// Parse regions from the given BED file and call the given callback with each. +/// Does *not* write them to standard output. /// Reads the optional name, is_reverse, and score fields if present, and populates the relevant Alignment fields. /// Skips and warns about malformed or illegal BED records. +void parse_bed_regions(istream& bedstream, const PathPositionHandleGraph* graph, const std::function& callback); +/// Parse regions from the given GFF file and call the given callback with each. +/// Does *not* write them to standard output. +void parse_gff_regions(istream& gtfstream, const PathPositionHandleGraph* graph, const std::function& callback); +/// Parse regions from the given BED file into the given vector. +/// Does *not* write them to standard output. void parse_bed_regions(istream& bedstream, const PathPositionHandleGraph* graph, vector* out_alignments); +/// Parse regions from the given GFF file into the given vector. +/// Does *not* write them to standard output. void parse_gff_regions(istream& gtfstream, const PathPositionHandleGraph* graph, vector* out_alignments); Position alignment_start(const Alignment& aln); diff --git a/src/subcommand/annotate_main.cpp b/src/subcommand/annotate_main.cpp index df9e5331fb..3da20e1d86 100644 --- a/src/subcommand/annotate_main.cpp +++ b/src/subcommand/annotate_main.cpp @@ -479,24 +479,31 @@ int main_annotate(int argc, char** argv) { } } else { + // We are converting annotations to GAM. + // Set up GAM output. + // TODO: use AlignmentEmitter? + vector buffer; + auto emit_alignment = [&](Alignment& aln) { + // Buffer and possibly write each record + buffer.emplace_back(std::move(aln)); + vg::io::write_buffered(cout, buffer, 1000); + }; + for (auto& bed_name : bed_names) { // Convert each BED file to GAM get_input_file(bed_name, [&](istream& bed_stream) { - vector buffer; - parse_bed_regions(bed_stream, xg_index, &buffer); - vg::io::write_buffered(cout, buffer, 0); // flush + parse_bed_regions(bed_stream, xg_index, emit_alignment); }); - - // TODO: We'll get an EOF marker per input file. } for (auto& gff_name : gff_names) { get_input_file(gff_name, [&](istream& gff_stream) { - vector buffer; - parse_gff_regions(gff_stream, xg_index, &buffer); - vg::io::write_buffered(cout, buffer, 0); // flush + parse_gff_regions(gff_stream, xg_index, emit_alignment); }); } + + // Flush the remaining stuff in the buffer + vg::io::write_buffered(cout, buffer, 0); } }