-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFrag.pl
executable file
·78 lines (55 loc) · 1.49 KB
/
getFrag.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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Cwd qw/abs_path/;
use File::Basename;
my ( $Help, $nFrag, $skip)
= ( 0 , 100, 0,);
GetOptions(
'help|?' => \$Help,
'n=i' => \$nFrag,
'skip=i' => \$skip,
);
die `pod2text $0` if $Help or @ARGV < 2;
my ($inbam, $ref) = @ARGV;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&showLog("START");
my $chr = ` head -1 $ref | sed 's/^>//' | awk '{print \$1}' `;
chomp $chr;
my $cnt = 0;
open my $IN, "samtools view $inbam |" or die $!;
while ( <$IN> ) {
next if $skip && $. <= $skip;
my ( $c, $p, $size ) = (split)[2,3,8];
next if $c ne $chr && $c ne "MT2";
if ( $size > 100 && $size < 1000 ) {
my $reg = "$chr:$p-".($p+$size-1);
my $str = `samtools faidx $ref $reg`;
$str =~ s/([ACGTNacgtn])\n/$1/g;
my $seq = (split /\n/, $str)[1];
my $qua = "I" x length($seq);
$cnt++;
print "\@$chr\_$p\_".($p+$size-1)."_0:0:0_0:0:0_$cnt/1\n";
print "$seq\n+\n$qua\n";
last if $cnt == $nFrag;
}
}
close $IN;
&showLog("END");
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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