Debian Codegraph @

src/ref_seq.cpp

raw symbols SCIP
1 #include "ref_seq.h"
2
3 // Complement base array
4 // Source: https://github.com/samtools/samtools/blob/024e3d5ef0a2b0b7049f1fde6faebe8249988a06/faidx.c#L56
5 const unsigned char comp_base[256] = {
6 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
8 32, '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
9 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
10 '@', 'T', 'V', 'G', 'H', 'E', 'F', 'C', 'D', 'I', 'J', 'M', 'L', 'K', 'N', 'O',
11 'P', 'Q', 'Y', 'S', 'A', 'A', 'B', 'W', 'X', 'R', 'Z', '[', '\\',']', '^', '_',
12 '`', 't', 'v', 'g', 'h', 'e', 'f', 'c', 'd', 'i', 'j', 'm', 'l', 'k', 'n', 'o',
13 'p', 'q', 'y', 's', 'a', 'a', 'b', 'w', 'x', 'r', 'z', '{', '|', '}', '~', 127,
14 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
15 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
16 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
17 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
18 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
19 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
20 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
21 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
22 };
23
24 char ref_antd::get_base(int64_t pos, std::string region) { // 1-based position
25 int len;
26 char base = 0;
27 if (!region.empty() && this->fai != NULL) {
28 seq = fai_fetch(this->fai, region.c_str(), &len);
29 }
30 if (seq) base = *(seq + (pos - 1));
31 free(seq);
32 return base;
33 }
34
35 void ref_antd::reverse_complement_codon(char* codon) {
36 char temp = comp_base[(unsigned char)codon[2]];
37 codon[2] = comp_base[(unsigned char)codon[0]];
38 codon[0] = temp;
39 codon[1] = comp_base[(unsigned char)codon[1]];
40 }
41
42 char *ref_antd::get_codon(int64_t pos, std::string region,
43 gff3_feature feature) {
44 int len;
45 seq = fai_fetch(this->fai, region.c_str(), &len);
46 int64_t edit_pos = feature.get_edit_position(), codon_start_pos;
47 std::string edit_sequence = feature.get_edit_sequence();
48 int64_t edit_sequence_size = edit_sequence.size();
49 char *codon = new char[3];
50 int64_t edit_offset = 0;
51 if (pos > edit_pos + edit_sequence_size && edit_pos != -1) {
52 edit_offset =
53 (pos - edit_pos) > edit_sequence_size
54 ? edit_sequence_size
55 : (pos - edit_pos); // Account for edits in position of insertion
56 }
57 // codon_start_pos is w.r.t the reference sequence
58 if (feature.get_strand() == '-'){
59 codon_start_pos =
60 (feature.get_end() - 1) - feature.get_phase() -
61 (((feature.get_end() - feature.get_phase() - (pos + edit_offset))) /
62 3) *
63 3;
64 codon_start_pos -= 2; // This is to get to codon start from the 3' end and then take reverse complement
65 } else {
66 codon_start_pos =
67 (feature.get_start() - 1) + feature.get_phase() +
68 (((pos + edit_offset - (feature.get_start() + feature.get_phase()))) /
69 3) *
70 3;
71 }
72 for (int i = 0; i < 3; i++) {
73 if (codon_start_pos + i < (int32_t)feature.get_start() - 1 ||
74 codon_start_pos + i >
75 (int32_t)feature.get_end() -
76 1) { // If before or after CDS region return 'N'.
77 codon[i] = 'N';
78 } else if (codon_start_pos + i < edit_pos - 1 ||
79 edit_pos == -1) { // If before edit or with no edit
80 codon[i] = *(seq + codon_start_pos + i);
81 } else if (codon_start_pos + i >= edit_pos - 1 &&
82 codon_start_pos + i <= edit_pos - 1 + edit_sequence_size -
83 1) { // size() - 1 since edit_pos
84 // include one base already
85 codon[i] = edit_sequence[codon_start_pos + i - (edit_pos - 1)];
86 } else if (codon_start_pos + i > edit_pos - 1 + edit_sequence_size - 1) {
87 edit_offset = (codon_start_pos + i) - (edit_pos - 1) > edit_sequence_size
88 ? edit_sequence_size
89 : (codon_start_pos + i) - (edit_pos - 1);
90 codon[i] = *(seq + codon_start_pos + i - edit_offset);
91 }
92 }
93
94 if (feature.get_strand() == '-') {
95 reverse_complement_codon(codon);
96 }
97
98 free(seq);
99 return codon;
100 }
101
102 char *ref_antd::get_codon(int64_t pos, std::string region, gff3_feature feature,
103 char alt) {
104 int len;
105 seq = fai_fetch(this->fai, region.c_str(), &len);
106 int64_t edit_pos = feature.get_edit_position(), codon_start_pos;
107 std::string edit_sequence = feature.get_edit_sequence();
108 int64_t edit_sequence_size = edit_sequence.size();
109 char *codon = new char[3];
110 int i;
111 int64_t edit_offset = 0, alt_pos = pos;
112 if (pos > edit_pos + edit_sequence_size && edit_pos != -1) {
113 edit_offset =
114 (pos - edit_pos) > edit_sequence_size
115 ? edit_sequence_size
116 : (pos - edit_pos); // Account for edits in position of insertion
117 }
118 // TODO: Remove code duplication with function above
119 // codon_start_pos is w.r.t the reference sequence
120 if (feature.get_strand() == '-'){
121 codon_start_pos =
122 (feature.get_end() - 1) - feature.get_phase() -
123 (((feature.get_end() - feature.get_phase() - (pos + edit_offset))) /
124 3) *
125 3;
126 codon_start_pos -= 2; // This is to get to codon start from the 3' end and then take reverse complement
127 } else {
128 codon_start_pos =
129 (feature.get_start() - 1) + feature.get_phase() +
130 (((pos + edit_offset - (feature.get_start() + feature.get_phase()))) /
131 3) *
132 3;
133 }
134 for (i = 0; i < 3; ++i) {
135 if (codon_start_pos + i < edit_pos - 1 ||
136 edit_pos == -1) { // If before edit or with no edit
137 codon[i] = *(seq + codon_start_pos + i);
138 } else if (codon_start_pos + i >= edit_pos - 1 &&
139 codon_start_pos + i <= edit_pos - 1 + edit_sequence_size -
140 1) { // size() - 1 since edit_pos
141 // include one base already
142 codon[i] = edit_sequence[codon_start_pos + i - (edit_pos - 1)];
143 } else if (codon_start_pos + i > edit_pos - 1 + edit_sequence_size - 1) {
144 edit_offset = (codon_start_pos + i) - (edit_pos - 1) > edit_sequence_size
145 ? edit_sequence_size
146 : (codon_start_pos + i) - (edit_pos - 1);
147 codon[i] = *(seq + codon_start_pos + i - edit_offset);
148 }
149 }
150 // Recompute alt position
151 edit_offset = 0;
152 if (alt_pos > edit_pos + edit_sequence_size && edit_pos != -1) {
153 edit_offset =
154 (pos - edit_pos) > edit_sequence_size
155 ? edit_sequence_size
156 : (pos - edit_pos); // Account for edits in position of insertion
157 }
158 alt_pos += edit_offset;
159 codon[alt_pos - 1 - codon_start_pos] = alt;
160
161 if (feature.get_strand() == '-') {
162 reverse_complement_codon(codon);
163 }
164
165 free(seq);
166 return codon;
167 }
168
169 int ref_antd::add_gff(std::string path) {
170 // Read GFF file
171 if (!path.empty()) gff.read_file(path);
172 return 0;
173 }
174
175 int ref_antd::add_seq(std::string path) {
176 this->fai = NULL;
177 // Read reference file
178 if (!path.empty()) this->fai = fai_load(path.c_str());
179 if (!this->fai && !path.empty()) {
180 std::cout << "Reference file does not exist at " << path << std::endl;
181 return -1;
182 }
183 return 0;
184 }
185
186 ref_antd::ref_antd(std::string ref_path) {
187 this->seq = NULL;
188 this->add_seq(ref_path);
189 }
190
191 ref_antd::ref_antd(std::string ref_path, std::string gff_path) {
192 this->seq = NULL;
193 this->add_seq(ref_path);
194 this->add_gff(gff_path);
195 }
196
197 ref_antd::~ref_antd() {
198 if (this->fai) fai_destroy(this->fai);
199 }
200
201 // used to add codon info to variants output
202 int ref_antd::codon_aa_stream(std::string region,
203 std::ostringstream &line_stream,
204 std::ofstream &fout, int64_t pos, char alt) {
205 std::vector<gff3_feature> features = gff.query_features(pos, "CDS");
206 if (features.size() == 0) { // No matching CDS
207 fout << line_stream.str() << "NA\tNA\tNA\tNA\tNA\tNA" << std::endl;
208 return 0;
209 }
210 std::vector<gff3_feature>::iterator it;
211 char *ref_codon, *alt_codon;
212 for (it = features.begin(); it != features.end(); it++) {
213 fout << line_stream.str();
214 // add in gene level info, control for case it's not present
215 std::string gene = it->get_attribute("gene");
216 if (gene.empty()) {
217 fout << it->get_attribute("ID") << "\t";
218 } else {
219 fout << gene + ":" + it->get_attribute("ID") << "\t";
220 }
221 ref_codon = this->get_codon(pos, region, *it);
222 fout << ref_codon[0] << ref_codon[1] << ref_codon[2] << "\t";
223 fout << codon2aa(ref_codon[0], ref_codon[1], ref_codon[2]) << "\t";
224 alt_codon = this->get_codon(pos, region, *it, alt);
225 fout << alt_codon[0] << alt_codon[1] << alt_codon[2] << "\t";
226 fout << codon2aa(alt_codon[0], alt_codon[1], alt_codon[2]) << "\t";
227
228 // adding amino acid position
229 // factor in translation direction
230 char strand = it->get_strand();
231 int64_t aa_pos;
232 if (strand == '-') {
233 int64_t end = it->get_end();
234 aa_pos = ((end - pos) / 3) + 1;
235 } else { // when strand is equal to '+', '?', or others
236 int64_t start = it->get_start();
237 aa_pos = ((pos - start) / 3) + 1;
238 }
239 fout << aa_pos << std::endl;
240
241 delete[] ref_codon;
242 delete[] alt_codon;
243 }
244 return 0;
245 }
246
247 std::vector<gff3_feature> ref_antd::get_gff_features() {
248 return gff.get_features();
249 }