Help: Count characters

Discussion in 'Perl Misc' started by Amy Lee, Oct 21, 2007.

  1. Amy Lee

    Amy Lee Guest

    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
    Amy Lee, Oct 21, 2007
    #1
    1. Advertising

  2. Amy Lee

    Ron Bergin Guest

    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
    Ron Bergin, Oct 21, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. omegaman
    Replies:
    1
    Views:
    562
    omegaman
    Sep 21, 2004
  2. Jerry
    Replies:
    2
    Views:
    2,459
    Jerry
    Jan 13, 2005
  3. Replies:
    5
    Views:
    676
    Jim Langston
    Oct 18, 2006
  4. Umesh
    Replies:
    17
    Views:
    781
    James Kanze
    Apr 26, 2007
  5. efelnavarro09
    Replies:
    2
    Views:
    934
    efelnavarro09
    Jan 26, 2011
Loading...

Share This Page