读取序列时未创建输出文件 [英] Output file not created when reading sequences

查看:73
本文介绍了读取序列时未创建输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当它运行时,应该将fasta文件的输出缩短为几个序列,然后将其输出到文本文件中,但是我在这里遇到的问题是未创建输出文件,而我没有不知道脚本中的错误是什么.我将在下面发布我的代码以供参考.感谢您阅读,

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 does not get created and I don't know what the error is within the script. I will post my code below for reference. Thanks for reading this if you do

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

推荐答案

请在下面查看更正的代码

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

这篇关于读取序列时未创建输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆