File: StringsAndSegments.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 (630 lines) | stat: -rw-r--r-- 27,016 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
.. sidebar:: ToC

    .. contents::

.. _tutorial-datastructures-sequences-strings-and-segments:

Strings and Segments
====================

Learning Objective
  You will learn about the SeqAn sequence concept and its main class :dox:`String` as well as the class :dox:`Segment`.
  After completing this tutorial, you will be able to use important functionalities of sequences in SeqAn and you will be ready to continue with the more specific tutorials, e.g. :ref:`tutorial-datastructures-alignment`, or :ref:`tutorial-algorithms-alignment-pairwise-sequence-alignment`.

Difficulty
  Very basic

Duration
  45 min

Prerequisites
  Basic C or C++ knowledge, the :ref:`tutorial-getting-started-first-steps-in-seqan` tutorial helps.

Sequences are the core concept of SeqAn.
A sequence is a container that stores an ordered list of values.
In SeqAn, there are three kinds of sequences: Strings, Sequence Adaptions and Segments.

The :dox:`String` class is one of the most fundamental classes in SeqAn.
It is designed as a generic data structure that can be instantiated for all kinds of values, both simple (e.g. ``char``, :dox:`Dna`, :dox:`AminoAcid`) and non-simple value types (e.g. :dox:`Tuple`, :dox:`String`).
With sequence adaptions, SeqAn offers an interface for accessing data types that are not part of SeqAn, namely standard library strings and c-style char arrays.
Thus those built-in types can be handled in a similar way as SeqAn strings, for example with the :dox:`ContainerConcept#length` function.
:dox:`Segment Segments` are contiguous subsequences that represent parts of other sequences.

This tutorial will deal with the SeqAn sequence classes :dox:`String` and :dox:`Segment`.

Strings
-------

In this section, we will have a detailed look at the SeqAn class :dox:`String`.
You will learn how to build and expand strings as well as how to compare and convert them.

Defining Strings
^^^^^^^^^^^^^^^^

Let's first have a look at a simple example on how to define a :dox:`String`.
The type of the contained value is specified by the first template argument, e.g. ``char`` or ``int``.

.. includefrags:: demos/tutorial/sequences/base.cpp
    :fragment: string_example

To fill the string with contents, we can simply assign a string literal to the created variable:

.. includefrags:: demos/tutorial/sequences/base.cpp
    :fragment: simple_string_construction

Any type that provides a default constructor, a copy constructor and an assignment operator can be used as the alphabet / contained type of a :dox:`String`.
This includes the C++ `POD types <https://isocpp.org/wiki/faq/intrinsic-types#pod-types>`_, e.g. ``char``, ``int``, ``double`` etc., or even more complex types complex types, such as :dox:`String Strings`.

.. includefrags:: demos/tutorial/sequences/base.cpp
    :fragment: string_of_strings_example

.. hint::

   Nested Sequences (aka "Strings of Strings")

   A collection of sequences can either be stored in a sequence of sequences, for example in a ``String< String<char> >``, or in a :dox:`StringSet`.
   The latter one allows for more auxiliary functionalities to improve the efficiency of working with large sequence collections.
   You can learn more about it in the tutorial :ref:`tutorial-datastructures-sequences-string-sets`.

SeqAn also provides the following types that are useful in bioinformatics: :dox:`AminoAcid`, :dox:`Dna`, :dox:`Dna5`, :dox:`DnaQ`, :dox:`Dna5Q`, :dox:`Finite`, :dox:`Iupac`, :dox:`Rna`, :dox:`Rna5`.
You can find detailed information in the tutorial :ref:`tutorial-datastructures-sequences-alphabets`.

.. includefrags:: demos/tutorial/sequences/base.cpp
    :fragment: special_types_example

For commonly used string parameterizations, SeqAn has a range of shortcuts implemented, e.g. :dox:`DnaString`, :dox:`RnaString` and :dox:`Peptide`.

.. includefrags:: demos/tutorial/sequences/base.cpp
    :fragment: shortcuts_example

Working with Strings
^^^^^^^^^^^^^^^^^^^^

The SeqAn String implementation provides the common C++ operators that you know already from the `vector <https://en.cppreference.com/w/cpp/container/vector>`_ class of the STL.
For example:

.. includefrags:: demos/tutorial/sequences/example_functionality1.cpp
    :fragment: main

.. includefrags:: demos/tutorial/sequences/example_functionality1.cpp.stdout

Each sequence object has a capacity, i.e. the maximum length of a sequence that can be stored in this object.
While some sequence types have a fixed capacity, the capacity of other sequence classes like :dox:`AllocString Alloc String` or ``std::basic_string`` can be changed at runtime.
The capacity can be set explicitly by functions such as :dox:`String#reserve` or :dox:`StringConcept#resize`.
It can also be set implicitly by functions like :dox:`StringConcept#append` or :dox:`StringConcept#replace`, if the operation's result exceeds the length of the target string.

In the following example we create a :dox:`String` of :dox:`Dna5String`. We first set the new length of the container with :dox:`StringConcept#resize` to two elements.
After assigning two elements we append one more element with :dox:`StringConcept#appendValue`.
In the last step the capacity is implicitly changed.

.. includefrags:: demos/tutorial/sequences/example_functionality2.cpp
    :fragment: main

Using the function :dox:`ContainerConcept#length`, we can now get the length of our strings, e.g.:

.. includefrags:: demos/tutorial/sequences/example_functionality2.cpp
    :fragment: print

.. includefrags:: demos/tutorial/sequences/example_functionality2.cpp.stdout

To empty a :dox:`String`, the function :dox:`StringConcept#clear` resets the object.

.. includefrags:: demos/tutorial/sequences/example_functionality2.cpp
    :fragment: clear

SeqAn offers a range of other functions for the work with the :dox:`String` class, e.g. :dox:`AssignableConcept#assign`, :dox:`RandomAccessContainerConcept#assignValue`, :dox:`ContainerConcept#empty`, etc.
The full list of functions you can find in the documentation :dox:`String`.

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

.. container:: assignment

   Type
     Review

   Objective
     In the following assignment, you will write a small function that builds the reverse complement of a given string.
     Copy the code below and add the following functionalities:

     #. Use the ``resize`` function to ``resize`` the ``revComplGenome`` variable.
     #. Using the ``getRevCompl`` function, get the reverse complement for every nucleotide ``genome`` and store it in reverse order ``revComplGenome``.
     #. Print out the original genome and the reverse complement.

        .. includefrags:: demos/tutorial/sequences/assignment_1_solution.cpp
           :fragment: top

        .. code-block:: cpp

           // Your code snippet here


        .. includefrags:: demos/tutorial/sequences/assignment_1_solution.cpp
           :fragment: bottom

   Hints
     Remember that the last element in ``genome`` is stored at position ``length(genome) - 1``.

   Solution
     Click *more...* to see the solution.

     .. container:: foldable

        .. includefrags:: demos/tutorial/sequences/assignment_1_solution.cpp
            :fragment: full

        Your output should look like this:

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

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

.. container:: assignment

   Type
     Review

   Objective
     In this assignment, you will do some simple string building tasks, and write a simple alignment of the given reads and chromosomes.
     Use the given code template to solve these subtasks:

   #. Assume we have mapped the reads to the positions 7, 100, 172, and 272 in 'chr1'.
      Store these positions in another string 'alignPosList'.
   #. Build another String bsChr1 as a copy of chr1, and exchange every 'C' with a 'T', as in a bisulfite treated genome.
   #. Print alignments of the reads and chr1 (or bschr1) using the function ``printAlign`` and the string ``alignPosList``.

    .. includefrags:: demos/tutorial/sequences/assignment_2_solution.cpp
          :fragment: one

    .. code-block:: cpp

        // Your code snippet here for 1.+2.

    .. includefrags:: demos/tutorial/sequences/assignment_2_solution.cpp
          :fragment: two

    .. code-block:: cpp

        // Your code snippet here for 3.

    .. includefrags:: demos/tutorial/sequences/assignment_2_solution.cpp
          :fragment: three

    .. code-block:: cpp

        // Your code snippet here for 3.

    .. includefrags:: demos/tutorial/sequences/assignment_2_solution.cpp
          :fragment: four

   Hints
     You have to create a copy of the fragment in chr1 (bsChr1) that is aligned to the read.

   Solution
     Click **more...** to see the solution.

     .. container:: foldable

        .. includefrags:: demos/tutorial/sequences/assignment_2_solution.cpp
          :fragment: full

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

Comparisons
^^^^^^^^^^^

Two sequences can be lexicographically **compared** using standard operators such as ``<`` or ``>=``.

.. includefrags:: demos/tutorial/sequences/example_comparisons.cpp
    :fragment: main

.. includefrags:: demos/tutorial/sequences/example_comparisons.cpp.stdout

Each comparison involves a scan of the two sequences for searching the first mismatch between the strings.
This could be costly if the two sequences share a long common prefix.
Suppose we want to branch in a program depending on whether ``a < b``, ``a == b``, or ``a > b``.

.. includefrags:: demos/tutorial/sequences/example_comparisons.cpp
    :fragment: first

In this case, although only one scan would be enough to decide what case is to be applied, each operator ``>`` and ``<`` performs a new comparison.
SeqAn offers the class :dox:`Lexical` to avoid unnecessary sequence scans.
Lexicals can store the result of a comparison, for example:

.. includefrags:: demos/tutorial/sequences/example_comparisons.cpp
    :fragment: second

Conversions
^^^^^^^^^^^

A sequence of type A values can be converted into a sequence of type B values, if A can be converted into B.
SeqAn offers different conversion alternatives.

**Copy conversion.**
The source sequence is copied into the target sequence.
This can be done by assignment (``operator=``) or using the function :dox:`AssignableConcept#assign`.

.. includefrags:: demos/tutorial/sequences/example_conversions_copy.cpp
    :fragment: main

.. includefrags:: demos/tutorial/sequences/example_conversions_copy.cpp.stdout

**Move conversion.**
If the source sequence is not needed any more after the conversion, it is always advisable to use :dox:`AssignableConcept#move` instead of :dox:`AssignableConcept#assign`.
The function :dox:`AssignableConcept#move` does not make a copy but can reuse the source sequence storage.
In some cases, :dox:`AssignableConcept#move` can also perform an in-place conversion.

.. includefrags:: demos/tutorial/sequences/example_conversions_move.cpp
    :fragment: main

.. includefrags:: demos/tutorial/sequences/example_conversions_move.cpp.stdout

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

.. container:: assignment

   Type
     Review

   Objective
     In this assignment you will sort nucleotides.
     Copy the code below. Adjust the code such that all nucleotides, which are lexicographically smaller than a Dna5 ``'G'`` are stored in a list ``lesser``, while all nucleotides which are greater, should be stored in a list ``greater``.
     Print out the final lists.

     .. includefrags:: demos/tutorial/sequences/assignment_3.cpp

   Solution
     Click **more...** to see the solution.

     .. container:: foldable

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

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

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

.. container:: assignment

   Type
     Transfer

   Objective
     In this task you will compare whole sequences.
     Reuse the code from above. Instead of a ``String<Dna5>`` we will now deal with a ``String<Dna5String>``.
     Build a string which contains the Dna5Strings "ATATANGCGT", "AAGCATGANT" and "TGAAANTGAC".
     Now check for all elements of the container, if they are lexicographically smaller or bigger than the  given subject sequence "GATGCATGAT" and append them to a appropriate list.
     Print out the final lists.

   Hints
     Try to avoid unnecessary sequence scans.

   Solution
     Click **more...** to see the solution.

     .. container:: foldable

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

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

.. _tutorial-datastructures-sequences-strings-and-segments-iterators:

Iteration
^^^^^^^^^

Very often you will be required to iterate over your string to either retrieve what's stored in the string or to write something at a specific position.
For this purpose SeqAn provides Iterators for all container types.
The metafunction :dox:`ContainerConcept#Iterator` can be used to determine the appropriate iterator type for a given a container.

An iterator always points to one value of the container.
The operator :dox:`IteratorAssociatedTypesConcept#operator*` can be used to access this value by reference.
Functions like :dox:`InputIteratorConcept#operator++(prefix)` or :dox:`BidirectionalIteratorConcept#operator--(prefix)` can be used to move the iterator to other values within the container.

The functions :dox:`ContainerConcept#begin` and :dox:`ContainerConcept#end`, applied to a container, return iterators to the begin and to the end of the container.
Note that similar to C++ standard library iterators, the iterator returned by :dox:`ContainerConcept#end` does not point to the last value of the container but to the position behind the last one.
If the container is empty then ``end() == begin()``.

The following code prints out a sequence and demonstrates how to iterate over a string.

.. includefrags:: demos/tutorial/iterators/base.cpp
    :fragment: use-case

.. includefrags:: demos/tutorial/iterators/base.cpp.stdout
    :fragment: use-case


Different Iterator Types
""""""""""""""""""""""""

Some containers offer several kinds of iterators, which can be selected by an optional template parameter of the Iterator class.
For example, the tag :dox:`ContainerIteratorTags#Standard` can be used to get an iterator type that resembles the C++ standard random access iterator.
For containers there is also a second variant available, the so called :dox:`ContainerIteratorTags#Rooted` iterator.
The rooted iterator knows its container by pointing back to it.
This gives us a nice interface to access member functions of the underlying container while operating on a rooted iterator.
The construction of an iterator in SeqAn, e.g. for a :dox:`DnaString Dna String`, could look like the following:

.. includefrags:: demos/tutorial/iterators/base.cpp
    :fragment: construction

.. tip::

   The default iterator implementation is :dox:`ContainerIteratorTags#Standard`.
   Rooted iterators offer some convenience interfaces for the user.
   They offer additional functions like :dox:`RootedIteratorConcept#container` for determining the container on which the iterator works, and they simplify the interface for other functions like :dox:`RootedIteratorConcept#atEnd`.
   Moreover, rooted iterators may change the container’s length or capacity, which makes it possible to implement a more intuitive variant of a remove algorithm.

   While rooted iterators can usually be converted into standard iterators, it is not always possible to convert standard iterators back into rooted iterators, since standard iterators may lack the information about the container they work on.
   Therefore, many functions that return iterators like :dox:`ContainerConcept#begin` or :dox:`ContainerConcept#end` return rooted iterators instead of standard iterators; this way, they can be used to set both rooted and standard iterator variables.
   Alternatively it is possible to specify the returned iterator type explicitly by passing the iterator kind as a tag argument, e.g. ``begin(str, Standard())``.

Assignment 5
""""""""""""

.. container:: assignment

   Type
     Review

   Objective
     Copy the code below, which replaces all N's of a given :dox:`String` with A's.
     Adjust the code to use iterators to traverse the container.
     Use the :dox:`ContainerIteratorTags#Standard` iterator.

     .. includefrags:: demos/tutorial/iterators/assignment_1.cpp

    Solution

      Click **more...** to see the solution.

      .. container:: foldable

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

Assignment 6
""""""""""""

.. container:: assignment

   Type
     Application

   Objective
     Use the code from above and change the :dox:`ContainerIteratorTags#Standard` to a :dox:`ContainerIteratorTags#Rooted` iterator.
     Try to shorten the code wherever possible.

   Solution
     Click **more...** to see the solution.

     .. container:: foldable

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

String Allocation Strategies
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Each sequence object has a capacity, i.e. the reserved space for this object.
The capacity can be set explicitly by functions such as :dox:`String#reserve` or :dox:`StringConcept#resize`.
It can also bet set implicitly by functions like :dox:`ContainerConcept#append`, :dox:`AssignableConcept#assign`, :dox:`StringConcept#insert` or :dox:`StringConcept#replace`, if the operation's result exceeds the length of the target sequence.

If the current capacity of a sequence is exceeded by chaining the length, we say that the sequence overflows.
There are several overflow strategies that determine what actually happens when a string should be expanded beyond its capacity.
The user can specify this for a function call by additionally handing over a tag.
If no overflow strategy is specified, a default overflow strategy is selected depending on the type of the sequence.

The following overflow strategies exist:

:dox:`OverflowStrategyTags#Exact`
  Expand the sequence exactly as far as needed. The capacity is only changed if the current capacity is not large enough.

:dox:`OverflowStrategyTags#Generous`
  Whenever the capacity is exceeded, the new capacity is chosen somewhat larger than currently needed.
  This way, the number of capacity changes is limited in a way that resizing the sequence only takes amortized constant time.

:dox:`OverflowStrategyTags#Limit`
  Instead of changing the capacity, the contents are limited to current capacity.
  All values that exceed the capacity are lost.

:dox:`OverflowStrategyTags#Insist`
  No capacity check is performed, so the user has to ensure that the container's capacity is large enough.

The next example illustrates how the different strategies could be used:

.. includefrags:: demos/tutorial/sequences_in_depth/example_overflow.cpp
   :fragment: example

.. includefrags:: demos/tutorial/sequences_in_depth/example_overflow.cpp.stdout

Assignment 7
""""""""""""

.. container:: assignment

   Type
     Review

   Objective
     Build a string of Dna (default specialization) and use the function ``appendValue`` to append a million times the nucleotide 'A'.
     Do it both using the overflow strategy ``Exact`` and ``Generous``.
     Measure the time for the two different strategies.

   Solution
      Click **more...** to see the solution.

      .. container:: foldable

         .. includefrags:: demos/tutorial/sequences_in_depth/assignment_exact_generous_solution.cpp

String Specializations
^^^^^^^^^^^^^^^^^^^^^^

The user can specify the kind of string that should be used in an optional second template argument of :dox:`String`.
The default string implementation is :dox:`AllocString Alloc String`.

.. includefrags:: demos/tutorial/sequences/base.cpp
    :fragment: sdefault_type

In most cases, the implementation :dox:`AllocString Alloc String` (the default when using a ``String<T>``) is the best choice.
Exceptions are when you want to process extremely large strings that are a bit larger than the available memory (consider :dox:`AllocString Alloc String`) or much larger so most of them are stored on the hard disk and only parts of them are loaded in main memory (consider :dox:`ExternalString External String`).
The following list describes in detail the different specializations:

Specialization :dox:`AllocString Alloc String`
  * **Description**
    Expandable string that is stored on the heap.
  * **Applications**
    The default string implementation that can be used for general purposes.
  * **Limitations**
    Changing the :dox:`StringConcept#capacity` can be very costly since all values must be copied.

Specialization :dox:`ArrayString Array String`
  * **Description**
    Fast but non-expandable string. Fast storing of fixed-size sequences.
  * **Limitations**
    :dox:`StringConcept#capacity Capacity` must already be known at compile time. Not suitable for storing large sequences.

Specialization :dox:`BlockString Block String`
  * **Description**
    String that stores its sequence characters in blocks.
  * **Applications**
    The :dox:`StringConcept#capacity` of the string can quickly be increased. Good choice for growing strings or stacks.
  * **Limitations**
    Iteration and random access to values is slightly slower than for :dox:`AllocString Alloc String`.

Specialization :dox:`PackedString Packed String`
  * **Description**
    A string that stores as many values in one machine word as possible.
  * **Applications**
    Suitable for storing large strings in memory.
  * **Limitations**
    Slower than other in-memory strings.

Specialization :dox:`ExternalString External String`
  * **Description**
    String that is stored in secondary memory.
  * **Applications**
    Suitable for storing very large strings (>2GB). Parts of the string are automatically loaded from secondary memory on demand.
  * **LimitationsApplications**
    Slower than other string classes.

Specialization :dox:`JournaledString Journaled String`
  * **Description**
    String that stores differences to an underlying text rather than applying them directly.
  * **Applications**
    Suitable for efficiently storing similar strings, if their differences to an underlying reference sequence are known.
  * **LimitationsApplications**
    Slower than other string classes, due to logarithmic penalty for random accesses.

Specialization :dox:`CStyleString CStyle String`
  * **Description**
    Allows adaption of strings to C-style strings.
  * **Applications**
    Used for transforming other String classes into C-style strings (i.e. null terminated char arrays). Useful for calling functions of C-libraries.
  * **Limitations**
    Only sensible if value type is ``char`` or ``wchar_t``.

.. includefrags:: demos/tutorial/sequences_in_depth/base.cpp
      :fragment: type_examples

.. includefrags:: demos/tutorial/sequences/base.cpp
    :fragment: external_string_spec

.. tip::

   String Simplify Memory Management

   One advantage of using Strings is that the user does not need to reserve memory manually with **new** and does not need **delete** to free memory.
   Instead, those operations are automatically handled by the :dox:`String` class.

   .. includefrags:: demos/tutorial/sequences/base.cpp
        :fragment: initialization_example

Segments
--------

The following section will introduce you into the :dox:`Segment` class of SeqAn.

:dox:`Segment Segments` are contiguous subsequences that represent parts of other sequences.
Therefore, their functionality is similar to the :dox:`String` functionality.
In SeqAn, there are three kinds of segments: :dox:`InfixSegment`, :dox:`PrefixSegment`, and :dox:`SuffixSegment`.
The metafunctions :dox:`SegmentableConcept#Infix`, :dox:`SegmentableConcept#Prefix`, and :dox:`SegmentableConcept#Suffix`, respectively, return the appropriate segment data type for a given sequence type.

For prefixes, we use the function :dox:`SegmentableConcept#prefix` to build the prefix.
The first parameter is the sequence we build the prefix from, the second the **excluding** end position.
For :dox:`SegmentableConcept#infix`\ es, we have to provide both the including start and the excluding end position.
For :dox:`SegmentableConcept#suffix`\ es, the second parameter of the function denotes the including starting position of the suffix:

.. includefrags:: demos/tutorial/sequences/example_segments.cpp
    :fragment: main

.. includefrags:: demos/tutorial/sequences/example_segments.cpp.stdout


Segments store a pointer on the underlying sequence object, the *host*, and an start and/or end position, depending on the type of segment.
The segment is *not* a copy of the sequence segment.

.. warning::

   Please note that it is not possible anymore to change the underlying sequence by changing the segment.
   If you want to change the host sequence, you have to explicitly modify this.
   If you want to modify only the segment, you have to explicitly make a copy of the string.

Assignment 8
^^^^^^^^^^^^

.. container:: assignment

   Type
     Application

   Objective
     In this task you will use a segment to pass over an infix of a given sequence to a function without copying the corresponding fragment.
     Use the code given below.
     Lets assume that we have given a ``genome`` and a ``read`` sequence as well as the begin position of a given alignment.
     In the main function a fragment of the Dna5String ``genome`` is copied and passed together with the Dna5String ``read`` to a ``print`` function.
     Adjust the code to use an infix of the genome, instead of copying the corresponding fragment.

     .. includefrags:: demos/tutorial/sequences/assignment_5_solution.cpp
          :fragment: top


     .. includefrags:: demos/tutorial/sequences/base.cpp
          :fragment: assignment5_code_to_change

     .. includefrags:: demos/tutorial/sequences/assignment_5_solution.cpp
          :fragment: bottom

   Solution
     Click **more...** to see the solution.

     .. container:: foldable

        .. includefrags:: demos/tutorial/sequences/assignment_5_solution.cpp
            :fragment: full

        .. includefrags:: demos/tutorial/sequences/assignment_5_solution.cpp.stdout

Assignment 9
^^^^^^^^^^^^

.. container:: assignment

   Type
     Review

   Objective
     Take the solution from the workshop assignment above and change it to use Segments for building the genome fragment.

   Hints
     Note that because ``printAlign`` uses templates, you don't have to change the function even though the type of ``genomeFragment`` is different.

   Solution
    Click **more...** to see the solution.

    .. container:: foldable

       .. includefrags:: demos/tutorial/sequences/assignment_6_solution.cpp

       .. includefrags:: demos/tutorial/sequences/assignment_6_solution.cpp.stdout