-
Notifications
You must be signed in to change notification settings - Fork 1
/
scan_sel_new.cc
269 lines (269 loc) · 5.38 KB
/
scan_sel_new.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
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <map>
#include <time.h>
using namespace std;
vector <char> new_seq;
map <string, long int> chr2pos;
vector <long int> chrpos;
double cvg_gc,cvg_n;
double cur_gc_r,cur_n_r;
int cur_gc,cur_n;
int fold=1;
long int genome_size;
char cur_nt;
struct fea
{
string chr_nm;
long int abs_ps;
long int chr_ps;
int win_ln;
double gc_rt;
double n_rt;
int status;
};
vector <fea> res;
void read_chr(const char *chr_path)
{
char fn[400];
int i;
string chr_name,temp;
for(i=0;i<23;i++)
{
if(i==22)
{
sprintf(fn,"%s/chrX.fa",chr_path);
chr_name="chrX";
}
else if(i==23)
{
sprintf(fn,"%s/chrY.fa",chr_path);
chr_name="chrY";
}
else
{
sprintf(fn,"%s/chr%d.fa",chr_path,i+1);
char tt[10];
sprintf(tt,"%d",i+1);
chr_name="chr"+string(tt);
}
cout<<chr_name<<" "<<fn<<endl;
int j;
ifstream cfl;
cfl.open(fn);
getline(cfl,temp);
chr2pos[chr_name]=new_seq.size();
chrpos.push_back(new_seq.size());
cout<<new_seq.size()<<endl;
while(!cfl.eof())
{
getline(cfl,temp);
for(j=0;j<temp.length();j++)
{
new_seq.push_back(temp[j]);
}
}
cfl.close();
}
genome_size=new_seq.size();
}
void output(const char* path)
{
ofstream res_fl;
res_fl.open(path,ios::out);
char log_fl[400];
sprintf(log_fl,"%s.log",path);
ofstream res_lg;
res_lg.open(log_fl,ios::out);
long int i,j;
for(i=0;i<res.size();i++)
{
res_fl<<">hg19:"<<res[i].chr_nm<<":"<<res[i].chr_ps<<"-"<<res[i].chr_ps+res[i].win_ln-1<<endl;
res_lg<<res[i].chr_nm<<":"<<res[i].chr_ps<<"-"<<res[i].chr_ps+res[i].win_ln-1<<"\t"<<res[i].gc_rt<<"\t"<<res[i].n_rt<<"\t"<<res[i].status<<endl;
for(j=res[i].abs_ps;j<res[i].abs_ps+res[i].win_ln;j++)
{
res_fl<<new_seq[j];
}
res_fl<<endl;
}
}
void plus_cur()
{
switch(cur_nt)
{
case 'N': cur_n++; break;
case 'X': cur_n++; break;
case 'G': cur_gc++; break;
case 'C': cur_gc++; break;
}
}
void minus_cur()
{
switch(cur_nt)
{
case 'N': cur_n--; break;
case 'X': cur_n--; break;
case 'G': cur_gc--; break;
case 'C': cur_gc--; break;
}
}
void compute_res(long int st,int len, double gc_r,double n_r,int tag)
{
int i;
string temp;
//cout<<st<<endl;
for(i=0;i<chrpos.size();i++)
{
if(chrpos[i]>st)
break;
}
fea temp_fea;
stringstream ss;
ss<<i;
temp="chr"+ss.str();
if(i==23)
temp="chrX";
if(i==24)
temp="chrY";
temp_fea.abs_ps=st;
temp_fea.chr_nm=temp;
temp_fea.chr_ps=st-chrpos[i-1]+1;
temp_fea.win_ln=len;
temp_fea.gc_rt=gc_r;
temp_fea.n_rt=n_r;
temp_fea.status=tag;
res.push_back(temp_fea);
}
int scan(int req_len,double req_gc,double req_n)
{
long int j,best_p;
srand((unsigned)time(NULL)+rand());
long int ran_st=(long int)((double)rand()/(double)RAND_MAX*(double)genome_size-(double)req_len);
//long int ran_st=(int)(rand()%(genome_size-req_len));
//cout<<ran_st<<endl;
cur_gc=0;
cur_n=0;
double diff_gc_r,diff_n_r,best_r,best_gc_r,best_n_r;
for(j=ran_st;j<ran_st+req_len;j++)
{
cur_nt=new_seq[j];
plus_cur();
}
cur_gc_r=(double)cur_gc/(double)(req_len-cur_n);
cur_n_r=(double)cur_n/(double)req_len;
diff_gc_r=fabs(cur_gc_r-req_gc);
diff_n_r=fabs(cur_n_r-req_n);
best_r=diff_gc_r+diff_n_r; best_p=ran_st; best_gc_r=cur_gc_r; best_n_r=cur_n_r;
if(diff_gc_r<=cvg_gc && diff_n_r<=cvg_n)
{
compute_res(ran_st, req_len, cur_gc_r, cur_n_r, 1);
return 1;
}
for(j=ran_st+1;j<genome_size-req_len;j++)
{
cur_nt=new_seq[j-1];
minus_cur();
cur_nt=new_seq[j+req_len-1];
plus_cur();
cur_gc_r=(double)cur_gc/(double)(req_len-cur_n);
cur_n_r=(double)cur_n/(double)req_len;
diff_gc_r=fabs(cur_gc_r-req_gc);
diff_n_r=fabs(cur_n_r-req_n);
if(diff_gc_r+diff_n_r<best_r)
{best_r=diff_gc_r+diff_n_r; best_p=j; best_gc_r=cur_gc_r; best_n_r=cur_n_r;}
if(diff_gc_r<=cvg_gc && diff_n_r<=cvg_n)
{
compute_res(j, req_len, cur_gc_r, cur_n_r, 1);
return 1;
}
}
cur_gc=0;
cur_n=0;
for(j=0;j<req_len;j++)
{
cur_nt=new_seq[j];
plus_cur();
}
cur_gc_r=(double)cur_gc/(double)(req_len-cur_n);
cur_n_r=(double)cur_n/(double)req_len;
diff_gc_r=fabs(cur_gc_r-req_gc);
diff_n_r=fabs(cur_n_r-req_n);
if(diff_gc_r+diff_n_r<best_r)
{best_r=diff_gc_r+diff_n_r; best_p=0; best_gc_r=cur_gc_r; best_n_r=cur_n_r;}
if(diff_gc_r<=cvg_gc && diff_n_r<=cvg_n)
{
compute_res(0, req_len, cur_gc_r, cur_n_r, 1);
return 1;
}
for(j=1;j<ran_st;j++)
{
cur_nt=new_seq[j-1];
minus_cur();
cur_nt=new_seq[j+req_len-1];
plus_cur();
cur_gc_r=(double)cur_gc/(double)(req_len-cur_n);
cur_n_r=(double)cur_n/(double)req_len;
diff_gc_r=fabs(cur_gc_r-req_gc);
diff_n_r=fabs(cur_n_r-req_n);
if(diff_gc_r+diff_n_r<best_r)
{best_r=diff_gc_r+diff_n_r; best_p=j; best_gc_r=cur_gc_r; best_n_r=cur_n_r;}
if(diff_gc_r<=cvg_gc && diff_n_r<=cvg_n)
{
compute_res(j, req_len, cur_gc_r, cur_n_r, 1);
return 1;
}
}
compute_res(best_p, req_len, best_gc_r, best_n_r, 0);
return 1;
}
void read_pos(const char* path)
{
ifstream pfl;
pfl.open(path);
string line, word;
int i=0;
int num_nt,j;
double num_gc, num_n;
while(!pfl.eof())
{
getline(pfl,line);
if(line=="") continue;
cout<<i++<<endl;
istringstream test(line);
getline(test,word,'\t');
getline(test,word,'\t');
num_nt=atoi(word.c_str());
getline(test,word,'\t');
num_gc=atof(word.c_str());
for(j=0;j<fold;j++)
{
scan(num_nt, num_gc, 0);
}
}
}
int main(int argc, char *argv[])
{
if(argc<=6)
{
cout<<"Usage: chromosome_dir input output gc_diff n_diff fold"<<endl;
exit(1);
}
cvg_gc=atof(argv[4]);
cvg_n=atof(argv[5]);
fold=atoi(argv[6]);
int i,j;
cout<<"Reading chromosomes"<<endl;
read_chr(argv[1]);
cout<<"Scanning chromosomes"<<endl;
read_pos(argv[2]);
cout<<"Outputing"<<endl;
output(argv[3]);
return 1;
}