forked from owensgl/reformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNPtable2randomthinnedset.pl
56 lines (53 loc) · 1.45 KB
/
SNPtable2randomthinnedset.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
#!/bin/perl
use warnings;
use strict;
#This script reduces a snp table to one snp every 1000 bp at max. It runs a thousand bp window and picks a random snp in each window.
my $dist = 1000; #Minimum distance between SNPs
my $previous_chr;
my $previous_pos;
my $current_end_bp = $dist;
my @snp_array;
while (<STDIN>){
chomp;
if ($. == 1){
print "$_";
}else{
my @a = split(/\t/,$_);
my $chr = $a[0];
my $pos = $a[1];
unless ($previous_chr){
$previous_chr = $chr;
until ($pos < $current_end_bp){
$current_end_bp+= $dist;
}
push(@snp_array, $_);
next;
}
if ($chr ne $previous_chr){
$previous_chr = $chr;
my $length = scalar(@snp_array);
my $rand = rand(int(@snp_array));
print "\n$snp_array[$rand]";
undef(@snp_array);
$current_end_bp = $dist;
until ($pos < $current_end_bp){
$current_end_bp+= $dist;
}
push(@snp_array, $_);
next;
}elsif ($current_end_bp >= $pos){
push(@snp_array, $_);
next;
}elsif ($current_end_bp < $pos){
my $length = scalar(@snp_array);
my $rand = rand(int(@snp_array));
print "\n$snp_array[$rand]";
undef(@snp_array);
until ($pos < $current_end_bp){
$current_end_bp+= $dist;
}
push(@snp_array, $_);
next;
}
}
}