簡體   English   中英

當我使用Perl One-liners時,如何解決“由於編譯錯誤而中止-e的執行。”

[英]How can I fix “Execution of -e aborted due to compilation errors.” when i'm working with perl one-liners

我正在用在MobaXterm上運行的Perl編寫程序。 我的問題是我希望Perl運行Perl單行代碼(通過另一個unix命令通過管道發送)。 當我在終端中鍵入我的單行代碼時,效果很好,但是我似乎無法通過Perl使其正常工作。

我在終端中運行的命令是

samtools view $file.bam | 
perl -ne 'if ($_ =~ m/NM:i:(\d+)/) {print $1, "chr(10)"}' > $file.nm 

我使用文件'M1.10.fasta'測試程序

我已經復制了我的代碼:

#!/usr/bin/perl -w

use strict;

my $read1 = 'Intesti-cocktail_R1.fastq'; 
my $read2 = 'Intesti-cocktail_R2.fastq'; 
my $dir = '/home/local1/balin/TestData/'; 

chdir($dir) or die "no dir"; #change directory 
opendir(DIR, '.') or die "no dir"; 
my @filelist = readdir(DIR); 
closedir(DIR); 

@filelist = grep(m/^M\d+\.\d+\.fasta$/, @filelist); # clean list of unwanted names


foreach my $file (@filelist) {
    my $cmd1 = "bwa index $file";
    my $cmd2 = "bwa mem $file $read1 $read2 | samtools view -Sb - > $file.bam"; 
    my $cmd3 = "samtools view $file.bam | perl -ne \"if ($_ =~ m/NM:i:(\\d+)/) {print $1, 'chr(10)'}\" > $file.nm";
    my $returnvalue1 = system($cmd1);
    my $returnvalue2 = system($cmd2);
    my $returnvalue3 = system($cmd3);
    print "Failed command1 ($returnvalue1): $cmd1\n" if $returnvalue1 != 0;
    print "Failed command2 ($returnvalue2): $cmd2\n" if $returnvalue2 != 0; 
    print "Failed command3 ($returnvalue3): $cmd3\n" if $returnvalue3 != 0;
}

我的錯誤消息是:

Use of uninitialized value $_ in concatenation (.) or string at Perl_BWAv2.pl line 21.
Use of uninitialized value $1 in concatenation (.) or string at Perl_BWAv2.pl line 21.
syntax error at -e line 1, near "( =~"
syntax error at -e line 1, near ";}"
Execution of -e aborted due to compilation errors.
Failed command3 (65280): samtools view M1.10.fasta.bam | perl -ne 'if ( =~ m/NM:i:(\d+)/) {print , "(10)"}' > M1.10.fasta.nm

您需要對perl命令中的變量進行轉義,以免插值:

my $cmd3 = qq{samtools view $file.bam | perl -ne "if (\$_ =~ m/NM:i:(\\d+)/) {print \$1, 'chr(10)'}" > $file.nm};
#                                                      ^                             ^

但是,我建議不要生成新的perl進程,因為當您在當前perl腳本中執行操作時,這樣做可以簡化調試:

open my $AM, '-|', 'samtools', 'view', "$file.bam" or die "Can't open samtools: $!";
open my $outfh, '>', "$file.nm" or die "Can't open $file.nm: $!";
while (<$AM>) {
    print $outfh $1, 'chr(10)' if m/NM:i:(\d+)/;
}
close $outfh;

在您的perl代碼中,您將使perl的命令以雙引號而不是單引號運行; Shell對這兩者的處理方式大不相同,除非您使用Shell變量,否則應堅持使用單引號。 恢復為單引號,並避免使用您不想在perl腳本中插入的$:

my $cmd3 = "samtools view $file.bam | perl -ne 'if (\$_ =~ m/NM:i:(\\d+)/) {print \$1, \"chr(10)\"}/ > $file.nm";

它也總是有助於打印您要饋送到系統或管道開口的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM