-
Notifications
You must be signed in to change notification settings - Fork 2
/
SNPtype2slidingwindow.pl
84 lines (75 loc) · 1.83 KB
/
SNPtype2slidingwindow.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
#!/bin/perl
use warnings;
use strict;
my $current_chr;
my $window_size = $ARGV[0];
my $speciesfile = $ARGV[1];
#Loads up names to species
my %specieshash;
open SPECIES, $speciesfile;
while(<SPECIES>){
chomp;
my @a = split(/\t/,$_);
$specieshash{$a[0]} = $a[1];
}
close SPECIES;
my $start = 0;
my $end = $start+$window_size;
my %h;
#starting variables
while(<STDIN>){
chomp;
my @a = split(/\t/,$_);
if ($. == 1){
print "Sample\tSpecies\tChr\tStartPos\tEndPos\tMidPos\tTotalSites\tP1_sites\tP2_sites\tUniqueSites";
next;
}else{
my $chr = $a[0];
my $pos = $a[1];
my $sample = $a[2];
my $type = $a[4];
unless($current_chr){
$current_chr = $chr;
}
if ($. == 2){
until ($pos < $end){
$start += $window_size;
$end = $start+$window_size;
}
}
if (($current_chr ne $chr) or ($pos >= $end)){
my $current_start = $start;
my $current_end = $end -1;
my $current_midpoint = $end - ($window_size/2);
my @samplelist = sort keys %h;
foreach my $current_sample(@samplelist){
my $P1count = 0;
my $P2count = 0;
my $uniquecount = 0;
if ($h{$current_sample}{"0"}){
$uniquecount = $h{$current_sample}{"0"};
}
if ($h{$current_sample}{"1"}){
$P1count = $h{$current_sample}{"1"};
}
if ($h{$current_sample}{"2"}){
$P2count = $h{$current_sample}{"2"};
}
my $total = $P1count + $P2count + $uniquecount;
print "\n$current_sample\t$specieshash{$current_sample}\t$current_chr\t$current_start\t$current_end\t$current_midpoint\t$total\t$P1count\t$P2count\t$uniquecount";
}
#Reset variables for new window
undef(%h);
if ($current_chr ne $chr){
$current_chr = $chr;
$start = 0;
$end = $start+$window_size;
}
until ($pos <$end){
$start += $window_size;
$end = $start+$window_size;
}
}
$h{$sample}{$type}++;
}
}