-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmut_freq_bias.pl
213 lines (191 loc) · 5.57 KB
/
mut_freq_bias.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/perl
#read_mutfreq.pl
use warnings;
use strict;
use Getopt::Std;
my $version = "0.3.0";
my %opts = (n=>55);
getopts('n:', \%opts);
die (qq/
Usage: plot mutation frequencies along reads
read_mutfreq.pl [options] <in.sam>
Options: -n INT number of positions along read to examine
Note: This script only uses reads with bitwise flag 99 and 147
\n/) unless @ARGV;
#set up hashes to count mutations, keys are reference_alternate
my %forward = (refmatch=>0, AC=>0, AG=>0, AT=>0, GA=>0, GC=>0,
GT=>0, CA=>0, CG=>0, CT=>0, TA=>0, TG=>0, TC=>0);
foreach (keys %forward) {
my $setup;
for (my $i = 0; $i <= $opts{n}-1; $i++) {
$setup->[$i] = 0;
}
$forward{$_} = $setup;
}
my %reverse = (refmatch=>0, AC=>0, AG=>0, AT=>0, GA=>0, GC=>0,
GT=>0, CA=>0, CG=>0, CT=>0, TA=>0, TG=>0, TC=>0);
foreach (keys %reverse) {
my $setup;
for (my $i = 0; $i <= $opts{n}-1; $i++) {
$setup->[$i] = 0;
}
$reverse{$_} = $setup;
}
# starting processing SAM file
open(SAM,'<',$ARGV[0]);
my ($read, $ref, $type, $length, $elem);
my $process = 0; # for tracking progress
while (<SAM>) {
next if $_ =~ /^\@HD\s+|^\@PG\s+|^\@SQ\s+/i;
my @s = split /\t/, $_;
next unless ($s[1] == 99 || $s[1] == 147); # try adding 83 and 163 bitwise flags later
my @seq = split "", $s[9];
my @md = $1 =~ /(\^[A-Za-z]+|0[A-Za-z]|[A-Za-z]|\d+)/g if $_ =~ /MD:Z:([^\s+]+)\s+/i;
if ($s[1] == 147) { # reverse CIGAR, SEQ, MD:Z:
my @revcig = $s[5] =~ /(\d+[A-Za-z])/ig;
@revcig = @revcig[reverse(0 .. $#revcig)];
$s[5] = join "", @revcig;
@seq = @seq[reverse(0 .. $#seq)];
@md = @md[reverse(0 .. $#md)];
}
my $pos = 0;
my %insert;
# adjust for insertions
if ($s[5] =~ /I/) {
my @mis;
if ($s[5] =~ /(^\d+H\d+S|^\d+H|^\d+S)/) {
@mis = $' =~ /(\d+[A-Za-z])/g;
} else {
@mis = $s[5] =~ /(\d+[A-Za-z])/g;
}
my $adj = 0;
my @adj;
push @adj, $adj;
my $ipos;
while (my $k = shift @mis) {
if ($k =~ /(\d+)[^D]/) {
push @adj, $adj;
$adj = $adj + $1;
if ($k =~ /(\d+)I/) {
$ipos = (pop @adj);
$insert{$ipos} = $1;
}
}
}
}
#subtract soft-clipped positions from SEQ
if ($s[5] =~ /(\d+)S/) {
for (my $j = 0; $j < $1; $j++) {
shift @seq;
}
}
# read1 forward/read1 reverse (flipped) for 5-3' along frag
if ($s[1] == 99) {
foreach (@md) {
if ($_ =~ /(^0[A-Za-z]|^[A-Za-z])/) {
$pos += $insert{$pos} if $insert{$pos}; #adjust for insert
if ($seq[$pos] =~ /N/i) {
$pos++; last if $pos > $opts{n}-1;
next;
}
my $base = $1; $base =~ s/0//;
$type = $base . $seq[$pos];
${$forward{$type}}[$pos]++;
$pos++; last if $pos > $opts{n}-1
} elsif ($_ =~ /^(\d+)$/) {
next if $1 == 0;
my $start = $pos;
for (my $i = $start; $i < $start + $1; $i++) {
$pos += $insert{$pos} if $insert{$pos}; #adjust for insert
${$forward{refmatch}}[$pos]++;
$pos++; last if $pos > $opts{n}-1;
}
} elsif ($_ =~ /\^[A-Za-z]+/) {
next;
}
}
$process++;
print STDERR "$process reads analyzed...\n" if ($process % 5000 == 0);
} elsif ($s[1] == 147) { #read2 reverse/read2 forwards (flipped) for 3-5' along frag
foreach (@md) {
if ($_ =~ /(^0[A-Za-z]|^[A-Za-z])/) {
$pos += $insert{$pos} if $insert{$pos}; #adjust for insert
if ($seq[$pos] =~ /N/i) {
$pos++; last if $pos > $opts{n}-1;
next;
}
my $base = $1; $base =~ s/0//;
$type = $base . $seq[$pos];
${$reverse{$type}}[$pos]++;
$pos++; last if $pos > $opts{n}-1;
} elsif ($_ =~ /^(\d+)$/) {
next if $1 == 0;
my $start = $pos;
for (my $i = $start; $i < $start + $1; $i++) {
$pos += $insert{$pos} if $insert{$pos}; #adjust for insert
${$reverse{refmatch}}[$pos]++;
$pos++; last if $pos > $opts{n}-1;
}
} elsif ($_ =~ /\^[A-Za-z]+/) {
next;
}
}
$process++;
print STDERR "$process reads analyzed...\n" if ($process % 5000 == 0);
}
}
print STDERR "$process total reads analyzed...finished!\n";
close SAM;
# mutation type (and ref match) frequency at each site
for(my $i=0; $i <= $opts{n} - 1; $i++) {
my $total = 0;
foreach (keys %forward) {
$total += ${$forward{$_}}[$i] if (${$forward{$_}}[$i]);
}
foreach (keys %forward) {
${$forward{$_}}[$i] /= $total if (${$forward{$_}}[$i]);
}
}
for(my $i=0; $i <= $opts{n} - 1; $i++) {
my $total = 0;
foreach (keys %reverse) {
$total += ${$reverse{$_}}[$i] if (${$reverse{$_}}[$i]);
}
foreach (keys %reverse) {
${$reverse{$_}}[$i] /= $total if (${$reverse{$_}}[$i]);
}
}
#print results: 1 for read1-forward (5'-3') & 2 for read2-reverse (3'-5')
print "ref1\tAC1\tAG1\tAT1\tGA1\tGC1\tGT1\tCA1\tCG1\tCT1\tTA1\tTC1\tTG1\n";
for (my $i=0; $i <= $opts{n} - 1; $i++) {
print "${$forward{refmatch}}[$i]\t";
print "${$forward{AC}}[$i]\t";
print "${$forward{AG}}[$i]\t";
print "${$forward{AT}}[$i]\t";
print "${$forward{GA}}[$i]\t";
print "${$forward{GC}}[$i]\t";
print "${$forward{GT}}[$i]\t";
print "${$forward{CA}}[$i]\t";
print "${$forward{CG}}[$i]\t";
print "${$forward{CT}}[$i]\t";
print "${$forward{TA}}[$i]\t";
print "${$forward{TC}}[$i]\t";
print "${$forward{TG}}[$i]\n";
}
print "\n\n"; #space between forward and reverse
print "ref2\tAC2\tAG2\tAT2\tGA2\tGC2\tGT2\tCA2\tCG2\tCT2\tTA2\tTC2\tTG2\n";
for (my $i=0; $i <= $opts{n} - 1; $i++) {
print "${$reverse{refmatch}}[$i]\t";
print "${$reverse{AC}}[$i]\t";
print "${$reverse{AG}}[$i]\t";
print "${$reverse{AT}}[$i]\t";
print "${$reverse{GA}}[$i]\t";
print "${$reverse{GC}}[$i]\t";
print "${$reverse{GT}}[$i]\t";
print "${$reverse{CA}}[$i]\t";
print "${$reverse{CG}}[$i]\t";
print "${$reverse{CT}}[$i]\t";
print "${$reverse{TA}}[$i]\t";
print "${$reverse{TC}}[$i]\t";
print "${$reverse{TG}}[$i]\n";
}