-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlean_fq.pl
executable file
·68 lines (52 loc) · 1.1 KB
/
lean_fq.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
#!/usr/bin/perl
use warnings; use strict;
use Getopt::Std;
my $usage = "usage: $0
-1 : read1
-2 : reads
-p : prefix of out put reads
-i : reference index file
-c : cpu number for bwa
-h : print this help
BWA should in your PATH
";
die $usage if (@ARGV ==0);
my %opt;
getopts("1:2:p:i:c:h",\%opt);
my($r1,$r2,$pre,$index,$cpu) = ($opt{1},$opt{2},$opt{p},$opt{i},$opt{c});
my $bwa = "bwa";
open R1,">$pre.fq1" or die $!;
open R2, ">$pre.fq2" or die $!;
open ALN, "$bwa mem -MT 20 -t $cpu $index $r1 $r2 2>/dev/null | samtools view -XS - |" or die $!;
my %reads;
my $last ;
while (<ALN>){
chomp;
next if (/^@/);
my($id,$flag,$seq,$q) = (split /\t/)[0,1,9,10];
if($last and $id ne $last){
prt($last);
}
if(eof(ALN)){
prt($id);
}
next if ($flag =~ /uU/);
next if ($flag =~ /s/);
my $r = $flag =~ /1/? 1:2;
if($flag =~ /r/){
$seq = reverse($seq);
$seq =~ tr/atcgATCG/tagcTAGC/;
}
$reads{$id}{$r} =">$id\n$seq\n";
$last = $id;
}
sub prt{
my $id = shift @_;
my $r1 = $reads{$id}{1};
my $r2 = $reads{$id}{2};
if($r1 and $r2){
print R1 $r1;
print R2 $r2;
}
undef $reads{$id};
}