读取序列时未创建输出文件

问题描述 投票:1回答:1

所以当它运行时,应该是将fasta文件的输出缩短为几个序列,并将该输出放到一个文本文件中,但我在这里遇到的问题是,输出文件没有被创建,我不知道脚本中的错误是什么。我把我的代码贴在下面,供大家参考。谢谢你的阅读,如果你有

#!/usr/bin/perl
use strict; use warnings; use Getopt::Long qw(GetOptions);

my $name = 'P2A.pl';

my $usage = <<"OPTIONS";

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]

OPTIONS



die "$usage" unless @ARGV;
my $file;
my @fasta;
my $output = 'output.txt';
GetOptions(
    'fa|fasta=s@{1,}' => \@fasta,
        'out|output=s'  => \$output
);

open OUT, ">", $output;

while (my $fasta = shift@fasta){
        open IN, "<", $file;
        my $sequence = 0; ## Keeping a counter
        while (my $line = <IN>){
                ## Looking for FASTA header
                if ($line =~ m/^>/){
                        $sequence++;
                }
        }
    print OUT "$fasta\t$sequence\n";
}
linux perl
1个回答
2
投票

请看下面的修正代码

#!/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;

我将按以下形式编写处理代码

#!/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
热门问题
推荐问题
最新问题