-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFastaLengths.pl
91 lines (71 loc) · 1.31 KB
/
getFastaLengths.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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
my %opts;
getopts('f:s',\%opts);
&varcheck;
my $shortHeader = $opts{'s'} ? 1 : 0;
open (FA,$opts{'f'}) or die "Can't open $opts{'f'}\n";
my $line=<FA>;
chomp $line;
my $header;
if ($shortHeader == 1) {
$line =~ /^>(\S+)/;
$header = $1;
}
else {
$header = $line;
}
my $seqLen = 0;
while ($line = <FA>){
chomp $line;
if( eof(FA) || $line=~/>/){
#print "here\n";
if ($line =~ />/){
print "$header\t$seqLen\n";
#print "here\n";
if ($shortHeader == 1) {
$line =~ /^>(\S+)/;
$header = $1;
}
else {
$header = $line;
}
# print "new=$header";
}
else {
$seqLen += length($line);
print "$header\t$seqLen\n";
}
$seqLen = 0;
}
else {
#print "bf=$seqLen, sc=$header\n";
$seqLen += length($line);
#print "af=$seqLen\n";
}
}
sub varcheck {
my $errors = "";
if (!$opts{'f'}){
$errors .= "-f flag not provided\n";
}
elsif(!(-e $opts{'f'})) {
$errors .= "Can't open $opts{'f'}\n";
}
if ($errors ne "") {
print "\n$errors";
&usage;
}
}
sub usage{
my $scriptName = $0;
$scriptName =~ s/\/?.*\///;
print "\nusage: perl $scriptName <-f file> [-s]\n";
print <<PRINTTHIS;
Measures the length of each element of a fasta file.
-s option returns only first field of the header
PRINTTHIS
exit;
}