forked from gringer/bioinfscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastx-rlength.pl
executable file
·144 lines (136 loc) · 3.51 KB
/
fastx-rlength.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
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long qw(:config auto_help pass_through);
#use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error);
#use IO::File;
sub rc {
my ($seq) = @_;
$seq =~ tr/ACGTUYRSWMKDVHBXN-/TGCAARYSWKMHBDVXN-/;
# work on masked sequences as well
$seq =~ tr/acgtuyrswmkdvhbxn/tgcaaryswkmhbdvxn/;
return(scalar(reverse($seq)));
}
sub rev {
my ($seq) = @_;
return(scalar(reverse($seq)));
}
sub printStats {
my ($seq, $seqID, $trim, $kmerLength) = @_;
my $len = length($seq);
my $sseq = "";
if($seqID && (length($seq) > $trim) && (length($seq) > $kmerLength)){
my $countTotal = 0;
my $countMax = 0;
my $maxKmer = "";
my %rptPos = ();
my %allGapCounts = ();
my %minGaps = ();
my $revCount = 1;
my $rcCount = 1;
for(my $p = 0; ($p + $kmerLength) <= $len; $p++){
$sseq = substr($seq, $p, $kmerLength);
if(exists($rptPos{$sseq})){
my $gap = $p - $rptPos{$sseq};
$allGapCounts{$gap}++;
if(!exists($minGaps{$sseq}) || ($minGaps{$sseq} > $gap)){
$minGaps{$sseq} = $gap;
}
}
if(exists($rptPos{rev($sseq)})){
$revCount++;
}
if(exists($rptPos{rc($sseq)})){
$rcCount++;
}
$rptPos{$sseq} = $p;
}
if($revCount == 1){
$revCount = 0;
}
if($rcCount == 1){
$rcCount = 0;
}
my $numKmers = scalar(keys(%rptPos));
my $kmerRatio = $numKmers/($len - $kmerLength + 1);
my $numRepeats = scalar(keys(%minGaps));
my @gaps = sort {$a <=> $b} (values(%minGaps));
my $medianGap = (@gaps) ? $gaps[$#gaps / 2] : 0;
my $medianCount = 0;
my $modalGap = 0;
my $modalCount = 0;
my $rangeCountMed = 0;
my $rangeCountMod = 0;
if($medianGap){
my %gapCounts = ();
foreach my $gap (@gaps){
$gapCounts{$gap}++;
}
$medianCount = ${allGapCounts{$medianGap}};
my @modalSort = sort {$allGapCounts{$b} <=> $allGapCounts{$a}} (@gaps);
$modalGap = $modalSort[0];
$modalCount = $allGapCounts{$modalGap};
for(my $gP = int($medianGap * 0.99); ($gP <= ($medianGap / 0.99));
$gP++){
$rangeCountMed += $allGapCounts{$gP} if($allGapCounts{$gP});
}
for(my $gP = int($modalGap * 0.99); ($gP <= ($modalGap / 0.99));
$gP++){
$rangeCountMod += $allGapCounts{$gP} if($allGapCounts{$gP});
}
}
printf("%8d %0.3f %6d %6d %5d %5d %6d %5d %5d %6d %6d %6d %s\n",
$len, $kmerRatio,
$numRepeats,
$countTotal,
$medianCount,
$rangeCountMed,
$medianGap,
$modalCount,
$rangeCountMod,
$modalGap,
$revCount,
$rcCount,
$seqID);
}
}
my $trim = 0;
my $kmerLength = 17; ## number of bases in hash keys
GetOptions("trim=s" => \$trim) or
die("Error in command line arguments");
my $inQual = 0; # false
my $seqID = "";
my $qualID = "";
my $seq = "";
my $qual = "";
my $buffer = "";
printf("%8s %5s %6s %6s %5s %5s %6s %5s %5s %6s %6s %6s %s\n",
"length", "kRat", "cntRep", "cntTot",
"medCt", "RCMed", "medGap", "modCt", "RCMod",
"modGap", "rvCnt", "rcCnt", "SeqID");
while(<>){
chomp;
chomp;
if(!$inQual){
if(/^(>|@)((.+?)( .*?\s*)?)$/){
my $newSeqID = $2;
my $newShortID = $3;
printStats($seq, $seqID, $trim, $kmerLength);
$seq = "";
$qual = "";
$buffer = "";
$seqID = $newSeqID;
} elsif(/^\+(.*)$/) {
$inQual = 1; # true
$qualID = $1;
} else {
$seq .= $_;
}
} else {
$qual .= $_;
if(length($qual) >= length($seq)){
$inQual = 0; # false
}
}
}
printStats($seq, $seqID, $trim, $kmerLength);