forked from z0on/2bRAD_denovo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcf2genepop.pl
executable file
·88 lines (74 loc) · 1.7 KB
/
vcf2genepop.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
#!/usr/bin/perl
$usage= "
vcf2genepop.pl :
Converts VCF to multiallelic GENEPOP, preserves chromosome and position info
Arguments:
vcf=[filename] : vcf file to convert
pops=[list] : comma-separated perl patterns to determine population
affiliation of samples in the VCF file based on sample names
Output:
GENEPOP formatted genotypes, printed to STDOUT
Example:
vcf2genepop.pl vcf=filtered.vcf pops=O,K,M,S > filtered.gen
";
my $vcf="";
my @pops=();
if ("@ARGV"=~/vcf=(\S+)/) {$vcf=$1;}
if ("@ARGV"=~/pops=(\S+)/) { @pops=split(/\,/, $1);}
open VCF, $vcf or die "cannot open input $vcf\n\n$usage";
my @samples=();
my $header;
while (<VCF>) {
chomp;
if ($_=~/CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t(.+)/) {
@samples=split(/\s/,$1);
last;
}
}
my $nsam=$#samples+1;
my @loci=();
my %lgts={};
my @gts=();
while (<VCF>) {
chomp;
my $line=$_;
if ($line=~/(\S+)\t(\S+)\t\S+\t\S+\t\S+\t\S+\t\S+\t\S+\t\S+\t(.+)/ ){
my $locus=$1."_".$2;
push @loci, $locus;
@gts=split(/\s/,$3);
for($g=0; $gt=$gts[$g];$g++) {
($gtt,@rest)=split(/:/,$gt);
($gt1,$gt2)=split(/[|\/]/,$gtt);
#print "$g $gt $gt1-$gt2 @samples[$g]\n";
if ($gt1 eq ".") {
$gt1=0;
$gt2=0;
}
else {
$gt1=$gt1+1;
$gt2=$gt2+1;
}
#print "\t\t$gt1-$gt2 @samples[$g]\n";
$lgts{$locus}{$samples[$g]}="0".$gt1."0".$gt2;
}
}
}
@samples=sort(@samples);
print "converted from $vcf\n";
print join(", ",@loci), "\n";
my $newpop;
my $oldpop;
foreach $sa (@samples){
foreach $p (@pops) {
if ($sa=~/$p/) {$newpop=$p;}
}
if ($newpop ne $oldpop) {
print "POP\n";
$oldpop=$newpop;
}
print $sa, ",";
foreach $l (@loci) {
print " ", $lgts{$l}{$sa};
}
print "\n";
}