-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.cc
196 lines (162 loc) · 5.64 KB
/
generator.cc
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
/*
*
*
*/
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_set>
#include <vector>
using namespace std;
const string kTestFileName = "test.fasta";
const string kTrainFileName = "train.fasta";
//-----------------------------------------------------------------------------
static vector<char> kNucleotides = { 'A', 'C', 'G', 'T' };
//-----------------------------------------------------------------------------
template<typename T>
T Shuffle(T permutation) {
for (int ii = 0; ii < permutation.size(); ++ii) {
int rand_offset = rand() % (permutation.size() - ii);
swap(permutation[ii], permutation[ii + rand_offset]);
}
return permutation;
}
//-----------------------------------------------------------------------------
string CreateRandomSequence(int sequence_length,
bool with_break_lines = true);
//-----------------------------------------------------------------------------
void WriteTestDataFile(const string& filename,
const string& motif_string,
int num_mutations,
int sequence_length,
int num_sequences);
//-----------------------------------------------------------------------------
void PrintSequenceWithMutations(ofstream& ofs,
const string& motif_string,
int sequence_length,
int num_mutations);
//-----------------------------------------------------------------------------
int main(int argc, char *argv[]) {
srand(time(NULL));
if (argc != 5) {
cout << "Incorret usage, expected: "
<< "./a.out <motif_length> <sequence_length> <num_mutations> "
<< "<num_sequences>" << endl;
return -1;
}
int motif_length = atoi(argv[1]);
const int sequence_length = atoi(argv[2]);
if (sequence_length < motif_length) {
cout << "Invalid input, sequence must be at least the size of the motif"
<< endl;
return -1;
}
string motif_string =
CreateRandomSequence(motif_length, false /* with_line_breaks */);
const int num_mutations = atoi(argv[3]);
const int num_sequences = atoi(argv[4]);
int motif_offset = sequence_length / 2;
assert((sequence_length / 2) > motif_string.size());
WriteTestDataFile(kTestFileName,
motif_string,
num_mutations,
sequence_length,
num_sequences);
/*
WriteTestDataFile(kTrainFileName,
motif_string,
num_mutations,
sequence_length,
num_sequences);
*/
}
//-----------------------------------------------------------------------------
void WriteTestDataFile(const string& filename,
const string& motif_string,
const int num_mutations,
const int sequence_length,
const int num_sequences) {
ofstream ofs;
ofs.open(filename.c_str(), std::ofstream::out);
if (!ofs.good()) {
cout << "Failed to open " << filename << endl;
}
ofs << ">Motif-" << motif_string << ":" << "size="
<< motif_string.size() << ":" << "num_mutations=" << num_mutations
<< '\n';
ofs << ">Offset: " << sequence_length / 2<< endl;
for (int ii = 0; ii < num_sequences; ++ii) {
ofs << ">Sequence-" << ii << ":\n";
PrintSequenceWithMutations(ofs, motif_string, sequence_length,
num_mutations);
if (!ofs.good()) {
cout << "Failed to write to " << filename << endl;
}
}
ofs.close();
}
//-----------------------------------------------------------------------------
char RandomMutation(char c) {
char m = c;
while (m == c) {
int rand_index = rand() % 4;
m = kNucleotides[rand_index];
}
return m;
}
//-----------------------------------------------------------------------------
string CreateRandomSequence(int sequence_length,
bool with_line_breaks) {
stringstream ss;
for (int ii = 0; ii < sequence_length; ++ii) {
if (with_line_breaks &&
ii % 60 == 0 && ii != 0) {
ss << '\n';
}
int rand_index = rand() % 4;
ss << kNucleotides[rand_index];
}
return ss.str();
}
//-----------------------------------------------------------------------------
void PrintSequenceWithMutations(ofstream& ofs,
const string& motif_string,
const int sequence_length,
const int num_mutations) {
assert(num_mutations < motif_string.size());
int motif_offset = sequence_length / 2;
// Add space to account for added endlines.
motif_offset += motif_offset / 60;
vector<int> shuffled_indices;
for (int ii = 0; ii < motif_string.size(); ++ii) {
shuffled_indices.push_back(ii);
}
shuffled_indices = Shuffle(shuffled_indices);
unordered_set<int> mutated_indices;
for (int ii = 0; ii < num_mutations; ++ii) {
mutated_indices.insert(shuffled_indices[ii]);
}
string new_sequence = CreateRandomSequence(sequence_length);
// Now overwrite the section where the motif is.
int kk = motif_offset;
int ii = 0;
while (ii < motif_string.size()) {
assert(kk < new_sequence.size());
if (new_sequence[kk] == '\n') {
++kk;
continue;
}
if (mutated_indices.count(ii) > 0) {
new_sequence[kk] = RandomMutation(motif_string[ii]);
} else {
new_sequence[kk] = motif_string[ii];
}
++kk;
++ii;
}
ofs << new_sequence << '\n';
}
//-----------------------------------------------------------------------------