SeqAn3
The Modern C++ library for sequence analysis.
Implementing your own read mapper with SeqAn

Learning Objective:
In this tutorial you will learn how to combine the components of previous tutorials to create your very first SeqAn application: a read mapper!

DifficultyHigh
Duration90 Minutes
Prerequisite tutorialsAll
Recommended reading

Introduction

Read mapping is a common task in bioinformatics and is often the first step of an in-depth analysis of Next Generation Sequencing data. Its aim is to identify positions where a query sequence (read) matches with up to e errors to a reference sequence.

In this example we will implement a read mapper step by step and make use of what we have learned in the previous tutorials. As it is common practice with read mappers, we will first create an indexer that creates an index from the reference and stores it to disk. After this, we will implement the actual read mapper that will use the stored index and map the reads.

Agenda

  • Implementing an indexer
    • Parse arguments
    • Read input files
    • Create and store index
  • Implementing a read mapper
    • Parse arguments
    • Read and load input, search for approximate matches
    • Align the search results
    • Write final results into a SAM file

The data

We provide an example reference and an example query file.

The indexer

Step 1 - Parsing arguments

As a first step, we want to parse command line arguments for our indexer. If you get into trouble, you can take a peek at the Argument Parser Tutorial or the API documentation of the seqan3::argument_parser for help.

Parsing arguments

Let's start our application by setting up the argument parser with the following options:

  • The path to the reference file
  • An output path for the index

Follow the best practice and create:

  • A function run_program that prints the parsed arguments
  • A struct cmd_arguments that stores the arguments
  • A function initialise_argument_parser to add meta data and options to the parser
  • A main function that parses the arguments and calls run_program

Use validators where applicable!

Your main may look like this:

Hint

int main(int argc, char const ** argv)
{
argument_parser parser("Indexer", argc, argv);
cmd_arguments args{};
initialise_argument_parser(parser, args);
try
{
parser.parse();
}
catch (parser_invalid_argument const & ext)
{
std::cerr << "[PARSER ERROR] " << ext.what() << '\n';
return -1;
}
run_program(args.reference_path, args.index_path);
return 0;
}

Solution

using namespace seqan3;
void run_program(std::filesystem::path const & reference_path,
std::filesystem::path const & index_path)
{
debug_stream << "reference_file_path: " << reference_path << '\n';
debug_stream << "index_path " << index_path << '\n';
}
struct cmd_arguments
{
std::filesystem::path reference_path{};
std::filesystem::path index_path{"out.index"};
};
void initialise_argument_parser(argument_parser & parser, cmd_arguments & args)
{
parser.info.author = "E. coli";
parser.info.short_description = "Creates an index over a reference.";
parser.info.version = "1.0.0";
parser.add_option(args.reference_path, 'r', "reference", "The path to the reference.", option_spec::REQUIRED,
input_file_validator{{"fa","fasta"}});
parser.add_option(args.index_path, 'o', "output", "The output index file path.", option_spec::DEFAULT,
output_file_validator{{"index"}});
}
int main(int argc, char const ** argv)
{
argument_parser parser("Indexer", argc, argv);
cmd_arguments args{};
initialise_argument_parser(parser, args);
try
{
parser.parse();
}
catch (parser_invalid_argument const & ext)
{
std::cerr << "[PARSER ERROR] " << ext.what() << '\n';
return -1;
}
run_program(args.reference_path, args.index_path);
return 0;
}

Step 2 - Reading the input

As a next step, we want to use the parsed file name to read in our reference data. This will be done using seqan3::sequence_file_input class. As a guide, you can take a look at the Sequence I/O Tutorial.

Reading the input

Extend your program to store the sequence information contained in the reference file into a struct.

To do this, you should create:

  • A struct reference_storage_t that stores the sequence information for both reference and query information within member variables
  • A function read_reference that fills a reference_storage_t object with information from the files and prints the reference IDs

You should also perform the following changes in run_program:

  • Construct of an object storage of type reference_storage_t
  • Add a call to read_reference

This is the signature of read_reference:

Hint

void read_reference(std::filesystem::path const & reference_path,
reference_storage_t & storage)

This is the reference_storage_t:

Hint

struct reference_storage_t
{
};

Solution

struct reference_storage_t
{
};
void read_reference(std::filesystem::path const & reference_path,
reference_storage_t & storage)
{
sequence_file_input reference_in{reference_path};
for (auto & [seq, id, qual] : reference_in)
{
storage.ids.push_back(std::move(id));
storage.seqs.push_back(std::move(seq));
}
debug_stream << "Reference IDs: " << storage.ids << '\n';
}
void run_program(std::filesystem::path const & reference_path,
std::filesystem::path const & index_path)
{
debug_stream << "reference_file_path: " << reference_path << '\n';
debug_stream << "index_path " << index_path << '\n';
reference_storage_t storage{};
read_reference(reference_path, storage);
}

Here is the complete program:

Hint

using namespace seqan3;
struct reference_storage_t
{
};
void read_reference(std::filesystem::path const & reference_path,
reference_storage_t & storage)
{
sequence_file_input reference_in{reference_path};
for (auto & [seq, id, qual] : reference_in)
{
storage.ids.push_back(std::move(id));
storage.seqs.push_back(std::move(seq));
}
debug_stream << "Reference IDs: " << storage.ids << '\n';
}
void run_program(std::filesystem::path const & reference_path,
std::filesystem::path const & index_path)
{
debug_stream << "reference_file_path: " << reference_path << '\n';
debug_stream << "index_path " << index_path << '\n';
reference_storage_t storage{};
read_reference(reference_path, storage);
}
struct cmd_arguments
{
std::filesystem::path reference_path{};
std::filesystem::path index_path{"out.index"};
};
void initialise_argument_parser(argument_parser & parser, cmd_arguments & args)
{
parser.info.author = "E. coli";
parser.info.short_description = "Creates an index over a reference.";
parser.info.version = "1.0.0";
parser.add_option(args.reference_path, 'r', "reference", "The path to the reference.", option_spec::REQUIRED,
input_file_validator{{"fa","fasta"}});
parser.add_option(args.index_path, 'o', "output", "The output index file path.", option_spec::DEFAULT,
output_file_validator{{"index"}});
}
int main(int argc, char const ** argv)
{
argument_parser parser("Indexer", argc, argv);
cmd_arguments args{};
initialise_argument_parser(parser, args);
try
{
parser.parse();
}
catch (parser_invalid_argument const & ext)
{
std::cerr << "[PARSER ERROR] " << ext.what() << '\n';
return -1;
}
run_program(args.reference_path, args.index_path);
return 0;
}

Step 3 - Index

Now that we have the necessary sequence information, we can create an index and store it. Read up on the Index Tutorial if you have any questions.

Index

We want to create a new function create_index:

  • It takes index_path and storage as parameters
  • Creates a bi_fm_index
  • Stores the bi_fm_index

We also need to change:

  • run_program to now also call create_index
  • run_program and read_reference to not print the debug output anymore

This is the signature of create_index:

Hint

void create_index(std::filesystem::path const & index_path,
reference_storage_t & storage)

Solution

void read_reference(std::filesystem::path const & reference_path,
reference_storage_t & storage)
{
sequence_file_input reference_in{reference_path};
for (auto & [seq, id, qual] : reference_in)
{
storage.ids.push_back(std::move(id));
storage.seqs.push_back(std::move(seq));
}
}
void create_index(std::filesystem::path const & index_path,
reference_storage_t & storage)
{
bi_fm_index index{storage.seqs};
{
std::ofstream os{index_path, std::ios::binary};
cereal::BinaryOutputArchive oarchive{os};
oarchive(index);
}
}
void run_program(std::filesystem::path const & reference_path,
std::filesystem::path const & index_path)
{
reference_storage_t storage{};
read_reference(reference_path, storage);
create_index(index_path, storage);
}

Here is the complete program:

Hint

#include <fstream>
#include <cereal/archives/binary.hpp>
using namespace seqan3;
struct reference_storage_t
{
};
void read_reference(std::filesystem::path const & reference_path,
reference_storage_t & storage)
{
sequence_file_input reference_in{reference_path};
for (auto & [seq, id, qual] : reference_in)
{
storage.ids.push_back(std::move(id));
storage.seqs.push_back(std::move(seq));
}
}
void create_index(std::filesystem::path const & index_path,
reference_storage_t & storage)
{
bi_fm_index index{storage.seqs};
{
std::ofstream os{index_path, std::ios::binary};
cereal::BinaryOutputArchive oarchive{os};
oarchive(index);
}
}
void run_program(std::filesystem::path const & reference_path,
std::filesystem::path const & index_path)
{
reference_storage_t storage{};
read_reference(reference_path, storage);
create_index(index_path, storage);
}
struct cmd_arguments
{
std::filesystem::path reference_path{};
std::filesystem::path index_path{"out.index"};
};
void initialise_argument_parser(argument_parser & parser, cmd_arguments & args)
{
parser.info.author = "E. coli";
parser.info.short_description = "Creates an index over a reference.";
parser.info.version = "1.0.0";
parser.add_option(args.reference_path, 'r', "reference", "The path to the reference.", option_spec::REQUIRED,
input_file_validator{{"fa","fasta"}});
parser.add_option(args.index_path, 'o', "output", "The output index file path.", option_spec::DEFAULT,
output_file_validator{{"index"}});
}
int main(int argc, char const ** argv)
{
argument_parser parser("Indexer", argc, argv);
cmd_arguments args{};
initialise_argument_parser(parser, args);
try
{
parser.parse();
}
catch (parser_invalid_argument const & ext)
{
std::cerr << "[PARSER ERROR] " << ext.what() << '\n';
return -1;
}
run_program(args.reference_path, args.index_path);
return 0;
}

The read mapper

Step 1 - Parsing arguments

Again, we want to parse command line arguments for our read mapper as a first step. If you get into trouble, you can take a peek at the Argumet Parser Tutorial or the API documentation of the seqan3::argument_parser for help.

Parsing arguments

Let's start our application by setting up the argument parser with the following options:

  • The path to the reference file
  • The path to the query file
  • The path to the index file
  • An output path
  • The maximum number of errors we want to allow (between 0 and 4)

Follow the best practice and create:

  • A function run_program that prints the parsed arguments
  • A struct cmd_arguments that stores the arguments
  • A function initialise_argument_parser to add meta data and options to the parser
  • A main function that parses the arguments and calls run_program

Use validators where applicable!

Your main may look like this:

Hint

int main(int argc, char const ** argv)
{
argument_parser parser("Mapper", argc, argv);
cmd_arguments args{};
initialise_argument_parser(parser, args);
try
{
parser.parse();
}
catch (parser_invalid_argument const & ext)
{
std::cerr << "[PARSER ERROR] " << ext.what() << '\n';
return -1;
}
run_program(args.reference_path, args.query_path, args.index_path, args.sam_path, args.errors);
return 0;
}

Solution

using namespace seqan3;
void run_program(std::filesystem::path const & reference_path,
std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
uint8_t const errors)
{
debug_stream << "reference_path: " << reference_path << '\n';
debug_stream << "query_path: " << query_path << '\n';
debug_stream << "index_path " << index_path << '\n';
debug_stream << "sam_path " << sam_path << '\n';
debug_stream << "errors: " << errors << '\n';
}
struct cmd_arguments
{
std::filesystem::path reference_path{};
std::filesystem::path query_path{};
std::filesystem::path index_path{};
std::filesystem::path sam_path{"out.sam"};
uint8_t errors{0};
};
void initialise_argument_parser(argument_parser & parser, cmd_arguments & args)
{
parser.info.author = "E. coli";
parser.info.short_description = "Map reads against a reference.";
parser.info.version = "1.0.0";
parser.add_option(args.reference_path, 'r', "reference", "The path to the reference.", option_spec::REQUIRED,
input_file_validator{{"fa","fasta"}});
parser.add_option(args.query_path, 'q', "query", "The path to the query.", option_spec::REQUIRED,
input_file_validator{{"fq","fastq"}});
parser.add_option(args.index_path, 'i', "index", "The path to the index.", option_spec::REQUIRED,
input_file_validator{{"index"}});
parser.add_option(args.sam_path, 'o', "output", "The output SAM file path.", option_spec::DEFAULT,
parser.add_option(args.errors, 'e', "error", "Maximum allowed errors.", option_spec::DEFAULT,
}
int main(int argc, char const ** argv)
{
argument_parser parser("Mapper", argc, argv);
cmd_arguments args{};
initialise_argument_parser(parser, args);
try
{
parser.parse();
}
catch (parser_invalid_argument const & ext)
{
std::cerr << "[PARSER ERROR] " << ext.what() << '\n';
return -1;
}
run_program(args.reference_path, args.query_path, args.index_path, args.sam_path, args.errors);
return 0;
}

Step 2 - Reading the input and searching

We also want to read the reference in the read mapper. This is done the same way as for the indexer. We can now load the index and conduct a search. Read up on the Search Tutorial if you have any questions.

Reading the input

Extend your program to read the reference file the same way the indexer does. After this you can load the index and print results of a search.

To do this, you should:

  • Carry over the read_reference function and the reference_storage_t struct from the indexer
  • Create a function map_reads that loads the index and prints the results of the search (allowing all error types) for the first 20 queries

You should also perform the following changes in run_program:

  • Remove the debug output
  • Construct an object storage of type reference_storage_t
  • Add a call to read_reference and map_reads

This is the signature of map_reads:

Hint

void map_reads(std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
reference_storage_t & storage,
uint8_t const errors)

Solution

struct reference_storage_t
{
};
void read_reference(std::filesystem::path const & reference_path,
reference_storage_t & storage)
{
sequence_file_input reference_in{reference_path};
for (auto & [seq, id, qual] : reference_in)
{
storage.ids.push_back(std::move(id));
storage.seqs.push_back(std::move(seq));
}
}
void map_reads(std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
reference_storage_t & storage,
uint8_t const errors)
{
bi_fm_index<dna5, text_layout::collection> index; // we need the alphabet and text layout before loading
{
std::ifstream is{index_path, std::ios::binary};
cereal::BinaryInputArchive iarchive{is};
iarchive(index);
}
sequence_file_input query_in{query_path};
configuration const search_config = search_cfg::max_error{search_cfg::total{errors}} |
search_cfg::mode{search_cfg::all_best};
for (auto & [query, id, qual] : query_in | views::take(20))
{
auto positions = search(query, index, search_config);
debug_stream << "id: " << id << '\n';
debug_stream << "positions: " << positions << '\n';
debug_stream << "======================" << '\n';
}
(void) sam_path; // prevent unused parameter warning
(void) storage; // prevent unused parameter warning
}
void run_program(std::filesystem::path const & reference_path,
std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
uint8_t const errors)
{
reference_storage_t storage{};
read_reference(reference_path, storage);
map_reads(query_path, index_path, sam_path, storage, errors);
}

Here is the complete program:

Hint

#include <fstream>
#include <cereal/archives/binary.hpp>
using namespace seqan3;
struct reference_storage_t
{
};
void read_reference(std::filesystem::path const & reference_path,
reference_storage_t & storage)
{
sequence_file_input reference_in{reference_path};
for (auto & [seq, id, qual] : reference_in)
{
storage.ids.push_back(std::move(id));
storage.seqs.push_back(std::move(seq));
}
}
void map_reads(std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
reference_storage_t & storage,
uint8_t const errors)
{
bi_fm_index<dna5, text_layout::collection> index; // we need the alphabet and text layout before loading
{
std::ifstream is{index_path, std::ios::binary};
cereal::BinaryInputArchive iarchive{is};
iarchive(index);
}
sequence_file_input query_in{query_path};
configuration const search_config = search_cfg::max_error{search_cfg::total{errors}} |
for (auto & [query, id, qual] : query_in | views::take(20))
{
auto positions = search(query, index, search_config);
debug_stream << "id: " << id << '\n';
debug_stream << "positions: " << positions << '\n';
debug_stream << "======================" << '\n';
}
(void) sam_path; // prevent unused parameter warning
(void) storage; // prevent unused parameter warning
}
void run_program(std::filesystem::path const & reference_path,
std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
uint8_t const errors)
{
reference_storage_t storage{};
read_reference(reference_path, storage);
map_reads(query_path, index_path, sam_path, storage, errors);
}
struct cmd_arguments
{
std::filesystem::path reference_path{};
std::filesystem::path query_path{};
std::filesystem::path index_path{};
std::filesystem::path sam_path{"out.sam"};
uint8_t errors{0};
};
void initialise_argument_parser(argument_parser & parser, cmd_arguments & args)
{
parser.info.author = "E. coli";
parser.info.short_description = "Map reads against a reference.";
parser.info.version = "1.0.0";
parser.add_option(args.reference_path, 'r', "reference", "The path to the reference.", option_spec::REQUIRED,
input_file_validator{{"fa","fasta"}});
parser.add_option(args.query_path, 'q', "query", "The path to the query.", option_spec::REQUIRED,
input_file_validator{{"fq","fastq"}});
parser.add_option(args.index_path, 'i', "index", "The path to the index.", option_spec::REQUIRED,
input_file_validator{{"index"}});
parser.add_option(args.sam_path, 'o', "output", "The output SAM file path.", option_spec::DEFAULT,
parser.add_option(args.errors, 'e', "error", "Maximum allowed errors.", option_spec::DEFAULT,
}
int main(int argc, char const ** argv)
{
argument_parser parser("Mapper", argc, argv);
cmd_arguments args{};
initialise_argument_parser(parser, args);
try
{
parser.parse();
}
catch (parser_invalid_argument const & ext)
{
std::cerr << "[PARSER ERROR] " << ext.what() << '\n';
return -1;
}
run_program(args.reference_path, args.query_path, args.index_path, args.sam_path, args.errors);
return 0;
}

Step 3 - Alignment

We can now use the obtained positions to align each query against the reference. Refer to the Alignment Tutorial if you have any questions.

Alignment

We want to extend map_reads to:

  • Use the output of the search to align the query against the reference
  • Print the query ID, alignment score, subrange of the reference sequence and the query (for the first 20 queries)

This is the alignment config:

Hint

configuration const align_config = align_cfg::edit |
align_cfg::aligned_ends{free_ends_first} |
align_cfg::result{with_alignment};

Solution

void map_reads(std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
reference_storage_t & storage,
uint8_t const errors)
{
bi_fm_index<dna5, text_layout::collection> index; // we need the alphabet and text layout before loading
{
std::ifstream is{index_path, std::ios::binary};
cereal::BinaryInputArchive iarchive{is};
iarchive(index);
}
sequence_file_input query_in{query_path};
configuration const search_config = search_cfg::max_error{search_cfg::total{errors}} |
search_cfg::mode{search_cfg::all_best};
configuration const align_config = align_cfg::edit |
align_cfg::aligned_ends{free_ends_first} |
align_cfg::result{with_alignment};
for (auto & [query, id, qual] : query_in | views::take(20))
{
auto positions = search(query, index, search_config);
for (auto & [idx, pos] : positions)
{
size_t start = pos ? pos - 1 : 0;
std::span text_view{std::data(storage.seqs[idx]) + start, query.size() + 1};
for (auto && alignment : align_pairwise(std::tie(text_view, query), align_config))
{
auto && [aligned_database, aligned_query] = alignment.alignment();
debug_stream << "id: " << id << '\n';
debug_stream << "score: " << alignment.score() << '\n';
debug_stream << "database: " << aligned_database << '\n';
debug_stream << "query: " << aligned_query << '\n';
debug_stream << "=============\n";
}
}
}
(void) sam_path; // prevent unused parameter warning
}

Here is the complete program:

Hint

#include <fstream>
#include <cereal/archives/binary.hpp>
#include <seqan3/std/span>
using namespace seqan3;
struct reference_storage_t
{
};
void read_reference(std::filesystem::path const & reference_path,
reference_storage_t & storage)
{
sequence_file_input reference_in{reference_path};
for (auto & [seq, id, qual] : reference_in)
{
storage.ids.push_back(std::move(id));
storage.seqs.push_back(std::move(seq));
}
}
void map_reads(std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
reference_storage_t & storage,
uint8_t const errors)
{
bi_fm_index<dna5, text_layout::collection> index; // we need the alphabet and text layout before loading
{
std::ifstream is{index_path, std::ios::binary};
cereal::BinaryInputArchive iarchive{is};
iarchive(index);
}
sequence_file_input query_in{query_path};
configuration const search_config = search_cfg::max_error{search_cfg::total{errors}} |
configuration const align_config = align_cfg::edit |
align_cfg::aligned_ends{free_ends_first} |
align_cfg::result{with_alignment};
for (auto & [query, id, qual] : query_in | views::take(20))
{
auto positions = search(query, index, search_config);
for (auto & [idx, pos] : positions)
{
size_t start = pos ? pos - 1 : 0;
std::span text_view{std::data(storage.seqs[idx]) + start, query.size() + 1};
for (auto && alignment : align_pairwise(std::tie(text_view, query), align_config))
{
auto && [aligned_database, aligned_query] = alignment.alignment();
debug_stream << "id: " << id << '\n';
debug_stream << "score: " << alignment.score() << '\n';
debug_stream << "database: " << aligned_database << '\n';
debug_stream << "query: " << aligned_query << '\n';
debug_stream << "=============\n";
}
}
}
(void) sam_path; // prevent unused parameter warning
}
void run_program(std::filesystem::path const & reference_path,
std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
uint8_t const errors)
{
reference_storage_t storage{};
read_reference(reference_path, storage);
map_reads(query_path, index_path, sam_path, storage, errors);
}
struct cmd_arguments
{
std::filesystem::path reference_path{};
std::filesystem::path query_path{};
std::filesystem::path index_path{};
std::filesystem::path sam_path{"out.sam"};
uint8_t errors{0};
};
void initialise_argument_parser(argument_parser & parser, cmd_arguments & args)
{
parser.info.author = "E. coli";
parser.info.short_description = "Map reads against a reference.";
parser.info.version = "1.0.0";
parser.add_option(args.reference_path, 'r', "reference", "The path to the reference.", option_spec::REQUIRED,
input_file_validator{{"fa","fasta"}});
parser.add_option(args.query_path, 'q', "query", "The path to the query.", option_spec::REQUIRED,
input_file_validator{{"fq","fastq"}});
parser.add_option(args.index_path, 'i', "index", "The path to the index.", option_spec::REQUIRED,
input_file_validator{{"index"}});
parser.add_option(args.sam_path, 'o', "output", "The output SAM file path.", option_spec::DEFAULT,
parser.add_option(args.errors, 'e', "error", "Maximum allowed errors.", option_spec::DEFAULT,
}
int main(int argc, char const ** argv)
{
argument_parser parser("Mapper", argc, argv);
cmd_arguments args{};
initialise_argument_parser(parser, args);
try
{
parser.parse();
}
catch (parser_invalid_argument const & ext)
{
std::cerr << "[PARSER ERROR] " << ext.what() << '\n';
return -1;
}
run_program(args.reference_path, args.query_path, args.index_path, args.sam_path, args.errors);
return 0;
}

Step 4 - Alignment output

Finally, we can write our results into a SAM file.

SAM out

We further need to extend map_reads to write the alignment results into a SAM file. Additionally, there should be no more debug output.

Try to write all available information into the SAM file. We can introduce a naive mapping quality by using mapping quality = 60 + alignment score.

This is the alignment_file_output construction:

Hint

alignment_file_output sam_out{sam_path, fields<field::SEQ,
field::ID,
field::REF_ID,
field::REF_OFFSET,
field::ALIGNMENT,
field::QUAL,
field::MAPQ>{}};

Solution

void map_reads(std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
reference_storage_t & storage,
uint8_t const errors)
{
bi_fm_index<dna5, text_layout::collection> index; // we need the alphabet and text layout before loading
{
std::ifstream is{index_path, std::ios::binary};
cereal::BinaryInputArchive iarchive{is};
iarchive(index);
}
sequence_file_input query_in{query_path};
alignment_file_output sam_out{sam_path, fields<field::SEQ,
field::ID,
field::REF_ID,
field::REF_OFFSET,
field::ALIGNMENT,
field::QUAL,
field::MAPQ>{}};
configuration const search_config = search_cfg::max_error{search_cfg::total{errors}} |
search_cfg::mode{search_cfg::all_best};
configuration const align_config = align_cfg::edit |
align_cfg::aligned_ends{free_ends_first} |
align_cfg::result{with_alignment};
for (auto & [query, id, qual] : query_in)
{
auto positions = search(query, index, search_config);
for (auto & [idx, pos] : positions)
{
size_t start = pos ? pos - 1 : 0;
std::span text_view{std::data(storage.seqs[idx]) + start, query.size() + 1};
for (auto && alignment : align_pairwise(std::tie(text_view, query), align_config))
{
auto aligned_seq = alignment.alignment();
size_t ref_offset = alignment.front_coordinate().first + 2 + start;
size_t map_qual = 60u + alignment.score();
sam_out.emplace_back(query, id, storage.ids[idx], ref_offset, aligned_seq, qual, map_qual);
}
}
}
}

Here is the complete program:

Hint

#include <fstream>
#include <cereal/archives/binary.hpp>
#include <seqan3/std/span>
using namespace seqan3;
struct reference_storage_t
{
};
void read_reference(std::filesystem::path const & reference_path,
reference_storage_t & storage)
{
sequence_file_input reference_in{reference_path};
for (auto & [seq, id, qual] : reference_in)
{
storage.ids.push_back(std::move(id));
storage.seqs.push_back(std::move(seq));
}
}
void map_reads(std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
reference_storage_t & storage,
uint8_t const errors)
{
bi_fm_index<dna5, text_layout::collection> index; // we need the alphabet and text layout before loading
{
std::ifstream is{index_path, std::ios::binary};
cereal::BinaryInputArchive iarchive{is};
iarchive(index);
}
sequence_file_input query_in{query_path};
configuration const search_config = search_cfg::max_error{search_cfg::total{errors}} |
configuration const align_config = align_cfg::edit |
align_cfg::aligned_ends{free_ends_first} |
align_cfg::result{with_alignment};
for (auto & [query, id, qual] : query_in)
{
auto positions = search(query, index, search_config);
for (auto & [idx, pos] : positions)
{
size_t start = pos ? pos - 1 : 0;
std::span text_view{std::data(storage.seqs[idx]) + start, query.size() + 1};
for (auto && alignment : align_pairwise(std::tie(text_view, query), align_config))
{
auto aligned_seq = alignment.alignment();
size_t ref_offset = alignment.front_coordinate().first + 2 + start;
size_t map_qual = 60u + alignment.score();
sam_out.emplace_back(query, id, storage.ids[idx], ref_offset, aligned_seq, qual, map_qual);
}
}
}
}
void run_program(std::filesystem::path const & reference_path,
std::filesystem::path const & query_path,
std::filesystem::path const & index_path,
std::filesystem::path const & sam_path,
uint8_t const errors)
{
reference_storage_t storage{};
read_reference(reference_path, storage);
map_reads(query_path, index_path, sam_path, storage, errors);
}
struct cmd_arguments
{
std::filesystem::path reference_path{};
std::filesystem::path query_path{};
std::filesystem::path index_path{};
std::filesystem::path sam_path{"out.sam"};
uint8_t errors{0};
};
void initialise_argument_parser(argument_parser & parser, cmd_arguments & args)
{
parser.info.author = "E. coli";
parser.info.short_description = "Map reads against a reference.";
parser.info.version = "1.0.0";
parser.add_option(args.reference_path, 'r', "reference", "The path to the reference.", option_spec::REQUIRED,
input_file_validator{{"fa","fasta"}});
parser.add_option(args.query_path, 'q', "query", "The path to the query.", option_spec::REQUIRED,
input_file_validator{{"fq","fastq"}});
parser.add_option(args.index_path, 'i', "index", "The path to the index.", option_spec::REQUIRED,
input_file_validator{{"index"}});
parser.add_option(args.sam_path, 'o', "output", "The output SAM file path.", option_spec::DEFAULT,
parser.add_option(args.errors, 'e', "error", "Maximum allowed errors.", option_spec::DEFAULT,
}
int main(int argc, char const ** argv)
{
argument_parser parser("Mapper", argc, argv);
cmd_arguments args{};
initialise_argument_parser(parser, args);
try
{
parser.parse();
}
catch (parser_invalid_argument const & ext)
{
std::cerr << "[PARSER ERROR] " << ext.what() << '\n';
return -1;
}
run_program(args.reference_path, args.query_path, args.index_path, args.sam_path, args.errors);
return 0;
}