File: reference_sequence.h

package info (click to toggle)
vt 0.57721%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, trixie
  • size: 21,680 kB
  • sloc: cpp: 51,772; makefile: 348; sh: 206
file content (222 lines) | stat: -rw-r--r-- 5,316 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
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
/* The MIT License

   Copyright (c) 2015 Adrian Tan <atks@umich.edu>

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

#ifndef REFERENCE_SEQUENCE_H
#define REFERENCE_SEQUENCE_H

#include "utils.h"
#include "hts_utils.h"

/**
 * A Reference Sequence object wrapping htslib's faidx.
 * This allows for buffered reading of seqeunces.
 */
class ReferenceSequence
{
    public:

    //reference file and index
    std::string ref_fasta_file;
    faidx_t *fai;

    uint32_t buffer_size;
    uint32_t buffer_size_mask;
    uint32_t window_size;

    //main buffer sequence
    std::vector<char> seq;

    //secondary buffer sequence for wrap around cases of the sequence.
    std::vector<char> cseq;

    int32_t tid;
    uint32_t beg0, end0; // index of P for the start and end position in 0 base coordinates

    std::string chrom;
    uint32_t gbeg1;

    //  beg0 points to the beginning of the buffered sequence
    //  end0 points to the position after the end of the buffered sequence
    //
    //      end0
    //       |
    //       V
    //  +--+--+--+--+--+--+--+--+--+--+--+--+
    //  |  |  |  |  |  |  |  |  |  |  |  |  |
    //  +--+--+--+--+--+--+--+--+--+--+--+--+
    //       ^
    //       |
    //      beg0
    //
    //  when beg0==end0, buffered sequence is empty
    //  gbeg1 - is the coordinate of the genome position that beg0 represents.
    //
    //  invariance:  buffered sequence is always continuous

    int32_t debug;

    public:

    /**
     * Constructor.
     *
     * @k - size of buffered sequence is 2^k.
     */
    ReferenceSequence(std::string& ref_fasta_file, uint32_t k=10, uint32_t window_size=256);

    /**
     * Fetches the number of sequences.
     */
    int32_t fetch_nseq();

    /**
     * Fetch name of the ith sequence.
     */
    std::string fetch_iseq_name(int32_t i);

    /**
     * Fetch length of sequence seq.
     */
    int32_t fetch_seq_len(std::string& seq);

    /**
     * Get a base.
     */
    char fetch_base(const char* chrom, int32_t pos1);

    /**
     * Get a base.
     */
    char fetch_base(std::string& chrom, int32_t pos1);

    /**
     * Fetches sequence chrom:beg1-end1.
     *
     * Retrieved sequence is in seq with the length of n.
     */
    void fetch_seq(std::string& chrom, int32_t start1, int32_t end1, char* seq, int32_t n);

    /**
     * Fetches sequence chrom:beg1-end1.
     */
    void fetch_seq(std::string& chrom, int32_t beg1, int32_t end1, std::string& seq);

    /**
     * Fetches sequence chrom:beg1-end1.
     */
    void fetch_seq(const char* chrom, int32_t beg1, int32_t end1, std::string& seq);

    /**
     * Fetches sequence chrom:beg1-end1.
     */
    char* fetch_seq(const char* chrom, int32_t beg1, int32_t end1);

    /**
     * Fetches sequence chrom:beg1-end1.
     */
    char* fetch_seq(char* chrom, int32_t beg1, int32_t end1);

    /**
     * Fetches sequence chrom:beg1-end1.
     */
    char* fetch_seq(std::string& chrom, int32_t beg1, int32_t end1);

    private:

    /**
     * Overloads subscript operator for accessing buffered sequence positions.
     */
    char& operator[] (const int32_t i);

    /**
     * Returns the maximum size of the buffered sequence.
     */
    uint32_t max_size();

    /**
     * Returns the size of the buffered sequence.
     */
    uint32_t size();

    /**
     * Checks if buffer is empty
     */
    bool is_empty();

    /**
     * Set reference fasta file.
     */
    void set_reference(std::string& ref_fasta_file);

    /**
     * Set debug.
     */
    void set_debug(int32_t debug);

    /**
     * Sets tid.
     */
    void set_tid(uint32_t tid);

    /**
     * Gets tid.
     */
    uint32_t get_tid();

    /**
     * Sets chrom.
     */
    void set_chrom(std::string& chrom);

    /**
     * Gets chrom.
     */
    std::string get_chrom();

    /**
     * Gets window_size.
     */
    uint32_t get_window_size();

    /**
     * Converts gpos1 to index in P.
     */
    uint32_t g2i(uint32_t gpos1);

    /**
     * Returns the difference between 2 buffer positions
     */
    uint32_t diff(uint32_t i, uint32_t j);

    /**
     * Print buffered sequence state.
     */
    void print_state();

    /**
     * Print buffered sequence state extent.
     */
    void print_state_extent();
};

#endif