Help: Count characters

Help: Count characters

am 21.10.2007 05:20:10 von Amy Lee

Hello,

I make a small perl script to count how many sequences in a fasta
formation file. In fasta formation file every sequence starts with a ">",
so I can count how many ">" in a file.

There's my code:

if (@ARGV != 0)
{
foreach $file (@ARGV)
{
unless (-e $file)
{
print "***Error: $file dose not exist.\n";
next;
}
unless (open $FILE_IN, '<', $file)
{
print "***Error: Cannot read $file.\n";
next;
}
$full_size = (stat($file))[7];
while (<$FILE_IN>)
{
s/>//g;
}
$mod_size = (stat($FILE_IN))[7];
$num = $full_size-$mod_size;
close $FILE_IN;
print "$file ==> $num seq.\n"
}
}

However, when I run this script, it always shows "0 seqs". I don't know
what happened. And is there any better way to count ">"?

Thank you very much~

Regards,

Amy Lee

Re: Help: Count characters

am 21.10.2007 06:02:08 von Tad McClellan

Amy Lee wrote:


> I make a small perl script to count how many sequences in a fasta
> formation file. In fasta formation file every sequence starts with a ">",
> so I can count how many ">" in a file.


You can do that with a one-liner:

perl -ne '$cnt += tr/>//; END{print "$cnt\n"}' file1 file2


> There's my code:
>
> if (@ARGV != 0)
> {
> foreach $file (@ARGV)
> {
> unless (-e $file)
> {
> print "***Error: $file dose not exist.\n";
> next;
> }
> unless (open $FILE_IN, '<', $file)


You open the file for reading.


> {
> print "***Error: Cannot read $file.\n";
> next;
> }
> $full_size = (stat($file))[7];
> while (<$FILE_IN>)
> {
> s/>//g;
> }
> $mod_size = (stat($FILE_IN))[7];


Why do you expect that a file you opened for reading would
change its file size?


> $num = $full_size-$mod_size;
> close $FILE_IN;
> print "$file ==> $num seq.\n"
> }
> }
>
> However, when I run this script, it always shows "0 seqs". I don't know
> what happened.


You never wrote the modified lines anywhere.


> And is there any better way to count ">"?


See above.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Help: Count characters

am 21.10.2007 06:08:40 von Ron Bergin

On Oct 20, 8:20 pm, Amy Lee wrote:
> Hello,
>
> I make a small perl script to count how many sequences in a fasta
> formation file. In fasta formation file every sequence starts with a ">",
> so I can count how many ">" in a file.
>
> There's my code:
>
> if (@ARGV != 0)
> {
> foreach $file (@ARGV)
> {
> unless (-e $file)
> {
> print "***Error: $file dose not exist.\n";
> next;
> }
> unless (open $FILE_IN, '<', $file)
> {
> print "***Error: Cannot read $file.\n";
> next;
> }
> $full_size = (stat($file))[7];
> while (<$FILE_IN>)
> {
> s/>//g;
> }
You're not maintaining a count of the number of substitutions.

> $mod_size = (stat($FILE_IN))[7];
> $num = $full_size-$mod_size;
> close $FILE_IN;
> print "$file ==> $num seq.\n"
> }
Since the file was opened in read only mode, its size will not have
been changed.
>
> }
>
> However, when I run this script, it always shows "0 seqs". I don't know
> what happened. And is there any better way to count ">"?
perldoc -q "how do I count the number of lines in a file"
>
> Thank you very much~
>
> Regards,
>
> Amy Lee