SeqAn3
The Modern C++ library for sequence analysis.
Sequence File Input and Output

Learning Objective:
You will get an overview of how file Input/Output is handled in SeqAn and learn how to read and write sequence files. This tutorial is a walk-through with links into the API documentation and also meant as a source for copy-and-paste code.

DifficultyEasy
Duration90 min
Prerequisite tutorialsSetup, Ranges, Alphabets
Recommended reading

File I/O in SeqAn

Most file formats in bioinformatics are structured as lists of records. In SeqAn we model our files as a range over records. This interface allows us to easily stream over a file, apply filters and convert formats, sometimes only in a single line of code. The file format is automatically detected by the file name extension and compressed files can be handled without any effort. We can even stream over files in a python-like way with range-based for loops:

for (auto & record : file)
// do something with my record

We will explain the details about reading and writing files in the Sequence File section below. Currently, SeqAn supports the following file formats:

Warning
Access to compressed files relies on external libraries. For instance, you need to have zlib installed for reading .gz files and libbz2 for reading .bz2 files. You can check whether you have installed these libraries by running cmake . in your build directory. If -- Optional dependency: ZLIB-x.x.x found. is displayed on the command line then you can read/write compressed files in your programs. TODO what about bz2

Basic layout of SeqAn file objects

Before we dive into the details, we will outline the general design of our file objects, hoping that it will make the following tutorial easier to understand.

As mentioned above, our file object is a range over records. More specifically over objects of type seqan3::record which is basically just a std::tuple that holds the data. To identify or specialise which data is read/written and contained in the records, we use seqan3::field tags (e.g. seqan3::field::SEQ denotes sequence information). The seqan3::field tags are shared between file formats and allow for easy file conversion.

Output files can handle various types that fulfill the requirements of the format (e.g. a sequence has to be a range over an alphabet). In contrast to this, input files have certain default types for record fields that can be modified via a traits type. For example, on construction you can specify seqan3::sequence_file_default_traits_dna or seqan3::sequence_file_default_traits_aa for reading dna and protein sequences respectively (section File traits will covers this in more detail).

Opening and closing files is also handled automatically. If a file cannot be opened for reading or writing, a seqan3::file_open_error is thrown.

Sequence file formats

Sequence files are the most generic and common biological files. Well-known formats include FASTA and FASTQ.

FASTA format

A FASTA record contains the sequence id and the sequence characters. Here is an example of a FASTA file:

>seq1
CCCCCCCCCCCCCCC
>seq2
CGATCGATC

In SeqAn we provide the seqan3::format_fasta to read sequence files in FASTA format.

FASTQ format

A FASTQ record contains an additional quality value for each sequence character. Here is an example of a FASTQ file:

@seq1
CCCCCCCCCCCCCCC
+
IIIIIHIIIIIIIII
@seq2
CGATCGATC
+
IIIIIIIII

In SeqAn we provide the seqan3::format_fastq to read sequence files in FASTQ format.

EMBL format

An EMBL record stores sequence and its annotation together. We are only interested in the id (ID) and sequence (SQ) information for a sequence file. Qualities are not stored in this format. Here is an example of an EMBL file:

ID X56734; SV 1; linear; mRNA; STD; PLN; 1859 BP.
XX
AC X56734; S46826;
XX
SQ Sequence 1859 BP; 609 A; 314 C; 355 G; 581 T; 0 other;
aaacaaacca aatatggatt ttattgtagc catatttgct ctgtttgtta ttagctcatt
cacaattact tccacaaatg cagttgaagc ttctactctt cttgacatag gtaacctgag

In SeqAn we provide the seqan3::format_embl to read sequence files in EMBL format.

File extensions

The formerly introduced formats can be identified by the following file name extensions (this is important for automatic format detection from a file name as you will learn in the next section).

File Format SeqAn format class File Extensions
FASTA seqan3::format_fasta .fa, .fasta, .fna, .ffn, .ffa, .frn
FASTQ seqan3::format_fastq .fq, .fastq
EMBL seqan3::format_embl .embl

You can access the valid file extension via the file_extension member variable in a format:

debug_stream << format_fastq::file_extensions << std::endl; // prints [fastq,fq]

You can also customise this list if you want to allow different or additional file extensions:

format_fastq::file_extensions.push_back("qq");
sequence_file_input fin{std::filesystem::temp_directory_path()/"my.qq"}; // detects FASTQ format

Fields

The Sequence file abstraction supports reading four different fields:

  1. seqan3::field::SEQ
  2. seqan3::field::ID
  3. seqan3::field::QUAL
  4. seqan3::field::SEQ_QUAL

The first three fields are retrieved by default (and in that order!). The last field may be selected to directly store sequence and qualities in a more memory-efficient combined container (see seqan3::qualified). This is more advanced than what we cover here, but if you are still interested you can take a look at the tutorial Alignment Input and Output in SeqAn which introduces reading a file with custom selected fields.

Reading a sequence file

You can include the SeqAn sequence file functionality with:

Construction

At first, you need to construct a seqan3::sequence_file_input object that handles file access. In most cases you construct from a file name:

int main()
{
using seqan3::operator""_dna4;
auto tmp_file = std::filesystem::temp_directory_path() / "my.fasta";
{
// Create a /tmp/my.fasta file.
fout.emplace_back("ACGT"_dna4, "TEST1");
fout.emplace_back("AGGCTGA"_dna4, "Test2");
fout.emplace_back("GGAGTATAATATATATATATATAT"_dna4, "Test3");
}
// FastA with DNA sequences assumed, regular std::ifstream taken as stream
}

All template parameters of the seqan3::sequence_file_input are automatically deduced, even the format! We detect the format by the file name extension. The file extension in the example above is .fasta so we choose the seqan3::format_fasta.

You can also construct a sequence file object directly from a stream (e.g. std::cin or std::stringstream), but then you need to know your format beforehand:

sequence_file_input fin{std::cin, format_fasta{}};

File traits

The seqan3::sequence_file_input needs to know the types of the data you are reading in (e.g. that your sequence is a dna sequence) at compile-time. These necessary types are defined in the traits type argument (seqan3::sequence_file_input::traits_type) which by default is set to seqan3::sequence_file_input_default_traits_dna.

We thereby assume that

In case you want to read a protein sequence instead we also provide the seqan3::sequence_file_input_default_traits_aa traits type which sets the SEQ field to a std::vector over the seqan3::aa27 alphabet.

You can specify the traits object as the first template argument for the sequence file:

sequence_file_input<sequence_file_input_default_traits_aa> fin{std::filesystem::temp_directory_path()/"my.fasta"};

You can also customise the types by inheriting from one of the default traits and changing the type manually. See the detailed information on seqan3::sequence_file_input_default_traits_dna for an example.

Reading records

After construction you can now read the sequence records. As described in the basic file layout, our file objects behave like ranges so you can use a range based for loop to conveniently iterate over the file:

#include <sstream>
auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
using seqan3::get;
for (auto & rec : fin)
{
seqan3::debug_stream << "ID: " << get<seqan3::field::ID>(rec) << '\n';
seqan3::debug_stream << "SEQ: " << get<seqan3::field::SEQ>(rec) << '\n';
// a quality field also exists, but is not printed, because we know it's empty for FastA files.
}
}
Attention
An input file is a single input range, which means you can only iterate over it once!

In the above example, rec has the type seqan3::sequence_file_input::record_type which is a specialisation of seqan3::record and behaves like an std::tuple (that's why we can access it via get).

Note
It is important to write auto & and not just auto, otherwise you will copy the record on every iteration.

Since the return type seqan3::record behaves like a tuple, you can also use structured bindings to decompose the record into its elements:

auto input = R"(> TEST1
ACGT
> Test2
AGGCTGA
> Test3
GGAGTATAATATATATATATATAT)";
int main()
{
for (auto & [seq, id, qual] : fin)
{
seqan3::debug_stream << "ID: " << id << '\n';
seqan3::debug_stream << "SEQ: " << seq << '\n';
seqan3::debug_stream << "EMPTY QUAL." << qual << '\n'; // qual is empty for FastA files
}
}

In this case you immediately get the two elements of the tuple: seq of seqan3::sequence_file_input::sequence_type and id of seqan3::sequence_file_input::id_type. But beware: with structured bindings you do need to get the order of elements correctly!

You can read up more on the different ways to stream over the file object in the detailed documentation of seqan3::sequence_file_input.

Exercise: Reading a FASTQ file

Copy and paste the following FASTQ file to some location, e.g. the tmp directory:

@seq1
AGCTAGCAGCGATCG
+
IIIIIHIIIIIIIII
@seq2
CGATCGATC
+
IIIIIIIII
@seq3
AGCGATCGAGGAATATAT
+
IIIIHHGIIIIHHGIIIH

and then create a simple program that reads in all the records from that file and prints the id, sequence and quality information to the command line using the seqan3::debug_stream. Do not use the structured bindings for now but access the record via get<>()!

Note: you include the seqan3::debug_stream with the following header

Solution

using namespace seqan3;
int main()
{
std::filesystem::path tmp_dir = std::filesystem::temp_directory_path(); // get the temp directory
sequence_file_input fin{tmp_dir/"my.fastq"};
for (auto & rec : fin)
{
debug_stream << "ID: " << get<field::ID>(rec) << '\n';
debug_stream << "SEQ: " << get<field::SEQ>(rec) << '\n';
debug_stream << "QUAL: " << get<field::QUAL>(rec) << '\n';
}
}

The code will print the following:

ID: seq1
SEQ: AGCTAGCAGCGATCG
QUAL: IIIIIHIIIIIIIII
ID: seq2
SEQ: CGATCGATC
QUAL: IIIIIIIII
ID: seq3
SEQ: AGCGATCGAGGAATATAT
QUAL: IIIIHHGIIIIHHGIIIH

The record type

In the examples above, we always use auto to deduce the record type automatically. In case you need the type explicitly, e.g. if you want to store the records in a variable, you can access the type member seqan3::sequence_file_input::record_type.

using record_type = typename decltype(fin)::record_type;
// Because `fin` is a range, we can access the first element by dereferencing fin.begin()
record_type rec = *fin.begin();

You can move the record out of the file if you want to store it somewhere without copying.

record_type rec = std::move(*fin.begin()); // avoid copying

Exercise: Storing records in a std::vector

Create a small program that reads in a FASTA file and stores all the records in a std::vector.

After reading, print the vector (this works natively with the seqan3::debug_stream).

Test your program with the following file:

>seq1
AGCT
>seq2
CGATCGA

It should print the following:

[(AGCT,seq1,),(CGATCGA,seq2,)]

Note that the quality (third tuple element) is empty because we are reading a FASTA file.

Solution

#include <seqan3/std/ranges> // std::ranges::copy
using namespace seqan3;
int main()
{
std::filesystem::path tmp_dir = std::filesystem::temp_directory_path(); // get the temp directory
sequence_file_input fin{tmp_dir/"my.fasta"};
using record_type = decltype(fin)::record_type;
// You can use a for loop:
// for (auto & rec : fin)
// {
// records.push_back(std::move(rec));
// }
// But you can also do this:
std::ranges::copy(fin, std::ranges::back_inserter(records));
debug_stream << records << std::endl;
}

Sequence files as views

Since SeqAn files are ranges, you can also create views over files. This enables us to create solutions for lot of use cases using only a few lines of code.

Reading a file in chunks

A common use case is to read chunks from a file instead of the whole file at once or line by line.

You can do so easily on a file range by using the ranges::view::chunk.

for (auto && records : fin | ranges::view::chunk(10)) // && is important! because views::chunk returns temporaries
{
// `records` contains 10 elements (or less at the end)
debug_stream << "Taking the next 10 sequences:\n";
debug_stream << "ID: " << get<field::ID>(*records.begin()) << '\n'; // prints first ID in batch
}

The example above will iterate over the file by reading 10 records at a time. If no 10 records are available any more, it will just print the remaining records.

Applying a filter to a file

In some occasions you are only interested in sequence records that fulfill a certain criteria, e.g. having a minimum sequence length or a minimum average quality. Just like in the example with ranges::view::chunk you can use std::ranges::filter for this purpose:

// std::views::filter takes a function object (a lambda in this case) as input that returns a boolean
auto minimum_quality_filter = std::views::filter([] (auto const & rec)
{
auto qual = get<field::QUAL>(rec) | std::views::transform([] (auto q) { return q.to_phred(); });
double sum = ranges::accumulate(qual.begin(), qual.end(), 0);
return sum / std::ranges::size(qual) >= 40; // minimum average quality >= 40
});
for (auto & rec : fin | minimum_quality_filter)
{
debug_stream << "ID: " << get<field::ID>(rec) << '\n';
}

To remind you what you have learned in the Ranges tutorial before, a view is not applied immediately but lazy evaluated. That means your file is still parsed record by record and not at once.

Reading paired-end reads

In modern Next Generation Sequencing experiments you often have paired-end read data which is split into two files. The read pairs are identified by their identical name/id and that their position in the two files is the same.

If you want to handle one pair of reads at a time, you can do so easily with a views::zip.

sequence_file_input fin2{std::filesystem::temp_directory_path()/"my.fastq"}; // for simplicity we take the same file
for (auto && [rec1, rec2] : views::zip(fin1, fin2)) // && is important! because views::zip returns temporaries
{
if (get<field::ID>(rec1) != get<field::ID>(rec2))
throw std::runtime_error("Oh oh your pairs don't match.");
}

Exercise: Fun with file ranges

Implement a small program that reads in a FASTQ file and prints the first 2 sequences that have a length of at least 5.

Hints:

  • You can use std::ranges::size to retrieve the size of a range.
  • You need the following includes for std::views::filter and std::take

Test your program on the following FASTQ file:

@seq1
CGATCGATC
+
IIIIIIIII
@seq2
AGCG
+
IIII
@seq3
AGCTAGCAGCGATCG
+
IIIIIHIIJJIIIII
@seq4
AGC
+
III
@seq5
AGCTAGCAGCGATCG
+
IIIIIHIIJJIIIII

It should output seq1 and seq3.

Solution

using namespace seqan3;
int main()
{
std::filesystem::path tmp_dir = std::filesystem::temp_directory_path(); // get the temp directory
sequence_file_input fin{tmp_dir/"my.fastq"};
auto length_filter = std::views::filter([] (auto const & rec)
{
return std::ranges::size(get<field::SEQ>(rec)) >= 5;
});
// you can use a for loop
// for (auto & rec : fin | length_filter | std::views::take(2))
// {
// debug_stream << "ID: " << get<field::ID>(rec) << '\n';
// }
// But you can also do this to retrieve all IDs into a vector:
| length_filter // apply length filter
| std::views::take(2) // take first two records
| views::get<field::ID> // select only ID from record
| views::convert<std::string &&> // mark ID to be moved out of record
| views::to<std::vector<std::string>>; // convert to container
// Note that you need to know the type of id (std::string)
// Converting to && prevents the IDs from being copied
}

Writing a sequence file

Construction

You construct the seqan3::sequence_file_output just like the seqan3::sequence_file_input by giving it a file name or a stream.

int main()
{
auto tmp_file = std::filesystem::temp_directory_path() / "my.fasta";
seqan3::sequence_file_output fout{tmp_file}; // FastA format detected, std::ofstream opened for file
}

Writing to std::cout:

#include <iostream>
int main()
{
using seqan3::operator""_dna5;
// ^ no need to specify the template arguments
fout.emplace_back("ACGTN"_dna5, "example_id"); // default order for fasta: SEQ, ID
}

Writing records

The easiest way to write to a sequence file is to use the seqan3::sequence_file_output::push_back() or seqan3::sequence_file_output::emplace_back() member functions. These work similarly to how they work on an std::vector.

#include <sstream>
#include <string>
#include <tuple>
int main()
{
using seqan3::operator""_dna5;
for (int i = 0; i < 5; ++i) // ...
{
std::string id{"test_id"};
seqan3::dna5_vector seq{"ACGT"_dna5};
// ...
fout.emplace_back(seq, id); // as individual variables
// or:
fout.push_back(std::tie(seq, id)); // as a tuple
}
}

If you pass a tuple to push_back() or give arguments to emplace_back() the order of elements is assumed to be the same as the one in the seqan3::sequence_file_output::selected_field_ids. For the above example the default FASTA fields are first seqan3::field::SEQ, second seqan3::field::ID and the third one seqan3::field::QUAL. You may give less fields than are selected if the actual format you are writing to can cope with less (e.g. for FastA it is sufficient to give sequence and name information).

Exercise: Writing a FASTQ file

Use your code (or the solution) from the previous exercise. Iterate over the records with a for loop and instead of just printing the ids, write out all the records that satisfy the filter to a new file called output.fastq.

Test your code on the same FASTQ file. The file output.fastq should contain the following records:

@seq1
CGATCGATC
+
IIIIIIIII
@seq3
AGCTAGCAGCGATCG
+
IIIIIHIIJJIIIII
@seq5
AGCTAGCAGCGATCG
+
IIIIIHIIJJIIIII

Solution

using namespace seqan3;
int main()
{
std::filesystem::path tmp_dir = std::filesystem::temp_directory_path(); // get the temp directory
sequence_file_input fin{tmp_dir/"my.fastq"};
sequence_file_output fout{tmp_dir/"output.fastq"};
auto length_filter = std::views::filter([] (auto const & rec)
{
return std::ranges::size(get<field::SEQ>(rec)) >= 5;
});
for (auto & rec : fin | length_filter)
{
fout.push_back(rec);
}
}

Files as views

Again we want to point out a convenient advantage of modelling files as ranges. In the "reading a file" section you already saw a few examples of how to pipe a view onto a seqan3::sequence_file_input object. In the same way you can pipe the output file:

sequence_file_input fin{tmp_dir/"my.fastq"};
sequence_file_output fout{tmp_dir/"output.fastq"};
// the following are equivalent:
fin | fout;
fout = fin;
sequence_file_output{tmp_dir/"output.fastq"} = sequence_file_input{tmp_dir/"my.fastq"};

Exercise: Fun with file ranges 2

Working on your solution from the previous exercise, try to remove the for loop in favour of a pipe notation.

The result should be the same.

Solution

using namespace seqan3;
int main()
{
std::filesystem::path tmp_dir = std::filesystem::temp_directory_path(); // get the temp directory
sequence_file_input fin{tmp_dir/"my.fastq"};
sequence_file_output fout{tmp_dir/"output.fastq"};
auto length_filter = std::views::filter([] (auto & rec)
{
return std::ranges::size(get<field::SEQ>(rec)) >= 5;
});
fout = fin | length_filter;
// This would also work:
// fin | length_filter | fout;
}

File conversion

As mentioned before, the seqan3::field tags are shared between formats which allows for easy file conversion. For example you can read in a FASTQ file and output a FASTA file in one line:

sequence_file_output{tmp_dir/"output.fasta"} = sequence_file_input{tmp_dir/"my.fastq"};

Yes that's it! Of course this only works because all fields that are required in FASTA are provided in FASTQ. The other way around would not work as easily because we have no quality information (and would make less sense too).