-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhapmap2tg
executable file
·184 lines (149 loc) · 3.41 KB
/
hapmap2tg
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
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use File::Basename;
use Getopt::Long;
use Pod::Usage;
=head1 NAME
hapmap2tg
=head1 SYNOPSIS
hapmap2tg [options] hg-files...
-h help
-o output root file name (default hapmap-panel)
hg-files hapmap-genotype-files
example: hapmap2tg genotype_chr1_ASW.txt genotype_chr2_ASW.txt ...
hapmap2tg -o asw genotype_chr1_ASW.txt genotype_chr2_ASW.txt ...
Converts hapmap genotype files to tg and marker files.
The output files will be <output-root-file-name>.tg and
<output-root-file-name>.mk
=head1 DESCRIPTION
=cut
#option variables
my $help;
my $tgFile;
my $mkFile;
my $outputRootFileName;
my $colNo;
my %label2Column;
#data structures
my %SAMPLE;
my %SNP;
my @samples;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('h'=>\$help, 'o=s'=>\$outputRootFileName)
|| scalar(@ARGV) ==0)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
for my $i (0 .. $#ARGV)
{
my $file = $ARGV[$i];
open(HAPMAP, $file) || die "Cannot open $file";
while (<HAPMAP>)
{
s/\r?\n?$//;
my @fields = split(" ", $_, -1);
if($.==1)
{
$colNo = s/\t/\t/g + 1;
if ($i==0)
{
#reads in the first hapmap genotype file and takes note of the
#order of the samples.
@samples = @fields;
if(defined($outputRootFileName))
{
$tgFile = "$outputRootFileName.tg";
$mkFile = "$outputRootFileName.mk";
}
else
{
$tgFile = "hapmap-panel.tg";
$mkFile = "hapmap-panel.mk";
}
open(TG, ">$tgFile") || die "Cannot open $tgFile";
open(MK, ">$mkFile") || die "Cannot open $mkFile";
print TG "snp-id";
for my $col (11 .. $#fields)
{
print TG "\t$fields[$col]";
}
print TG "\n";
print MK "snp-id\tchromosome\tposition\tstrand\talleles\tbuild\tgenotyping-center\tprotocol\tassay\tpanel\tqc-code\n";
}
else
{
if($colNo != s/\t/\t/g + 1)
{
die "Column numbers not consistent";
}
#subsequent reads of the following files will check that the
#samples are in order.
for my $col (0 .. $#fields)
{
die "Headers not the same: $fields[$col] and $samples[$col]" if $fields[$col] ne $samples[$col];
}
}
}
else
{
my $snp = $fields[0];
if (exists($SNP{$fields[0]}))
{
die "SNP $fields[0] already exists";
}
my @alleles = split("/", $fields[1]);
my $chromosome = $fields[2];
$chromosome =~ s/chr//;
my $position = $fields[3];
my $strand = $fields[4];
my $build = $fields[5];
my $genotypingCenter = $fields[6];
my $protocol = $fields[7];
my $assay = $fields[8];
my $panel = $fields[9];
my $qcCode = $fields[10];
print MK "$snp\t$chromosome\t$position\t$strand\t$alleles[0]/$alleles[1]\t$build\t$genotypingCenter\t$protocol\t$assay\t$panel\t$qcCode\n";
my $AA = "$alleles[0]$alleles[0]";
my $AB = "$alleles[0]$alleles[1]";
my $BB = "$alleles[1]$alleles[1]";
my $NN = "NN";
print TG "$snp";
for my $col (11 .. $#fields)
{
my $genotype = $fields[$col];
if ($genotype eq $AA)
{
print TG "\t0";
}
elsif ($genotype eq $AB)
{
print TG "\t1";
}
elsif ($genotype eq $BB)
{
print TG "\t2";
}
elsif ($genotype eq $NN)
{
print TG "\t-1";
}
else
{
warn "Genotype $genotype not recognised for $snp";
}
}
print TG "\n";
}
}
close(HAPMAP);
}