微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐