如何计算文件中每行的特定字符数?

时间:2013-08-20 01:58:26

标签: perl fasta

我正在尝试计算FASTA文件中'N'的数量:

>Header
AGGTTGGNNNTNNGNNTNGN
>Header2
AGNNNNNNNGNNGNNGNNGN

所以最后我想得到'N'的数量,每个标题是一个读数,所以我想做一个直方图,所以我会在最后输出这样的东西:

# of N's   # of Reads

0            300
1            240

等...

所以有300个序列或读数有0个'N'

use strict;
use warnings;

my $file = shift;
my $output_file = shift;

my $line;
my $sequence;
my $length;
my $char_N_count = 0;
my @array;
my $count = 0;

if (!defined ($output_file)) {
    die "USAGE: Input FASTA file\n";
}
open (IFH, "$file") or die "Cannot open input file$!\n";
open (OFH, ">$output_file") or die "Cannot open output file $!\n";

while($line = <IFH>) {
chomp $line;
next if $line =~ /^>/;
$sequence = $line;
@array = split ('', $sequence);
foreach my $element (@array) {
if ($element eq 'N') {
$char_N_count++;
 }
 }
  print "$char_N_count\n";
 }

1 个答案:

答案 0 :(得分:2)

试试这个。我改变了一些像使用标量文件句柄的东西。在Perl中有很多方法可以做到这一点,所以有些人会有其他的想法。在这种情况下,我使用了一个可能有间隙的数组 - 另一种选择是通过计数将结果存储在哈希和密钥中。

编辑:刚刚意识到我没有使用$ output_file,因为我不知道你想用它做什么:)如果你打算写的话,只需将最后的'print'更改为'print $ out_fh'它。

use strict;
use warnings;

my $file = shift;
my $output_file = shift;

if (!defined ($output_file)) {
    die "USAGE: $0 <input_file> <output_file>\n";
}
open (my $in_fh, '<', $file) or die "Cannot open input file '$file': $!\n";
open (my $out_fh, '>', $output_file) or die "Cannot open output file '$output_file': $!\n";

my @results = ();
while (my $line = <$in_fh>) {
    next if $line =~ /^>/;
    my $num_n = ($line =~ tr/N//);
    $results[$num_n]++;
}

print "# of N's\t# of Reads\n";

for (my $i = 0; $i < scalar(@results) ; $i++) {
    unless (defined($results[$i])) {
        $results[$i] = 0;
        # another option is to 'next' if you don't want to show the zero totals
    }
    print "$i\t\t$results[$i]\n";
}
close($in_fh);
close($out_fh);
exit;
相关问题