-
Notifications
You must be signed in to change notification settings - Fork 2
/
stat_feature_mapping.pl
executable file
·178 lines (165 loc) · 5.3 KB
/
stat_feature_mapping.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
#!/usr/bin/perl -w
###########################################################################
# Count reads mapping on genomic features: AS,IGR,mRNA,rRNA,tRNA
#
# Using 'featureCounts' to count reads number.
#
# Wang Ming wangmcas(AT)gmail.com 2015-09-09
###########################################################################
use strict;
use warnings;
use Getopt::Std;
use File::Basename qw(basename dirname);
use File::Spec::Functions qw(catfile catdir);
use File::Path qw(make_path remove_tree);
use Cwd qw(abs_path);
### TOOLs
my $find_feature = "/home/wangming/work/bin/temp/find_genomefeatures.pl";
my $featureCounts = "/home/wangming/work/bin/temp/chk_feature2count.pl";
my $sort2bed = "/home/wangming/work/bin/temp/sort2bed.pl";
# NEED 'featureCounts' in PATH
stat_feature_mapping();
sub stat_feature_mapping {
my %opts = ();
getopts("b:f:o:", \%opts);
usage() if(@ARGV != 1);
die("[-f] need reference *.fna \n") if(! defined $opts{f});
my $outdir = $ARGV[0];
### prepare features
my @tmp = prep_feature($opts{f}, $outdir);
my $feature_dir = $tmp[0];
my @feature_files = @{ $tmp[1] };
### prepare BAM
my @bams = readBAMlist($opts{b}, $outdir);
### count each features
my @stats = ();
for my $f (@feature_files) {
### convert bed to gff
my $gff = $f;
$gff =~ s/\.bed$/.gff/;
system("perl $sort2bed -t bed2gff -f exon -i $f -o $gff");
my @s = count_feature($gff, \@bams, $outdir);
push @stats, @s;
}
### all
my $f_stat = catfile($outdir, 'feature.stats');
open my $fh_f, "> $f_stat" or die "Cannot write $f_stat, $!\n";
print $fh_f join("\n", @stats) . "\n";
close $fh_f;
print join("\n", @stats) . "\n";
}
sub prep_feature {
my $ref = $_[0];
my $outdir = $_[1];
###
my $fname = $ref;
$fname =~ s/\.f(n)?a//;
my $gff = $fname . '.gff';
my $ptt = $fname . '.ptt';
my $rnt = $fname . '.rnt';
for my $f ($gff, $ptt, $rnt) {
die("[$f] file not exists\n") if(! -e $f);
}
### feature_dir, chr
my $feature_dir = catdir($outdir, '1.Features');
make_path($feature_dir) if( ! -d $feature_dir);
my $chr = 'chr';
my %h = ();
open my $fh_in, "< $gff" or die "Cannot open $gff, $!\n";
while(<$fh_in>) {
next if(/^\#|^\s*$/);
my $n = (split /\t/, $_)[0];
$h{$n} ++;
last if($. > 100);
}
close $fh_in;
### select the name, present in >80% lines
for my $i (keys %h) {
if($h{$i} > 80) {
$chr = $i;
last;
}
}
###
system "perl $find_feature -a $ref -n $chr $feature_dir";
my @fs = glob("$feature_dir/*.bed");
return($feature_dir, \@fs);
}
sub readBAMlist {
my $bamdir = $_[0];
my $outdir = $_[1];
my $bamlist = catfile($outdir, 'bam.list');
system "find $bamdir -name \"*.f.s.bam\" | sort > $bamlist";
my @bams = ();
open my $fh_in, "< $bamlist" or die "Cannot open $bamlist, $!";
while(<$fh_in>) {
chomp;
next if(/(^\s*$)|(^\#)/);
my $b = (split /\s+/)[0];
die("[$b] in line:$. of $bamlist, not found\n") if(! -e $b);
push @bams, abs_path($b);
}
close $fh_in;
return @bams;
}
sub count_feature {
my $file = $_[0];
my @bams = @{ $_[1] };
my $outdir = $_[2];
my $count_dir = catdir($outdir, '2.count');
my $stat_dir = catdir($outdir, 'output');
make_path($count_dir);
make_path($stat_dir);
my @stats = ();
for my $bam (@bams) {
my $stat_sum = featureCounts_count($bam, $file, $count_dir);
push @stats, $stat_sum;
}
my $stat_file = catfile($stat_dir, basename($file) . '.stat');
# open my $fh_stat, "> $stat_file" or die "Cannot open $stat_file, $!";
# print $fh_stat join("\n", @stats) . "\n";
# close $fh_stat;
return(@stats);
}
sub featureCounts_count {
my $bam = $_[0];
my $file = $_[1]; # gff
my $count_dir = $_[2];
###
my $bam_name = basename($bam);
$bam_name =~ s/(\.|\.s.|\.f.s.|\.trim\.gz\.f\.s\.)bam//;
my ($file_name) = basename($file) =~ /(.*)\.(\w+)$/; # gff
my $fcount = catfile($count_dir, $file_name . '_count.txt');
my $flog = catfile($count_dir, 'count.log');
my $para = '';
if($bam_name =~ /\_[12]$/) {
### PE: dUTP -s 2 (reverse)
$para = join(' ', '-M --fraction --donotsort -O -f -T 5 -g gene_id -t exon -p -P -d 40 -D 500 -s 2 -a', $file, '-o', $fcount);
}else {
$para = join(' ', '-M --fraction --donotsort -O -f -T 5 -g gene_id -t exon -s 1 -a', $file, '-o', $fcount);
}
my $run = join(' ', 'featureCounts', $para, $bam, ">> $flog 2>&1");
system"$run";
my $summary = $fcount . '.summary';
open my $fh_sum, "< $summary" or die "Cannot open $summary, $!\n";
my @nums = ();
while(<$fh_sum>) {
chomp;
my $num = (split /\s+/, $_)[1];
push @nums, $num;
}
close $fh_sum;
my $stat_sum = join("\t", $file_name, $bam_name, @nums);
return($stat_sum);
}
sub usage {
die("
Usage: stat_feature_mapping.pl [options]
Options: -b <STR> : the directory of BAM files
-f <STR> : reference file (*.fna),
need *.gff, *.ptt, *.rnt in the same dir
Example:
perl $0 -b alignment -f ref.fna out_dir
\n");
}
### END OF FILE ###