-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_sex_loci_from_GT.pl
133 lines (114 loc) · 3.42 KB
/
find_sex_loci_from_GT.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
131
132
133
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
use List::MoreUtils qw{uniq};
my ($in, $out, $popmap, $cov, $help, $pops, $cnt, $error, $type, %map_ind);
my ($in_fh);
GetOptions (
"in=s" =>\$in,
"out=s" =>\$out,
"popmap=s" =>\$popmap,
"cov=f" =>\$cov,
"error=f" =>\$error,
"type=s" =>\$type,
"help:1" =>\$help
) or die "$!";
my $usage = '
-i: input GT file.
-o: output file.
-p: popmap file.
-c: coverage.
-e: error rate.
-h: help message.
';
$cov = defined $cov ? $cov : 0.6;
$error = defined $error ? $error : 0.05;
die $usage if ($help || !($in && $out && $popmap));
if (substr($in, -2) eq 'gz') {
open( $in_fh, "gzip -dc $in|") or die "$!";
} else {
open($in_fh, "$in") or die "$!";
}
($pops, $cnt) = parse_popmap($popmap);
print STDERR $cnt->{'male'}+$cnt->{'female'}, " individuals, $cnt->{'male'} males, $cnt->{'female'} females\n";
open(my $out_fh, ">$out") or die "$!";
print $out_fh join("\t", 'chrom', 'pos', 'female_'.$cnt->{'female'}, 'male_'.$cnt->{'male'}, 'het_f', 'het_m');
my $th_male = $cov >= 1 ? $cov : $cov*$cnt->{'male'};
my $e_male = $error >= 1 ? $error : $error*$cnt->{'female'};
my $th_female = $cov >= 1 ? $cov : $cov*$cnt->{'female'};
my $e_female = $error >= 1 ? $error : $error*$cnt->{'male'};
my $vcf_header = 0;
my $sex_loci_cnt = 0;
while (<$in_fh>) {
# GT from vcftools
# CHROM POS IND...
my ($chrom, $pos, $id, $i, @females, @males, @parts, @GT, %het, %hom, %all, $hom_gt);
next if /^#|^$/;
chomp;
if ($vcf_header == 0) {
$vcf_header = 1;
$i = 0;
@parts = split;
map {$map_ind{++$i} = $_;print $out_fh "\t$_";} @parts[2..$#parts];
print $out_fh "\n";
next;
}
@parts = split;
$chrom = $parts[0];
$pos = $parts[1];
$i = 0;
foreach(@parts[2..$#parts]) {
push @GT, $_;
++$i;
my $gt = $_;
next if $gt eq './.';
my $ind = $map_ind{$i};
my $sex = $pops->{$ind};
if ($gt eq '0/1') {
$het{$sex}++;
} else {
$hom_gt->{$sex}->{$gt}++; # gt type for hom.
$hom{$sex}++;
}
$all{$sex}++;
}
my $f = $het{'female'} ? $het{'female'} : 0;
my $m = $het{'male'} ? $het{'male'} : 0;
if (($f >= $th_female && $m <= $e_female && scalar(keys %{$hom_gt->{'male'}}) == 1) ||
($m >= $th_male && $f <= $e_male && scalar(keys %{$hom_gt->{'female'}}) == 1)) {
# putative sex loci.
my $f_het = sprintf "%.5f", $f/$all{'female'};
my $m_het = sprintf "%.5f", $m/$all{'male'};
print $out_fh join("\t", $chrom, $pos, $f, $m, $f_het, $m_het, @GT), "\n";
$sex_loci_cnt++;
}
}
close $in_fh;
close $out_fh;
print STDERR "Total $sex_loci_cnt putative sex specific loci.\n";
sub parse_popmap {
my $pop = shift;
my ($pops, $cnt);
open(my $in_fh, $pop) or die "$!";
while(<$in_fh>) {
next if /^#|^$/;
$_ =~ s/\r//g;
chomp;
# indv sex
my @parts = split;
die if scalar(@parts) ne 2;
my $ind = $parts[0];
my $sex = lc($parts[1]);
$pops->{$ind} = $sex;
$cnt->{$sex}++;
}
close $in_fh;
return $pops, $cnt;
}
sub max_cnt {
my $hash = shift;
return 0 if not %$hash;
my @cnt = sort {$b<=>$a} values %$hash;
return $cnt[0];
}