-
Notifications
You must be signed in to change notification settings - Fork 1
/
convertSam.pl
48 lines (42 loc) · 986 Bytes
/
convertSam.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
# Converts SAM file to fastq
use strict;
use warnings;
die "Need file name\n" if (scalar @ARGV < 2);
open(IN, $ARGV[0]) || die "Cannot open\n";
open(OUT, ">$ARGV[1]");
my $count = 0;
my $yes = 0;
my $no = 0;
while (my $line = <IN>) {
chomp $line;
next if (substr($line, 0, 1) eq "@");
my @spl = split("\t", $line);
if ($spl[1] & 0x800) {
next;
}
if ($spl[1] & 0x4) {
$no++;
} else {
$count++;
my $st = 0;
my $end = length $spl[9];
if ($spl[5] =~ m/^(\d+)S/) { # change S to [SI] to include insertions
$st = $1;
}
if ($spl[5] =~ m/(\d+)S$/) {
$end -= $1;
}
#if ($spl[5] =~ m/S/) {
# print "$spl[9]\n$spl[5]\n", substr($spl[9], $st, $end-$st), "\n";
# my $wait = <STDIN>;
#}
print OUT '@', "$spl[0] $spl[2] $spl[3] $spl[4]\n",
substr($spl[9], $st, $end-$st), "\n+\n",
substr($spl[10], $st, $end-$st), "\n";
}
}
close IN;
close OUT;
print "Reads printed: $count\n";
print "Not mapped: $no",
"\n";