forked from owensgl/reformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNPtable2fstat.pl
68 lines (67 loc) · 1.47 KB
/
SNPtable2fstat.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
#!/bin/perl
use warnings;
use strict;
#This takes a SNPtable and a population file and turns it into a FSTAT format
my $popfile = $ARGV[0];
my $TMPFILE = "tmpfile.tab";
open TMPFILE, "> $TMPFILE"
or die "Couldn't open `$TMPFILE' for writing: $!; aborting";
print TMPFILE while <STDIN>;
open TMP, "< $TMPFILE";
my %t;
$t{"A"}="1";
$t{"C"}="2";
$t{"G"}="3";
$t{"T"}="4";
$t{"N"}="0";
my %pop;
open POP, $popfile;
while(<POP>){
chomp;
my @a = split(/\t/,$_);
$pop{$a[0]} = $a[1];
}
my $nloci;
my $nsample;
while(<TMP>){
chomp;
my @a = split(/\t/,$_);
if ($. == 1){
my $tmp_sample = ($#a - 1);
$nsample = $tmp_sample;
}else{
$nloci++;
}
}
close TMP;
open TMP, "< $TMPFILE";
my %sample;
my $locus_counter =0;
my %data;
while (<TMP>){
chomp;
my @a = split(/\t/,$_);
if($. == 1){
print "$nsample $nloci 4 1"; #Header line
foreach my $i (2..$#a){
$sample{$i} = $a[$i];
}
}else{
$locus_counter++;
print "\n$a[0]_$a[1]"; #Print locus information;
# print "\nloc-$locus_counter";
foreach my $i (2..$#a){
my @bases = split(//,$a[$i]);
$bases[0] = $t{$bases[0]};
$bases[1] = $t{$bases[1]};
$data{$sample{$i}}{$locus_counter}=$bases[0].$bases[1];
}
}
}
foreach my $i (2..($nsample+1)){
print "\n$pop{$sample{$i}}";
foreach my $j (1..$locus_counter){
print "\t$data{$sample{$i}}{$j}";
}
}
unlink $TMPFILE;