-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgt2structure
executable file
·444 lines (393 loc) · 11.3 KB
/
gt2structure
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/perl
use warnings;
use strict;
use fralib;
use Getopt::Long;
use File::Basename;
use Pod::Usage;
=head1 NAME
gt2structure
=head1 SYNOPSIS
gt2structure [options] <genotype-file>
-v verbose
-d debug
-a autosomal snps only
-s sample annotation file (required)
a)sample-id
b)population-id
c)sex
-m snp annotation file (not required if autosomal switch is on)
a)snp-id
b)chromosome
gt-file sample major genotype file
example: gt2structure -s pscalare.sa -m pscalare.mk pscalare.gt
gt2structure -s pscalare.sa -a pscalare.gt
Outputs structure input data file and parameter files.
Also outputs a population id and population name tab delimited file.
structure -c 2 -n <numpops> -o <output-file> -i pscalare.dat
=head1 DESCRIPTION
=cut
#option variables
my $verbose;
my $debug;
my $help;
my $gtFile;
my $sampleAnnotationFile;
my $snpAnnotationFile;
my $paramFile;
my $extraParamFile;
#common data variables
my $colNo;
my %label2col;
#data structures
my %SAMPLE;
my %SNP;
my %POPULATION;
my $snpNo;
my $sampleNo;
my $popNo;
my @col2snp;
my $autosomal;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions ('v'=>\$verbose, 'd'=>\$debug, 'h'=>\$help, 's=s'=>\$sampleAnnotationFile, 'm=s'=>\$snpAnnotationFile, 'a'=>\$autosomal)
||!defined($sampleAnnotationFile) || (!$autosomal && !defined($snpAnnotationFile)) || scalar(@ARGV)!=1)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
$gtFile = $ARGV[0];
#checks if input is not a genotype file
isGt($gtFile) || die "$gtFile not a gt file";
#read sample annotation
open(SAMPLE_ANNOTATION, $sampleAnnotationFile) || die "Cannot open $sampleAnnotationFile";
while(<SAMPLE_ANNOTATION>)
{
s/\r?\n?$//;
if($.==1)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
my @labels;
if ($autosomal)
{
@labels = ('sample-id', 'population-id');
}
else
{
@labels = ('sample-id', 'population-id', 'sex');
}
SEARCH_LABEL: for my $label (@labels)
{
for my $col (0 .. $#fields)
{
if ($fields[$col] eq $label)
{
$label2col{$label}=$col;
next SEARCH_LABEL;
}
}
die "Cannot find '$label' in $sampleAnnotationFile";
}
}
else
{
my @fields = split('\t', $_, $colNo);
my $sampleID = $fields[$label2col{'sample-id'}];
my $population = $fields[$label2col{'population-id'}];
$SAMPLE{$sampleID}{POPULATION} = $population;
$POPULATION{$population}{ID} = 0;
if (!$autosomal)
{
my $sex = $fields[$label2col{'sex'}];
$SAMPLE{$sampleID}{SEX} = $sex;
}
}
}
close(SAMPLE_ANNOTATION);
if (!$autosomal)
{
#read SNP annotation
open(SNP_ANNOTATION, $snpAnnotationFile) || die "Cannot open $snpAnnotationFile";
while(<SNP_ANNOTATION>)
{
s/\r?\n?$//;
if($.==1)
{
$colNo = s/\t/\t/g + 1;
my @fields = split('\t', $_, $colNo);
SEARCH_LABEL: for my $label ('snp-id', 'chromosome')
{
for my $col (0 .. $#fields)
{
if ($fields[$col] eq $label)
{
$label2col{$label}=$col;
next SEARCH_LABEL;
}
}
die "Cannot find '$label' in $snpAnnotationFile";
}
}
else
{
my @fields = split('\t', $_, $colNo);
my $snpID = $fields[$label2col{'snp-id'}];
my $chromosome = $fields[$label2col{'chromosome'}];
$SNP{$snpID}{CHROMOSOME} = $chromosome;
}
}
close(SNP_ANNOTATION);
}
#prepare structure data file
open (GENO, $gtFile) || die "Cannot open $gtFile";
$popNo = 0;
my ($name, $path, $ext) = fileparse($ARGV[0], '\..*');
my $dataFile = "$name.dat";
open (DATA, ">$dataFile") || die "Cannot open $dataFile";
my $popIdNameFile = "popid-popname-$name.txt";
open (POP_ID_NAME, ">$popIdNameFile") || die "Cannot open $popIdNameFile";
while (<GENO>)
{
s/\r?\n?$//;
if($.==1)
{
$colNo = s/\t/\t/g + 1;
$snpNo = $colNo - 1;
my @fields = split('\t', $_, $colNo);
for my $col (1..$#fields)
{
$col2snp[$col] = $fields[$col];
}
}
else
{
my @fields = split('\t', $_, $colNo);
my $sample = $fields[0];
exists($SAMPLE{$sample}) || die "$sample not found in $sampleAnnotationFile";
my $sampleID = $.-1;
my $sex = $SAMPLE{$sample}{SEX} if (!$autosomal);
my $population = $SAMPLE{$sample}{POPULATION};
my $populationID = $POPULATION{$population}{ID};
my $genotype;
my $snp;
if ($populationID==0)
{
$populationID = ++$popNo;
$POPULATION{$population}{ID} = $populationID;
print POP_ID_NAME "$populationID\t$population\n"
}
if ($autosomal || $sex eq 'female')
{
print DATA "$sample\t$populationID";
for my $col (1..$#fields)
{
$genotype = $fields[$col];
if($genotype==0)
{
print DATA "\t1";
}
elsif($genotype==1)
{
print DATA "\t1";
}
elsif($genotype==2)
{
print DATA "\t2";
}
elsif($genotype==-1)
{
print DATA "\t-1";
}
else
{
die "Unrecognised genotype: $genotype at $col";
}
}
print DATA "\n";
print DATA "$sample\t$populationID";
for my $col (1..$#fields)
{
$genotype = $fields[$col];
if($genotype==0)
{
print DATA "\t1";
}
elsif($genotype==1)
{
print DATA "\t2";
}
elsif($genotype==2)
{
print DATA "\t2";
}
elsif($genotype==-1)
{
print DATA "\t-1";
}
else
{
die "Unrecognised genotype: $genotype at $col";
}
}
print DATA "\n";
}
elsif ($sex eq 'male')
{
print DATA "$sample\t$populationID";
for my $col (1..$#fields)
{
my $snp = $col2snp[$col];
$genotype = $fields[$col];
if ($SNP{$snp}{CHROMOSOME} eq 'X')
{
if($genotype==0)
{
print DATA "\t1";
}
elsif($genotype==1)
{
print DATA "\t-1";
}
elsif($genotype==2)
{
print DATA "\t2";
}
elsif($genotype==-1)
{
print DATA "\t-1";
}
else
{
die "Unrecognised genotype: $genotype at $col";
}
}
else
{
if($genotype==0)
{
print DATA "\t1";
}
elsif($genotype==1)
{
print DATA "\t1";
}
elsif($genotype==2)
{
print DATA "\t2";
}
elsif($genotype==-1)
{
print DATA "\t-1";
}
else
{
die "Unrecognised genotype: $genotype at $col";
}
}
}
print DATA "\n";
print DATA "$sample\t$populationID";
for my $col (1..$#fields)
{
my $snp = $col2snp[$col];
$genotype = $fields[$col];
if ($SNP{$snp}{CHROMOSOME} eq 'X')
{
if($genotype==0)
{
print DATA "\t-1";
}
elsif($genotype==1)
{
print DATA "\t-1";
}
elsif($genotype==2)
{
print DATA "\t-1";
}
elsif($genotype==-1)
{
print DATA "\t-1";
}
else
{
die "Unrecognised genotype: $genotype at $col";
}
}
else
{
if($genotype==0)
{
print DATA "\t1";
}
elsif($genotype==1)
{
print DATA "\t2";
}
elsif($genotype==2)
{
print DATA "\t2";
}
elsif($genotype==-1)
{
print DATA "\t-1";
}
else
{
die "Unrecognised genotype: $genotype at $col";
}
}
}
print DATA "\n";
}
else
{
die "Unrecognised sex: $sample, $sex";
}
}
}
$sampleNo = $. - 1;
close(GENO);
close(POP_ID_NAME);
close(DATA);
#prepare structure options file
$paramFile = "mainparams";
open (PARAMETERS, ">$paramFile") || die "Cannot open $paramFile";
print PARAMETERS <<PARAMS;
#define INFILE $dataFile
#define OUTFILE $dataFile
#define NUMINDS $sampleNo
#define NUMLOCI $snpNo
#define LABEL 1
#define POPDATA 1
#define POPFLAG 0
#define PHENOTYPE 0
#define EXTRACOLS 0
#define PHASEINFO 0
#define MISSING -1
#define PLOIDY 2
#define ONEROWPERIND 0
#define MAPDISTANCES 0
#define MAXPOPS $popNo
#define BURNIN 10000
#define NUMREPS 10000
#define INFERLAMBDA 0
#define FREQSCORR 1
PARAMS
close(PARAMETERS);
$extraParamFile = "extraparams";
open (EXTRA_PARAMETERS, ">$extraParamFile") || die "Cannot open $extraParamFile";
print EXTRA_PARAMETERS <<EXTRA_PARAMS;
#define ANCESTDIST 1
EXTRA_PARAMS
close(EXTRA_PARAMETERS);
print "No of samples: $sampleNo\n";
print "No of snps: $snpNo\n";
print "No of populations: $popNo\n";