-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamRdf2fq.pl
executable file
·137 lines (107 loc) · 3.26 KB
/
bamRdf2fq.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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Cwd qw/abs_path/;
use File::Basename;
my ( $Help, $minSizeOfRdf, )
= ( 0, 12, );
GetOptions(
'help|?' => \$Help,
'minSizeOfRdf=i' => \$minSizeOfRdf,
);
die `pod2text $0` if $Help or @ARGV < 3;
my ($inbam, $outpre, $chrRange ) = @ARGV;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&showLog("START");
my ($chr, $start, $end) = split /[:-]/, $chrRange;
die "please set start <= end for chromosome range" if $start > $end;
open my $IN, "<$inbam.rdf" or die $!;
open my $BAM, "samtools view $inbam |" or die $!;
open my $FQ1, "| gzip > $outpre\_1.fq.gz" or die $!;
open my $FQ2, "| gzip > $outpre\_2.fq.gz" or die $!;
my $fix = "TGNCT";
while ( <$IN> ) {
my ($c, $s, $e, $n) = split;
if ( $c lt $chr ) {
map { my $a = <$BAM>; } 1 .. $n;
next;
}
if ($c gt $chr || ( $c eq $chr && $s > $end) ) {
last;
}
if ( $n >= $minSizeOfRdf ) {
if ( $e < $start || $s > $end ) { # not overlap
map { my $a = <$BAM>; } 1 .. $n;
}
else {
for (1 .. $n/2) {
my (%a, %b);
my @t = qw/id flag seq qua/;
@a{@t} = (split /\t/, <$BAM>, 12)[0,1,9,10];
@b{@t} = (split /\t/, <$BAM>, 12)[0,1,9,10];
if ( $a{flag} & 0x10 ) {
$a{seq} = reverse_complement( $a{seq} );
$a{qua} = reverse $a{qua};
}
if ( $b{flag} & 0x10 ) {
$b{seq} = reverse_complement( $b{seq} );
$b{qua} = reverse $b{qua};
}
my $tag = (split /\|/, $a{id})[-1];
my $tag_a = substr($tag, 0, 12);
my $tag_b = substr($tag, 12 );
if ( $a{flag} & 0x40 ) {
$a{id} =~ s/\|$tag$/\/1/;
$b{id} =~ s/\|$tag$/\/2/;
print $FQ1 "\@$a{id}\n$tag_a$fix$a{seq}\n+\n".("B" x 17)."$a{qua}\n";
print $FQ2 "\@$b{id}\n$tag_b$fix$b{seq}\n+\n".("B" x 17)."$b{qua}\n";
}
else {
$b{id} =~ s/\|$tag$/\/1/;
$a{id} =~ s/\|$tag$/\/2/;
print $FQ1 "\@$b{id}\n$tag_a$fix$b{seq}\n+\n".("B" x 17)."$b{qua}\n";
print $FQ2 "\@$a{id}\n$tag_b$fix$a{seq}\n+\n".("B" x 17)."$a{qua}\n";
}
}
}
}
else {
map { my $a = <$BAM>; } 1 .. $n;
}
}
close $IN;
close $BAM;
close $FQ1;
close $FQ2;
&showLog("END");
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sub reverse_complement {
my @a = split //, $_[0];
my $re;
while ( my $nt = pop @a ) {
$re .= (
$nt eq "A" ? "T"
: $nt eq "T" ? "A"
: $nt eq "G" ? "C"
: $nt eq "C" ? "G"
: $nt
);
}
return $re;
}
sub showLog {
my @t = localtime();
printf STDERR "[%04d-%02d-%02d %02d:%02d:%02d]\t%s\n", $t[5] + 1900, $t[4] + 1, @t[3,2,1,0], $_[0];
}
__END__
=head1 Function
=head1 Usage
perl $0
=head1 Options
-h help
=head1 Author
yerui; [email protected]
=head1 Version
v1.0; 2012-
=cut