-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtg2het
executable file
·200 lines (156 loc) · 3.38 KB
/
tg2het
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use File::Basename;
use Getopt::Long;
use Pod::Usage;
=head1 NAME
tg2het
=head1 SYNOPSIS
tg2het [options] <tg-file>
-h help
-s sample annotation list (required)
a)sample-id
b)population-id
example: tg2het -s pscalare.sa pscalare.tg
Calculates the average heterozygosity from a tg-file.
=head1 DESCRIPTION
=cut
#option variables
my $help;
my $saFile;
my $mkFile;
my $tgFile;
my $colNo;
my %label2col;
my $ignoreXChromosome;
my $headerProcessed;
my $hetFile;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help, 's=s'=>\$saFile)
|| !defined($saFile) || scalar(@ARGV)!=1)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
$tgFile = $ARGV[0];
isTg($tgFile) || die "$tgFile not a tgfile";
#data structures
my %SAMPLE;
#read sample annotation file
open(SA, $saFile) || die "Cannot open $saFile";
$headerProcessed = 0;
while(<SA>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
SEARCH_LABEL: for my $label ('sample-id', 'population-id')
{
for my $col (0 .. $#fields)
{
if ($fields[$col] eq $label)
{
$label2col{$label} = $col;
next SEARCH_LABEL;
}
}
}
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, $colNo);
if ($colNo!=scalar(@fields))
{
die "Current row does not have the same number of columns(" . scalar(@fields) . ") as preceding rows($colNo)";
}
my $sample = $fields[$label2col{'sample-id'}];
my $population = $fields[$label2col{'population-id'}];
$SAMPLE{$sample}{POPULATION} = $population;
}
}
close(SA);
#read genotype file
open(TG, $tgFile) || die "Cannot open $tgFile";
$headerProcessed = 0;
my %POPULATION;
my @sortedPopulations;
my @col2sample;
if (!defined($hetFile))
{
my($name, $path, $ext) = fileparse($tgFile, '\..*');
$hetFile = "$name.het";
}
open(HET, ">$hetFile") || die "Cannot open $hetFile";
while(<TG>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
for my $col (1..$#fields)
{
my $sample = $fields[$col];
$col2sample[$col] = $sample;
if (!defined($POPULATION{$SAMPLE{$sample}{POPULATION}}))
{
$POPULATION{$SAMPLE{$sample}{POPULATION}} = ();
}
push(@{$POPULATION{$SAMPLE{$sample}{POPULATION}}}, $col);
}
@sortedPopulations = sort(keys(%POPULATION));
print HET "snp-id\t" . join("\t", @sortedPopulations) . "\n";
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, $colNo);
if ($colNo!=scalar(@fields))
{
die "Current row does not have the same number of columns(" . scalar(@fields) . ") as preceding rows($colNo)";
}
my $snp = $fields[0];
print HET $snp;
for my $pop (@sortedPopulations)
{
my $count = 0;
my $total = 0;
for my $col (@{$POPULATION{$pop}})
{
my $genotype = $fields[$col];
if ($genotype!=-1)
{
if ($genotype==1)
{
$count++
}
$total++;
}
}
if ($total==0)
{
print HET "\tn/a";
warn "No valid genotypes for $snp";
}
else
{
my $paf = $count/$total;
printf HET "\t%.4f", $paf;
}
}
print HET "\n";
}
}
close(TG);