-
Notifications
You must be signed in to change notification settings - Fork 0
/
featurecounts.pl
60 lines (43 loc) · 2.26 KB
/
featurecounts.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
#!/usr/bin/perl
use strict;
use warnings;
use File::Path 'make_path';
# Create relevant directories
my $name_sorted_bams_dir = "$ENV{HOME}/automation/name_sorted_bams";
my $counts_dir = "$ENV{HOME}/automation/counts";
make_path($name_sorted_bams_dir);
make_path($counts_dir);
print "Successfully created the following directories: $name_sorted_bams_dir and $counts_dir\n";
# Define the directory with the BAM files
my $bam_dir = "$ENV{HOME}/automation/coordinate_sorted_bams";
# Change to directories with the BAM files
chdir $bam_dir or die "Cannot change to directory $bam_dir: $!";
print "Successfully changed directory: $bam_dir\n";
# Create name-sorted bam file
# Getting all the files matching the pattern *Aligned.sortedByCoord.out.bam
my @files = glob("*Aligned.sortedByCoord.out.bam");
foreach my $file (@files) {
# Extracting the base name for the output file using substitution regular expression
(my $base = $file) =~ s/Aligned\.sortedByCoord\.out\.bam//; # substitute the pattern found in between / / with nothing
# Constructing the samtools command
my $samtools = "samtools sort -n -@ 12 -o $name_sorted_bams_dir/${base}_namesorted.bam $file";
# Execute the command
system($samtools) == 0
or die "Failed to execute $samtools: $!";
}
print "Successfully name sorted bam files\n";
# Change directory
chdir $name_sorted_bams_dir or die "Cannot change to directory $name_sorted_bams_dir: $!";
print "Successfully changed directory: $name_sorted_bams_dir\n";
# Construct featureCounts command for reverse-stranded libraries - note -a will need to be edited to the path of your reference genome
my $featurecounts = "featureCounts -T 12 -s 2 -p --countReadPairs -C -a /media/newdrive/data/Reference_genomes/Human/UCSC/hg38.ncbiRefSeq.gtf -o $counts_dir/featurecounts.txt *namesorted.bam 2> ~/FYP/counts/featurecounts.screen-output.log";
system($featurecounts) == 0
or die "Failed to execute $featurecounts: $!";
# Change directory
chdir $counts_dir or die "Cannot change to directory $counts_dir: $!";
print "Successfully changed directory: $counts_dir\n";
# Run MultiQC
my $multiqc = "multiqc $counts_dir -n featurecounts_multiqc";
system($multiqc) == 0
or die "Failed to execute MultiQC on $counts_dir: $!";
print "MultiQC report generated in $counts_dir\n";