File: alignment_file_snippets.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 (132 lines) | stat: -rw-r--r-- 3,557 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
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
#include <fstream>

#include <seqan3/core/debug_stream.hpp>
#include <seqan3/std/filesystem>

struct write_file_dummy_struct
{
    std::filesystem::path const file_path = std::filesystem::temp_directory_path()/"example.sam";

    write_file_dummy_struct()
    {

auto file_raw = R"//![sam_file](
@HD	VN:1.6	SO:coordinate
@SQ	SN:ref	LN:45
r001	99	ref	7	30	8M2I4M1D3M	=	37	39	TTAGATAAAGGATACTG	*
r003	0	ref	9	30	5S6M	*	0	0	GCCTAAGCTAA	*
r004	0	ref	16	30	6M14N5M	*	0	0	ATAGCTTCAGC	*
r003	2064	ref	29	17	5M	*	0	0	TAGGC	*
r001	147	ref	37	30	9M	=	7	-39	CAGCGGCAT	*	NM:i:1
)//![sam_file]";

        std::ofstream file{file_path};
        std::string str{file_raw};
        file << str.substr(1); // skip first newline
    }

    ~write_file_dummy_struct()
    {
        std::error_code ec{};
        std::filesystem::remove(file_path, ec);

        if (ec)
            seqan3::debug_stream << "[WARNING] Could not delete " << file_path << ". " << ec.message() << '\n';
    }
};

write_file_dummy_struct go{};

//![main]
#include <seqan3/io/alignment_file/all.hpp>

int main()
{
//![main]

{
//![writing]
    auto filename = std::filesystem::temp_directory_path()/"out.sam";

    seqan3::alignment_file_output fout{filename, seqan3::fields<seqan3::field::flag, seqan3::field::mapq>{}};

    size_t mymapq{0};
    seqan3::sam_flag flag{seqan3::sam_flag::unmapped};

    // ...

    fout.emplace_back(flag, mymapq);
    // or:
    fout.push_back(std::tie(flag, mymapq));
//![writing]
}

{
//![file_extensions]
    seqan3::debug_stream << seqan3::format_sam::file_extensions << '\n'; // prints [fastq,fq]
    seqan3::format_sam::file_extensions.push_back("sm");
//![file_extensions]
}

{
/*
//![filename_construction]
    seqan3::alignment_file_input fin_from_filename{"/tmp/my.sam"};

    seqan3::alignment_file_input fin_from_stream{std::cin, seqan3::format_sam{}};
//![filename_construction]
*/
}


{
//![read_custom_fields]
    auto filename = std::filesystem::temp_directory_path()/"example.sam";

    seqan3::alignment_file_input fin{filename, seqan3::fields<seqan3::field::id,
                                                              seqan3::field::seq,
                                                              seqan3::field::flag>{}};

    for (auto & [id, seq, flag /*order!*/] : fin)
    {
        seqan3::debug_stream << id << '\n';
        seqan3::debug_stream << seq << '\n';
        seqan3::debug_stream << flag << '\n';
    }
//![read_custom_fields]
}

{
//![alignments_without_ref]
    auto filename = std::filesystem::temp_directory_path()/"example.sam";

    seqan3::alignment_file_input fin{filename, seqan3::fields<seqan3::field::id, seqan3::field::alignment>{}};

    for (auto & [ id, alignment ] : fin)
    {
        seqan3::debug_stream << id << ": " << std::get<1>(alignment) << '\n';
    }
//![alignments_without_ref]
}

{
//![alignments_with_ref]
    using seqan3::operator""_dna5;

    auto filename = std::filesystem::temp_directory_path()/"example.sam";

    std::vector<std::string> ref_ids{"ref"}; // list of one reference name
    std::vector<seqan3::dna5_vector> ref_sequences{"AGAGTTCGAGATCGAGGACTAGCGACGAGGCAGCGAGCGATCGAT"_dna5};

    seqan3::alignment_file_input fin{filename, ref_ids, ref_sequences, seqan3::fields<seqan3::field::alignment>{}};

    for (auto & [ alignment ] : fin)
    {
        seqan3::debug_stream << alignment << '\n'; // Now you can print the whole alignment!
    }
//![alignments_with_ref]
}
std::filesystem::remove(std::filesystem::temp_directory_path()/"out.sam");
//![main_end]
}
//![main_end]