Hello Peter Keystrokes,
I notices a few things on your code.
You have open (my $fh, '>', $filename) or die "Could not open '$filename' $!";. From the open documentation:
Simple examples to open a file for reading:
open(my $fh, "<", "input.txt")
or die "Can't open < input.txt: $!";
and for writing:
open(my $fh, ">", "output.txt")
or die "Can't open > output.txt: $!";
Also on your while loop you have while (my $line = <>){ without the file handler. From How can I read in an entire file all at once?:
while (<$input>) {
chomp;
# do something with $_
}
At the end of your code you are trying to write to a file print $fh $count;. You have opened the file in writing mode from the beginning but I am wondering how you are reading the file.
So I assume you meant to write something like that?
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'counts.txt';
open (my $fh, '+<', $filename)
or die "Could not open '$filename' $!";
my $count = "";
while (<$fh>) {
chomp;
if ($_ =~ /^>hsa/) {
$count .= length $_;
$count .= " ";
}
}
print $fh $count . "\n";
close $fh
or die "Could not close '$filename' $!";
__END__
>hsa_circ_0000001|chr1:1080738-1080845-|None|None
ATGGGGTTGGGTCAGCCGTGCGGTCAGGTCAGGTCGGCCATGAGGTCAGGTGGGGTCGGCCATGAAGGTG
+GTGGGGGTCATGAGGTCACAAGGGGGTCGGCCATGTG
>hsa_circ_0000002|chr1:1158623-1159348-|NM_016176|SDF4
GGTGGATGTGAACACTGACCGGAAGATCAGTGCCAAGGAGATGCAGCGCTGGATCATGGAGAAGACGGCC
+GAGCACTTCCAGGAGGCCATGGAGGAGAGCAAGACACACTTCCGCGCCGTGGACCCTGACGGGGACGGT
+CACGTGTCTTGGGACGAGTATAAGGTGAAGTTTTTGGCGAGTAAAGGCCATAGCGAGAAGGAGGTTGCC
+GACGCCATCAGGCTCAACGAGGAACTCAAAGTGGATGAGGAAA
49 54
Notice the open (my $fh, '+<', $filename) or die "Could not open '$filename' $!";. On this mode you can read and write to the file.
I do not know exactly why you want to read and write to your file, for me it would made more sense to read and print the output based on characters that they where counted but if this is what you desire.
I would prefer to do it like this, just personal preference nothing special.
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'counts.txt';
open (my $fh, '+<', $filename)
or die "Could not open '$filename' $!";
chomp(my @lines = <$fh>);
for (@lines) { print $fh length($_) . " " if ($_ =~ /^>hsa/); }
close $fh
or die "Could not close '$filename' $!";
__END__
>hsa_circ_0000001|chr1:1080738-1080845-|None|None
ATGGGGTTGGGTCAGCCGTGCGGTCAGGTCAGGTCGGCCATGAGGTCAGGTGGGGTCGGCCATGAAGGTG
+GTGGGGGTCATGAGGTCACAAGGGGGTCGGCCATGTG
>hsa_circ_0000002|chr1:1158623-1159348-|NM_016176|SDF4
GGTGGATGTGAACACTGACCGGAAGATCAGTGCCAAGGAGATGCAGCGCTGGATCATGGAGAAGACGGCC
+GAGCACTTCCAGGAGGCCATGGAGGAGAGCAAGACACACTTCCGCGCCGTGGACCCTGACGGGGACGGT
+CACGTGTCTTGGGACGAGTATAAGGTGAAGTTTTTGGCGAGTAAAGGCCATAGCGAGAAGGAGGTTGCC
+GACGCCATCAGGCTCAACGAGGAACTCAAAGTGGATGAGGAAA
49 54
Update 3: Thanks to Anonymous Monk, AnomalousMonk, and Laurent_R I finally understand my mistakes and this is the updated recommended version.
#!/usr/bin/perl
use strict;
use warnings;
my $count;
while (<>) {
if ($_ =~ /^>hsa/){
chomp (my $line = <>);
$count .= length($line);
$count .= " ";
print "Count final: " . $count . "\n";
}
} continue {
close ARGV if eof; # reset $. each file
}
my $filename = 'counts.txt';
open (my $fh, '>', $filename)
or die "Could not open '$filename' $!";
print $fh $count;
close $fh
or die "Could not close '$filename' $!";
__END__
$ perl test.pl test.txt
Count final: 107
Count final: 107 251
And the way that I would try to resolve it.
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'counts.txt';
open (my $fh, '>', $filename)
or die "Could not open '$filename' $!";
chomp(my @lines = <>);
for (@lines) {
next if ($_ =~ /^>hsa/);
print $fh length($_) . " ";
}
print $fh "\n";
close $fh
or die "Could not close '$filename' $!";
__END__
$ cat counts.txt
107 251
Update: 4 Even better as Anonymous Monk point it out:
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'counts.txt';
open (my $fh, '>', $filename)
or die "Could not open '$filename' $!";
chomp(my @lines = <>);
for (@lines) { print $fh length($_) . " " if ($_ =~ tr/ACGT//); }
print $fh "\n";
close $fh
or die "Could not close '$filename' $!";
__END__
$ cat counts.txt
107 251
Hope this helps.
Seeking for Perl wisdom...on the process of learning...not there...yet!
|