-
Notifications
You must be signed in to change notification settings - Fork 10
/
sam2fastq.pl
161 lines (99 loc) · 3.3 KB
/
sam2fastq.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
#!/usr/bin/perl -w
#
# sam2fastq.pl -- Convert sam format to fastq format.
#
# Author: Nowind
# Created: 2012-02-21
# Updated: 2016-03-02
# Version: 1.1.0
#
# Change logs:
# Version 1.0.0 14/01/21: The initial version.
# Version 1.1.0 16/03/02: Updated: remove duplicated records with same id.
use strict;
use Data::Dumper;
use Getopt::Long;
use MyPerl::FileIO qw(:all);
##################### Main ####################
my $CMDLINE = "perl $0 @ARGV";
my $VERSION = '1.1.0';
my $HEADER = "##$CMDLINE\n##Version: $VERSION\n";
my $SOURCE = (scalar localtime()) . " Version: $VERSION";
my ($input, $output, $no_rc, $use_rg_id);
GetOptions(
"I|input=s" => \$input,
"output=s" => \$output,
"no-rc" => \$no_rc,
"use-rg" => \$use_rg_id,
);
my $show_help = ($CMDLINE =~ /\-help/) ? 0 : 1;
unless( $input && $show_help ) {
print <<EOF;
$0 -- Extract all reads with the
Version: $VERSION
Usage: perl $0 [options]
Options:
-i, --input <filename>
input file of query positions, required
-o, --output <filename>
output prefix, required
-n, --no-rc
do not reverse complement sequence with negtive strand
-u, --use-rg
add read group id to extracted records
EOF
exit(1);
}
$|++;
print STDERR "# $0 v$VERSION\n# " . (scalar localtime()) . "\n";
print STDERR ">> Start converting $input ... ";
sam2fastq($input, $output);
print STDERR "done!\n";
print STDERR "# " . (scalar localtime()) . "\n";
######################### Sub #########################
=head2 sam2fastq
About : Convert sam file to fastq files.
Usage : sam2fastq($sam_file, $out_prefix);
Args : File in SAM format;
Prefix of output files.
Returns : Null
=cut
sub sam2fastq
{
my ($in, $out_prefix) = @_;
my %read_ids = ();
open (FQ1, "> $out_prefix" . "_1.fq") || die $!;
open (FQ2, "> $out_prefix" . "_2.fq") || die $!;
my $fh = getInputFilehandle($input);
while (<$fh>)
{
next if (/^@/ || /^\s+$/); ## skip header
chomp(my $record = $_);
my ($QNAME, $FLAG, $RNAME, $POS, $MAPQ, $CIGAR,
$MRNM, $NPOS, $TLEN, $SEQ, $QUAL, @OPT) = (split /\s+/, $record);
my $rg_id = ($record =~ /RG:Z:(.*?)\s+/);
$QNAME =~ s/\/\d$//;
my $read_id = $use_rg_id ? "$rg_id:$QNAME" : $QNAME;
## re-reverse bases and qualities of reads with negative strand
## flag set
if (($FLAG & 16) && !$no_rc) {
$SEQ =~ tr/ATGCatgc/TACGtacg/;
$SEQ = reverse $SEQ;
$QUAL = reverse $QUAL;
}
my $pair_id = 0;
if ($FLAG & 64) { ## first in pair
$pair_id = 1;
next if (exists($read_ids{"$read_id:$pair_id"}));
print FQ1 "\@$read_id/1\n$SEQ\n\+\n$QUAL\n";
}
elsif ($FLAG & 128) { ## second in pair
$pair_id = 2;
next if (exists($read_ids{"$read_id:$pair_id"}));
print FQ2 "\@$read_id/2\n$SEQ\n\+\n$QUAL\n";
}
$read_ids{"$read_id:$pair_id"}++;
}
close FQ1;
close FQ2;
}