-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfastq2wm64.pl
executable file
·291 lines (227 loc) · 6.96 KB
/
fastq2wm64.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/perl -w
# IMPORTANT: Set max read length $maxlength
# IMPORTANT: Set base for qual scores $qbase (33 or 64)
$maxlength=76;
# Specify quality scores used
$qbase = 64; # Base for transforming to ascii
$qmax = 40; # Max quality score
# Genome base composition
@Q = ( 0.25, 0.25, 0.25, 0.25 );
# Damage
# Set the probability of a C->T and G->A at any position in the
# 5'end of the read up to position $Ndamage.
# Cytosine to thymine misincorporation rates are highest (30.7%) at
# the first position of the sequences and decrease by approximately
# twofold per position as the read progresses (Fig. 4, bottom).
# This rate was reduced to 3.2% at the fifth nucleotide.
$Ndamage = 6;
@CT5 = (0.307,0.16,0.067,0.043,0.032,0.024);
@GA5 = (0.,0.,0.,0.,0.,0.);
# Similar for the 3' end of the read (starting with last base of read)
@CT3 = (0.,0.,0.,0.,0.,0.);
@GA3 = (0.307,0.16,0.067,0.043,0.032,0.024);
# Mutations
# If we have an overall mutation frequency A and assume that
# transversions and transitions have same prob,
# P(a|g)=A/3 for a different from g
$ptransition = 0.005/3;
$ptransversion = $ptransition;
# Alphabet
@alph = ( "A", "C", "G", "T");
# for ($i=0;$i<4;++$i) { $letternum{$alph[$i]} = $i; }
$logConstant = 1.0/log(2.0);
# Print 4x4 matrix
sub print_mat {
my $mat=$_[0];
for ($i=0; $i<4; ++$i) {
print "$alph[$i]";
for ($j=0; $j<4; ++$j) {
print " $mat->[$i][$j]";
}
print "\n";
}
}
# This is the prob P(a|g) for sample base a and genome g.
sub mutation_matrix {
# Initialize as transversions
for ($i=0; $i<4; ++$i) {
for ($j=0; $j<4; ++$j) { $mut[$i][$j] = $ptransversion; }
}
# Transitions
$mut[0][2]=$mut[2][0]=$mut[1][3]=$mut[3][1]=$ptransition;
# Diagonal
$mut[0][0]=$mut[1][1]=$mut[2][2]=$mut[3][3]
=1.-2.*$ptransversion-$ptransition;
return @mut;
}
# This is the product matrix P(b|g) = \sum_a P(b|a)P(a|g)
# for damaged b, sample a and genome g.
# For each position in the 5' and 3' end it differs
#
# For a given set of parameters, it returns a reference to a matrix
sub damage_matrix {
my $ct=$_[0];
my $ga=$_[1];
my @mut = @{$_[2]};
my @dam = ();
# First calculate the damage P(b|a) = $dam[$b][$a]
# Initialize as diagonal
for ($b=0; $b<4; ++$b) {
for ($a=0; $a<4; ++$a) { $dam[$b][$a] = 0; }
$dam[$b][$b] = 1.;
}
# C->T
$dam[3][1]=$ct; # P(T|C)
$dam[1][1]=1.-$ct; # P(C|C)
# G->A
$dam[0][2]=$ga; # P(A|G)
$dam[2][2]=1.-$ga; # P(G|G)
# print "Damage matrix\n";
# print_mat(\@dam);
# Product \sum_a P(b|a)P(a|g) where P(a|g) is $mut[$a][$g]
my $r = [([(0,0,0,0)],[(0,0,0,0)],[(0,0,0,0)],[(0,0,0,0)])];
for ($b=0; $b<4; ++$b) {
for ($g=0; $g<4; ++$g) {
for ($a=0; $a<4; ++$a) {
$r->[$b][$g] += $dam[$b][$a]*$mut[$a][$g];
}
}
}
# print "Product matrix\n";
# print_mat($r);
return $r;
}
# Correction to qual score probability
# A(b,g) = P(b,g)/\sum_g' P(b,g')
# where P(b,g)=P(b|g)P(g)
sub correction_matrix {
my $r = $_[0]; # P(b|g)
my $q = $_[1]; # P(g)
my $A = [([(0,0,0,0)],[(0,0,0,0)],[(0,0,0,0)],[(0,0,0,0)])];
for ($b=0; $b<4; ++$b) {
$sum=0;
for ($g=0; $g<4; ++$g) { $sum += $r->[$b][$g] * $q->[$g]; }
for ($g=0; $g<4; ++$g) { $A->[$b][$g] = $r->[$b][$g] * $q->[$g]/$sum; }
}
# print "Correction matrix\n";
# print_mat($A);
return $A;
}
# The actual genomic probabilities for x - the called base and qual
# Returns the 4 base probs P(g|x)
sub genome_probs {
my $A = $_[0]; # correction matrix A(b,g)
my $p = $_[1]; # P(b|x) 4 x 1
my $r = [(0,0,0,0)];
for ($g=0; $g<4; ++$g) {
for ($b=0; $b<4; ++$b) { $r->[$g] += $p->[$b] * $A->[$b][$g]; }
}
return $r;
}
# For a given qual score (number) and called base (number),
# return the scores of the four bases using the correction matrix $A
sub adjusted_scores {
my $A = $_[0]; # correction matrix A(b,g)
my $base = $_[1]; # called base
my $qual = $_[2]; # quality score (interger between 0 and qmax)
my $q = $_[3]; # P(g)
my $e = 10.**(-0.1*$qual);
$e=0.75 if ($e>0.75);
my @p = ($e/3,$e/3,$e/3,$e/3);
$p[$base] = 1.-$e;
my $r = genome_probs($A,\@p);
# Calculate log-scores
for ($g=0; $g<4; ++$g) { $r->[$g] = $logConstant*log($r->[$g]/$q->[$g]); }
return $r;
}
# Make a huge hash table of matrix scores for all possible
# combinations of base and qual score
# This needs to be done separately for each position in the
# 5' and 3' end
# Hash keys:
# "CZ1" means base C quality Z, position 1 (5')
# "Gu-2" means base G quality u, position -2 (3')
# "A[" means base A quality [, at a position more than $Ndamage from both ends
# Position 0 is the first base of the read, ad position -0 (!) is the last
@MUT = mutation_matrix();
# First away from ends
# No damage, so R=MUT
$A = correction_matrix(\@MUT,\@Q);
for ($i=0; $i<=$qmax; ++$i) {
for ($x=0; $x<4; ++$x) {
$key = $alph[$x] . chr($qbase+$i);
$scoreHash{$key} = adjusted_scores($A,$x,$i,\@Q);
# for ($g=0; $g<4; ++$g) { print $scoreHash{$key}->[$g] ." "; }
# print "$key\n";
}
}
# Do the same for 5' positions
for ($k=0; $k<$Ndamage; ++$k) {
$R = damage_matrix($CT5[$k],$GA5[$k],\@MUT);
$A = correction_matrix($R,\@Q);
for ($i=0; $i<=$qmax; ++$i) {
for ($x=0; $x<4; ++$x) {
$key = $alph[$x] . chr($qbase+$i) . $k;
$scoreHash{$key} = adjusted_scores($A,$x,$i,\@Q);
# for ($g=0; $g<4; ++$g) { print $scoreHash{$key}->[$g] ." "; }
# print "$key\n";
}
}
}
# Do the same for 3' positions
for ($k=0; $k<$Ndamage; ++$k) {
$R = damage_matrix($CT3[$k],$GA3[$k],\@MUT);
$A = correction_matrix($R,\@Q);
for ($i=0; $i<=$qmax; ++$i) {
for ($x=0; $x<4; ++$x) {
$key = $alph[$x] . chr($qbase+$i) . "-" . $k;
$scoreHash{$key} = adjusted_scores($A,$x,$i,\@Q);
# for ($g=0; $g<4; ++$g) { print $scoreHash{$key}->[$g] ." "; }
# print "$key\n";
}
}
}
# Now read in fastq file from stdin and convert to PSSMs
while (<>) {
++$k;
$k=0 if (/^@/);
print if ($k<2); # Print ID and called sequence
if ($k==1) {
chop;
@seq = split(//,uc($_));
}
if ($k==3) {
print "&\n";
chop;
@qual = split(//,$_);
$l = @qual;
next if ($l<2*$Ndamage+2);
@oline = ("","","","");
for ($i=0;$i<$l;++$i) {
if ( $seq[$i] !~ /[ACGT]/) { $seq[$i]="A"; $qual[$i]=chr($qbase); }
$key = $seq[$i] . $qual[$i];
if ($i<$Ndamage) { $key .= $i; }
# Assume NO DAMAGE in full length reads
if ( $l<$maxlength ) {
$j = $l-$i-1;
if ($j<$Ndamage) { $key .= "-".$j; }
}
# print STDERR "$key $i $l\n";
$r = $scoreHash{$key};
for ($g=0; $g<4; ++$g) { $oline[$g] .= sprintf("%.2f ", $r->[$g]); }
}
for ($g=0; $g<4; ++$g) {
$oline[$g] =~s/ $/\n/;
print $oline[$g];
}
}
}
exit(0);
print "Mutation matrix P(a|g)\n";
print_mat(\@MUT);
$R = damage_matrix($CT5[0],$GA5[0],\@MUT);
print "product matrix P(b|g)\n";
print_mat($R);
$A = correction_matrix($R,\@Q);
print "Correction matrix A(b,g)\n";
print_mat($A);