forked from mccrowjp/Dropseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_bam_trans_reads_counts.pl
executable file
·51 lines (43 loc) · 1.04 KB
/
get_bam_trans_reads_counts.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
#!/usr/bin/env perl
use strict;
my $infile = shift;
unless($infile) {
die "Usage: $0 [BAM file (with XC and GE tags)]\n";
}
my %xccount;
my %xcgecount;
my %xcnotmapped;
my $count_noxc = 0;
open(IN, "samtools view $infile 2>/dev/null |");
print STDERR "reading $infile\n";
while(<IN>) {
chomp;
my ($id, $flag, $rname, $pos, $mapq, $cigar, $rnext, $pnext, $tlen, $seq, $qual, @cols) = split(/\t/);
my $xc = "";
my $ge = "";
foreach my $v (@cols) {
if($v =~ /^XC:Z:(\S+)/) {
$xc = $1;
} elsif($v =~ /^GE:Z:(\S+)/) {
$ge = $1;
}
}
if($xc) {
if($ge) {
$xcgecount{$xc}{$ge}++;
$xccount{$xc}++;
} else {
$xcnotmapped{$xc}++;
}
} else {
$count_noxc++;
}
}
close(IN);
print join("\t", ('barcode', 'reads', 'genes', 'unmapped_reads'))."\n";
foreach my $xc (sort {$xccount{$b}<=>$xccount{$a}} keys %xccount) {
print join("\t", ($xc, $xccount{$xc}, scalar(keys %{$xcgecount{$xc}}), $xcnotmapped{$xc}))."\n";
}
if($count_noxc > 0) {
print STDERR "Reads with no XC tag: $count_noxc\n";
}