-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path6_Get_Expression_featureCounts.pl
98 lines (90 loc) · 2.23 KB
/
6_Get_Expression_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
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
use strict;
use warnings;
if (@ARGV != 2) {die "Please provide directory of featurecounts output and an output file name\n";}
my $dir = $ARGV[0];
my $outfile = $ARGV[1];
open(my $ofh, ">", $outfile) or die $!;
my %Gene2ID2FragCount = ();
my %Gene2Length = ();
my @IDs = ();
foreach my $file (glob("$dir/*.fragmentcounts")) {
my $ID = "ERR";
# if ($file =~ /([ATCG]{5,})A/) {
# if ($file =~ /([^\/]*)_1/) {
# if ($file =~ /(Plate\d_Cell\d+)/) {
# if ($file =~ /(_1_\d+)/) {
# if ($file =~ /(E\w+)\.sort/) {
# if ($file =~ /_([^_]+_Cell\d\d)/) {
if ($file =~ /([^\/]*?)\./) {
$ID = $1;
} else {
die "$file does not match\n";
}
push(@IDs,$ID);
my $fail = 0;
open(my $ifh, $file) or die $!;
while (<$ifh>) {
chomp;
if ($_ =~ /^#/ || $_ =~ /^Geneid/) {next;} #skip header & comments
my @record=split(/\t/);
my $gene = $record[0]; $gene =~ s/\s+//g;
if (!defined($record[6])) {
$fail = 1; last;
}
$Gene2ID2FragCount{$gene}->{$ID} = $record[6];
$Gene2Length{$gene} = $record[5];
} close ($ifh);
if ($fail) {
print STDERR "Fail: $file\n";
#system("rm $file");
pop(@IDs);
}
}
print $ofh "Length\t".join("\t",@IDs)."\n";
foreach my $gene (keys(%Gene2ID2FragCount)) {
print $ofh "$gene";
print $ofh "\t".$Gene2Length{$gene};
foreach my $ID (@IDs) {
my $count = "NA";
if (exists($Gene2ID2FragCount{$gene}->{$ID})) {
$count = $Gene2ID2FragCount{$gene}->{$ID};
} else {
$count = "0";
}
print $ofh "\t".$count;
}
print $ofh "\n";
}
my %ID2Unassigned = ();
foreach my $file (glob("$dir/*.fragmentcounts.summary")) {
my $ID = "ERR";
#if ($file =~ /_([^_]+_Cell\d\d)/) {
#if ($file =~ /([ATCG]{5,})A/) {
#if ($file =~ /([^\/]*)_1/) {
#if ($file =~ /(Plate\d_Cell\d+)/) {
#if ($file =~ /(E\w+)\.sort/) {
#if ($file =~ /(_1_\d+)/) {
if ($file =~ /([^\/]*?)\./) {
$ID = $1;
} else {
die "$file does not match\n";
}
my $fail = 0;
open(my $ifh, $file) or die $!;
<$ifh>; # header
<$ifh>; #Assigned
while (<$ifh>) {
chomp;
my @record=split(/\t/);
if (!defined($record[1])) {$fail = 1;last;}
$ID2Unassigned{$ID} += $record[1];
} close ($ifh);
if ($fail) {
system("rm $file");
}
}
print $ofh "__Unassigned_Various\t1";
foreach my $ID (@IDs) {
print $ofh "\t".$ID2Unassigned{$ID};
}
print $ofh "\n";