File: parallel_align_pairwise_with_callback.cpp

package info (click to toggle)
seqan3 3.0.2%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,052 kB
  • sloc: cpp: 144,641; makefile: 1,288; ansic: 294; sh: 228; xml: 217; javascript: 50; python: 27; php: 25
file content (57 lines) | stat: -rw-r--r-- 2,236 bytes parent folder | download
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
#include <mutex>
#include <vector>

#include <seqan3/alignment/configuration/all.hpp>
#include <seqan3/alignment/pairwise/align_pairwise.hpp>
#include <seqan3/alphabet/nucleotide/dna4.hpp>
#include <seqan3/core/debug_stream.hpp>

int main()
{
    // Generate some sequences.
    using seqan3::operator""_dna4;
    using sequence_pair_t = std::pair<seqan3::dna4_vector, seqan3::dna4_vector>;
    std::vector<sequence_pair_t> sequences{100, {"AGTGCTACG"_dna4, "ACGTGCGACTAG"_dna4}};

    // Use edit distance with 4 threads.
    auto const alignment_config = seqan3::align_cfg::method_global{} |
                                  seqan3::align_cfg::edit_scheme |
                                  seqan3::align_cfg::parallel{4};

    // Compute the alignments in parallel and output them in order based on the input.
    for (auto && result : seqan3::align_pairwise(sequences, alignment_config))
        seqan3::debug_stream << result << '\n';

    seqan3::debug_stream << '\n';
    // prints:
    // [id: 0 score: -4]
    // [id: 1 score: -4]
    // [id: 2 score: -4]
    // [id: 3 score: -4]
    // [id: 4 score: -4]
    // [id: 5 score: -4]
    // ...
    // [id: 98 score: -4]
    // [id: 99 score: -4]

    // Compute the alignments in parallel and output them unordered using the callback (order is not deterministic).
    std::mutex write_to_debug_stream{}; // Need mutex to synchronise the output.
    auto const alignment_config_with_callback = alignment_config |
                                                seqan3::align_cfg::on_result{[&] (auto && result)
                                                {
                                                    std::lock_guard sync{write_to_debug_stream}; // critical section
                                                    seqan3::debug_stream << result << '\n';
                                                }};
    seqan3::align_pairwise(sequences, alignment_config_with_callback);  // seqan3::align_pairwise is now declared void.

    // might print:
    // [id: 0 score: -4]
    // [id: 1 score: -4]
    // [id: 2 score: -4]
    // [id: 6 score: -4]
    // [id: 7 score: -4]
    // [id: 3 score: -4]
    // ...
    // [id: 99 score: -4]
    // [id: 92 score: -4]
}