-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSNPtable2bayenv.pl
91 lines (79 loc) · 2 KB
/
SNPtable2bayenv.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
#!/usr/bin/perl
use warnings;
use strict;
#unless (@ARGV == 3) {die;}
my $in = $ARGV[0]; #Infile SNP table
my $popfile = $ARGV[1]; #Population file for each sample
my $out = $ARGV[2]; #Prefix for outfile.
my %pop;
my %samples;
my @samples;
my %popList;
my $locicount=0;
my $NumColBad=2;
my $popnumber=0;
open (OUTFILE, "> $out.bayenv.txt") or die "Could not open a file\n";
open (POPKEY, "> $out.popkey.bayenv.txt") or die "Could not open a file\n";
open LOCINFO, "> $out.LocInfo.txt";
my %h;
my %alleles;
my %loci;
my %lociname;
if ($popfile){
open POP, $popfile;
while (<POP>){
chomp;
my @a = split (/\t/,$_);
$pop{$a[0]}=$a[1];
$popList{$a[1]}++;
}
close POP;
}
open IN, $in;
while (<IN>){
chomp;
my @a = split (/\t/,$_);
if ($. == 1 ){
foreach my $i ($NumColBad..$#a){
$samples{$i}=$a[$i];
push(@samples,$a[$i]);
}
}else{
$locicount++;
foreach my $i ($NumColBad..$#a){
#print "$samples{$i}\n";
if($pop{$samples{$i}}){
unless ($a[$i] eq "NN"){
my @tmp = split('',$a[$i]);
$h{$locicount}{$pop{$samples{$i}}}{$tmp[0]}++;
$h{$locicount}{$pop{$samples{$i}}}{$tmp[1]}++;
$alleles{$locicount}{$tmp[0]}++;
$alleles{$locicount}{$tmp[1]}++;
# $loci{$locicount}{$samples{$i}}{"1"} = $tmp[0];
# $loci{$locicount}{$samples{$i}}{"2"} = $tmp[1];
}
}
}
my $c;
foreach my $allele (sort keys %{$alleles{$locicount}}){
$c++;
}
if ($c ==2){
print LOCINFO "$a[0]-$a[1]\n";
foreach my $allele (sort keys %{$alleles{$locicount}}){
foreach my $eachpop (sort keys %popList){
if ($h{$locicount}{$eachpop}{$allele}){
print OUTFILE "$h{$locicount}{$eachpop}{$allele}\t";
} else {
print OUTFILE "0\t";
}
}
print OUTFILE "\n";
}
}
}
}
my $popcount = 1;
foreach my $eachpop (sort keys %popList){
print POPKEY "$popcount\t"."$eachpop\n";
}