-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathweeSAMv1.1
executable file
·314 lines (287 loc) · 10.8 KB
/
weeSAMv1.1
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
#!/usr/bin/perl -w
# Script written by Joseph Hughes at the University of Glasgow
# Use this script to produce information on the breadth and depth of coverage
# from a SAM file. Gives the number of mapped reads. Average coverage
# Min and Max coverage
# Modified 15-11-2016 to add -d 10000000 to samtools depth command
use Getopt::Long;
use strict;
use CGI ':standard';
use GD::Graph::lines;
use GD::Text;
use Statistics::R;
# declaring global variables
my $cutoff=0;
my ($samfile,$bamfile,$out,$help,%info,$totalunmapped,$totalmapped,%depth);
&GetOptions(
'-s:s' => \$samfile,#the sam file
'-b:s' => \$bamfile,#the bam file
'-c:i' => \$cutoff,# the number of reads to use as a cut-off
"h|help|?" => \$help,#the help
'-out=s' => \$out,#file with coverage of for the file
);
if (($help)||(!$bamfile&&!$samfile)){
print " _)_ Usage : weeSAM.pl <list of arguments>\n";
print " .-'(/ '-. -s <txt> - the input SAM file\n";
print " / ` \\ -b <txt> - the input BAM file\n";
print " / - - \\ -c <int> - cut-off value for number of mapped reads [default = 0]\n";
print " (` a a `) -out <txt> - the output file name\n";
print " \\ ^ / -help|h|? - Get this help\n";
print " '. '---' .' \n";
print " .-`'---'`-. \n";
print " / \\ \n";
print " / / ' ' \\ \\ \n";
print " _/ /| SAM |\\ \\_ \n";
print " `/|\\` |+++++++|`/|\\` \n";
print " /\ /\\ \n";
print " | `-._.-` | \n";
print " \\ / \\ / \n";
print " |_ | | _| \n";
print " | _| |_ | \n";
print " (ooO Ooo) \n";
exit();
}
if ($bamfile){
#print "$bamfile\n";
makeSumStats($bamfile);
}
if ($samfile){
print "Converting bam to sam ...\n";
my $filename=$samfile;
my $stub=$samfile;
my $filename_sorted;
# my $sorted=$filename."_sorted"; #added variable for samtools 1.3 SM
$filename=~s/\.sam/\.bam/;
$stub=~s/\.sam//;
my $sorted=$stub."_sorted"; #added variable for samtools 1.3 SM
system("samtools view -S $samfile -b -o $filename");
print "Sorting bam ...\n";
system("samtools sort -o $sorted $filename"); #updated sort command for samtools 1.3
print "bam indexing ...\n";
system("samtools index $sorted");
makeSumStats($sorted);
}
sub makeSumStats{
my $bam=shift;
print "$bam\n";
open(OUT,">$out")||die "Can't open output\n";
system("samtools idxstats $bam > tmp_mapped.txt");
open(MAPPED,"<tmp_mapped.txt")||die "Can't open tmp_mapped.txt\n";
while(<MAPPED>){
chomp($_);
my @cols=split(/\t/,$_);
if ($_=~/^\*/){
$totalunmapped+=$cols[3];
}else{
$totalmapped+=$cols[2];
$totalunmapped+=$cols[3];
if ($cols[2]>$cutoff){
#print "$_","\n";
$info{$cols[0]}{"reflength"}=$cols[1];
$info{$cols[0]}{"mapped"}=$cols[2];
}
}
}
close(MAPPED);
system("rm tmp_mapped.txt");
print "Total of mapped = $totalmapped\nTotal of unmapped = $totalunmapped\n";
my $totalreads=$totalunmapped+$totalmapped;
print "Total reads $totalreads\n";
system("samtools depth -d 10000000 $bam > tmp_depth.txt");
open(DEPTH,"<tmp_depth.txt")||die "Can't open tmp_depth.txt\n";
while(<DEPTH>){
chomp($_);
my @cols=split(/\t/,$_);
if ($info{$cols[0]}{"reflength"}){
$depth{$cols[0]}{$cols[1]}=$cols[2];
$info{$cols[0]}{"breadth"}++;
$info{$cols[0]}{"sumdepth"}+=$cols[2];
if (!$info{$cols[0]}{"min"}){
$info{$cols[0]}{"min"}=$cols[2];
}elsif ($info{$cols[0]}{"min"}>$cols[2] && $info{$cols[0]}{"min"}){
$info{$cols[0]}{"min"}=$cols[2];
}
if (!$info{$cols[0]}{"max"}){
$info{$cols[0]}{"max"}=$cols[2];
}elsif ($info{$cols[0]}{"max"}<$cols[2] && $info{$cols[0]}{"max"}){
$info{$cols[0]}{"max"}=$cols[2];
}
}
}
close(DEPTH);
system("rm tmp_depth.txt");
print OUT "ReferenceID\tRefLength\tMappedReads\tBreadth\tPercentCovered\tMinDepth\tMaxDepth\tAverageDepth\n";
for my $id (keys %info){
if ($info{$id}{"reflength"}){
print "Processing $id ....\n";
print OUT "$id\t".$info{$id}{"reflength"}."\t";
print OUT $info{$id}{"mapped"}."\t";
print OUT $info{$id}{"breadth"}."\t";
print OUT $info{$id}{"breadth"}*100/$info{$id}{"reflength"}."\t";
print OUT $info{$id}{"min"}."\t".$info{$id}{"max"}."\t";
print OUT $info{$id}{"sumdepth"}/$info{$id}{"breadth"}."\n";
# R plots causing problems
#coveragePlotR($id,%{$depth{$id}});
}
}
}
# sub coveragePlotR{
# my (@x,@y);
# my ($id,%deep)=@_;
# my $length=$info{$id}{"reflength"};
# #print "$length\n";
# #print "$id\n";
# while (my ($x, $y) = each %deep) {
# push @x, $x;
# push @y, $y;
# #print "$x\t$y\n";
# }
# #print scalar(@x)."\t".scalar(@y)."\n";
# my $outputname=$id;
# $outputname=~s/\s/_/g;
# $outputname=~s/\./_/g;
# $outputname=~s/\,/_/g;
# $outputname=~s/\|/_/g;
# $outputname=~s/\\/_/g;
# $outputname=~s/\//_/g;
# print "$outputname\n";
# my $R = Statistics::R->new() ;
# #my $R = Statistics::R->new(shared => 1) ;
# #my $R2 = Statistics::R->new( shared => 1);
# $R->startR ;
# $R->run(qq`x<-vector()`);
# $R->run(qq`y<-vector()`);
# # for (my $i=0; $i<scalar(@x); $i=$i+1000){
# # my @subsetx=@x[$i..999];
# # $R->set( 'xsubset', \@subsetx );
# # $R->run(qq`x=c(x,xsubset)`);
# # my @subsety=@y[$i..999];
# # $R->set( 'ysubset', \@subsety );
# # $R->run(qq`y=c(y,ysubset)`);
# #
# # }
# $R->set( 'x', \@x );
# $R->set( 'y', \@y );
# $R->set( 'xmax',$length );
# $R->run(qq`data=as.data.frame(cbind(x,y))`);
# #my $xdata = $R->get('x');
# #print "@x\n\nRdata\n\n@$xdata\n";
#
# my $data = $R->get('data');
# print "@$data\n";
# $R->run(q`sorted.data<-data[order(data$x),]`);
# $R->run(qq`all.sites <- seq(1, xmax)`);
# $R->run(qq`all.sites.frame <- data.frame(list(x=all.sites))`);
# $R->run(qq`merged.data <- merge(all.sites.frame, sorted.data, all=T)`);
# $R->run(q`merged.data$y[which(is.na(merged.data$y))] <- 0`);
# my $mergeddata = $R->get('merged.data');
# print "MERGED DATA @$mergeddata\n";
# $R->run(qq`library(ggplot2)`);
# $R->run(qq`coverage<-ggplot(merged.data, aes(x = x)) + geom_ribbon(aes(ymin=0, ymax=y), size = 1) + xlab("Site") + ylab("Reads") + xlim(1, xmax) + ylim(0, max(y)) + theme_bw() + opts(plot.title = element_text(size = 10))+ ggtitle("Reference: $id\nMapped reads: $info{$id}{"mapped"}\nBreadth: $info{$id}{"breadth"} sites\n") `);
# $R->run(qq`ggsave(coverage,file="$outputname\.pdf", width=8, height=4)`);
# $R->stopR() ;
# }
sub coveragePlotR{
my (@x,@y);
my ($id,%deep)=@_;
my $length=$info{$id}{"reflength"};
#print "$length\n";
#print "$id\n";
while (my ($x, $y) = each %deep) {
push @x, $x;
push @y, $y;
#print "$x\t$y\n";
}
my $outputname=$id;
$outputname=~s/\s/_/g;
$outputname=~s/\./_/g;
$outputname=~s/\,/_/g;
$outputname=~s/\|/_/g;
$outputname=~s/\\/_/g;
$outputname=~s/\//_/g;
print "$outputname\n";
my $R = Statistics::R->new() ;
#my $R = Statistics::R->new(shared => 1) ;
#my $R2 = Statistics::R->new( shared => 1);
# $R->set( 'x', \@x );
# $R->set( 'y', \@y );
$R->run(qq`x<-vector()`);
$R->run(qq`y<-vector()`);
while ( my @chunksx = splice @x, 0, 5000 ) {
$R->set( 'xsubset', \@chunksx );
$R->run(qq`x=c(x,xsubset)`);
}
while ( my @chunksy = splice @y, 0, 5000 ) {
$R->set( 'ysubset', \@chunksy );
$R->run(qq`y=c(y,ysubset)`);
}
# for (my $i=0; $i<scalar(@x); $i=$i+1000){
# my @subsetx=@x[$i..999];
# $R->set( 'xsubset', \@subsetx );
# $R->run(qq`x=c(x,xsubset)`);
# my @subsety=@y[$i..999];
# $R->set( 'ysubset', \@subsety );
# $R->run(qq`y=c(y,ysubset)`);
# }
$R->set( 'xmax',$length );
$R->run(qq`data=as.data.frame(cbind(x,y))`);
#my $xdata = $R->get('x');
#print "@x\n\nRdata\n\n@$xdata\n";
#my $data = $R->get('data');
#print "@$data\n";
$R->run(q`sorted.data<-data[order(data$x),]`);
$R->run(qq`all.sites <- seq(1, xmax)`);
$R->run(qq`all.sites.frame <- data.frame(list(x=all.sites))`);
$R->run(qq`merged.data <- merge(all.sites.frame, sorted.data, all=T)`);
$R->run(q`merged.data$y[which(is.na(merged.data$y))] <- 0`);
#my $mergeddata = $R->get('merged.data');
#print "MERGED DATA @$mergeddata\n";
$R->run(qq`library(ggplot2)`);
$R->run(qq`coverage<-ggplot(merged.data, aes(x = x)) + geom_ribbon(aes(ymin=0, ymax=y), size = 1) + xlab("Site") + ylab("Reads") + xlim(1, xmax) + ylim(0, max(y)) + theme_bw() + opts(plot.title = element_text(size = 10))+ ggtitle("Reference: $id\nMapped reads: $info{$id}{"mapped"}\nBreadth: $info{$id}{"breadth"} sites\n") `);
$R->run(qq`ggsave(coverage,file="$outputname\.pdf", width=8, height=4)`);
}
sub coveragePlotGD{
my (@x,@y);
my ($length,%deep)=@_;
print "Length = $length\n";
# foreach my $key (sort {$a<=>$b} keys %deep){
# print "$key $deep{$key}\n";
# }
while (my ($x, $y) = each %deep) {
push @x, $x;
push @y, $y;
}
my @data = (\@x, \@y);
my $mygraph = GD::Graph::lines->new(900, 300);
$mygraph->set(
x_label => 'Site',
y_label => 'Depth',
title => 'Depth of coverage',
# Draw datasets in 'solid', 'dashed' and 'dotted-dashed' lines
line_types => [1],
# Set the thickness of line
line_width => 2,
# Set colors for datasets
dclrs => ['red'],
x_tick_number => 'auto',
) or warn $mygraph->error;
#$mygraph->set_x_axis_font(GD::Font->Large);
$mygraph->set_y_axis_font('/usr/share/fonts/truetype/freefont/FreeSans.ttf',14);
# $mygraph->set_y_axis_font(GD::Font->Large);
$mygraph->set_x_label_font('/usr/share/fonts/truetype/freefont/FreeSans.ttf',16);
$mygraph->set_y_label_font('/usr/share/fonts/truetype/freefont/FreeSans.ttf',16);
$mygraph->set_x_axis_font('/usr/share/fonts/truetype/freefont/FreeSans.ttf',14);
$mygraph->set_y_axis_font('/usr/share/fonts/truetype/freefont/FreeSans.ttf',14);
$mygraph->set_values_font('/usr/share/fonts/truetype/freefont/FreeSans.ttf',14);
$mygraph->set_title_font('/usr/share/fonts/truetype/freefont/FreeSans.ttf',20);
my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
#print "Content-type: image/png\n\n";
#print $myimage->png;
# Open a file for writing
open(PICTURE, ">picture.png") or die("Cannot open file for writing");
# Make sure we are writing to a binary stream
binmode PICTURE;
# Convert the image to PNG and print it to the file PICTURE
print PICTURE $myimage->png;
close PICTURE;
}