-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseqlen.pl
executable file
·158 lines (138 loc) · 2.88 KB
/
seqlen.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
#!/usr/bin/perl -w
# Script: seqlen.pl
# Description: Extracts sequence lengths from a fasta file
# Author: Steven Ahrendt
# email: sahrendt0@gmail.com
# Date: 6.17.11
# v 1.0: Can probably make more general/robust
# Make a simple histogram
# Open data in oocalc
# v 1.5: Updated w/ bioperl
# No histogram
# No oocalc
# works for any fastafile
# v 1.6: added stats
# v 1.7: correct "div-by-0" error
# v 1.8: added -v option: print to screen + file (default prints only to file)
# v 2.0: changed -v option: default is screen, -v is "print to file"
#####################################################
# Usage: seqlen.pl -i fastafile [-v]
#####################################################
use strict;
use warnings;
use Bio::SeqIO;
use List::Util qw[min max];
use Getopt::Long;
## Variables
my ($verb,$help);
my $numseqs = 0;
my (@tmp,@len,@lengths);
my $infile;
GetOptions("i|input=s" => \$infile,
"v|verbose" => \$verb,
"h|help" => \$help);
my $usage = "Usage: seqlen.pl -i inputfile [-v]\n";
die $usage if ($help);
die "No input.\n$usage" if (!$infile);
## Input processing
#print `perl -pi -e 's/\r/\n/g' $infile`;
my $fastafile = Bio::SeqIO->new(-file=>"<$infile",
-format=>"fasta");
my $csvfile = join(".",infilename($infile),"csv");
if($verb){open(OUT,">$csvfile");}
while(my $seq = $fastafile->next_seq)
{
push(@lengths,$seq->length);
if($verb){print OUT $seq->display_id,",",$seq->length,"\n";}
#print $seq->display_id,",",$seq->length,"\n";
}
if($verb){close(OUT);}
$numseqs = @lengths;
print "$numseqs sequence(s)\n";
print "Min:\t",min(@lengths),"\n";
print "Max:\t",max(@lengths),"\n";
printf("Mean:\t%.2f\n",mean(@lengths));
if($numseqs == 1)
{
printf("Stdv:\tNA\n");
}
else
{
printf("Stdv:\t%.2f\n",stddev(@lengths));
}
if($verb)
{
print "-----\n";
print "Lengths are in \"$csvfile\"\n";
print "-----\n";
}
#########
## Subroutines
#########
sub stddev {
my @a = @_;
my $n = @a;
my $c = (sum(@a) ** 2)/$n;
my $num = sumsqr(@a) - $c;
#print "\n";
#print sumsqr(@a);
#print " - ";
#print "(",sum(@a),"^2)/($n)\n";
#print "-----------------------\n";
#print $n,"-1\n";
return ($num/($n-1)) ** 0.5;
}
sub mean {
my @a = @_;
my $n = @a;
return (sum(@a)/$n);
}
sub sumsqr {
my @a = @_;
my $ret = 0;
foreach my $i (@a)
{
$ret += ($i**2);
}
return $ret;
}
sub sum {
my @a = @_;
my $ret = 0;
foreach my $i (@a)
{
$ret += $i;
}
return $ret;
}
sub round {
my($number) = shift;
return int($number + .5);
}
sub infilename {
my $file = shift;
return substr($file,0,index($file,"."));
}
sub histogram{
my @data = @_;
my $size = @data;
my $bins = 10;
my $min = 0;
my $max = max(@data);
print "$max\n";
if(($max%10) != 0)
{
$max /= 10;
$max = round($max);
$max *= 10;
}
my $c = $min;
print "|";
while($c<$max)
{
printf(" %3d |",$c);
$c += ($max/10);
}
print " $max |";
print "\n";
}