File: GenomeAnnotations.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 (314 lines) | stat: -rw-r--r-- 15,598 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
.. sidebar:: ToC

    .. contents::

.. _tutorial-datastructures-store-genome-annotations:

Genome Annotations
==================

Learning Objective
  You will learn how to work with annotations in SeqAn.
  After this tutorial, you will be able to write your own programs using annotations and analyzing them.
  You will be ready to continue with the :ref:`tutorial-datastructures-store-fragment-store` Tutorial, e.g. if you want to combine your annotations with information from alignments.

Difficulty
  Average

Duration
  1 h

Prerequisites
  :ref:`tutorial-datastructures-sequences`

This tutorial will present SeqAn's efficient and easy-to-use data structures to work with annotations.
They allow to annotate genome regions with features like 'gene', 'mRNA', 'exon', 'intron' and if required with custom features.
We will give you an understanding of how to load annotations from a `GFF <https://genome.ucsc.edu/FAQ/FAQformat.html#format3>`_ or `GTF <https://genome.ucsc.edu/FAQ/FAQformat.html#format4>`_ file, store them in efficient data structures, as well as how to traverse and access these information.

AnnotationStore as Part of the FragmentStore
--------------------------------------------

This section will give you a short introduction to data structures relevant for working with annotations.

In SeqAn, annotations are stored in the so-called :dox:`FragmentStore::annotationStore`, which is part of the :dox:`FragmentStore`.
The annotationStore can only be used together with the FragmentStore, because the latter stores additional information, e.g. the contig names or sequences.
The FragmentStore is a data structure specifically designed for read mapping, genome assembly or gene annotation.

The FragmentStore can be seen as a database, where each table (called "store") is implemented as a :dox:`String`.
Each row of the table corresponds to an element in the string.
The position of each element in the string implicitly represents the Id of such element in the table.
All such strings are members of the class :dox:`FragmentStore`, are always present and empty if unused.
For example, the member :dox:`FragmentStore::contigStore` is a string of elements, each one containing among others a contig sequence.

For detailed information about the :dox:`FragmentStore` read the :ref:`tutorial-datastructures-store-fragment-store` Tutorial.

Accordingly, the :dox:`FragmentStore::annotationStore` is a :dox:`String`, where each element represents one annotation.
Each element holds the necessary information, e.g. beginPos, endPos, parentId etc., as data members.

.. Like many other stores, the :dox:`FragmentStore::annotationStore` has an associated name store, namely the :dox:`FragmentStore::annotationNameStore`, to store its element names.
   The name store is a  StringSet that stores the element name at the position of its id.

AnnotationStore
---------------

In this section you will learn how to work with the :dox:`FragmentStore::annotationStore` itself.

Annotations are represented hierarchically by a tree having at least a root node.

A typical annotation tree looks as follows.

.. figure:: AnnotationTree.png
   :width: 400px

   Annotation tree example


The following entity-relationship diagram shows the tables holding store annotations, their relationships and cardinalities.

.. figure:: AnnotationStore.png
   :width: 600px

   Stores involved in gene annotation

The instantiation of an :dox:`FragmentStore::annotationStore` happens implicitly with the instantiation of a :dox:`FragmentStore`.
To access the FragmentStore definitions we'll need to include the correct header:

.. includefrags:: demos/tutorial/genome_annotations/base.cpp
    :fragment: INCLUDE

Now we can simply write:

.. includefrags:: demos/tutorial/genome_annotations/base.cpp
    :fragment: SIGNATURE

Loading an Annotation File
^^^^^^^^^^^^^^^^^^^^^^^^^^

Before we deal with the actual annotation tree, we will first describe how you can easily load annotations from a `GFF <https://genome.ucsc.edu/FAQ/FAQformat.html#format3>`_ or `GTF <https://genome.ucsc.edu/FAQ/FAQformat.html#format4>`_ file into the :dox:`FragmentStore`.

An annotation file can be read from an :dox:`GffFileIn` with the function :dox:`FragmentStore#readRecords`.
The file extension specifies if we want to read a GFF, GTF or UCSC file.
The following example shows how to read an GTF file:

.. includefrags:: demos/tutorial/genome_annotations/base.cpp
    :fragment: LOAD

The GFF-reader is also able to detect and read GTF files.
The UCSC Genome Browser uses two separate files, the ``knownGene.txt`` and ``knownIsoforms.txt``.
They must be read by using two different :dox:`UcscFileIn` objects (one for ``knownGene.txt`` and one for ``knownIsoforms.txt``).
Finally you call :dox:`FragmentStore#readRecords` with both :dox:`UcscFileIn` objects.

.. tip::

    An annotation can be loaded without loading the corresponding contigs.

    In that case empty contigs are created in the contigStore with names given in the annotation.
    A subsequent call of :dox:`FragmentStore#loadContigs` would load the sequences of these contigs, if they have the same identifier in the contig file.

Traversing the Annotation Tree
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This section will illustrate how to use iterators to traverse the annotation tree.

The annotation tree can be traversed and accessed with the :dox:`AnnotationTreeIterator AnnotationTree Iterator`.
Again we use the metafunction :dox:`ContainerConcept#Iterator` to determine the appropriate iterator type for our container.
A new AnnotationTree iterator can be obtained by calling :dox:`ContainerConcept#begin` with a reference to the :dox:`FragmentStore` and the ``AnnotationTree`` tag:

.. includefrags:: demos/tutorial/genome_annotations/base.cpp
    :fragment: ITERATOR

The AnnotationTree iterator starts at the root node and can be moved to adjacent tree nodes with the functions :dox:`AnnotationTreeIterator#goDown`, :dox:`AnnotationTreeIterator#goUp`, and :dox:`AnnotationTreeIterator#goRight`.
These functions return a boolean value that indicates whether the iterator could be moved.
The functions :dox:`AnnotationTreeIterator#isLeaf`, :dox:`AnnotationTreeIterator#isRoot`, :dox:`AnnotationTreeIterator#isLastChild` return the same boolean without moving the iterator.
With :dox:`AnnotationTreeIterator#goRoot` or :dox:`AnnotationTreeIterator#goTo` the iterator can be moved to the root node or an arbitrary node given its annotationId.
If the iterator should not be moved but a new iterator at an adjacent node is required, the functions :dox:`AnnotationTreeIterator#nodeDown`, :dox:`AnnotationTreeIterator#nodeUp`, :dox:`AnnotationTreeIterator#nodeRight` can be used.

.. includefrags:: demos/tutorial/genome_annotations/base.cpp
    :fragment: MOVE

The AnnotationTree iterator supports a preorder DFS traversal and therefore can also be used in typical begin-end loops with the functions :dox:`RootedRandomAccessIteratorConcept#goBegin` (== :dox:`AnnotationTreeIterator#goRoot`), :dox:`RootedRandomAccessIteratorConcept#goEnd`, :dox:`InputIteratorConcept#goNext`, :dox:`RootedIteratorConcept#atBegin`, :dox:`RootedIteratorConcept#atEnd`.
During a preorder DFS, the descent into subtree can be skipped by :dox:`AnnotationTreeIterator#goNextRight`, or :dox:`AnnotationTreeIterator#goNextUp` which proceeds with the next sibling or returns to the parent node and proceeds with the next node in preorder DFS.

.. includefrags:: demos/tutorial/genome_annotations/base.cpp
    :fragment: DFS

Asignment 1
"""""""""""

.. container:: assignment

   Type
     Review

   Objective
     Copy the code below, which loads the annotations from a given GTF file into the FragmentStore and initializes an iterator on the AnnotationTree.
     Download the GTF file :download:`assignment_annotations.gtf <assignment_annotations.gtf>`, whose annotations build an AnnotationTree of the typical structure with gene, mRNA and exon level.
     Adjust the code to go down to the exon level and iteratate over all children of the first mRNA and count them.
     Print the result.

     Click **more...** to see the code.

     .. container:: foldable

        .. includefrags:: demos/tutorial/genome_annotations/assignment_1_hint.cpp

     Hints
       In the given data the left-most leaf is a child of mRNA and has siblings.
       You can use the function :dox:`AnnotationTreeIterator#goRight` to traverse over all siblings.

     Solution
      Click **more...** to see one possible solution.

      .. container:: foldable

         .. includefrags:: demos/tutorial/genome_annotations/assignment_1_solution.cpp

         .. includefrags:: demos/tutorial/genome_annotations/assignment_1_solution.cpp.stdout


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

.. container:: assignment

   Type
     Review

   Objective
     Reuse the code and the GTF file from above.
     Instead of counting only the children of the first mRNA adjust the code to count the children for each given mRNA.
     Print the results.

   Hints
     After you reached the last child of the first mRNA you can use the functions :dox:`InputIteratorConcept#goNext` and :dox:`AnnotationTreeIterator#goDown` to traverse to the next leaf.

   Solution
     Click **more...** to see one possible solution.

     .. container:: foldable

        .. includefrags:: demos/tutorial/genome_annotations/assignment_2_solution.cpp

        .. includefrags:: demos/tutorial/genome_annotations/assignment_2_solution.cpp.stdout


Accessing the Annotation Tree
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Let us now have a closer look how to access the information stored in the different stores representing the annotation tree.

To access or modify the node an iterator points at, the iterator returns the node's annotationId by the :dox:`IteratorAssociatedTypesConcept#value` function (== ``operator*``).
With the annotationId the corresponding entry in the annotationStore could be modified manually or by using convenience functions.
The function :dox:`AnnotationTreeIterator#getAnnotation` returns a reference to the corresponding entry in the annotationStore.
:dox:`AnnotationTreeIterator#getName` and :dox:`AnnotationTreeIterator#setName` can be used to retrieve or change the identifier of the annotation element.
As some annotation file formats don't give every annotation a name, the function :dox:`AnnotationTreeIterator#getUniqueName` returns the name if non-empty or generates one using the type and id.
The name of the parent node in the tree can be determined with :dox:`AnnotationTreeIterator#getParentName`.
The name of the annotation type, e.g. 'mRNA' or 'exon', can be determined and modified with :dox:`AnnotationTreeIterator#getType` and :dox:`AnnotationTreeIterator#setType`.

Assume we have loaded the file ``example.gtf`` with the following content to the :dox:`FragmentStore` *store* and instantiated the iterator *it* of the corresponding annotation tree.

.. includefrags:: demos/tutorial/genome_annotations/example.gtf

We now want to iterate to the first exon and output a few pieces of information:

.. includefrags:: demos/tutorial/genome_annotations/base.cpp
    :fragment: ACCESS

For our example the output would be:

.. includefrags:: demos/tutorial/genome_annotations/base.cpp.stdout
    :lines: 5-7

An annotation can not only refer to a region of a contig but also contain additional information given as key-value pairs.
The value of a key can be retrieved or set by :dox:`AnnotationTreeIterator#getValueByKey` and :dox:`AnnotationTreeIterator#assignValueByKey`.
The values of a node can be cleared with :dox:`AnnotationTreeIterator#clearValues`.

A new node can be created as first child, last child, or right sibling of the current node with :dox:`AnnotationTreeIterator#createLeftChild`, :dox:`AnnotationTreeIterator#createRightChild`, or :dox:`AnnotationTreeIterator#createSibling`.
All three functions return an iterator to the newly created node.

.. includefrags:: demos/tutorial/genome_annotations/base.cpp
    :fragment: CREATE

The following list summarizes the functions provided by the AnnotationTree iterator.

:dox:`AnnotationTreeIterator#getAnnotation`, :dox:`IteratorAssociatedTypesConcept#value`
  Return annotation object/id of current node
:dox:`AnnotationTreeIterator#getName`, :dox:`AnnotationTreeIterator#setName`, :dox:`AnnotationTreeIterator#getType`, :dox:`AnnotationTreeIterator#setType`
  Access name or type of current annotation object
:dox:`AnnotationTreeIterator#getParentName`
  Access parent name of current annotation object
:dox:`AnnotationTreeIterator#clearValues clearValue`, :dox:`AnnotationTreeIterator#getValueByKey`, :dox:`AnnotationTreeIterator#assignValueByKey`
  Access associated values
:dox:`RootedRandomAccessIteratorConcept#goBegin`, :dox:`RootedRandomAccessIteratorConcept#goEnd`, :dox:`RootedIteratorConcept#atBegin`, :dox:`RootedIteratorConcept#atEnd`
  Go to or test for begin/end of DFS traversal
:dox:`InputIteratorConcept#goNext`, :dox:`AnnotationTreeIterator#goNextRight`, :dox:`AnnotationTreeIterator#goNextUp`
  go next, skip subtree or siblings during DFS traversal
:dox:`AnnotationTreeIterator#goRoot`, :dox:`AnnotationTreeIterator#goUp`, :dox:`AnnotationTreeIterator#goDown`, :dox:`AnnotationTreeIterator#goRight`
  Navigate through annotation tree
:dox:`AnnotationTreeIterator#createLeftChild`, :dox:`AnnotationTreeIterator#createRightChild`, :dox:`AnnotationTreeIterator#createSibling`
  Create new annotation nodes
:dox:`AnnotationTreeIterator#isRoot`, :dox:`AnnotationTreeIterator#isLeaf`
  Test for root/leaf node

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

.. container:: assignment

   Type
     Application

   Objective
     Again use the given GTF file :download:`assignment_annotations.gtf <assignment_annotations.gtf>` and create an iterator on the annotation tree.
     Now iterate to the first node of type "exon" and output the following features:

     #. type
     #. begin position
     #. end position
     #. its Id
     #. the Id of its parent
     #. the name of its parent

   Solution
    Click **more...** to see one possible solution.

    .. container:: foldable

       .. includefrags:: demos/tutorial/genome_annotations/assignment_3_solution.cpp

       .. includefrags:: demos/tutorial/genome_annotations/assignment_3_solution.cpp.stdout


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

.. container:: assignment
     Type
       Transfer

     Objective
       Write a small statistical tool to analyse a given set of annotations.

       #. Load the annotations given in the GTF file :download:`assignment_annotations.gtf <assignment_annotations.gtf>`.
       #. Output the average number of mRNAs for genes.
       #. Output the average number of exons for mRNAs.
       #. Additionally output the average exon length.
       #. Test your program also on large data, e.g. the annotation of the mouse genome `Mus_musculus.NCBIM37.61.gtf.zip <https://ftp.seqan.de/manual_files/seqan-1.4/Mus_musculus.NCBIM37.61.gtf.zip>`_ (don't forget to unzip first).

     Solution
      Click **more...** to see one possible solution.

      .. container:: foldable

         .. includefrags:: demos/tutorial/genome_annotations/assignment_4_solution.cpp

         .. includefrags:: demos/tutorial/genome_annotations/assignment_4_solution.cpp.stdout


Write an Annotation File
^^^^^^^^^^^^^^^^^^^^^^^^

To write an annotation to a file use the function :dox:`FragmentStore#writeRecords`. Note that the format (``Gff()`` or ``Gtf()``) is specified by the file extension.

.. includefrags:: demos/tutorial/genome_annotations/base.cpp
    :fragment: OUT