File: SequenceIO.rst

package info (click to toggle)
seqan2 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 228,748 kB
  • sloc: cpp: 257,602; ansic: 91,967; python: 8,326; sh: 1,056; xml: 570; makefile: 229; awk: 51; javascript: 21
file content (221 lines) | stat: -rw-r--r-- 7,366 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
.. sidebar:: ToC

    .. contents::

.. _tutorial-io-sequence-io:

Sequence I/O
============

Learning Objective
  You will learn how to read and write sequence files in FASTA, FASTQ, EMBL or GenBank format.

Difficulty
  Basic

Duration
  20 min

Prerequisites
  :ref:`tutorial-datastructures-sequences`, :ref:`tutorial-io-input-output-overview`

This tutorial explains how to read and write sequence files using the :dox:`SeqFileIn` and :dox:`SeqFileOut` classes.
These classes provide an API for accessing sequence files in different file formats, either compressed or uncompressed.


FASTA/FASTQ Format
------------------

FASTA/FASTQ are record-based files.
A FASTA record contains the sequence id and the sequence characters.
Here is an example of FASTA file:

.. includefrags:: demos/tutorial/sequence_io/example.fa

In addition to that, a FASTQ record contains also a quality value for each sequence character.
Here is an example of FASTQ file:


.. includefrags:: demos/tutorial/sequence_io/example.fq


SeqFile Formats
---------------

We can read sequence files with the :dox:`SeqFileIn` class and write them with the :dox:`SeqFileOut` class.
These classes support files in FASTA, FASTQ, EMBL or GenBank format.

Note that :dox:`SeqFileOut` will guess the format from the file name.

+--------------+----------------------+
| File Format  | File Extension       |
+==============+======================+
| FASTA        | ``.fa``, ``.fasta``  |
+--------------+----------------------+
| FASTQ        | ``.fq``, ``.fastq``  |
+--------------+----------------------+
| EMBL         | ``.embl``            |
+--------------+----------------------+
| GenBank      | ``.gbk``             |
+--------------+----------------------+

A First Working Example
-----------------------

Let us start out with a minimal working example.
The following program reads a FASTA file called ``example.fa`` and prints out the identifier and the sequence of the first record.

.. includefrags:: demos/tutorial/sequence_io/example1.cpp

We call the :dox:`FormattedFile#FormattedFile SeqFileIn constructor` with the path to the file to read.
Successively, we call the function :dox:`SeqFileIn#readRecord` to read the first record from the file.
Note that, differently from all others :dox:`FormattedFileIn` classes, :dox:`SeqFileIn#readRecord` accepts **separate** identifier and sequence :dox:`String Strings` rather than one single record object.

Assignment 1
""""""""""""

.. container:: assignment

   Type
     Reproduction

   Objective
     Copy the above example of a FASTA file in a new file ``example.fa`` in a directory of your choice.

     Copy the program above into a new application ``basic_seq_io_example``, adjust the path ``"example.fa"`` to the just created FASTA file, compile the program, and run it.

     You should see the following output:

     .. includefrags:: demos/tutorial/sequence_io/example1.cpp.stdout

   Solution
     .. container:: foldable

        .. includefrags:: demos/tutorial/sequence_io/solution1.cpp


Handling Errors
---------------

As explained in the :ref:`tutorial-io-input-output-overview` tutorial, :dox:`SeqFileIn` and :dox:`SeqFileOut` throw exceptions to signal eventual errors.
Invalid characters inside an input file will be signaled by :dox:`SeqFileIn#readRecord` via parsing exceptions.

Assignment 2
""""""""""""

.. container:: assignment

   Type
     Application

   Objective
     Improve the above program to handle errors.

   Hint
     You can use the generic class :dox:`Exception` to catch both low-level and high-level I/O errors.

   Solution
     .. container:: foldable

        .. includefrags:: demos/tutorial/sequence_io/solution2.cpp


Accessing Records in Batches
----------------------------

There are three use cases for reading or writing record-based files:

#. read or write the file **record by record**;
#. read or write a **batch of records**, e.g. 100k records at a time;
#. read or write **all records** from or to the file.

The class :dox:`SeqFileIn` provides the functions :dox:`SeqFileIn#readRecord` and :dox:`SeqFileIn#readRecords`, while the class :dox:`SeqFileOut` provides the functions :dox:`SeqFileOut#writeRecord` and :dox:`SeqFileOut#writeRecords`.

.. tip::

    Reading records in batches is more efficient than reading single records.


Note that the function :dox:`SeqFileIn#readRecords` uses :dox:`StringSet` instead of :dox:`String`.
By default, :dox:`SeqFileIn#readRecords` reads **all** remaining records.
Optionally, one can specify a batch of records to be read.

.. includefrags:: demos/tutorial/sequence_io/base.cpp
      :fragment: batch


Assignment 3
""""""""""""

.. container:: assignment

   Type
     Application

   Objective
     Change your program from above to load all sequences and print them in the same fashion.

     You should be able to run your program on the example file we created above and see the following output:

     .. includefrags:: demos/tutorial/sequence_io/solution3.cpp.stdout

   Hint
     You can use the function :dox:`SeqFileIn#readRecords` to load all records at once.

   Solution
     .. container:: foldable

        .. includefrags:: demos/tutorial/sequence_io/solution3.cpp


Accessing Qualities
-------------------

Functions :dox:`SeqFileIn#readRecord`, :dox:`SeqFileIn#readRecords`, :dox:`SeqFileOut#writeRecord` and :dox:`SeqFileOut#writeRecords` are available in two variants:

#. the first variant accepts only the sequence identifier and sequence characters, besides the :dox:`SeqFileIn` object;
#. the second variant accepts an additional :dox:`CharString` for a PHRED base quality string.

If the first variant is used on an output file containing qualities, e.g. a FASTQ file, then :dox:`SeqFileOut#writeRecord` writes qualities as ``'I'``, i.e. PHRED score 40.
If the second variant is used on an input file containing no qualities, e.g. a FASTA file, then :dox:`SeqFileIn#readRecord` returns **empty** quality strings.

Here is an example for the second variant of :dox:`SeqFileIn#readRecord`:

.. includefrags:: demos/tutorial/sequence_io/base.cpp
      :fragment: qual

.. tip::

    When :dox:`DnaQ` or :dox:`Dna5Q` :dox:`String Strings` are used, then you should use the second variant.
    The qualities are simply stored directly in the sequence characters.


Assignment 4
""""""""""""

.. container:: assignment

   Type
     Application

   Objective
     Copy the above example of FASTQ file in a new file ``example.fq`` in a directory of your choice.

     Change your result of Assignment 3 to use the variant of :dox:`SeqFileIn#readRecord` that also reads in the qualities and writes them next to the sequences.

     When your program is called on this file, the result should look as follows.

     .. includefrags:: demos/tutorial/sequence_io/solution4.cpp.stdout

   Solution
     .. container:: foldable

        .. includefrags:: demos/tutorial/sequence_io/solution4.cpp


Next Steps
----------

* Read the Wikipedia articles about the `FASTA file format <https://en.wikipedia.org/wiki/FASTA_format>`_ and the `FASTQ file format and quality values <https://en.wikipedia.org/wiki/FASTQ_format>`_ to refresh your knowledge.
* Read the :ref:`tutorial-io-indexed-fasta-io` tutorial to learn how to read FASTA files efficiently in a random-access fashion.
* Continue with the :ref:`tutorial`.