/
DataGenerator.cpp
294 lines (284 loc) · 9.7 KB
/
DataGenerator.cpp
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
/*
* DataGenerator.cpp
*
* Created on: Oct 27, 2014
* Author: ada
*/
#include <boost/program_options.hpp>
#include <fstream>
#include <iostream>
#include <string>
#include <boost/random/discrete_distribution.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/shared_ptr.hpp>
#include <math.h>
#include <vector>
boost::random::mt19937 rng;
namespace po = boost::program_options;
void addOptions(int argc, char* argv[], po::options_description& desc,
po::variables_map& vm);
void checkOptions(po::options_description& desc, po::variables_map& vm);
void printPar();
std::vector<int> GenerateQTLData(std::vector<boost::shared_ptr<short[]> > haps,
int snpnum, int ploidy, int samplenum,
std::string prefix);
std::vector<int> GenerateBinaryData(
std::vector<boost::shared_ptr<short[]> > haps, int snpnum, int casenum,
int ctrlnum, int ploidy, std::string prefix);
void printHap(std::vector<boost::shared_ptr<short[]> > haps,
std::vector<int> count, int snpnum);
std::vector<boost::shared_ptr<short[]> > GenerateHaplotype(int alleletype,
int hapnum,
int snpnum);
struct args {
args()
: casenum(100),
ctrlnum(100),
qtlnum(0),
ploidy(2),
snp(10),
output("output"),
hap(5),
seed(1234),
allele(2),
missing(0) {}
int casenum;
int ctrlnum;
int qtlnum;
int ploidy;
int snp;
int hap;
int allele;
float missing;
std::string output;
int seed;
} par;
int main(int argc, char* argv[]) {
po::options_description desc("Allowed options");
po::variables_map vm;
addOptions(argc, argv, desc, vm);
checkOptions(desc, vm);
printPar();
rng.seed(par.seed);
std::vector<boost::shared_ptr<short[]> > haplotypes =
GenerateHaplotype(par.allele, par.hap, par.snp);
std::vector<int> hapcount;
if (par.qtlnum == 0) {
hapcount = GenerateBinaryData(haplotypes, par.snp, par.casenum, par.ctrlnum,
par.ploidy, par.output);
} else {
hapcount = GenerateQTLData(haplotypes, par.snp, par.ploidy, par.qtlnum,
par.output);
}
printHap(haplotypes, hapcount, par.snp);
}
void addOptions(int argc, char* argv[], po::options_description& desc,
po::variables_map& vm) {
desc.add_options()("help", "produce help message")(
"case", po::value<int>(), "number of cases")("ctrl", po::value<int>(),
"number of controls")(
"qtl", po::value<int>(), "number of sample with quantitative trait")(
"snp", po::value<int>(), "number of snps")(
"ploidy", po::value<int>(), "number of ploidy")("hap", po::value<int>(),
"number of haplotypes")(
"allele", po::value<int>(), "number of allele types")(
"output", po::value<std::string>(), "output prefix")(
"seed", po::value<int>(), "seed for random generator")(
"missing", po::value<float>(), "missing rate");
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
}
void checkOptions(po::options_description& desc, po::variables_map& vm) {
if (vm.count("help")) {
std::cout << desc << "\n";
exit(0);
}
if (vm.count("ploidy")) {
par.ploidy = vm["ploidy"].as<int>();
};
if (vm.count("case") + vm.count("ctrl") == 1) {
throw std::runtime_error(
"case number and control number should both be given.");
}
if (vm.count("qtl") && (vm.count("case") || vm.count("ctrl"))) {
throw std::runtime_error(
"--qtl cannot be specified together with --case or --ctrl");
}
if (vm.count("case")) {
par.casenum = vm["case"].as<int>();
};
if (vm.count("qtl")) {
par.qtlnum = vm["qtl"].as<int>();
}
if (vm.count("ctrl")) {
par.ctrlnum = vm["ctrl"].as<int>();
};
if (vm.count("snp")) {
par.snp = vm["snp"].as<int>();
}
if (vm.count("seed")) {
par.seed = vm["seed"].as<int>();
}
if (vm.count("hap")) {
par.hap = vm["hap"].as<int>();
}
if (vm.count("allele")) {
par.allele = vm["allele"].as<int>();
}
if (vm.count("output")) {
par.output = vm["output"].as<std::string>();
}
if (vm.count("missing")) {
par.missing = vm["missing"].as<float>();
}
}
void printPar() {
std::cout << "parameters:\n";
if (0 == par.qtlnum) {
std::cout << "case: " << par.casenum << ", control: " << par.ctrlnum
<< "\n";
} else {
std::cout << "qtl sample: " << par.qtlnum << "\n";
}
std::cout << "snpnum: " << par.snp << ", ploidy: " << par.ploidy
<< ", allele types: " << par.allele << "\n";
std::cout << "seed: " << par.seed << ", hap: " << par.hap << "\n";
std::cout << "missing rate: " << par.missing << ", output: " << par.output
<< "\n";
}
int translateRandomNumber(int number) { return (number / 10 + 1); }
std::vector<boost::shared_ptr<short[]> > GenerateHaplotype(int alleletype,
int hapnum,
int snpnum) {
std::vector<boost::shared_ptr<short[]> > res;
boost::random::uniform_int_distribution<> index_dist(1, alleletype * 10 - 1);
for (int i = 0; i < hapnum; i++) {
boost::shared_ptr<short[]> sp(new short[snpnum]);
for (int j = 0; j < snpnum; j++) {
sp[j] = (short)translateRandomNumber(index_dist(rng));
} //
res.push_back(sp);
}
return res;
}
std::vector<int> GenerateBinaryData(
std::vector<boost::shared_ptr<short[]> > haps, int snpnum, int casenum,
int ctrlnum, int ploidy, std::string prefix) {
std::vector<int> hapcount;
hapcount.resize(haps.size(), 0);
std::ofstream casefile;
std::ofstream ctrlfile;
casefile.open((prefix + "_case.txt").c_str());
if (!casefile)
throw std::runtime_error("Unable to open output file: " + prefix +
"_case.txt");
ctrlfile.open((prefix + "_ctrl.txt").c_str());
if (!ctrlfile)
throw std::runtime_error("Unable to open output file: " + prefix +
"_ctrl.txt");
std::vector<int> selection;
boost::random::uniform_int_distribution<> HapSelection(0, haps.size() - 1);
boost::random::uniform_int_distribution<> Missing(0, 1000);
for (int i = 0; i < casenum; i++) {
casefile << "case" << i << " ";
selection.resize(ploidy, -1);
for (int p = 0; p < ploidy; p++) {
int idx = HapSelection(rng);
selection[p] = idx;
hapcount[idx]++;
}
for (int s = 0; s < snpnum; s++) {
std::vector<int> tmp;
for (int select = 0; select < selection.size(); select++) {
BOOST_ASSERT(selection[select] != -1);
if (Missing(rng) < par.missing * 1000)
tmp.push_back(0);
else
tmp.push_back(haps[selection[select]][s]);
}
std::random_shuffle(tmp.begin(), tmp.end());
for (int select = 0; select < selection.size(); select++) {
casefile << tmp[select] << " ";
}
}
casefile << "\n";
}
casefile.close();
for (int i = 0; i < ctrlnum; i++) {
ctrlfile << "ctrl" << i << " ";
selection.resize(ploidy, -1);
for (int p = 0; p < ploidy; p++) {
int idx = HapSelection(rng);
selection[p] = idx;
hapcount[idx]++;
}
for (int s = 0; s < snpnum; s++) {
std::vector<int> tmp;
for (int select = 0; select < selection.size(); select++) {
BOOST_ASSERT(selection[select] != -1);
if (Missing(rng) < par.missing * 1000)
tmp.push_back(0);
else
tmp.push_back(haps[selection[select]][s]);
}
std::random_shuffle(tmp.begin(), tmp.end());
for (int select = 0; select < selection.size(); select++) {
ctrlfile << tmp[select] << " ";
}
}
ctrlfile << "\n";
}
ctrlfile.close();
return hapcount;
}
std::vector<int> GenerateQTLData(std::vector<boost::shared_ptr<short[]> > haps,
int snpnum, int ploidy, int samplenum,
std::string prefix) {
std::vector<int> hapcount;
hapcount.resize(haps.size(), 0);
std::ofstream qtlfile;
qtlfile.open((prefix + "_qtl.txt").c_str());
if (!qtlfile)
throw std::runtime_error("Unable to open output file: " + prefix +
"_case.txt");
std::vector<int> selection;
boost::random::uniform_int_distribution<> HapSelection(0, haps.size() - 1);
boost::random::uniform_int_distribution<> Missing(0, 1000);
boost::normal_distribution<> normal(1, 10);
for (int i = 0; i < samplenum; i++) {
qtlfile << "qtl" << i << " " << abs(normal(rng)) << " ";
selection.resize(ploidy, -1);
for (int p = 0; p < ploidy; p++) {
int idx = HapSelection(rng);
selection[p] = idx;
hapcount[idx]++;
}
for (int s = 0; s < snpnum; s++) {
std::vector<int> tmp;
for (int select = 0; select < selection.size(); select++) {
BOOST_ASSERT(selection[select] != -1);
if (Missing(rng) < par.missing * 1000)
tmp.push_back(0);
else
tmp.push_back(haps[selection[select]][s]);
}
std::random_shuffle(tmp.begin(), tmp.end());
for (int select = 0; select < selection.size(); select++) {
qtlfile << tmp[select] << " ";
}
}
qtlfile << "\n";
}
qtlfile.close();
return hapcount;
}
void printHap(std::vector<boost::shared_ptr<short[]> > haps,
std::vector<int> count, int snpnum) {
for (int hap = 0; hap < haps.size(); hap++) {
for (int snp = 0; snp < snpnum; snp++) {
std::cout << haps[hap][snp];
}
std::cout << " :" << count[hap] << "\n";
}
}