-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
95 lines (78 loc) · 2.63 KB
/
main.cpp
File metadata and controls
95 lines (78 loc) · 2.63 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
/** ------------------------------------------------------------------------
FindGeneSequences
main.cpp
Purpose: Displays possible gene sequences from a genome string.
@author Fion Chan
@since Apr 29, 2019
Notes:
- Output gene sequences exclude start and stop codons
- Frameshifts are not predicted
- UTRs are not removed
--------------------------------------------------------------------------*/
#include <iostream>
#include <string>
using namespace std;
// VARIABLES
static const string STOP_CODONS[3] = { "TAG", "TAA", "TGA" };
// FUNCTIONS
bool verifyGene(const string& sequence);
string geneBuilder(int start, int stop, const string& sequence);
void geneFinder(const string& sequence);
int main()
{
string genome;
cout << "Enter a DNA string:" << endl;
getline(cin, genome);
cout << "Possible gene sequences found:" << endl;
geneFinder(genome);
return 0;
}
/**
Purpose: Checks the sequence for nucleotide length divisible by 3,
and checks for the exclusion of start and stop codons.
@param sequence is a DNA sequence
@return true if sequence satisfies described purpose
@return false if sequence does not satisfy described purpose
*/
bool verifyGene(const string& sequence) {
if ((sequence.length() % 3) != 0) return false;
if (sequence.find("ATG") != -1) return false;
for (int i = 0; i < STOP_CODONS->size(); i++) {
if (sequence.find(STOP_CODONS[i]) != -1)
return false;
}
return true;
}
/**
Purpose: Builds a potential gene string, and checks it using verifyGene.
@param start is the start codon's starting position
@param stop is the stop codon's starting position
@param sequence is a DNA sequence
@return gene string if verifyGene returns true
@return empty string if verifyGene returns false
*/
string geneBuilder(int start, int stop, const string& sequence) {
int length = stop - start - 3;
string gene = sequence.substr(start + 3, length);
if (verifyGene(gene)) return gene;
return "";
}
/**
Purpose: Searches for and prints possible gene sequences from input.
@param sequence is a DNA sequence
*/
void geneFinder(const string& sequence) {
int start = sequence.find("ATG");
int stop;
string gene;
while (start != -1) {
for (int i = 0; i < STOP_CODONS->size(); i++) {
stop = sequence.find(STOP_CODONS[i], start + 3);
if (stop != -1) {
gene = geneBuilder(start, stop, sequence);
if (!gene.empty()) cout << gene << endl;
}
}
start = sequence.find("ATG", start + 1);
}
}