-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaligner_test.pl
executable file
·333 lines (278 loc) · 8.69 KB
/
aligner_test.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use English;
use Getopt::Long;
use Data::Dumper;
use Parallel::ForkManager;
my $alignment = "";
my $reference = "";
my $output_dir = "aligner_test_out";
my $threads = 1;
my $commands = "";
my $maxseqs = 0;
my $options_okay = GetOptions(
'alignment=s' => \$alignment,
'reference=s' => \$reference,
'commands=s' => \$commands,
'output=s' => \$output_dir,
'threads=i' => \$threads,
'maxseqs=i' => \$maxseqs,
);
croak
"Please specify a file containing a gapped alignment in FASTA format with -a"
if $alignment eq "";
croak
"Please specify the name of the reference sequence in the alignment with -r"
if $reference eq "";
croak "Please specify a file containing aligner commands with -c"
if $commands eq "";
my $seqs = load_alignment($alignment);
croak "Reference must be present in alignment"
if !defined $seqs->{$reference};
my $aligners = load_commands($commands);
-d $output_dir or mkdir $output_dir;
prepare_reference( $reference, $output_dir, $seqs, $aligners );
my $pm = new Parallel::ForkManager($threads);
$pm->set_max_procs($threads);
my $seqsdone = 0;
for my $seqname ( keys %{$seqs} ) {
next if $seqname eq $reference;
last if $maxseqs and $seqsdone == $maxseqs;
my ( $testbp, $refbp, $refpos, $stats ) =
get_aligned_bases( $seqname, $reference, $seqs );
next if !defined $testbp;
for my $readlen (50) {
$pm->start and next;
generate_read_alignment( $testbp, $refbp, $refpos, $seqname, $readlen,
$seqs, $reference, $output_dir );
my $refpath = "$output_dir/$reference";
my $fastqpath = "$output_dir/$seqname.$readlen";
for my $aligner ( sort keys %{$aligners} ) {
run_aligner( $aligners->{$aligner}, $refpath, $fastqpath );
my $result = compare_alignment( $fastqpath, $aligner );
print
"$stats->{bases}\t$stats->{matches}\t$stats->{ns}\t$stats->{snps}\t$stats->{ins}\t$stats->{del}\t$readlen\t$aligner\t$result\t$seqname\n";
}
$pm->finish;
}
$seqsdone++;
}
$pm->wait_all_children;
sub compare_alignment {
my ( $fastqpath, $aligner ) = @_;
open my $result, "<", "$fastqpath.$aligner.sam"
or croak "Can't open results file $fastqpath.$aligner.sam!\n";
my $matchpos = 0;
my $matchcigar = 0;
my $reads = 0;
while ( my $samline = <$result> ) {
chomp $samline;
next if $samline =~ /^@/;
$reads++;
my ( $readname, $flag, $ref, $refpos, $mq, $cigar, @other ) =
split /\t/, $samline;
my ( $true_readpos, $snps, $snppos, $true_cigar ) = split /_/,
$readname;
$matchpos++ if ( $true_readpos eq $refpos );
$matchcigar++ if ( $true_cigar eq $cigar );
}
close $result;
return "$reads\t$matchpos\t$matchcigar";
}
sub run_aligner {
my ( $aligner, $refpath, $fastqpath ) = @_;
for my $cmd ( @{ $aligner->{align}{cmd} } ) {
$cmd =~ s/REFERENCE/$refpath/g;
$cmd =~ s/FASTQ/$fastqpath/g;
system($cmd);
}
return;
}
sub generate_read_alignment {
my ( $testbp, $refbp, $refpos, $seqname, $readlen,
$seqs, $reference, $output_dir )
= @_;
open my $sam, ">", "$output_dir/$seqname.$readlen.sam"
or croak "Can't open SAM file for $seqname, $readlen bp reads\n";
open my $fq, ">", "$output_dir/$seqname.$readlen.fq"
or croak "Can't open read 1 FASTQ file for $seqname, $readlen bp reads\n";
for my $i ( 1 .. $#{$refbp} - $readlen + 1 ) {
my @testread;
my @refread;
my $cigar = "";
my $start;
my @snps;
my $ins = 0;
my $del = 0;
my $mat = 0;
my $write_ins = 0;
my $write_del = 0;
my $write_mat = 0;
my $j = $i - 1;
while ( @testread < $readlen and $j <= $#{$refbp} ) {
if ( $testbp->[$j] eq '-' ) {
$write_ins++ if $ins > 0;
$write_mat++ if $mat > 0;
$del++;
}
elsif ( $refbp->[$j] eq '-' ) {
$write_del++ if $del > 0;
$write_mat++ if $mat > 0;
$ins++;
}
else {
$write_ins++ if $ins > 0;
$write_del++ if $del > 0;
$mat++;
push @snps, $refpos->[$j]
if ( $testbp->[$j] ne $refbp->[$j]
and $testbp->[$j] ne 'N'
and $refbp->[$j] ne 'N' );
}
if ($write_ins) {
$cigar .= $ins . "I";
$write_ins = $ins = 0;
}
elsif ($write_del) {
$cigar .= $del . "D";
$write_del = $del = 0;
}
elsif ($write_mat) {
$cigar .= $mat . "M";
$write_mat = $mat = 0;
}
if ( $del == 0 ) {
$start = $refpos->[$j] if not defined $start;
push @testread, $testbp->[$j];
push @refread, $refbp->[$j];
}
$j++;
}
$cigar .= $ins . "I" if $ins > 0;
$cigar .= $del . "D" if $del > 0;
$cigar .= $mat . "M" if $mat > 0;
my $testreadseq = join( '', @testread );
# next if $testreadseq =~ /N/;
next if length($testreadseq) < $readlen;
my $snpstr = join ',', @snps;
my $snpnum = @snps;
print $sam "$start\t"
. join( '', @testread ) . "\t"
. join( '', @refread )
. "\t$snpnum\t$snpstr\t$cigar\n";
print $fq '@'
. "$start\_$snpnum\_$snpstr\_$cigar\n$testreadseq\n+\n"
. 'I' x $readlen . "\n";
}
close $fq;
close $sam;
}
sub get_aligned_bases {
my ( $seqname, $reference, $seqs ) = @_;
my @testbases;
my @refbases;
my @refpos;
my %stats;
$stats{bases} = 0;
$stats{ins} = 0;
$stats{del} = 0;
$stats{ns} = 0;
$stats{matches} = 0;
my $testseq = $seqs->{$seqname};
my $refseq = $seqs->{$reference};
print STDERR
"$seqname alignment is not the same length as reference alignment!\n"
and return
if length($testseq) != length($refseq);
my $refi = 0;
for my $i ( 0 .. length($refseq) ) {
my $ti = substr $testseq, $i, 1;
my $ri = substr $refseq, $i, 1;
next if ( $ti eq '-' and $ri eq '-' );
$stats{bases}++;
push @testbases, $ti;
push @refbases, $ri;
if ( $ri eq '-' ) {
push @refpos, '-';
$stats{ins}++;
}
else {
$refi++;
push @refpos, $refi;
if ( $ti eq '-' ) {
$stats{del}++;
}
elsif ( $ti eq 'N' or $ri eq 'N' ) {
$stats{ns}++;
}
elsif ( $ti ne $ri ) {
$stats{snps}++;
}
else {
$stats{matches}++;
}
}
}
( \@testbases, \@refbases, \@refpos, \%stats );
}
sub load_alignment {
my $alignment = shift;
my %seqs;
open my $fa, '<', $alignment
or croak "Can't open alignment file! $OS_ERROR\n";
my $seqname = "";
my $seqbases = "";
while ( my $faline = <$fa> ) {
chomp $faline;
if ( $faline =~ /^>([^\s]+)/ ) {
$seqs{$seqname} = $seqbases if $seqname ne "";
$seqname = $1;
$seqbases = "";
}
else {
$seqbases .= $faline;
}
}
$seqs{$seqname} = $seqbases;
close $fa;
\%seqs;
}
sub load_commands {
my $commands = shift;
open my $cmdfile, '<', $commands
or croak "Can't open commands file $commands! $OS_ERROR\n";
my $aligner = "";
my $stage = "";
my %aligners;
while ( my $cmdline = <$cmdfile> ) {
chomp $cmdline;
if ( $cmdline =~ /^#(.+)(\s+)(.+)$/ ) {
$aligner = $1;
$stage = $3;
}
else {
push @{ $aligners{$aligner}{$stage}{cmd} }, $cmdline;
}
}
close $cmdfile;
\%aligners;
}
sub prepare_reference {
my ( $reference, $output_dir, $seqs, $aligners ) = @_;
my $refpath = "$output_dir/$reference";
open my $refout, ">", "$refpath.fa"
or croak "Can't open output reference FASTA file!\n";
my $refoutseq = $seqs->{$reference};
$refoutseq =~ s/-//g;
print $refout ">$reference\n$refoutseq\n";
close $refout;
for my $aligner ( keys %{$aligners} ) {
for my $cmd ( @{ $aligners->{$aligner}{index}{cmd} } ) {
$cmd =~ s/REFERENCE/$refpath/g;
print "$aligner:$cmd\n";
system($cmd);
}
}
}