-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpliceFisher_exon_pair.pl
166 lines (155 loc) · 7.73 KB
/
SpliceFisher_exon_pair.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
# Author: Jiwoong Kim ([email protected])
use strict;
use warnings;
local $SIG{__WARN__} = sub { die $_[0] };
use Bio::DB::Sam;
use Getopt::Long qw(:config no_ignore_case);
GetOptions(
'h' => \(my $help = ''),
'q=i' => \(my $minimumMappingQuality = 0),
's=s' => \(my $stranded = ''),
);
if($help || scalar(@ARGV) == 0) {
die <<EOF;
Usage: perl SpliceFisher_exon_pair.pl [options] exon_pair.txt group1.bam,[...] group2.bam,[...] > exon_pair.count.txt
Options: -h display this help message
-q INT minimum mapping quality [$minimumMappingQuality]
-s stranded, "f" or "r"
EOF
}
my ($exonPairFile, @bamFilesList) = @ARGV;
my @samListList = ();
foreach my $bamFiles (@bamFilesList) {
my @samList = map {Bio::DB::Sam->new(-bam => $_)} split(/,/, $bamFiles);
push(@samListList, \@samList);
}
my %chromosomeHash = ();
$chromosomeHash{$_} = 1 foreach(map {$_->seq_ids} map {@$_} @samListList);
my @columnList = ('chromosome', 'start1', 'end1', 'start2', 'end2', 'strand', 'pairType', 'gene');
push(@columnList, map {"count$_->[1]$_->[2]_$_->[0]"} getCombinationList([map {$_ + 1} 0 .. $#samListList], ['Head', 'Tail', 'Body'], [1, 2]));
print join("\t", @columnList), "\n";
open(my $reader, $exonPairFile);
while(my $line = <$reader>) {
chomp($line);
my @tokenList = split(/\t/, $line, -1);
my ($chromosome, $start1, $end1, $start2, $end2, $strand, $pairType, $gene) = @tokenList;
next unless($chromosomeHash{$chromosome});
my @readCountListListList = map {[getExonPairReadCountListList($chromosome, $start1, $end1, $start2, $end2, $strand, $pairType, @$_)]} @samListList;
print join("\t", $chromosome, $start1, $end1, $start2, $end2, $strand, $pairType, $gene, (map {join(',', @$_)} map {@$_} @readCountListListList)), "\n";
}
close($reader);
sub getExonPairReadCountListList {
my ($chromosome, $start1, $end1, $start2, $end2, $strand, $pairType, @samList) = @_;
if($pairType eq 'MXE') { # alternative exons
($start1, $end1, $start2, $end2) = ($start2, $end2, $start1, $end1) if($strand eq '-');
} elsif(($pairType eq 'A5SS' && $strand eq '+') || ($pairType eq 'A3SS' && $strand eq '-')) { # alternative ends
$start2 = $end1 + 1;
} elsif(($pairType eq 'A5SS' && $strand eq '-') || ($pairType eq 'A3SS' && $strand eq '+')) { # alternative starts
$end1 = $start2 - 1;
}
my @readCountListList = ();
foreach my $index (0 .. $#samList) {
$readCountListList[$_]->[$index] = 0 foreach(0 .. 3);
{
my %readCountHash = ();
foreach my $alignment ($samList[$index]->get_features_by_location(-seq_id => $chromosome, -start => $start1, -end => $end1)) {
next if($alignment->qual < $minimumMappingQuality);
next if($stranded ne '' && getReadStrand($alignment->flag) ne $strand);
my @junctionStartEndList = ($alignment->cigar_str =~ /[0-9]+N/) ? getJunctionStartEndList($alignment->start, $alignment->cigar_str) : ();
if($pairType eq 'MXE') { # alternative exons
$readCountListList[0]->[$index] += 1 if(grep {$_->[1] == $start1 - 1} @junctionStartEndList); # upstream junction
$readCountListList[2]->[$index] += 1 if(grep {$_->[0] == $end1 + 1} @junctionStartEndList); # downstream junction
} elsif(($pairType eq 'A5SS' && $strand eq '+') || ($pairType eq 'A3SS' && $strand eq '-')) { # alternative ends
$readCountListList[1]->[$index] += 1 if(grep {$_->[0] == $end1 + 1} @junctionStartEndList);
$readCountListList[2]->[$index] += 1 if(scalar(@junctionStartEndList) == 0 && $alignment->end > $end1);
} elsif(($pairType eq 'A5SS' && $strand eq '-') || ($pairType eq 'A3SS' && $strand eq '+')) { # alternative starts
$readCountListList[0]->[$index] += 1 if(grep {$_->[1] == $start1 - 1} @junctionStartEndList);
}
$readCountHash{$alignment->qname} += 1 unless(grep {$_->[0] <= $start1 && $end1 <= $_->[1]} @junctionStartEndList);
}
if($pairType eq 'MXE') { # alternative exons
$readCountListList[4]->[$index] = scalar(grep {$readCountHash{$_} > 0} keys %readCountHash);
} elsif(($pairType eq 'A5SS' && $strand eq '+') || ($pairType eq 'A3SS' && $strand eq '-')) { # alternative ends
$readCountListList[3]->[$index] = $readCountListList[1]->[$index];
$readCountListList[5]->[$index] = scalar(grep {$readCountHash{$_} > 0} keys %readCountHash);
} elsif(($pairType eq 'A5SS' && $strand eq '-') || ($pairType eq 'A3SS' && $strand eq '+')) { # alternative starts
$readCountListList[4]->[$index] = scalar(grep {$readCountHash{$_} > 0} keys %readCountHash);
}
}
{
my %readCountHash = ();
foreach my $alignment ($samList[$index]->get_features_by_location(-seq_id => $chromosome, -start => $start2, -end => $end2)) {
next if($alignment->qual < $minimumMappingQuality);
next if($stranded ne '' && getReadStrand($alignment->flag) ne $strand);
my @junctionStartEndList = ($alignment->cigar_str =~ /[0-9]+N/) ? getJunctionStartEndList($alignment->start, $alignment->cigar_str) : ();
if($pairType eq 'MXE') { # alternative exons
$readCountListList[1]->[$index] += 1 if(grep {$_->[1] == $start2 - 1} @junctionStartEndList); # upstream junction
$readCountListList[3]->[$index] += 1 if(grep {$_->[0] == $end2 + 1} @junctionStartEndList); # downstream junction
} elsif(($pairType eq 'A5SS' && $strand eq '+') || ($pairType eq 'A3SS' && $strand eq '-')) { # alternative ends
$readCountListList[0]->[$index] += 1 if(grep {$_->[0] == $end2 + 1} @junctionStartEndList);
} elsif(($pairType eq 'A5SS' && $strand eq '-') || ($pairType eq 'A3SS' && $strand eq '+')) { # alternative starts
$readCountListList[1]->[$index] += 1 if(grep {$_->[1] == $start2 - 1} @junctionStartEndList);
$readCountListList[2]->[$index] += 1 if(scalar(@junctionStartEndList) == 0 && $alignment->start < $start2);
}
$readCountHash{$alignment->qname} += 1 unless(grep {$_->[0] <= $start2 && $end2 <= $_->[1]} @junctionStartEndList);
}
if($pairType eq 'MXE') { # alternative exons
$readCountListList[5]->[$index] = scalar(grep {$readCountHash{$_} > 0} keys %readCountHash);
} elsif(($pairType eq 'A5SS' && $strand eq '+') || ($pairType eq 'A3SS' && $strand eq '-')) { # alternative ends
$readCountListList[4]->[$index] = scalar(grep {$readCountHash{$_} > 0} keys %readCountHash);
} elsif(($pairType eq 'A5SS' && $strand eq '-') || ($pairType eq 'A3SS' && $strand eq '+')) { # alternative starts
$readCountListList[3]->[$index] = $readCountListList[1]->[$index];
$readCountListList[5]->[$index] = scalar(grep {$readCountHash{$_} > 0} keys %readCountHash);
}
}
}
if($pairType eq 'MXE') { # alternative exons
@readCountListList[0, 1, 2, 3] = @readCountListList[2, 3, 0, 1] if($strand eq '-');
}
return @readCountListList;
}
sub getJunctionStartEndList {
my ($position, $cigar) = @_;
my @junctionStartEndList = ();
while($cigar =~ s/^([0-9]+)([MIDNSHP=X])//) {
my ($length, $operation) = ($1, $2);
$position += $length if($operation eq 'M');
$position += $length if($operation eq 'D');
if($operation eq 'N') {
push(@junctionStartEndList, [$position, $position + $length - 1]);
$position += $length;
}
}
return @junctionStartEndList;
}
sub getReadStrand {
my ($flag) = @_;
if($stranded eq 'f') {
if($flag & 1) {
return '+' if(grep {$_ == $flag} (99, 147));
return '-' if(grep {$_ == $flag} (83, 163));
} else {
return '+' if($flag == 0);
return '-' if($flag == 16);
}
}
if($stranded eq 'r') {
if($flag & 1) {
return '+' if(grep {$_ == $flag} (83, 163));
return '-' if(grep {$_ == $flag} (99, 147));
} else {
return '+' if($flag == 16);
return '-' if($flag == 0);
}
}
return '';
}
sub getCombinationList {
return if(scalar(@_) == 0);
return map {[$_]} @{$_[0]} if(scalar(@_) == 1);
my @combinationList = ();
foreach my $element (@{$_[0]}) {
push(@combinationList, map {[$element, @$_]} getCombinationList(@_[1 .. $#_]));
}
return @combinationList;
}