-
Notifications
You must be signed in to change notification settings - Fork 0
/
getvar_genefilesbz2_bygeno.pl
170 lines (143 loc) · 3.98 KB
/
getvar_genefilesbz2_bygeno.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
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
#!perl
#
# Description:
#
# Usage: perl untitled.pl
#
#
# Created by Jessica on 2012-03-26
use strict;
use warnings;
# use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error);
use Getopt::Long;
my ($genevarfile, $desiredgeno, $targetchr, $targetstart, $targetend, $outputfile);
GetOptions(
'i=s' => \$genevarfile,
'genotype=s' => \$desiredgeno,
'chr:s' => \$targetchr,
'start:i' => \$targetstart,
'end:i' => \$targetend,
'o=s' => \$outputfile,
);
if (!defined $genevarfile) {
optionUsage("option --i not defined\n");
} elsif (!defined $desiredgeno) {
optionUsage("option --genotype not defined\n");
} elsif (!defined $outputfile) {
optionUsage("option --o not defined\n");
}
sub optionUsage {
my $errorString = $_[0];
print "$errorString";
print "perl $0 \n";
print "\t--i\tinput gene-ASM* file\n";
print "\t--genotype\tgenotype desired, either: homref / het / homalt\n";
print "\t--chr\tchromosome (optional)\n";
print "\t--start\tstart position (optional)\n";
print "\t--end\tend position (optional)\n";
print "\t--o\toutput file\n";
die;
}
my $header;
my $prevallele;
my $countlines = 0;
open (OUT, ">$outputfile") or die "Cannot write to $outputfile: $!.\n";
open (IN, "bzcat $genevarfile |") or die "Cannot read $genevarfile: $!.\n";
while (<IN>) {
$countlines++;
my $nextline = $_;
if ($nextline =~ m/^#/ || $nextline =~ m/^\s*$/) { # skip header lines
next;
} elsif ($_ =~ m/^\>/) {
$nextline =~ s/\s+$//; # Remove line endings
my @headerline = split("\t", $nextline);
print OUT join("\t", @headerline[3..5]);
print OUT "\tref\ta1\ta2\t";
print OUT join("\t", @headerline[9,13..23])."\taminochange\n";
} else {
$nextline =~ s/\s+$//; # Remove line endings
my @line = split ("\t", $nextline);
my ($a1, $a2);
my ($allelenum, $thischr, $thisstart, $thisend, $vartype, $refallele, $calledallele) = @line[2..8];
my $varloc = $line[15];
# if ($countlines > 50) { # DEBUG
# print STDERR $nextline;
# print STDERR "$allelenum\n$thischr\n$thisstart\n$thisend\n$vartype\n$refallele\n$calledallele\n";
# exit;
# }
if (!defined $targetchr || ($thischr eq $targetchr && $thisstart>=$targetstart && $thisend<=$targetend)) {
if ($thischr eq 'chrX' || $thischr eq 'chrY' || $thischr eq 'chrM') {
last;
} elsif ($vartype eq 'snp' || $vartype eq 'ref') {
if ($allelenum == 1) {
$prevallele = $calledallele;
# print STDERR "for $allelenum, call=$calledallele\n"; # DEBUG
} elsif ($allelenum == 2 && testGenoClass($desiredgeno, $prevallele, $calledallele, $refallele) == 1) {
print OUT "$thischr\t$thisstart\t$thisend\t$refallele\t$prevallele\t$calledallele\t";
if (defined $line[9]) {
print OUT "$line[9]\t";
} else {
print OUT "\t";
}
for (my $i=13; $i<=23; $i++) {
if (defined $line[$i]) {
print OUT "$line[$i]\t";
} else {
print OUT "\t";
}
}
if (defined $line[20] && defined $line[22] && defined $line[23]) {
print OUT "$line[23]$line[20]$line[22]";
} else {
print OUT "\t";
}
print OUT "\n";
}
}
}
}
}
close IN;
close OUT;
print STDERR "done\n";
sub testGenoClass {
my ($testtype, $prevallele, $calledallele, $refallele) = @_;
my $testresult;
if ("$prevallele$calledallele" =~ '0' || "$prevallele$calledallele" =~ 'N') {
$testresult = '0';
}
if ($testtype eq 'het') {
$testresult = isHet($prevallele, $calledallele, $refallele);
}
if ($testtype eq 'homalt') {
$testresult = isHomAlt($prevallele, $calledallele, $refallele);
}
if ($testtype eq 'homref') {
$testresult = isHomRef($prevallele, $calledallele, $refallele);
}
return $testresult;
}
sub isHet {
my ($a1, $a2, $ref) = @_;
if ($a1 ne $a2 && "$a1$a2" =~ $ref) {
return 1;
} else {
return 0;
}
}
sub isHomAlt {
my ($a1, $a2, $ref) = @_;
if ($a1 eq $a2 && $a1 ne $ref) {
return 1;
} else {
return 0;
}
}
sub isHomRef {
my ($a1, $a2, $ref) = @_;
if ($a1 eq $a2 && $a1 eq $ref) {
return 1;
} else {
return 0;
}
}