forked from riverlee/pileup2base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pileup2baseindel.pl
229 lines (208 loc) · 6.14 KB
/
pileup2baseindel.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
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
#!/usr/bin/perl
#############################################
#Author: Jiang Li
#email: [email protected]
#Creat Time: Fri 03 Aug 2012 10:34:54 PM CDT
#Vanderbilt Center for Quantitative Sciences
#############################################
use strict;
use warnings;
use File::Basename;
use Getopt::Long;
use IO::File;
######### Update from pile2base.pl ####################
# 1).it will parse the insert and deletion too,
# 2).it can parse pileups from multiple sample,
# 3).you don't need to define the outputfile,instead,
# you will need to provide 'prefix' which will name result from each sample
# as prefix1.txt, prefix2.txt,etc. Default, prefix is sample.
# 4).Provide option to define the base quality score offset, default is 33 (Sanger standard).
# 5).Read parameters from command line with options
#Usage: perl pileup2baseindel.pl -i <pileupfile> -bq [BQcutoff] -prefix [sample] -offset [33]
my $usage = <<USAGE;
Usage: perl pileup2base.pl -i <pileupfile> -bq [BQcutoff] -prefix [sample] -offset [33]
-i input pileup file, could be from 1 sample or multiple samples
-bq base quality score cutoff for each mapped/unmapped base,
only those larger than cutoff will be output in the result, default is -5, means no filter
-prefix output file prefix, default is sample, the output will be named as prefix1.txt, prefix2.txt, etc.
-offset Offset to change ASCII character to base quality score, default is 33 (sanger format).
-h print out this
USAGE
my ($input,$BQcut,$offset,$prefix,$help) = (undef,-5,33,"sample",undef);
GetOptions(
"i=s"=>\$input,
"bq=i"=>\$BQcut,
"offset=i"=>\$offset,
"prefix=s"=>\$prefix,
"h"=>\$help
);
if($help){
print $usage;
exit(0);
}
unless ($input){
print "Input file does not provide yet\n";
print "\n$usage\n";
exit(1);
}
if(! -e $input){
print "Input file '$input' does not exists\n";
print "\n$usage\n";
exit(1);
}
#Do the parsing
open FILE, $input or die "error, can not open $input";
print "[",scalar(localtime),"] Begin parsing...\n";
my $line = <FILE>;
$line=~s/\r|\n//g;
my ($chr,$loc,$ref,@dp_bases_bq) = split /\s+/, $line;
my $n = int (scalar(@dp_bases_bq)/3); #determine how many samples, use int just in safe
my %files;
foreach my $i (1..$n){
my $fh = new IO::File;
$fh->open("> ${prefix}${i}.txt");
print $fh "chr\t"."loc\t"."ref\t"."A\t"."T\t"."C\t"."G\t"."a\t"."t\t"."c\t"."g\t"."Insertion\t"."Deletion\n";
my @region=(3*($i-1),3*($i-1)+1,3*($i-1)+2);
my ($dp,$bases,$bq) = @dp_bases_bq[@region];
my $str = parsePileup($bases,$bq,$BQcut,$offset);
if($str ne "*"){
print $fh join "\t",($chr,$loc,$ref,$str);
}
$files{$i}=$fh;
}
while(<FILE>){
s/\r|\n//g;
my ($chr,$loc,$ref,@dp_bases_bq) = split /\s+/;
my $n = int (scalar(@dp_bases_bq)/3); #determine how many samples, use int just in safe
foreach my $i (1..$n){
my $fh = $files{$i};
my @region=(3*($i-1),3*($i-1)+1,3*($i-1)+2);
my ($dp,$bases,$bq) = @dp_bases_bq[@region];
my $str = parsePileup($bases,$bq,$BQcut,$offset);
if($str ne "*"){
print $fh join "\t",($chr,$loc,$ref,$str);
}
}
}
close FILE;
foreach my $k (keys %files){
$files{$k}->close;
}
print "[",scalar(localtime),"] Finished\n";
sub parsePileup{
my ($bases,$bq,$BQcut,$offset) = @_;
if($bases eq "*"){
return "*";
}
#do some modificaton on $base to remove additional characters
#1,remove the ^. pattern
$bases=~s/\^.//g;
#2,remove the $ pattern
$bases=~s/\$//g;
#3,remove -[0-9]+[ACGTNacgtn]+ pattern
my %hash=();
my %deletion=();
while($bases=~/-(\d+)/g){
$hash{$1}=1;
}
#get the deletion sequences and delete them
foreach my $k (keys %hash){
while($bases=~/-$k([ACGTNacgtn]{$k})/g){
$deletion{$1}++;
}
$bases=~s/-$k[ACGTNacgtn]{$k}//g;
}
%hash=();
my %insertion=();
while($bases=~/\+(\d+)/g){
$hash{$1}=1;
}
foreach my $k (keys %hash){
while($bases=~/\+$k([ACGTNacgtn]{$k})/g){
$insertion{$1}++;
}
$bases=~s/\+$k[ACGTNacgtn]{$k}//g;
}
#Now @base and @bq have the same length
my @base=split (//,$bases);
my @bq=split(//,$bq);
#I have check it
#if(scalar(@base) ne scalar(@bq)){
# print $_,"\n";
#}
#foreach my $c (@base){
# $check{$c}++;
#}
my $forward_A=0;
my $forward_T=0;
my $forward_C=0;
my $forward_G=0;
my $reverse_A=0;
my $reverse_T=0;
my $reverse_C=0;
my $reverse_G=0;
#start the loop
for(my $i=0;$i<@base;$i++){
my $ch=$base[$i];
my $score=ord($bq[$i])-$offset;
if($score>=$BQcut){
if($ch eq "A"){
$forward_A++;
}elsif($ch eq "T"){
$forward_T++;
}elsif($ch eq "C"){
$forward_C++;
}elsif($ch eq "G"){
$forward_G++;
}elsif($ch eq "a"){
$reverse_A++;
}elsif($ch eq "t"){
$reverse_T++;
}elsif($ch eq "c"){
$reverse_C++;
}elsif($ch eq "g"){
$reverse_G++;
}elsif($ch eq "."){
if($ref eq "A"){
$forward_A++;
}elsif($ref eq "T"){
$forward_T++;
}elsif($ref eq "C"){
$forward_C++;
}elsif($ref eq "G"){
$forward_G++;
}
}elsif($ch eq ","){
if($ref eq "A"){
$reverse_A++;
}elsif($ref eq "T"){
$reverse_T++;
}elsif($ref eq "C"){
$reverse_C++;
}elsif($ref eq "G"){
$reverse_G++;
}
}
}#end the condition $score>=$BQcut
}#end the loop
#my $str="$chr\t$loc"."\t".$ref."\t".$forward_A."\t".$forward_T."\t".$forward_C."\t".$forward_G."\t".$reverse_A."\t".$reverse_T."\t".$reverse_C."\t".$reverse_G."\t";
my $str=$forward_A."\t".$forward_T."\t".$forward_C."\t".$forward_G."\t".$reverse_A."\t".$reverse_T."\t".$reverse_C."\t".$reverse_G."\t";
my $insertion="NA";
my $deletion="NA";
if(scalar(keys %insertion)){
$insertion="";
foreach my $k (sort {$insertion{$b}<=>$insertion{$a}} keys %insertion){
$insertion.=$insertion{$k}.":".$k."|";
}
chop($insertion);
}
if(scalar(keys %deletion)){
$deletion="";
foreach my $k (sort {$deletion{$b}<=>$deletion{$a}} keys %deletion){
$deletion.=$deletion{$k}.":".$k."|";
}
chop($deletion);
}
$str.=$insertion."\t".$deletion."\n";
return $str;
}