-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathillumina2tg
More file actions
executable file
·190 lines (156 loc) · 3.87 KB
/
Copy pathillumina2tg
File metadata and controls
executable file
·190 lines (156 loc) · 3.87 KB
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
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use File::Basename;
use Getopt::Long;
use Pod::Usage;
=head1 NAME
illumina2tg
=head1 SYNOPSIS
illumina2tg [options] <filename>
-h help
-m SNP annotation file
a)snp-id
b)alleles
example: illumina2tg -m pscalare.mk pscalare.txt
Please try and ensure that a marker file has the following fields:
1)snp-id (rs2345) - unique identifier, required for converting to tg format
2)alleles (A/T) - encoding, position matters! required for converting to tg format
3)alleles-strand (opp, ref) - alleles with reference to flanks, required for strand conversion
4)flanks (ACGTACGTAC[T/C]ATGTGTCAGC) - flanks that define the SNP, required for strand conversion
5)dbsnp (126, 127) - useful
6)chromosome (3, 4) - useful
7)position (23142) - useful
Converts a matrix file with [ACGT][ACGT] and --, NN and <blank> encoding to a tgfile.
=head1 DESCRIPTION
=cut
#option variables
my $help;
my $snpAnnotationFile;
my $illuminaGenotypeFile;
my $genotypeFile;
my @col2snp;
my %SNP;
my $colNo;
my %anno2col;
my $headerProcessed;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help, 'm=s'=>\$snpAnnotationFile)
|| !defined($snpAnnotationFile))
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
$illuminaGenotypeFile = $ARGV[0];
open(ANNOTATION, $snpAnnotationFile) || die "Cannot open $snpAnnotationFile";
$headerProcessed = 0;
while(<ANNOTATION>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
SEARCH_LABEL: for my $data ('snp-id', 'alleles')
{
for my $col (0 .. $#fields)
{
if ($fields[$col] eq $data)
{
$anno2col{$data}=$col;
next SEARCH_LABEL;
}
}
die "Cannot find '$data' in $snpAnnotationFile";
}
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, $colNo);
my $snp = $fields[$anno2col{'snp-id'}];
my $allele = $fields[$anno2col{'alleles'}];
my @alleles;
if($allele=~/([ACGT])\/([ACGT])/)
{
@alleles = ($1, $2);
$SNP{$snp}{"$alleles[0]$alleles[0]"} = 0;
$SNP{$snp}{"$alleles[0]$alleles[1]"} = 1;
$SNP{$snp}{"$alleles[1]$alleles[1]"} = 2;
}
elsif ($allele=~/([ACGT]+|-)\/([ACGT]+|-)/
&& ($1 eq '-' xor $2 eq '-'))
{
if($1 eq '-')
{
$SNP{$snp}{"DD"} = 0;
$SNP{$snp}{"DI"} = 1;
$SNP{$snp}{"ID"} = 1;
$SNP{$snp}{"II"} = 2;
}
else
{
$SNP{$snp}{"II"} = 0;
$SNP{$snp}{"ID"} = 1;
$SNP{$snp}{"DI"} = 1;
$SNP{$snp}{"DD"} = 2;
}
}
else
{
die "$snp has invalid alleles";
}
}
}
open(IN, "$illuminaGenotypeFile") || die "Cannot open $illuminaGenotypeFile\n";
my($name, $path, $ext) = fileparse($illuminaGenotypeFile, '\..*');
$genotypeFile = "$name.tg";
open(OUT, ">$genotypeFile") || die "Cannot open $genotypeFile\n";
$headerProcessed = 0;
while (<IN>)
{
s/\r?\n?$//;
if(!$headerProcessed)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, 2);
print OUT "snp-id\t$fields[1]\n";
$headerProcessed = 1;
}
else
{
my @fields = split('\t', $_, $colNo);
print OUT "$fields[0]";
my $snp = $fields[0];
for my $col (1..$#fields)
{
my $genotype = $fields[$col];
if ($genotype ne '--' && $genotype ne '' && $genotype ne 'NN')
{
if (exists($SNP{$snp}{$genotype}))
{
print OUT "\t$SNP{$snp}{$genotype}";
}
else
{
warn "Unrecognised $snp genotype: $genotype keys:" . join('/',keys(%{$SNP{$snp}}));
}
}
else
{
print OUT "\t-1";
}
}
print OUT "\n";
}
}
close(OUT);
close(IN);