Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用perl在一个文件中使用列查找另一个文件中的匹配列时出现定义错误_Arrays_Loops_Perl_Foreach_Global Variables - Fatal编程技术网

Arrays 使用perl在一个文件中使用列查找另一个文件中的匹配列时出现定义错误

Arrays 使用perl在一个文件中使用列查找另一个文件中的匹配列时出现定义错误,arrays,loops,perl,foreach,global-variables,Arrays,Loops,Perl,Foreach,Global Variables,我有一个制表符分隔的输入文件,格式如下: + Chr1 www - Chr2 zzz ... 我想逐行查看以引用选项卡分隔的文件(下面代码中的成绩单),格式如下: Chr1 + xxx UsefulInfo1 Chr2 - yyy UsefulInfo2 ... 并希望输出如下所示: + Chr1 UsefulInfo1 - Chr2 UsefulInfo2 ... 以下是我尝试从命令行获取变量名,从输入文

我有一个制表符分隔的输入文件,格式如下:

+    Chr1    www
-    Chr2    zzz
...
我想逐行查看以引用选项卡分隔的文件(下面代码中的成绩单),格式如下:

Chr1    +    xxx    UsefulInfo1
Chr2    -    yyy    UsefulInfo2
...
并希望输出如下所示:

+    Chr1    UsefulInfo1
-    Chr2    UsefulInfo2
...
以下是我尝试从命令行获取变量名,从输入文件中获取某些信息,并从参考文件中附加有用的信息:

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

my $inFile = $ARGV[0];
my $outFile = $ARGV[1];

open(INFILE, "<$inFile") || die("Couldn't open $inFile: $!\n");
open(OUTFILE, ">$outFile") || die("Couldn't create $outFile: $!\n");

open(TRANSCRIPTS, "</path/TranscriptInfo") || die("Couldn't open reference file!");
my @transcripts = split(/\t+/, <TRANSCRIPTS>);
chomp @transcripts;

#Define desired information from input for later
while (my @columns = split(/\t+/, <INFILE>)) {
    chomp @columns;
    my $strand = $columns[0];
    my $chromosome = $columns[1];

    #Attempt to search reference file line by line for matching criteria and copying a column of matching lines
    foreach my $reference(@transcripts) {
        my $refChr = $reference[0]; #Error for this line
        my $refStrand = $reference[1]; #Error for this line
        if ($refChr eq $chromosome && $refStrand eq $strand) {
            my $info = $reference[3]; #Error for this line
            print OUTFILE "$strand\t$chromosome\t\$info\n";
        }
    }
}

close(OUTFILE); close(INFILE);
#/usr/bin/perl
严格使用;
使用警告;
使用诊断;
my$infle=$ARGV[0];
my$outFile=$ARGV[1];
打开(填充),固定:

使用严格;
使用警告;
使用特征qw(例如);
我的$in_qfn=$ARGV[0];
my$out_qfn=$ARGV[1];
my$transcripts_qfn=“/path/TranscriptInfo”;
我的抄本;
{
打开(我的$transcripts\u fh,“,$out\u qfn)
或者死亡(“无法创建\“$out\u qfn\”:$!\n”);
而(){
咀嚼;
我的($strand,$chr)=拆分(/\t/,$\u1);
我的$成绩单(@成绩单){
我的$ref_chr=$transcript->[0];
我的$ref_strand=$transcript->[1];
if($chr eq$ref\U chr&$strand eq$ref\U strand){
我的$info=$transcript->[2];
说出$out\U fh join(“\t”、$strand、$chr、$info);
}
}
}
}

也就是说,上面的方法效率很低。我们调用N$transcript_qfn中的行数,调用M$in_qfn中的行数。内部循环执行的次数等于N*M。实际上,它只需要执行N次

use strict;
use warnings;
use feature qw( say );

my $in_qfn          = $ARGV[0];
my $out_qfn         = $ARGV[1];
my $transcripts_qfn = "/path/TranscriptInfo";

my %to_print;
{
   open(my $in_fh, "<", $in_qfn)
      or die("Can't open \"$in_qfn\": $!\n");
   while (<$in_fh>) {
      chomp;
      my ($strand, $chr) = split(/\t/, $_, -1);
      ++$to_print{$strand}{$chr};
   }    
}

{
   open(my $transcript_fh, "<", $transcript_qfn)
      or die("Can't open \"$transcript_qfn\": $!\n");
   open(my $out_fh, ">", $out_qfn)
      or die("Can't create \"$out_qfn\": $!\n");
   while (<$transcript_fh>) {
      chomp;
      my ($ref_chr, $ref_strand, $info) = split(/\t/, $_, -1);
      next if !$to_print{$ref_strand};
      next if !$to_print{$ref_strand}{$ref_chr};
      say $out_fh join("\t", $ref_strand, $ref_chr, $info);
   }
}
使用严格;
使用警告;
使用特征qw(例如);
我的$in_qfn=$ARGV[0];
my$out_qfn=$ARGV[1];
my$transcripts_qfn=“/path/TranscriptInfo”;
我的打印百分比;
{
打开(我的$in_fh,“,$out_qfn)
或者死亡(“无法创建\“$out\u qfn\”:$!\n”);
而(){
咀嚼;
我的($ref\u chr,$ref\u strand,$info)=拆分(/\t/,$);
下一个if!$to_print{$ref_strand};
下一个if!$to_print{$ref_strand}{$ref_chr};
说出$out\u fh join(“\t”,$ref\u strand,$ref\u chr,$info);
}
}

awk'{print$2}'文件1>temp1;egrep-f temp1文件2 | awk'{print$2,$1,$4}'
use strict;
use warnings;
use feature qw( say );

my $in_qfn          = $ARGV[0];
my $out_qfn         = $ARGV[1];
my $transcripts_qfn = "/path/TranscriptInfo";

my %to_print;
{
   open(my $in_fh, "<", $in_qfn)
      or die("Can't open \"$in_qfn\": $!\n");
   while (<$in_fh>) {
      chomp;
      my ($strand, $chr) = split(/\t/, $_, -1);
      ++$to_print{$strand}{$chr};
   }    
}

{
   open(my $transcript_fh, "<", $transcript_qfn)
      or die("Can't open \"$transcript_qfn\": $!\n");
   open(my $out_fh, ">", $out_qfn)
      or die("Can't create \"$out_qfn\": $!\n");
   while (<$transcript_fh>) {
      chomp;
      my ($ref_chr, $ref_strand, $info) = split(/\t/, $_, -1);
      next if !$to_print{$ref_strand};
      next if !$to_print{$ref_strand}{$ref_chr};
      say $out_fh join("\t", $ref_strand, $ref_chr, $info);
   }
}