So when it runs it is supposed to shorten the output of a fasta file to a few sequences and put that output to a text file but the issue I am having here is that the output file
Please see corrected code bellow
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Getopt::Long qw(GetOptions);
my $name = 'P2A.pl';
my $version = '0.0.1';
my $usage =
"
NAME $name
VERSION $version
SYNOPSIS This script would calculate the number of individual
sequences per multifasta file
COMMAND P2A.pl -fa *.fasta -out output.txt
-fa (--fasta) FASTA input files
-out (--output) Desired output file name [Default: output.txt]
";
die "$usage" unless @ARGV;
my @fasta;
my $output = 'output.txt';
GetOptions(
'fa|fasta=s@{1,}' => \@fasta,
'out|output=s' => \$output
);
open my $fh_out, '>', $output
or die "Can't top open $output";
while (my $file = shift @fasta){
open my $fh_in, '<', $file
or die "Can't to open $file";
my $sequence = 0; ## Keeping a counter
while (my $line = <$fh_in>){
## Looking for FASTA header
if ($line =~ m/^>/){
$sequence++;
}
}
print $fh_out "$file\t$sequence\n";
}
close $fh_out;
I would write processing code in following form
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Getopt::Long qw(GetOptions);
use Pod::Usage;
my $name = 'P2A.pl';
my $version = '0.0.1';
my %opt = ( output => 'output.txt' );
my @param = ('input|i=s@{1,}', qw/output|o=s screen|s debug|d help|h man|m/);
my %count;
GetOptions( \%opt, @param )
or pod2usage(2);
pod2usage(1) if $opt{help};
pod2usage(-exitval => 0, -verbose => 2) if $opt{man};
usage() unless $opt{input};
for my $fname ( @{$opt{input}} ) {
open my $fh, '<', $fname
or die "Can't to open $fname";
/^>/ && $count{$fname}++ while <$fh>;
close $fh;
}
if( $opt{screen} ) {
say "$_: $count{$_}" for keys %count;
} else {
open my $fh, '>', $opt{output}
or die "Can't to open $opt{output}";
say $fh "$_: $count{$_}" for keys %count;
close $fh;
}
sub usage {
say
"
NAME $name
VERSION $version
SYNOPSIS This script counts the number of individual sequences
per multifasta file
COMMAND P2A.pl -i *.fasta -o output.txt
-i,--input FASTA input files
-o,--output Desired output file name [Default: output.txt]
";
exit;
}
__END__
=head1 NAME
P2A.pl - counts fasta sequencies in files
=head1 SYNOPSIS
P2A.pl [options]
Options:
-i,--input input filename(s)
-o,--output output filename
-s,--screen output result to screen
-d,--debug output debug information
-?,--help brief help message
--man full documentation
=head1 OPTIONS
=over 4
=item B<-i,--input>
Input filename
=item B<-o,--output>
Output filename
=item B<-d,--debug>
Print debug information.
=item B<-?,--help>
Print a brief help message and exits.
=item B<--man>
Prints the manual page and exits.
=back
B<This program> processes B<fasta> files and outputs B<count> for sequencies to B<screen> or B<a file>
=cut