-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNA.cpp
309 lines (287 loc) · 7.1 KB
/
RNA.cpp
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
#include "RNA.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
char convert(int* code) {
if (code[0] == 0 && code[1] == 0) return 'A';
if (code[0] == 0 && code[1] == 1) return 'G';
if (code[0] == 1 && code[1] == 0) return 'C';
if (code[0] == 1 && code[1] == 1) return 'U';
}
void convert(int* code, char nukl) {
if (nukl == 'A') {
code[0] = 0;
code[1] = 0;
}
if (nukl == 'G') {
code[0] = 0;
code[1] = 1;
}
if (nukl == 'C') {
code[0] = 1;
code[1] = 0;
}
if (nukl == 'U') {
code[0] = 1;
code[1] = 1;
}
}
RNA::RNA() {
rna = new char [1];
usedlen = 0;
totallen = 4;
}
RNA::RNA(size_t length) {
size_t arsize = (length) / amountofnukl;
if ((length) % amountofnukl != 0) arsize++;
rna = new char[arsize];
usedlen = length;
totallen = arsize * amountofnukl;
}
RNA::RNA(const RNA& orig) {
size_t max = orig.totallen / amountofnukl;
rna = new char[max];
//for (size_t i = 0; i < max; i++) {
// rna[i] = orig.rna[i];
//}
memcpy(rna, orig.rna, orig.totallen / 4);
usedlen = orig.usedlen;
totallen = orig.totallen;
}
RNA::RNA(size_t length, char nukl) { //0 - adenin, 1 - guanin, 2 - citasin, 3 - uracil checked
size_t arsize = (length) / amountofnukl;
if ((length) % amountofnukl != 0) arsize++;
rna = new char[arsize];
totallen = arsize * amountofnukl;
usedlen = length;
int* code = new int[2];
convert(code, nukl);
size_t i = 0;
int spacecount = amountofnukl;
size_t charcount = 0;
rna[charcount] = 0;
for (i; i < usedlen; i++) { //filling with nukleotide
if (spacecount <= 0) {
charcount++;
spacecount = amountofnukl;
rna[charcount] = 0;
}
rna[charcount] = (((rna[charcount] << 1) + code[0]) << 1 )+ code[1];
spacecount--;
}
rna[charcount] = rna[charcount] << (spacecount * 2);
delete[] code;
}
RNA::~RNA() {
//std::cout << "destructor finished\n";
delete[] rna;
}
void RNA::place(size_t index, char nukl) {
size_t charnum = (index - 1) / amountofnukl;
size_t pos = (index - 1) % amountofnukl;
int* code = new int[2];
convert(code, nukl);
char copy = rna[charnum];
char mask1 = 0, mask2 = 0;
int i = 0;
for (i; i < 8; i++) {
mask1 = mask1 << 1;
mask2 = mask2 << 1;
if ((i == 2 * pos) || (i == 2 * pos + 1)) {
mask1 = mask1 + 0;
if (i == 2 * pos) mask2 = mask2 + code[0];
if (i == 2 * pos + 1) mask2 = mask2 + code[1];
}
else {
mask1 = mask1 + 1;
mask2 = mask2 + 0;
}
}
copy = copy & mask1;
copy = copy | mask2;
rna[charnum] = copy;
delete[] code;
}
char RNA::get(size_t index) const {
size_t charnum = (index - 1) / amountofnukl;
size_t pos = (index - 1) % amountofnukl;
char copy = rna[charnum];
int move = amountofnukl - pos - 1;
copy = copy >> move * 2;
int* code = new int[2];
code[1] = copy & 00000001;
code[0] = (copy >> 1) & 00000001;
char nukl = convert(code);
delete[] code;
return nukl;
}
RNA RNA::operator+(const RNA & added) {
size_t i = 1;
if (added.usedlen + usedlen > totallen) {
RNA result(added.usedlen + usedlen);
for (i = 0; i < totallen/4; i++) {
result.rna[i] = rna[i];
}
i = 1;
for (i; i <= added.usedlen; i++) {
char nukl = added.get(i);
result.place(i + usedlen, nukl);
}
return result;
}
else {
RNA result(*this);
i = 1;
for (i; i <= added.usedlen; i++) {
char nukl = added.get(i);
result.place(i + usedlen, nukl);
}
return result;
}
}
RNA RNA::operator+(char nukl) { //strategy of adding : *2
if (usedlen < totallen) {
RNA result(*this);
result.place(usedlen + 1, nukl);
result.usedlen++;
return result;
}
else {
RNA result(totallen * 2);
result.totallen = totallen * 2;
size_t arsize = totallen / amountofnukl;
//for (size_t i = 0; i < arsize; i++) {
// result.rna[i] = this->rna[i];
//}
memcpy(result.rna, rna, totallen / 4);
result.place(usedlen + 1, nukl);
result.usedlen = usedlen ++;
return result;
}
}
bool RNA::operator==(const RNA & another) {
if (usedlen != another.usedlen) return 0;
else {
size_t i = 0;
for (i; i < totallen / amountofnukl; i++) {
if (rna[i] != another.rna[i]) return 0;
}
}
return 1;
}
bool RNA::operator!=(const RNA& another) {
if (*this == another) return 0;
else return 1;
}
RNA RNA::split(size_t index) {
RNA second(usedlen - index);
size_t i = index + 1;
size_t arsize = (index) / amountofnukl;
if ((index) % amountofnukl != 0) arsize++;
char* array = new char[arsize];
memcpy(array, rna, arsize);
for (i; i <= usedlen; i++) {
char nukl = get(i);
second.place(i - index, nukl);
}
delete[] rna;
rna = array;
usedlen = index;
if (index % amountofnukl == 0) totallen = index;
else totallen = index + amountofnukl;
return second;
}
bool RNA::iscomplementary(RNA & another) {
if (usedlen != another.usedlen) return 0;
else {
size_t i = 1;
int* code1 = new int[2];
int* code2 = new int[2];
for (i; i <= usedlen; i++) {
char nukl1 = get(i);
char nukl2 = another.get(i);
convert(code1, nukl1);
convert(code2, nukl2);
if (2 * code1[0] + code1[1] + 2 * code2[0] + code2[1] != amountofnukl - 1) {
delete[] code1;
delete[] code2;
return 0;
}
}
delete[] code1;
delete[] code2;
}
return 1;
}
RNA RNA::operator!() {
size_t i;
RNA second(usedlen);
int* code = new int[2];
for (i = 0; i < usedlen; i++) {
char nukl = get(i + 1);
convert(code, nukl);
int reversecode = ((amountofnukl - 1) - (code[0] * 2 + code[1]));
code[0] = reversecode / 2;
code[1] = reversecode % 2;
nukl = convert(code);
second.place(i + 1, nukl);
}
delete[] code;
return second;
}
char RNA::operator[](size_t index) const {
if (index > usedlen) index = 1;
size_t charnum = (index - 1) / amountofnukl;
size_t pos = (index - 1) % amountofnukl;
char copy = rna[charnum];
int move = amountofnukl - pos - 1;
copy = copy >> move;
int* code = new int[2];
code[1] = copy & 00000001;
code[0] = (copy >> 1) & 00000001;
char nukl = convert(code);
delete[] code;
return nukl;
}
RNA& RNA::operator=(const RNA & second) {
if (*this == second) return *this;
else {
if (totallen != second.totallen){
delete[] rna;
char *array = new char[second.totallen / amountofnukl];
rna = array;
totallen = second.totallen;
}
usedlen = second.usedlen;
size_t arsize = totallen / amountofnukl;
for (size_t i = 0; i < arsize; i++) {
rna[i] = second.rna[i];
}
return *this;
}
}
Reference::Reference(RNA & rna1, size_t indx) {
index = indx;
link = &rna1;
//std::cout << "ref constructor finished\n";
}
Reference::~Reference() {
//std::cout << "ref destructor finished\n";
}
Reference& Reference::operator=(Reference second) {
(*link).place(index, (second.link)->get(second.index));
return *this;
}
Reference& Reference::operator=(char nukl) {
(*link).place(index, nukl);
return *this;
}
Reference RNA::operator[](size_t index) {
if (index > usedlen) index = 1;
Reference nukl(*this, index);
return nukl;
}
Reference::operator char() {
char nukl = (*link).get(index);
return nukl;
}