-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamgenerator.cpp
218 lines (199 loc) · 4.87 KB
/
gamgenerator.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
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <fstream>
#include <cassert>
#include <map>
#include <set>
using namespace std;
#define dbg(x) cerr << #x << "=" << x << endl
//#define DEBUG
struct GAMelem{
int node, basenum;
string base;
};
bool operator==(const GAMelem& a, const GAMelem& b){
if(a.node == b.node && a.base == b.base)return true;
else return false;
}
typedef vector<GAMelem> Haplo;
typedef vector<Haplo> GAM;
vector<string> split(const string &s, char c1, char c2) {
vector<string> elems;
string item;
int lev = 0;
for (char ch: s) {
if (ch == c1) {
lev++;
} else if(ch == c2) {
elems.push_back(item);
lev--;
item.clear();
} else if(lev > 0){
item += ch;
}
}
return elems;
}
string getfileName(string prf, string suf, int fileidx){
return "./testdata" + prf + to_string(fileidx) + suf;
}
GAM inputGAM(){
ifstream inputfile("input.txt");
string pathinfo, haploinfo;
Haplo inputhaplo;
getline(inputfile, pathinfo);
getline(inputfile, haploinfo);
inputfile.close();
map<int, int> node2idx;
GAM inputgam;
// get GAM data
haploinfo = haploinfo.substr(1,haploinfo.size()-2);
vector<string> gs = split(haploinfo, '(', ')');
for(string str : gs){
int node = 0, basenum;
string base;
int idx = 0;
while(idx < (int)str.size() && str[idx] != ','){
node *= 10;
node += str[idx++] - '0';
}
idx += 3;
while(idx < (int)str.size() && str[idx] != '\''){
base += str[idx++];
}
basenum = base.size();
inputhaplo.push_back((GAMelem){node, basenum, base});
node2idx[node] = inputhaplo.size() - 1;
}
// get path info
pathinfo = pathinfo.substr(1,pathinfo.size()-2);
vector<string> hs = split(pathinfo, '[', ']');
for(string str : hs){
int idx = 0;
Haplo path;
while(1){
int node = 0;
while(idx < (int)str.size() && isdigit(str[idx])){
node *= 10;
node += str[idx++] - '0';
}
path.push_back(inputhaplo[node2idx[node]]);
if(idx == (int)str.size() )break;
while(idx < (int)str.size() && !isdigit(str[idx])){
idx++;
}
}
inputgam.push_back(path);
}
return inputgam;
}
void outputGAM(const GAM& hs, string filename){
ofstream ofs(filename);
for(Haplo h : hs){
ofs << h[0].node;
for(int i = 1; i < (int)h.size(); i++){
ofs << "," << h[i].node;
}
ofs << endl;
}
ofs << "0" << endl; // END-MARKER
set<pair<int,string> > seqset;
for(Haplo h : hs){
for(GAMelem e : h){
seqset.insert(pair<int,string>(e.node,e.base));
}
}
for(pair<int,string> p : seqset){
ofs << p.first << "," << p.second << endl;
}
ofs << "0" << endl;
ofs.close();
}
Haplo createHaplo(const GAM& gam){
int haploNum = gam.size();
random_device rnd;
mt19937 mt(rnd());
uniform_int_distribution<> randh(0, haploNum - 1);
uniform_int_distribution<> rand100(0, 99);
int crtHaplo = randh(mt); // select random haplo
int crtNode = gam[crtHaplo][0].node;
double piC = 0.7; // current piC
Haplo resultHaplo;
for(int i = 0; ; ){
resultHaplo.push_back((GAMelem){crtNode, gam[crtHaplo][i].basenum, gam[crtHaplo][i].base});
vector<pair<int,int> > canditate;
for(int j = 0; j < haploNum; j++){
for(int k = 0; k < (int)gam[j].size(); k++){
if(gam[j][k].node != crtNode) continue;
canditate.push_back(pair<int,int>(j,k));
break;
}
}
double piR = (1.0 - piC) / (double) canditate.size();
for(int cnt = 0; cnt < gam[crtHaplo][i].basenum; cnt++){
double p = (double) rand100(mt) / 100.0;
if(p >= piC){
int nxtHaploidx = (int)floor((p - piC) / piR);
nxtHaploidx = min(nxtHaploidx, (int)canditate.size() - 1);
crtHaplo = canditate[nxtHaploidx].first;
i = canditate[nxtHaploidx].second;
}
}
if(i + 1 < (int)gam[crtHaplo].size()) {
crtNode = gam[crtHaplo][i+1].node; i++;
} else break;
}
return resultHaplo;
}
GAM gamGenerate(const GAM& gam){
GAM resultSet;
while(1){
Haplo newh = createHaplo(gam);
bool ok = true;
for(Haplo h : gam){
if(newh == h){
ok = false;
break;
}
}
if(ok){
resultSet.push_back(newh);
break;
}
}
return resultSet;
}
int main(int argc, char *argv[]){
GAM inputgam = inputGAM();
#ifdef DEBUG
// for DEBUG
for(Haplo h : inputgam){
cerr << " ===== " << endl;
int len = 0;
for(GAMelem g : h){
cerr << g.node << " " ;
len += g.basenum;
}
cerr << endl;
dbg(len);
}
cerr<<endl;
#endif
GAM resultgam = gamGenerate(inputgam);
#ifdef DEBUG
// for DEBUG
for(Haplo h : resultgam){
cerr << " ===== " << endl;
int len = 0;
for(GAMelem g : h){
cerr << g.node << " ";
len += g.basenum;
}
cerr << endl;
}
#endif
outputGAM(resultgam, "output.csv");
return 0;
}