fasta – 如何从stdin和perl中的文件中解压缩透明gzip?

我已经编写了一些用于处理FASTA / FASTQ文件的脚本(例如 fastx-length.pl),但是希望使它们更通用,并接受压缩和未压缩文件作为命令行参数和标准输入(以便脚本“只是工作“当你向他们扔掉随机文件时”.对我来说,在未压缩和压缩文件(例如压缩读取文件,未压缩的组合基因组)上工作是很常见的,并且像<(zcat file.fastq.gz)之类的插槽很快就会烦人. 这是fastx-length.pl脚本中的一个示例块:

...
my @lengths = ();
my $inQual = 0; # false
my $seqID = "";
my $qualID = "";
my $seq = "";
my $qual = "";
while(<>){
  chomp; chomp; # double chomp for Windows CR/LF on Linux machines
  if(!$inQual){
    if(/^(>|@)((.+?)( .*?\s*)?)$/){
      my $newSeqID = $2;
      my $newShortID = $3;
      if($seqID){
        printf("%d %s\n",length($seq),$seqID);
        push(@lengths,length($seq));
      }
...

我可以看到IO :: Uncompress :: Gunzip通过以下方式支持透明解压缩:

If this option is set and the input file/buffer is not compressed data,the module will allow reading of it anyway.

In addition,if the input file/buffer does contain compressed data and there is non-compressed data immediately following it,setting this option will make this module treat the whole file/buffer as a single data stream.

我想基本上将透明的解压缩插入the diamond operator,在加载每个文件和从文件输入中读取一行之间.有谁知道我怎么做到这一点?

解决方法

我经常使用:

die("Usage: prog.pl [file [...]]\n") if @ARGV == 0 && -t STDIN;
push(@ARGV,"-") unless @ARGV;
for my $fn (@ARGV) {
    open(FH,$fn =~ /\.gz$/? "gzip -dc $fn |" : $fn =~ /\.bz2$/? "bzip2 -dc $fn |" : $fn) || die;
    print while (<FH>);
    close(FH);
}

此策略仅在您具有gzip等和具有适当文件扩展名的文件名时才有效,但是一旦满足这些要求,它就可以同时处理各种文件类型.至于-t STDIN,见explanation here.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...