-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_subject_genotypes.pl
131 lines (112 loc) · 3.42 KB
/
get_subject_genotypes.pl
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
#!perl
#
# Description: From a given testvar file, get all variants in a specific region and produce a PLINK-like output of genotypes
#
#
#
# Created by Jessica Chong on 2012-06-11
use strict;
use warnings;
use Getopt::Long;
my ($testvardir, $desiredchr, $desiredstart, $desiredend, $outputfile);
GetOptions(
'testvardir=s' => \$testvardir,
'chr=s' => \$desiredchr,
'start=i' => \$desiredstart,
'end=i' => \$desiredend,
'out=s' => \$outputfile,
);
if (!defined $testvardir) {
optionUsage("option --testvardir not defined\n");
} elsif (!defined $outputfile) {
optionUsage("option --out not defined\n");
} elsif (!defined $desiredchr) {
optionUsage("options --chr not defined\n");
} elsif (!defined $desiredstart) {
optionUsage("options --start not defined\n");
} elsif (!defined $desiredend) {
optionUsage("options --end not defined\n");
}
if ($desiredchr !~ 'chr') {
$desiredchr = "chr$desiredchr";
}
if ($desiredend < $desiredstart) {
print STDERR "End $desiredend is before start $desiredstart\n";
die;
} elsif ($desiredstart > $desiredend) {
print STDERR "Start $desiredstart is after end $desiredend\n";
die;
}
my $testvarfile = "$testvardir/all.2012-09-20.testvar.$desiredchr.tsv.gz";
my (%findiv2CGI, %CGI2findiv);
open (FILE, "/ober_resources/CGI_98Hutterites_WGS/README.assembly_sample_subject.csv") or die "Cannot read README.assembly_sample_subject.csv file.\n";
<FILE>;
while ( <FILE> ) {
$_ =~ s/\s+$//; # Remove line endings
my @line = split (",", $_);
$findiv2CGI{$line[2]} = $line[0];
$CGI2findiv{$line[0]} = $line[2];
}
close FILE;
# my $desiredCGIid = $findiv2CGI{$desiredfindiv};
open (FILE, "zcat $testvarfile |") or die "Cannot read $testvarfile file: $!.\n";
my $headerline = <FILE>;
$headerline =~ s/\s+$//; # Remove line endings
my @testvarIDs = split("\t", $headerline);
close FILE;
open (OUT, ">$outputfile") or die "Cannot write to $outputfile: $!.\n";
print OUT "variantId\tChr\tStart\tStop\tvarType\tref\talt\tFindiv\ta1\ta2\n";
my @variants = `tabix $testvarfile $desiredchr:$desiredstart-$desiredend`;
foreach my $variant (@variants) {
$variant =~ s/\s+$//; # Remove line endings
my @line = split("\t", $variant);
my ($thisvarnum, $thischr, $thisstart, $thisend) = @line[0..3];
my ($ref, $alt) = @line[5..6];
if ($line[4] eq 'ins') {
$ref = "ref";
$alt = "ins$alt";
}
if ($line[4] eq 'del') {
$alt = "del$ref";
}
for (my $i=8; $i<=$#line; $i++) {
print OUT join("\t", @line[0..4]);
print OUT "\t$ref\t$alt";
# if ($line[5] !~ /^$/) { # insertions have a blank space for the ref allele
# print OUT "\t$line[5]";
# } else {
# print OUT "\tref";
# }
# if ($line[6] !~ /^$/) {
# print OUT "\t$line[6]";
# } else {
# print OUT "\t";
# }
print OUT "\t$CGI2findiv{$testvarIDs[$i]}\t";
my $genotype = $line[$i];
my @genoalleles = split("", $line[$i]);
my @formattedgeno;
foreach my $allele (@genoalleles) {
if ($allele eq '0') {
push(@formattedgeno, $ref);
} elsif ($allele eq '1') {
push(@formattedgeno, $alt);
} elsif ($allele eq 'N') {
push(@formattedgeno, 'N');
}
}
print OUT join("\t", @formattedgeno)."\n";
}
}
close OUT;
sub optionUsage {
my $errorString = $_[0];
print "$errorString";
print "perl $0 \n";
print "\t--testvardir\ttestvar directory\n";
print "\t--out\toutput file\n";
print "\t--chr\tdesired chr, must be 'chrX, chr1' style\n";
print "\t--start\tdesired start\n";
print "\t--end\tdesired end\n";
die;
}