-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnptable2alelle_freq.pl
130 lines (125 loc) · 3.38 KB
/
snptable2alelle_freq.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
#!/bin/perl
use warnings;
use strict;
##This takes a tab delimited file and calculates allele frequency for populations. It also takes a second file that has "groups", which are made up of one or more populations. It calculates the allele frequency for both populations and any groups
#usage: cat SNPTABLE.tab | perl this_script.pl sampleinfo.txt groupinfo.txt > output.txt
#sampleinfo is two columns, first column sample name, second column population name
#groupinfo is two columns, first column population name, second column group name
my $min_size = 4; #minimum number of samples needed to calculate an allele frequency.
my $samplefile = $ARGV[0];
my %pop;
my %poplist;
my %group;
my %grouplist;
open SAMPLE, $samplefile;
while(<SAMPLE>){
chomp;
my @a = split(' ',$_);
my $name = $a[0];
my $pop = $a[1];
$pop{$name} = $pop;
$poplist{$pop}++;
}
close SAMPLE;
if ($ARGV[1]){
my $metagroupfile = $ARGV[1];
open GROUP, $metagroupfile;
while(<GROUP>){
chomp;
my @a = split(' ',$_);
my $pop = $a[0];
my $group = $a[1];
$group{$pop} = $group;
$grouplist{$group}++;
}
}
my %name;
print "chr\tpos";
foreach my $pop (sort keys %poplist){
print "\t$pop";
}
if ($ARGV[1]){
foreach my $group (sort keys %grouplist){
print "\t$group";
}
}
while(<STDIN>){
chomp;
my @a = split(' ',$_);
if ($. == 1){
foreach my $i (2..$#a){
$name{$i} = $a[$i];
}
}else{
my $chr = $a[0];
my $pos = $a[1];
my %bases;
foreach my $i (2..$#a){
if ($a[$i] eq "NN"){next;}
my @alleles = split(//,$a[$i]);
foreach my $j (0..1){
$bases{$alleles[$j]}++;
}
}
my @ordered_bases = sort { $bases{$b} <=> $bases{$a} } keys %bases;
my $n_alleles = scalar @ordered_bases;
if ($n_alleles != 2){next;} #Skip not biallelic sites.
my $major = $ordered_bases[0];
my %major_count;
my %total_count;
foreach my $i (2..$#a){
if ($a[$i] eq "NN"){next;}
if ($pop{$name{$i}}){
my @alleles = split(//,$a[$i]);
foreach my $j (0..1){
if ($alleles[$j] eq $major){
$major_count{$pop{$name{$i}}}++;
$total_count{$pop{$name{$i}}}++;
}else{
$total_count{$pop{$name{$i}}}++;
}
}
}
if ($group{$pop{$name{$i}}}){
my @alleles = split(//,$a[$i]);
foreach my $j (0..1){
if ($alleles[$j] eq $major){
$major_count{$group{$pop{$name{$i}}}}++;
$total_count{$group{$pop{$name{$i}}}}++;
}else{
$total_count{$group{$pop{$name{$i}}}}++;
}
}
}
}
print "\n$chr\t$pos";
foreach my $pop (sort keys %poplist){
unless ($total_count{$pop}){
print "\tNA";
}elsif ($total_count{$pop} < $min_size){
print "\tNA";
}else{
unless ($major_count{$pop}){
$major_count{$pop} = 0;
}
my $freq = $major_count{$pop} / $total_count{$pop};
print "\t$freq";
}
}
if ($ARGV[1]){
foreach my $group (sort keys %grouplist){
unless ($total_count{$group}){
print "\tNA";
}elsif ($total_count{$group} < $min_size){
print "\tNA";
}else{
unless ($major_count{$group}){
$major_count{$group} = 0;
}
my $freq = $major_count{$group} / $total_count{$group};
print "\t$freq";
}
}
}
}
}