-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake_hmm_introgress_regions.pl
470 lines (377 loc) · 17.5 KB
/
make_hmm_introgress_regions.pl
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
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/perl -w
#David Turissini
use Storable qw(dclone);
use DBI;
prepareDbh();
my ($prefix, $introgress_group, $hmm_suffix, $hmm_type, $repeats_table) = @ARGV;
my $combined_tracks_table = $prefix . '_hmm_' . $hmm_type . '_pab_' . $hmm_suffix . '_tracks';
my $combined_tracks_fil_table = $prefix . '_hmm_' . $hmm_type . '_pab_' . $hmm_suffix . '_tracks_fil';
my $combined_unftrk_table = $prefix . '_hmm_' . $hmm_type . '_pab_' . $hmm_suffix . '_unftrk';
my $regions_table = $prefix . '_hmm_' . $hmm_type . '_pab_' . $hmm_suffix . '_regions';
my $regions_inds_table = $prefix . '_hmm_' . $hmm_type . '_pab_' . $hmm_suffix . '_regions_inds';
my $blocks_table = $prefix . '_hmm_' . $hmm_type . '_pab_' . $hmm_suffix . '_blocks';
my $blocks_inds_table = $prefix . '_hmm_' . $hmm_type . '_pab_' . $hmm_suffix . '_blocks_inds';
my $tmp_repeat_table = $prefix . '_hmm_' . $hmm_type . '_pab_' . $hmm_suffix . '_tmprep';
$dbh->do(qq{drop table if exists $combined_tracks_table});
$dbh->do(qq{create table $combined_tracks_table
(cict_id int auto_increment primary key,
region_id int,
ind varchar(30),
genotype varchar(20),
arm varchar(5),
start int,
end int,
total_snps int,
introgress_snps int,
track_len int,
cum_cov int,
per_repeat decimal(5,2),
index(ind),
index(start))});
$dbh->do(qq{drop table if exists $combined_tracks_fil_table});
$dbh->do(qq{create table $combined_tracks_fil_table
(cict_id int auto_increment primary key,
region_id int,
ind varchar(30),
genotype varchar(20),
arm varchar(5),
start int,
end int,
total_snps int,
introgress_snps int,
track_len int,
cum_cov int,
per_repeat decimal(5,2),
index(ind),
index(start))});
$dbh->do(qq{drop table if exists $combined_unftrk_table});
$dbh->do(qq{create table $combined_unftrk_table
(cuft_id int auto_increment primary key,
ind varchar(30),
genotype varchar(20),
arm varchar(5),
start int,
end int,
total_snps int,
introgress_snps int,
track_len int,
cum_cov int,
index(ind),
index(start))});
$dbh->do(qq{drop table if exists $regions_table});
$dbh->do(qq{create table $regions_table
(region_id int auto_increment primary key,
arm varchar(5),
start int,
end int,
inds int,
max_introgress_snps int,
distinct_haps int,
index(start))});
$dbh->do(qq{drop table if exists $regions_inds_table});
$dbh->do(qq{create table $regions_inds_table
(fssfhri_id int auto_increment primary key,
region_id int,
ind varchar(100),
index(region_id),
index(ind))});
$dbh->do(qq{drop table if exists $blocks_table});
$dbh->do(qq{create table $blocks_table
(block_id int auto_increment primary key,
arm varchar(5),
start int,
end int,
inds int,
block_len int,
per_repeat decimal(5,2),
index(start))});
$dbh->do(qq{drop table if exists $blocks_inds_table});
$dbh->do(qq{create table $blocks_inds_table
(bi_id int auto_increment primary key,
block_id int,
ind varchar(100),
index(block_id),
index(ind))});
my @arms = ('2L', '2R', '3L', '3R', 'X');
my $ind_query = $dbh->prepare(qq{select distinct ind
from tracking_groups
where group_name in ('$introgress_group')});
$ind_query->execute();
my $total_inds = 0;
while (my ($ind) = $ind_query->fetchrow_array())
{
$total_inds++;
$ind =~ tr/\./\_/;
my $tracks_table = $prefix . '_' . $ind . '_hmm_' . $hmm_type . '_pab_' . $hmm_suffix . '_tracks';
$dbh->do(qq{insert into $combined_tracks_table
select null, null, ind, genotype, arm, start, end, total_snps, introgress_snps, track_len, cum_cov, per_repeat
from $tracks_table
order by arm, start});
my $unftrk_table = $prefix . '_' . $ind . '_hmm_' . $hmm_type . '_pab_' . $hmm_suffix . '_unftrk';
$dbh->do(qq{insert into $combined_unftrk_table
select null, ind, genotype, arm, start, end, total_snps, introgress_snps, track_len, cum_cov
from $unftrk_table
order by arm, start});
#tracks_fil table
foreach my $arm (@arms)
{
my $filter_query = $dbh->prepare(qq{select straight_join cict_id cict_id_start, cict_id + 1 cict_id_end, 'homo_1'
from $combined_tracks_table
where cict_id in (select min(cict_id)
from $combined_tracks_table
where arm = '$arm'
and ind = '$ind')
and genotype in ('het', 'homo_2')
and ind = '$ind'
and (end - start + 1 <= 500
or introgress_snps < 10
or per_repeat >= 30)
union all
select cict_id - 1, cict_id, 'homo_1'
from $combined_tracks_table
where cict_id in (select max(cict_id)
from $combined_tracks_table
where arm = '$arm'
and ind = '$ind')
and genotype in ('het', 'homo_2')
and ind = '$ind'
and (end - start + 1 <= 500
or introgress_snps < 10
or per_repeat >= 30)
union all
select straight_join t.cict_id, t3.cict_id, 'homo_1'
from $combined_tracks_table t, $combined_tracks_table t3, $combined_tracks_table t2
where t2.cict_id = t.cict_id + 1
and t3.cict_id = t2.cict_id + 1
and t.genotype = 'homo_1'
and t2.genotype in ('het', 'homo_2')
and t3.genotype = 'homo_1'
and t.ind = '$ind'
and t2.ind = t.ind
and t3.ind = t.ind
and (t2.end - t2.start + 1 <= 500
or t2.introgress_snps < 10
or t2.per_repeat >= 30)
order by cict_id_start, cict_id_end});
$filter_query->execute();
my %filtered_cict_ids = ();
while (my ($cict_id_start, $cict_id_end, $genotype) = $filter_query->fetchrow_array())
{
$filtered_cict_ids{$cict_id_start}{end} = $cict_id_end;
$filtered_cict_ids{$cict_id_start}{genotype} = $genotype;
}
my $track_query = $dbh->prepare(qq{select cict_id
from $combined_tracks_table
where arm = '$arm'
and ind = '$ind'
order by cict_id});
$track_query->execute();
while (my ($cict_id) = $track_query->fetchrow_array())
{
my $filtered = 'N';
foreach my $cict_id_start (keys %filtered_cict_ids)
{
if ($cict_id >= $cict_id_start and $cict_id <= $filtered_cict_ids{$cict_id_start}{end})
{
$filtered = 'Y';
if ($cict_id == $cict_id_start)
{
$dbh->do(qq{insert into $combined_tracks_fil_table
select null, null, ind, '$filtered_cict_ids{$cict_id_start}{genotype}',
arm, min(start), max(end),
sum(total_snps), sum(introgress_snps), max(end) - min(start) + 1, sum(cum_cov), null
from $combined_tracks_table
where arm = '$arm'
and ind = '$ind'
and cict_id between $cict_id_start and $filtered_cict_ids{$cict_id_start}{end}
group by ind, arm, '$filtered_cict_ids{$cict_id_start}{genotype}'});
}
}
elsif ($cict_id > $filtered_cict_ids{$cict_id_start}{end})
{delete $filtered_cict_ids{$cict_id_start};}
}
if ($filtered eq 'N')
{
$dbh->do(qq{insert into $combined_tracks_fil_table
select null, null, ind, genotype, arm, start, end, total_snps, introgress_snps, track_len, cum_cov, per_repeat
from $combined_tracks_table
where cict_id = $cict_id});
}
}
}
}
foreach my $arm (@arms)
{
my $time = localtime;
print "$time\t$arm\n";
$time = localtime;
print "$time\nidentify regions\n";
my $region_query = $dbh->prepare(qq{select start, end, count(*), max(introgress_snps)
from $combined_tracks_table
where arm = '$arm'
and genotype != 'homo_1'
and per_repeat < 30
group by start, end
order by start, end});
$region_query->execute();
my $region_start = -1;
my $last_end = -1;
my $max_introgress_snps = 0;
my %haps = ();
while (my ($start, $end, $inds, $introgress_snps) = $region_query->fetchrow_array())
{
if ($region_start == -1)
{$region_start = $start;}
if ($start > $last_end and $last_end != -1)
{
my $distinct_haps = scalar keys %haps;
$dbh->do(qq{insert into $regions_table
values
(null, '$arm', $region_start, $last_end, null, $max_introgress_snps, $distinct_haps)});
%haps = ();
$region_start = $start;
$max_introgress_snps = 0;
}
if ($end > $last_end)
{$last_end = $end;}
if ($introgress_snps > $max_introgress_snps)
{$max_introgress_snps = $introgress_snps}
my $hap = $start . '-' . $end;
$haps{$hap}++;
}
my $distinct_haps = scalar keys %haps;
$dbh->do(qq{insert into $regions_table
values
(null, '$arm', $region_start, $last_end, null, $max_introgress_snps, $distinct_haps)});
#make blocks
my $breakpoint_query = $dbh->prepare(qq{select pos, sum(if(track_end = 'start', 1, 0)) starts,
sum(if(track_end = 'end', 1, 0)) ends,
sum(if(track_end = 'start' and genotype = 'het', 1, 0)) het_starts,
sum(if(track_end = 'end' and genotype = 'het', 1, 0)) het_ends
from (select start pos, 'start' track_end, genotype
from $combined_tracks_table
where arm = '$arm'
and end - start + 1 > 500
and introgress_snps >= 10
and per_repeat < 30
and genotype != 'homo_1'
union all
select end, 'end' track_end, genotype
from $combined_tracks_table
where arm = '$arm'
and end - start + 1 > 500
and introgress_snps >= 10
and per_repeat < 30
and genotype != 'homo_1') x
group by pos});
$breakpoint_query -> execute();
my $last_pos = -1;
my $num_inds = 0;
my $num_hets = 0;
while (my ($pos, $num_starts, $num_ends, $num_het_starts, $num_het_ends) = $breakpoint_query->fetchrow_array())
{
#print "$pos, $num_starts, $num_ends\n";
unless ($num_inds == 0)
{
my $block_start = $last_pos;
my $block_end = $pos;
if ($num_starts > 0)
{$block_end = $pos - 1;}
$dbh->do(qq{insert into $blocks_table
values
(null, '$arm', $block_start, $block_end, $num_inds, $num_het_inds, $block_end - $block_start + 1, 0)});
if ($num_starts > 0 and $num_ends > 0)
{
$dbh->do(qq{insert into $blocks_table
values
(null, '$arm', $pos, $pos, $num_inds + $num_starts, $num_het_inds + $num_het_starts, 1, 0)});
}
}
$num_inds += $num_starts - $num_ends;
$num_het_inds += $num_het_starts - $num_het_ends;
$last_pos = $pos;
if ($num_ends > 0)
{$last_pos++;}
}
}
#update block per_repeat
$dbh->do(qq{create table $tmp_repeat_table
as select t.block_id, sum(rep_len) / (end - start + 1) * 100 per_repeat
from (select straight_join block_id, r.end - t.start + 1 rep_len
from $blocks_table t, $repeats_table r
where t.arm = r.arm
and r.start < t.start
and r.end between t.start and t.end
union all
select straight_join block_id, t.end - r.start + 1
from $blocks_table t, $repeats_table r
where t.arm = r.arm
and r.start between t.start and t.end
and r.end > t.end
union all
select straight_join block_id, r.end - r.start + 1
from $blocks_table t, $repeats_table r
where t.arm = r.arm
and r.start between t.start and t.end
and r.end between t.start and t.end
union all
select straight_join block_id, t.end - t.start + 1
from $repeats_table r, $blocks_table t
where t.arm = r.arm
and t.start between r.start and r.end
and t.end between r.start and r.end
and t.start > r.start
and t.end < r.end) x, $blocks_table t
where t.block_id = x.block_id
group by t.block_id, start, end});
$dbh->do(qq{alter table $tmp_repeat_table add unique index(block_id)});
$dbh->do(qq{update $blocks_table t, $tmp_repeat_table x
set t.per_repeat = x.per_repeat
where t.block_id = x.block_id});
$dbh->do(qq{drop table $tmp_repeat_table});
$dbh->do(qq{insert into $blocks_inds_table
select straight_join null, block_id, ind
from $combined_tracks_table t, $blocks_table b
where b.arm = t.arm
and b.start between t.start and t.end
and t.end - t.start + 1 > 500
and introgress_snps >= 10
and t.per_repeat < 30
and genotype != 'homo_1'});
#filter het stacks
my $het_block_query = $dbh->prepare(qq{select arm, start, end, inds, het_inds
from $blocks_table
where het_inds / inds > .9
and inds >= .25 * $total_inds});
$het_block_query->execute();
while (my () = $het_block_query->fetchrow_array())
{
}
$dbh->do(qq{update $combined_tracks_table t, $regions_table r
set t.region_id = r.region_id
where t.arm = r.arm
and t.start between r.start and r.end
and t.end between r.start and r.end
and genotype != 'homo_1'});
$time = localtime;
print "$time\tpopulating regions_inds table\n";
$dbh->do(qq{insert into $regions_inds_table
select null, region_id, ind
from $combined_tracks_table f
group by null, region_id, ind});
$dbh->do(qq{update $regions_table r, (select region_id, count(distinct ind) total_inds
from $regions_inds_table
group by region_id) x
set inds = total_inds
where r.region_id = x.region_id});
$time = localtime;
print "$time\tdone\n";
sub prepareDbh
{
my $username = "";
my $password = "";
my $database = "";
my $host = "";
my $dsn = "DBI:mysql:$database:$host";
our $dbh = DBI->connect($dsn,$username,$password);
}