-
Notifications
You must be signed in to change notification settings - Fork 0
/
defineChunks.R
executable file
·458 lines (376 loc) · 16.2 KB
/
defineChunks.R
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/Rscript
#args=commandArgs(TRUE)
#inFile=args[1]
inFile="./torin_FINAL_150609_87fb294"
####################################################################
# This procedure is necessary, since Impute2 requires chromosomes to be fed in chunks,
# and I don't want the chunks to be non-smart (spanning big gaps)
# This script generates ~500 chunks, each sized ~6 Mb. Jonas
####################################################################
# note: created based on I-can't-remember-what type of genotyping chip,
# thus you might need to customize it for your particular chip: include
# some script checking previously skipped chromosomes (e.g., chr 17 18 19)
# that were uniformly covered with SNPs in my project (and needed no smart chunking).
# note: assumes that your bim file is in hg19 coordinate system (same as 1kG ref)
# note: outputs a text file with chunk coordinated and an image of all chunks on chromosomes.
###############################################################
# 1) FIND WHAT POSITION IS THE HIGHEST FOR EACH CHROMOSOME
dat=data.frame(chr=seq(23),max=NA)
# autosomes
for (i in 1:23) {
print(i)
nam=paste("~/soft/ref1000G/ALL_1000G_phase1integrated_v3_chr",i,"_impute.legend.gz",sep="")
fil=read.table(gzfile(nam),header=T, sep=" ", colClasses=c("NULL","numeric",rep("NULL",10)))
dat[i,"max"]=max(fil$POSITION)
}
# chromosome X
#nam="~/soft/ref1000G/ALL_1000G_phase1integrated_v3_chrX_nonPAR_impute.legend.gz"
#fil=read.table(gzfile(nam),header=T, sep=" ", colClasses=c("NULL","numeric",rep("NULL",10)))
#dat[23,"max"]=max(fil$position)
##############################################################
# 2) READ the SNP map
bim=read.table(paste(inFile,".bim",sep=""))
##############################################################
# 2) explore each chromosome
par(mfrow=c(4,1))
for (chr in 1:23) {
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),xaxt="n",yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
abline(v=c(mn,mx))
}
###############################################################
# 3) CREATE THE INITIAL CHUNK MAP (to be tuned later)
m=matrix(NA,nr=23,nc=50) # collector of CHUNK cuts (gap coordinates)
for (chr in 1:23) {
mn= min(bim[bim$V1==chr,4]) # minimum coordinate on this chromosome
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] ) # prevent too big chromosome
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6)) # make chunks of equal sizes
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
}
###############################################################
# 4) manually deal with problematic chromosomes
############################## chr1
par(mfrow=c(1,1))
chr=1
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=1.3e8
pos=bim[bim$V1==chr,4]
lft=max(pos[pos<mid])
lft=ifelse( (lft+2e6)<rgh , lft+2e6 , lft+1e6 ) # prevent negative values
rgh=min(pos[pos>mid])
rgh=ifelse( (rgh-2e6)>lft , rgh-2e6 , lft-1e6 ) # prevent negative values
abline(v=c(lft,rgh),col="red")
pnts1=seq(mn,lft,by= (lft-mn)/floor((lft-mn)/6e6))
pnts2=seq(rgh,mx,by= (mx-rgh)/floor((mx-rgh)/6e6))
pnts=unique(c(pnts1,pnts2))
points(pnts,rep(0.75,length(pnts)),pch=19,col="red")
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr2
par(mfrow=c(1,1))
chr=2
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=0.93e8
abline(v=mid,col="red")
pnts1=seq(mn,mid,by=(mid-mn)/floor((mid-mn)/6e6))
points(pnts1,rep(0.77,length(pnts1)),pch=19,col="red")
pnts2=seq(mid,mx,by=(mx-mid)/floor((mx-mid)/6e6))
points(pnts2,rep(0.77,length(pnts2)),pch=19,col="red")
pnts=sort(unique(c(pnts1,pnts2)))
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr4
par(mfrow=c(1,1))
chr=4
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=0.513e8
abline(v=mid,col="red")
pnts1=seq(mn,mid,by=(mid-mn)/floor((mid-mn)/6e6))
points(pnts1,rep(0.77,length(pnts1)),pch=19,col="red")
pnts2=seq(mid,mx,by=(mx-mid)/floor((mx-mid)/6e6))
points(pnts2,rep(0.77,length(pnts2)),pch=19,col="red")
pnts=sort(unique(c(pnts1,pnts2)))
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr7
par(mfrow=c(1,1))
chr=7
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=6e7
abline(v=mid,col="red")
pnts1=seq(mn,mid,by=(mid-mn)/floor((mid-mn)/6e6))
points(pnts1,rep(0.77,length(pnts1)),pch=19,col="red")
pnts2=seq(mid,mx,by=(mx-mid)/floor((mx-mid)/6e6))
points(pnts2,rep(0.77,length(pnts2)),pch=19,col="red")
pnts=sort(unique(c(pnts1,pnts2)))
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr8
par(mfrow=c(1,1))
chr=8
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=4.5e7
abline(v=mid,col="red")
pnts1=seq(mn,mid,by=(mid-mn)/floor((mid-mn)/6e6))
points(pnts1,rep(0.77,length(pnts1)),pch=19,col="red")
pnts2=seq(mid,mx,by=(mx-mid)/floor((mx-mid)/6e6))
points(pnts2,rep(0.77,length(pnts2)),pch=19,col="red")
pnts=sort(unique(c(pnts1,pnts2)))
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
par(mfrow=c(1,1))
############################## chr9
chr=9
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=6e7
pos=bim[bim$V1==chr,4]
lft=max(pos[pos<mid])
lft=ifelse( (lft+2e6)<rgh , lft+1e6 , lft+0.5e6 ) # prevent negative values
rgh=min(pos[pos>mid])
rgh=ifelse( (rgh-2e6)>lft , rgh-2e6 , lft-1e6 ) # prevent negative values
abline(v=c(lft,rgh),col="red")
pnts1=seq(mn,lft,by= (lft-mn)/floor((lft-mn)/8e6))
pnts2=seq(rgh,mx,by= (mx-rgh)/floor((mx-rgh)/7e6))
pnts=unique(c(pnts1,pnts2))
points(pnts,rep(0.75,length(pnts)),pch=19,col="red")
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr10
par(mfrow=c(1,1))
chr=10
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=4.1e7
abline(v=mid,col="red")
pnts1=seq(mn,mid,by=(mid-mn)/floor((mid-mn)/6e6))
points(pnts1,rep(0.77,length(pnts1)),pch=19,col="red")
pnts2=seq(mid,mx,by=(mx-mid)/floor((mx-mid)/6e6))
points(pnts2,rep(0.77,length(pnts2)),pch=19,col="red")
pnts=sort(unique(c(pnts1,pnts2)))
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr11
par(mfrow=c(1,1))
chr=11
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=5.3e7
abline(v=mid,col="red")
pnts1=seq(mn,mid,by=(mid-mn)/floor((mid-mn)/6e6))
points(pnts1,rep(0.77,length(pnts1)),pch=19,col="red")
pnts2=seq(mid,mx,by=(mx-mid)/floor((mx-mid)/6e6))
points(pnts2,rep(0.77,length(pnts2)),pch=19,col="red")
pnts=sort(unique(c(pnts1,pnts2)))
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr16
chr=16
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=4e7
pos=bim[bim$V1==chr,4]
lft=max(pos[pos<mid])
lft=ifelse( (lft+2e6)<rgh , lft+1e6 , lft+.5e6 ) # prevent negative values
rgh=min(pos[pos>mid])
rgh=ifelse( (rgh-2e6)>lft , rgh-1e6 , lft-.5e6 ) # prevent negative values
abline(v=c(lft,rgh),col="red")
pnts1=seq(mn,lft,by= (lft-mn)/floor((lft-mn)/6e6))
pnts2=seq(rgh,mx,by= (mx-rgh)/floor((mx-rgh)/6e6))
pnts=unique(c(pnts1,pnts2))
points(pnts,rep(0.75,length(pnts)),pch=19,col="red")
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr18
chr=18
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=1.7e7
pos=bim[bim$V1==chr,4]
lft=max(pos[pos<mid])
rgh=min(pos[pos>mid])
lft=ifelse( (lft+2e6)<rgh , lft+1e6 , lft+.5e6 ) # prevent negative values
rgh=ifelse( (rgh-2e6)>lft , rgh-1e6 , lft-.5e6 ) # prevent negative values
abline(v=c(lft,rgh),col="red")
pnts1=seq(mn,lft,by= (lft-mn)/floor((lft-mn)/6e6))
pnts2=seq(rgh,mx,by= (mx-rgh)/floor((mx-rgh)/6e6))
pnts=unique(c(pnts1,pnts2))
points(pnts,rep(0.75,length(pnts)),pch=19,col="red")
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr20
par(mfrow=c(1,1))
chr=20
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
mid=2.8e7
abline(v=mid,col="red")
pnts1=seq(mn,mid,by=(mid-mn)/floor((mid-mn)/6e6))
points(pnts1,rep(0.77,length(pnts1)),pch=19,col="red")
pnts2=seq(mid,mx,by=(mx-mid)/floor((mx-mid)/6e6))
points(pnts2,rep(0.77,length(pnts2)),pch=19,col="red")
pnts=sort(unique(c(pnts1,pnts2)))
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr21
par(mfrow=c(1,1))
chr=21
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-1e6)<=0 , 0 , (mn-1e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
############################## chr23
par(mfrow=c(1,1))
chr=23
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.7)
mn= min(bim[bim$V1==chr,4])
mn=ifelse( (mn-2e6)<=0 , 0 , (mn-2e6) ) # prevent negative values
mx= max(bim[bim$V1==chr,4])
mx=ifelse( abs(dat[dat$chr==chr,"max"]-mx)>2e6 , (mx+2e6), dat[dat$chr==chr,"max"] )
pnts=seq(mn,mx,by= (mx-mn)/floor((mx-mn)/6e6))
points(pnts,rep(0.8,length(pnts)),pch=19,col="red")
abline(v=6e7,col="red")
md=6e7
pnts1=seq(mn,md,by=(md-mn)/floor((md-mn)/6e6))
points(pnts1,rep(0.77,length(pnts1)),pch=19,col="red")
pnts2=seq(md,mx,by=(mx-md)/floor((mx-md)/6e6))
points(pnts2,rep(0.77,length(pnts2)),pch=19,col="red")
pnts=sort(unique(c(pnts1,pnts2)))
pnts=c(pnts,rep(NA,50-length(pnts)))
m[chr,]=pnts
###############################################################
# 5) PREVIEW RESULTS
# explore each chromosome and visually check whether chunking was successful
par(mfrow=c(4,1))
for (chr in 1:23) {
plot(NA,xlim=c(0,max(bim[bim$V1==chr,4])),ylim=c(0,1),xaxt="n",yaxt="n",main=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.8)
points(m[chr,],rep(0.9,length(pnts)),pch=19,col="red")
}
###############################################################
# 6) CREATE CHUNK LIST WITH COORDINATES IN STANDARD FORMAT
CHUNKS=NULL
for (chr in 1:23) {
cts=round(m[chr,][!is.na(m[chr,])],0) # cut points
cts=sort(cts)
chunks=NULL
for (i in 1:(length(cts)-1)) {
chunks=rbind(chunks, data.frame(chr=chr, left=cts[i]+1,right=cts[i+1]) )
}
CHUNKS=rbind(CHUNKS,chunks)
}
###############################################################
# 6) CLEAN THE CHUNK LIST (from empty chunks)
# if chunk has no SNPs - delete the chunk
for (i in 1:dim(CHUNKS)[1]) {
ch=CHUNKS[i,]
pos=bim[bim$V1==ch$chr,"V4"]
if (sum((pos>ch$left)&(pos<ch$right))==0) {CHUNKS[i,]=NA}
}
CHUNKS=CHUNKS[ !is.na(CHUNKS$left),]
dim(CHUNKS)
options("scipen"=100,digits=10)
write.table(CHUNKS,"./prephased/chunks_output.txt",
row.names=F,col.names=T,quote=F,sep="\t")
dim(CHUNKS)
###############################################################
# 6) PREPARE THE VISUAL MAP OF CHUNKS
mx=max(bim$V4)
jpeg("./prephased/chunk_map.jpeg",width=30,height=20,units="cm",res=300)
par(mfrow=c(23,1), mar=c(0, 4, 0, 1) + 0.1)
for (chr in 1:23) {
plot(NA,xlim=c(0,mx),ylim=c(0,1),xaxt="n",yaxt="n",xlab=NA,ylab=chr)
segments(x0= bim[bim$V1==chr,4], x1= bim[bim$V1==chr,4], y0=0,y1=0.5)
points(m[chr,],rep(0.5,length(pnts)),pch=19,col="red")
texdat=data.frame(CHUNKS,sq=seq(dim(CHUNKS)[1]),mid=as.numeric(apply(CHUNKS[,2:3],1,mean)))
sub=texdat[texdat$chr==chr,]
text(x=sub$mid,y=rep(0.75,dim(sub)[1]),labels=sub$sq)
}
dev.off()