-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNA.cpp
More file actions
485 lines (456 loc) · 10.6 KB
/
RNA.cpp
File metadata and controls
485 lines (456 loc) · 10.6 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "RNA.h"
#include<array>
#include<list>
//default constructor to set default value to variables.
RNA::RNA()
{
this->type = mRNA;
}
//parameterize constructor to set sequence and type.
RNA::RNA(char * _seq, RNA_Type atype)
{
//copying value from parameter sequence to inherited sequence.
int length = 0;
//getting length of given sequence.
while(_seq[length] != '\0')
{
length++;
}
seq = new char[length + 1];
try
{
//checking if the length is divisible by 3
if(length%3 != 0){
throw 'd';
}
char s;
//checking if there is any errors in the input sequence.
for(int i = 0 ; i < length ; i++)
{
if((_seq[i] != 'A') && (_seq[i] != 'C') && (_seq[i] != 'G') && (_seq[i] != 'U'))
{
throw 99;
}
this->seq[i] = _seq[i];
Seqvalid = true;
}
}
catch(int x)
{
cout << "A, U, C, G are only acceptable. Created Failed." << endl;
Seqvalid = false;
}catch(char z){
cout << "Length must be divisible by 3. Creation failed" << endl;
Seqvalid = false;
}
seq[length] = '\0';
//setting RNA type to enumeration
int t;
tryAgainType:
try
{
if((atype != mRNA )&& (atype != pre_mRNA ) && (atype != mRNA_exon ) && (atype != mRNA_intron ))
{
throw 98;
}
this->type = atype;
}
catch(...)
{
Typevalid = false;
}
if(!Typevalid)
{
cout << endl << "Invalid RNA sequence Type !!!" << endl
<< endl <<"Type of RNA sequence must be one of the following 1-mRNA. \n2-pre_mRNA. \n3-mRNA_exon. \n4-mRNA_intron."<<endl<<
"Try Again : ";
cin >> t;
goto tryAgainType;
}
}
///Copy Constructor
RNA::RNA(RNA& rhs)
{
int length = 0;
while(rhs.seq[length] != '\0')
{
length++;
}
length++;
seq = new char[length];
for(int i=0; i<length; i++)
{
seq[i] = rhs.seq[i];
}
type = rhs.type;
}
///Destructor to delete sequence.
RNA::~RNA()
{
delete seq;
}
///virtual function to print RNA sequence and its type.
void RNA::Print()
{
if(Seqvalid)
{
cout << "RNA: ";
int i=0;
while(seq[i] != '\0')
{
cout << seq[i];
i++;
}
cout << endl << "RNA Type: ";
switch(type)
{
case 0:
cout << "mRNA" << endl;
break;
case 1:
cout << "pre_mRNA" << endl;
break;
case 2:
cout << "mRNA_exon" << endl;
break;
case 3:
cout << "mRNA_intron" << endl;
break;
}
}
else
{
cout << "Sorry can't print your wrong Data !!"<<endl;
}
}
//Function to convert from RNA to DNA.
DNA RNA::ConvertToDNA()
{
int length = 0;
while(seq[length] != '\0')
{
length++;
}
char* newSeq = new char[length + 1];
for(int i = 0 ; i < length ; i++)
{
if (seq[i]=='U')
{
newSeq[i]='T';
}
else
{
newSeq[i] = seq[i];
}
}
newSeq[length] = '\0';
start :
cout << "Enter a number for your type \n 0 for promoter \n 1 for motif \n 2 for tail \n 3 for noncoding " << endl;
char t;
cin >> t;
typeTry:
DNA_Type typee;
switch(t){
case '0':
typee = promoter;
break;
case '1':
typee = motif;
break;
case '2':
typee = tail;
break;
case '3':
typee = noncoding;
break;
default:
cout << "Enter a number between 0 and 3. Try again: " << endl;
cin >> t;
goto typeTry;
break;
}
DNA ob(newSeq, typee);
return ob;
}
///Function to convert from RNA to Protein.
Protien RNA::ConvertToProtein()
{
int length = 0;
while(seq[length]!='\0')
{
length++;
}
//sequence to store Protein in.
char* subSeq = new char[4];
char* NEWSeq = new char[length / 3 + 1];
//array to put each 3 characters in.
//setting every 3 characters in seq to newseq.
for (int i = 0; i < length ; i+=3)
{
//Setting sunSeq value.
subSeq[0] = seq[i];
subSeq[1] = seq[i+1];
subSeq[2] = seq[i+2];
subSeq[3] = '\0';
//sub character to store acid in .
char Acid;
//Setting Amino Acid to Acid .
Acid = codonsTable.getAminoAcid(subSeq).AminoAcid;
//checking if amino acid is correct.
NEWSeq[i/3] = Acid;
}
NEWSeq[length/3] = '\0';
Protien ob(NEWSeq);
return ob ;
}
//istream function to take RNA from user.
istream& operator >> (istream& in, RNA &rna2)
{
int len;
char s;
char t;
cout << "Please enter your length: ";
cin >> len;
while(len%3 != 0){
cout << "Length must be divisible by 3. Try again: " << endl;
cin >> len;
}
cout << "Enter a number for your type \n 0 for mRNA \n 1 for pre_mRNA \n 2 for mRNA_exon \n 3 for mRNA_intron " << endl;
cin >> t;
typeTry:
switch(t){
case '0':
rna2.type = mRNA;
break;
case '1':
rna2.type = pre_mRNA;
break;
case '2':
rna2.type = mRNA_exon;
break;
case '3':
rna2.type = mRNA_intron;
break;
default:
cout << "Enter a number between 0 and 3. Try again: " << endl;
cin >> t;
goto typeTry;
break;
}
rna2.seq = new char [len+1];
start:
cout << "Enter you sequence: ";
for(int i = 0 ; i < len; i++){
cin >> s;
if(s == 'A' || s == 'G' || s == 'U' || s == 'C'){
rna2.seq[i] = s;
}else{
cout << "Please enter only A or T or G or C" << endl;
goto start;
}
}
rna2.seq[len] = '\0';
return in;
}
ostream& operator<<(ostream& out, RNA &rna)
{
rna.Print();
return out;
}
//Function to add two RNA sequences .
RNA RNA::operator+(RNA &inseq)
{
int length1=0, length2=0;
while(seq[length1] != '\0'){
length1++;
}
length1++;
while(inseq.seq[length2] != '\0'){
length2++;
}
length2++;
int newLength = length2 + length1-1;
char *addedSeq = new char[newLength];
for(int i=0; i<length1-1; i++){
addedSeq[i]=seq[i];
}
for(int i=length1-1; i<newLength; i++){
addedSeq[i] = inseq.seq[abs(length1-1-i)];
}
RNA sum1(addedSeq, type);
return sum1;
}
bool RNA::operator==(RNA& inseq)
{
int length1 = 0;
int length2 = 0;
while(this->seq[length1] != '\0')
{
length1++;
}
while(inseq.seq[length2] != '\0')
{
length2++;
}
if(type == inseq.type)
{
if(length1 == length2)
{
for(int i = 0 ; i < length1 ; i++)
{
if(this->seq[i] != inseq.seq[i])
return false;
}
return true;
}
else
return false;
}
else
return false;
}
bool RNA::operator!=(RNA& inseq)
{
int length1 = 0;
int length2 = 0;
while(this->seq[length1] != '\0')
{
length1++;
}
while(inseq.seq[length2] != '\0')
{
length2++;
}
if(type == inseq.type)
{
if(length1 == length2)
{
for(int i = 0 ; i < length1 ; i++)
{
if(this->seq[i] != inseq.seq[i])
return true;
}
return false;
}
else
return true;
}
else
return true;
}
//function to load sequence from file.
void RNA::loadSequenceFromFile()
{
string fileName;
cout << "Enter the name of the file you want to load" << endl;
cin >> fileName;
fileName = fileName + ".txt";
ifstream loadSequence(fileName.c_str());
int length;
loadSequence >> length;
seq = new char[length+1];
for(int i = 0 ; i <length ; i++)
{
loadSequence >> seq[i];
}
seq[length] = '\0';
int h;
loadSequence >> h;
switch(h)
{
case 0:
type = mRNA;
break;
case 1:
type = pre_mRNA;
break;
case 2:
type = mRNA_exon;
break;
case 3:
type = mRNA_intron;
break;
}
}
//function to create new file for user and save it.
void RNA::saveSequenceToFile()
{
int length = 0;
while(seq[length] != '\0')
{
length ++;
}
string fileName;
cout << "Enter file name" << endl;
cin >> fileName;
fileName = fileName + ".txt";
ofstream createSequence(fileName.c_str());
createSequence << length << ' ';
for(int i=0; i<length; i++)
{
createSequence << seq[i];
}
createSequence << ' ' << type;
}
//function to check RNA sequence validation.
void RNA::checkRNAValidation()
{
int length = 0;
while(seq[length] != '\0')
{
length++;
}
for(int i = 0 ; i <length ; i++)
{
try
{
if((seq[i] != 'A') && (seq[i] != 'C') && (seq[i] != 'G') && (seq[i] != 'U'))
{
throw 99;
}
Seqvalid = true;
}
catch(...)
{
cout << endl << "Invalid RNA sequence !!!" << endl << endl << "RNA sequence must contain only A , C , G or U"<<endl;
cout << "Try Again : ";
Seqvalid = false;
}
}
}
//function to check RNA Type validation.
void RNA::check_RNAType_Validation(RNA_Type atype)
{
try
{
if((atype != mRNA )&& (atype != pre_mRNA ) && (atype != mRNA_exon ) && (atype != mRNA_intron ))
{
throw 98;
}
Typevalid = true;
}
catch(...)
{
cout << endl << "Invalid RNA sequence Type !!!" << endl
<< endl <<"Type of RNA sequence must be one of the following 1-mRNA. \n2-pre_mRNA. \n3-mRNA_exon. \n4-mRNA_intron.";
Typevalid = false;
}
}
void RNA::operator =(RNA &rna2)
{
int counter = 0;
while(rna2.seq[counter] != '\0')
{
counter +=1;
}
seq = new char[counter+1];
for(int i=0; i<counter; i++)
{
seq[i] = rna2.seq[i];
}
seq[counter] = '\0';
type = rna2.type;
}
char RNA::getElementInSeq(int index){
return seq[index];
}