-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequenome2tg
executable file
·263 lines (216 loc) · 5.46 KB
/
sequenome2tg
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use Getopt::Long;
use File::Basename;
use Pod::Usage;
=head1 NAME
sequenome2tg
=head1 SYNOPSIS
sequenome2tg [options] <sequenome-file>
-v verbose
-d debug
-o output file
by default, the output file name will be
the original file name with its extension
replaced by .tg
-m marker file
a)snp-id
b)alleles
sequenome-file sequenome output file
a)ASSAY_ID
b)GENOTYPE_ID
c)SAMPLE_ID
example: sequenome2tg pscalare.txt -m pscalare.mk
sequenome2tg -o paltum.tg pscalare.txt -m pscalare.mk
Converts a sequenome genotype file to tg-file.
=head1 DESCRIPTION
=cut
my $verbose;
my $debug;
my $help;
my $sequenomeFile;
my $tgFile;
my $mkFile;
my $colNo;
my %SNP_SAMPLE;
my %SNP;
my %SAMPLE;
my %NON_EXISTENT_SNP;
my $fpos = 0;
my $snpID = 0;
my $sampleID = 0;
my @sortedSNP;
my @sortedSAMPLE;
my %label2col;
my $sampleNo;
my $snpNo;
my $headerProcessed;
my $validRowNo = 0;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('v'=>\$verbose,'d'=>\$debug, 'h'=>\$help, 'o=s'=>\$tgFile, 'm=s'=>\$mkFile)
|| !defined($mkFile)
|| scalar(@ARGV)!=1)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
$sequenomeFile = $ARGV[0];
if (!defined($tgFile))
{
my ($name, $dir, $ext) = fileparse($sequenomeFile, '\..*');
$tgFile = "$name.tg";
}
open(MK, $mkFile) || die "Cannot open $mkFile\n";
$headerProcessed = 0;
while (<MK>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
SEARCH_LABEL: for my $label ('snp-id', 'alleles')
{
for my $col (0 .. $#fields)
{
if ($fields[$col] eq $label)
{
$label2col{$label}=$col;
next SEARCH_LABEL;
}
}
die "Cannot find '$label' in $mkFile";
}
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, $colNo);
my $snp = $fields[$label2col{'snp-id'}];
my $alleles = $fields[$label2col{'alleles'}];
my @alleles = split("/", $alleles, -1);
$SNP{$snp}{ALLELE_A} = $alleles[0];
$SNP{$snp}{ALLELE_B} = $alleles[1];
}
}
close(MK);
open(SEQUENOME, $sequenomeFile) || die "Cannot open $sequenomeFile\n";
$headerProcessed = 0;
while (<SEQUENOME>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
SEARCH_LABEL: for my $label ('ASSAY_ID', 'GENOTYPE_ID', 'SAMPLE_ID')
{
for my $col (0 .. $#fields)
{
if ($fields[$col] eq $label)
{
$label2col{$label}=$col;
next SEARCH_LABEL;
}
}
die "Cannot find '$label' in $sequenomeFile";
}
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, $colNo);
my $snp = $fields[$label2col{'ASSAY_ID'}];
my $sample = $fields[$label2col{'SAMPLE_ID'}];
if (exists($SNP{$snp}))
{
if(!exists($SNP{$snp}{ID}))
{
$SNP{$snp}{ID} = ++$snpID;
push(@sortedSNP, $snp);
}
if(!exists($SAMPLE{$sample}{ID}))
{
$SAMPLE{$sample}{ID} = ++$sampleID;
push(@sortedSAMPLE, $sample);
}
my $key = "$SNP{$snp}{ID}-$SAMPLE{$sample}{ID}";
$SNP_SAMPLE{$key} = $fpos;
$validRowNo++;
}
else
{
if(!exists($NON_EXISTENT_SNP{$snp}))
{
$NON_EXISTENT_SNP{$snp}++;
warn "$snp exists in genotype file but not in marker file";
}
}
}
$fpos += length($_) + 1;
}
open(OUT, ">$tgFile") || die "Cannot open $tgFile\n";
@sortedSNP = sort(@sortedSNP);
@sortedSAMPLE = sort(@sortedSAMPLE);
$snpNo = scalar(@sortedSNP);
$sampleNo = scalar(@sortedSAMPLE);
#check number of rows
if ($snpNo*$sampleNo!=$validRowNo)
{
die "$sequenomeFile expected to have " . ($snpNo*$sampleNo+1) . " rows based on $sampleNo samples and $snpNo SNPs but instead has $validRowNo rows";
}
print OUT "snp-id\t" . join("\t", @sortedSAMPLE) . "\n";
for my $snp (@sortedSNP)
{
print OUT $snp;
for my $sample (@sortedSAMPLE)
{
my $key = "$SNP{$snp}{ID}-$SAMPLE{$sample}{ID}";
seek(SEQUENOME, $SNP_SAMPLE{$key}, 0);
$_ = <SEQUENOME>;
s/\r?\n?$//;
my @fields = split('\t', $_, $colNo);
my $genotype = $fields[$label2col{'GENOTYPE_ID'}];
my $genotypeLength = length($genotype);
my $genotypeEncoding = -1;
if ($genotypeLength==1)
{
if ($genotype eq $SNP{$snp}{ALLELE_A})
{
$genotypeEncoding = 0;
}
elsif ($genotype eq $SNP{$snp}{ALLELE_B})
{
$genotypeEncoding = 2;
}
else
{
die "Unrecognised allele for $snp: $genotype not in $SNP{$snp}{ALLELE_A}/$SNP{$snp}{ALLELE_B}";
}
}
elsif ($genotypeLength==2)
{
if ($genotype eq "$SNP{$snp}{ALLELE_A}$SNP{$snp}{ALLELE_B}")
{
$genotypeEncoding = 1;
}
else
{
die "Unrecognised allele for $snp: $genotype not in $SNP{$snp}{ALLELE_A}/$SNP{$snp}{ALLELE_B}";
}
}
print OUT "\t$genotypeEncoding"
}
print OUT "\n";
}
close(SEQUENOME);
close(OUT);