amount.pl:
#!/usr/bin/perl
################################################################################
# use for calculate the amount of w or not
#
#
#
################################################################################
use warnings;
use strict;
use Getopt::Long;
sub usage{
print STDOUT "use for calculate the amount of w or not\n";
print STDOUT "perl $0 -a prof -w character -n -c -r -cn -rn -h\n";
print STDOUT "\t-a str file to calculate, required\n";
print STDOUT "\t-w str character to search, required\n";
print STDOUT "\t-n calculate amount of not w\n";
print STDOUT "\t-c calculate by column\n";
print STDOUT "\t-r calculate by row\n";
print STDOUT "\t-cn the file contain colnames\n";
print STDOUT "\t-rn the file contain rownames\n";
print STDOUT "\t-h help info\n";
exit(1);
}
our ($opt_a, $opt_w, $opt_n, $opt_c, $opt_r, $opt_cn, $opt_rn, $opt_h);
my (@total_c, @total_r, @seq);
my ($i, $j, $cn);
GetOptions("a:s" => \$opt_a, # 指定文本
"w:s" => \$opt_w, # 指定字符串
"n" => \$opt_n, # 统计不是指定字符串的个数
"c" => \$opt_c, # 按列统计
"r" => \$opt_r, # 按行统计
"cn" => \$opt_cn, # 指定文本有列名
"rn" => \$opt_rn, # 指定文本有行名
"h" => \$opt_h); # 帮助
&usage unless(defined $opt_a && defined $opt_w);
&usage unless($opt_c || $opt_r);
&usage if($opt_h);
if (defined $opt_c) {
open I, $opt_a or die "can not open $opt_a";
$cn = <I> if (defined $opt_cn);
$opt_rn = 0 if (!defined $opt_rn);
$j = 0;
while (<I>) {
chomp;
@seq = split/\t/;
$j = $#seq if ($#seq - $opt_rn > $j);
for ($i = $opt_rn; $i <= $#seq; ++$i) {
if (defined $opt_n) {
++$total_c[$i - $opt_rn] if ($seq[$i] ne $opt_w);
} else {
++$total_c[$i - $opt_rn] if ($seq[$i] eq $opt_w);
}
}
}
if (defined $cn) {
print $cn;
@seq = split /\t/, $cn;
print "\t" if ($seq[0] eq "");
}
$total_c[0] = 0 if (!defined $total_c[0]);
print "$total_c[0]";
for ($i = 1; $i <= $j - $opt_rn; ++$i) {
$total_c[$i] = 0 if (!defined $total_c[$i]);
print "\t$total_c[$i]";
}
print "\n";
close I;
}
if (defined $opt_r) {
open I, $opt_a or die "can not open $opt_a";
$cn = <I> if (defined $opt_cn);
$j = 0;
$opt_rn = 0 if (!defined $opt_rn);
while (<I>) {
chomp;
@seq = split/\t/;
$total_r[$j] = 0;
for ($i = $opt_rn; $i <= $#seq; ++$i) {
if (defined $opt_n) {
++$total_r[$j] if ($seq[$i] ne $opt_w);
} else {
++$total_r[$j] if ($seq[$i] eq $opt_w);
}
}
if ($opt_rn) {
print "$seq[0]\t$total_r[$j]\n";
} else {
print "$total_r[$j]\n";
}
++$j;
}
close I;
}