-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
330 lines (280 loc) · 10.7 KB
/
Copy pathmain.cpp
File metadata and controls
330 lines (280 loc) · 10.7 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
#include <iostream>
#include <cassert>
#include <string>
#include <cmath>
using namespace std;
bool isValidBase(char base){
if (base == 'A' || base == 'C' || base == 'G' || base == 'T'){
return true;
}else{
return false;
}
}
bool isValidStrand(string strand){
if (strand == ""){
return false;
}
for (char n : strand){
if (isValidBase(n) == false){
return false;
}
}
return true;
}
double strandSimilarity(string strand1, string strand2){
if (strand1.length() != strand2.length()){
return 0;
}else if(strand1 == "" || strand2 == ""){
return 0;
}
// why use size_t?
size_t i = 0;
double similar = 0.0, count = 0.0;
while (i < strand1.length()){
if (strand1[i] == strand2[i]){
similar ++;
}
count ++;
i ++;
}
return similar / count;
}
int bestStrandMatch(string input_strand, string target_strand){
//define similarity score:
double similarity = 0;
int position = 0;
//check if input lenght is longer than target length
if (input_strand.length() < target_strand.length()) {
cout << "Best similarity score: " << "0.0" << endl;
return -1;
}
//
for (unsigned int i = 0; i <= input_strand.length() - target_strand.length(); i ++){
double score = strandSimilarity(input_strand.substr(i, target_strand.length()), target_strand);
if (score > similarity){
similarity = score;
position = i;
}
}
cout << "Best similarity score: " << similarity << endl;
return position;
}
void identifyMutations(string input_strand, string target_strand){
// define variables:
string substring1, substring2;
double similarity = strandSimilarity(substring1, substring2);
int alignment;
// define alignment while also prints similarity for most cases
if (input_strand.size() > target_strand.size()){
alignment = bestStrandMatch(input_strand, target_strand);
}else if(input_strand.size() < target_strand.size()){
alignment = bestStrandMatch(target_strand, input_strand);
}else if(input_strand == target_strand){ // for completely similar cases
similarity = 1;
cout << "Best similarity score: " << similarity << endl;
alignment = 0;
}else{ //for cases that only need substitution
alignment = bestStrandMatch(target_strand, input_strand);
}
// define substring1 and substring 2
// first find substring so the two strings are the same length.
if (input_strand.size() > target_strand.size()){
substring1 = input_strand.substr(alignment, target_strand.length());
substring2 = target_strand;
}else if(input_strand.size() < target_strand.size()){
substring1 = input_strand;
substring2 = target_strand.substr(alignment, input_strand.length());
}else{
substring1 = input_strand;
substring2 = target_strand;
}
// print alignment:
cout << "Best alignment index: " << alignment << endl;
//check for insertion and deletion BEFORE substring1 and substring 2
if (input_strand.size() < target_strand.size()){
for (int i = 0; i < alignment; i++){
cout << "Insertion at position "<< i + 1<< ": " << target_strand[i] << " is inserted in target strand" << endl;
}
}else if(input_strand.size() > target_strand.size()){
for (int i = 0; i < alignment; i++){
cout << "Deletion at position "<< i + 1 << ": " << input_strand[i] << " is deleted in target strand" << endl;
}
}
//substitution in the MIDDLE
for (unsigned int i = 0; i < substring1.length(); i ++){
if (substring1[i] != substring2[i]){
cout << "Substitution at position " << alignment + i + 1 << ": " << substring1[i] << " -> " << substring2[i] << endl;
}
}
//check for insertion and deletion AFTER substring1 and substring 2
if (input_strand.size() < target_strand.size()){
for (unsigned int i = alignment + input_strand.length(); i < target_strand.length(); i++){
cout << "Insertion at position "<< i + 1<< ": " << target_strand[i] << " is inserted in target strand" << endl;
}
}else if(input_strand.size() > target_strand.size()){
for (unsigned int i = alignment + target_strand.length(); i < input_strand.length(); i++){
cout << "Deletion at position "<< i + 1 << ": " << input_strand[i] << " is deleted in target strand" << endl;
}
}else if(similarity == 1){
cout << "No mutations found." << endl;
return;
}
}
void transcribeDNAtoRNA(string strand){
for (unsigned int i = 0; i < strand.length(); i++){
if (strand[i] == 'T'){
cout << 'U';
}else{
cout << strand[i];
}
}
}
void reverseComplement(string strand){
for (int i = strand.length(); i >= 0; i--){
if (strand[i] == 'A'){
cout << 'T';
}else if (strand[i] == 'T'){
cout << 'A';
}else if (strand[i] == 'C'){
cout << 'G';
}else if (strand[i] == 'G'){
cout << 'C';
}
}
}
void getCodingFrames(string strand) {
bool found = false;
for (unsigned int i = 0; i <= strand.length() - 3; i++) {
if (strand.substr(i, 3) == "ATG") { // Start codon found
unsigned int j = i + 3;
while (j <= strand.length() - 3) {
string codon = strand.substr(j, 3);
if (codon == "TAA" || codon == "TAG" || codon == "TGA") { // Stop codon found
cout << strand.substr(i, j + 3 - i) << endl;
found = true;
i = j + 2; // Move `i` to the end of this frame
break;
}
j += 3;
}
}
}
if (!found) {
cout << "No reading frames found." << endl;
}
}
int main(){
int user_input = 0;
string sequence1, sequence2;
while (user_input != 7){
cout << R"(--- DNA Analysis Menu ---
1. Calculate the similarity between two sequences of the same length
2. Calculate the best similarity between two sequences of either equal or unequal length
3. Identify mutations
4. Transcribe DNA to RNA
5. Find the reverse complement of a DNA sequence
6. Extract coding frames
7. Exit
Please enter your choice (1 - 7): )" << endl;
cin >> user_input;
switch (user_input)
{
case 1:
cout << "Enter the first DNA sequence: " << endl;
cin >> sequence1;
while (!isValidStrand(sequence1)){
cout << "Invalid input. Please enter a valid sequence." << endl;
cout << "Enter the first DNA sequence: " << endl;
cin >> sequence1;
}
cout << "Enter the second DNA sequence: " << endl;
cin >> sequence2;
while (!isValidStrand(sequence2)){
cout << "Invalid input. Please enter a valid sequence." << endl;
cout << "Enter the second DNA sequence: " << endl;
cin >> sequence2;
}
if (strandSimilarity(sequence1, sequence2) == 0){
cout << "Error: Input strands must be of the same length." << endl;
}else{
cout << "Similarity score: " << strandSimilarity(sequence1, sequence2) << endl;
}
break;
case 2:
cout << "Enter the first DNA sequence: " << endl;
cin >> sequence1;
while (!isValidStrand(sequence1)){
cout << "Invalid input. Please enter a valid sequence." << endl;
cout << "Enter the first DNA sequence: " << endl;
cin >> sequence1;
}
cout << "Enter the second DNA sequence: " << endl;
cin >> sequence2;
while (!isValidStrand(sequence2)){
cout << "Invalid input. Please enter a valid sequence." << endl;
cout << "Enter the second DNA sequence: " << endl;
cin >> sequence2;
}
bestStrandMatch(sequence1, sequence2);
break;
case 3:
cout << "Enter the first DNA sequence: " << endl;
cin >> sequence1;
while (!isValidStrand(sequence1)){
cout << "Invalid input. Please enter a valid sequence." << endl;
cout << "Enter the first DNA sequence: " << endl;
cin >> sequence1;
}
cout << "Enter the second DNA sequence: " << endl;
cin >> sequence2;
while (!isValidStrand(sequence2)){
cout << "Invalid input. Please enter a valid sequence." << endl;
cout << "Enter the second DNA sequence: " << endl;
cin >> sequence2;
}
identifyMutations(sequence1, sequence2);
break;
case 4:
cout << "Enter the DNA sequence to be transcribed:" << endl;
cin >> sequence1;
while (!isValidStrand(sequence1)){
cout << "Invalid input. Please enter a valid sequence." << endl;
cout << "Enter the DNA sequence to be transcribed: " << endl;
cin >> sequence1;
}
cout << "The transcribed DNA is: ";
transcribeDNAtoRNA(sequence1);
cout << endl;
break;
case 5:
cout << "Enter the DNA sequence:" << endl;
cin >> sequence1;
while (!isValidStrand(sequence1)){
cout << "Invalid input. Please enter a valid sequence." << endl;
cout << "Enter the DNA sequence: " << endl;
cin >> sequence1;
}
cout << "The reverse complement is: ";
reverseComplement(sequence1);
cout << endl;
break;
case 6:
cout << "Enter the DNA sequence: " << endl;
cin >> sequence1;
while (!isValidStrand(sequence1)){
cout << "Invalid input. Please enter a valid sequence." << endl;
cout << "Enter the DNA sequence: " << endl;
cin >> sequence1;
}
cout << "The extracted reading frames are: " << endl;
getCodingFrames(sequence1);
break;
case 7:
cout << "Exiting program." << endl;
return 0;
default:
cout << "Invalid input. Please select a valid option." << endl;
break;
}
}
}