-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
167 lines (132 loc) · 4.51 KB
/
Copy pathMain.cpp
File metadata and controls
167 lines (132 loc) · 4.51 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Aminoacid.h"
#include "Protein.h"
#include "ORF.h"
using namespace std;
string readSequence(const string& path) {
string seq;
ifstream file(path);
if (file.is_open()) {
getline(file, seq);
file.close();
}
return seq;
}
string transcribe(string dna) {
for (char& c : dna) {
if (c == 'T') c = 'U';
}
return dna;
}
// zwraca wektor wskaznikow do dynamicznie alokowanych orfow.
vector<ORF*> findProteins(const string& seq) {
vector<ORF*> proteins;
const char* mrna = seq.c_str(); // wskaznik na poczatek bufora mrna
for (int frame = 0; frame < 3; ++frame) {
size_t pos = frame;
while (true) {
size_t start = seq.find("AUG", pos);
if (start == string::npos) break;
if ((start - frame) % 3 != 0) {
pos = start + 1;
continue;
}
Protein p;
bool foundStop = false;
for (size_t i = start; i + 3 <= seq.size(); i += 3) {
Aminoacid a(mrna + i);
if (a.aminoacid == '*') {
foundStop = true;
pos = i + 3;
break;
}
p.push(a);
}
proteins.push_back(new ORF{frame, p, foundStop}); // alokacja dynamiczna
if (!foundStop) break;
}
}
return proteins;
}
void printProteins(const vector<ORF*>& proteins) {
cout << "Liczba bialek: " << proteins.size() << endl << endl;
for (size_t i = 0; i < proteins.size(); ++i) {
const ORF* orf = proteins[i];
cout << "Bialko " << i + 1 << " (ORF " << orf->frame + 1 << ")";
if (!orf->complete) cout << " (brak kodonu STOP)";
cout << ":" << endl;
string seqProt = orf->protein.getProteinSequence();
cout << "Sekwencja: " << seqProt << endl;
cout << "Liczba aminokwasow: " << seqProt.size() << endl;
cout << "Czestosci aminokwasow:" << endl;
for (const auto& [aa, count] : orf->protein.countAminoacids()) {
cout << aa << " " << count << endl;
}
cout << endl;
}
}
// showincomplete pomija bialka bez kodonu stop
void printProteins(const vector<ORF*>& proteins, bool showIncomplete) {
size_t shown = 0;
for (const ORF* orf : proteins) {
if (orf->complete || showIncomplete) shown++;
}
cout << "Liczba bialek: " << shown << endl << endl;
int index = 0;
for (size_t i = 0; i < proteins.size(); ++i) {
const ORF* orf = proteins[i];
if (!orf->complete && !showIncomplete) continue;
++index;
cout << "Bialko " << index << " (Ramka " << orf->frame + 1 << ")";
if (!orf->complete) cout << " [brak kodonu STOP]";
cout << ":" << endl;
string seqProt = orf->protein.getProteinSequence();
cout << "Sekwencja: " << seqProt << endl;
cout << "Liczba aminokwasow: " << seqProt.size() << endl;
cout << "Czestosci aminokwasow:" << endl;
for (const auto& [aa, count] : orf->protein.countAminoacids()) {
cout << aa << " " << count << endl;
}
cout << endl;
}
}
void freeProteins(vector<ORF*>& proteins) {
for (ORF* orf : proteins) {
delete orf;
}
proteins.clear();
}
// postanowilem ze bardziej "biologicznym" podejsciem bedzie zrobienie po prostu trzech ramek odczytu zamiast
// tworzenia jednego ORFa w miejscu gdzie pojawia sie pierwszy kodon start
// poslugiwalem sie tym toolem do weryfikacji wynikow: https://web.expasy.org/translate/
int main(int argc, char* argv[]) {
if (argc < 2) {
cerr << "Usage: " << argv[0]
<< " <file.txt> [--show-incomplete]" << endl;
return 1;
}
string path = argv[1];
bool showIncomplete = false; // domyslnie nie pokazujemy niekompletnych
for (int i = 2; i < argc; ++i) {
string arg = argv[i];
if (arg == "--show-incomplete") {
showIncomplete = true;
} else {
cerr << "Unknown option: " << arg << endl;
return 1;
}
}
string dna = readSequence(path);
if (dna.empty()) {
cerr << "Unable to read file: " << path << endl;
return 1;
}
string mrna = transcribe(dna); // transkrypcja
vector<ORF*> proteins = findProteins(mrna);
printProteins(proteins, showIncomplete); // wypisywanie wyniku
freeProteins(proteins); // trzeba po sobie posprzatac
return 0;
}