perl - 比较 2 个文件中的列并以与 file1 中相同的顺序打印匹配和不匹配的行并在匹配和不匹配的行末尾打印 YES/NO

标签 perl foreach split

文件1

3 14573 ab712 A T
8 12099 ab002 G A
9 12874 ab790 A C
3 19879 ab734 G T

文件2

3 14573 ab712 A T
9 12874 ab790 A C

输出

3 14573 ab712 A T YES
8 12099 ab002 G A NO
9 12874 ab790 A C YES
3 19879 ab734 G T NO

我在 file1 和 2 上尝试了 perl foreach 循环
生成的输出如下-

3 14573 ab712 A T YES
8 12099 ab002 G A NO
9 12874 ab790 A C NO
3 19879 ab734 G T NO
4 34565 ab992 C G NO
9 12874 ab790 A C YES
3 14573 ab712 A T NO
8 12099 ab002 G A NO
9 12874 ab790 A C NO
3 19879 ab734 G T NO
4 34565 ab992 C G NO

我尝试过的脚本

foreach $arr1 (@arr1) {
  chomp $arr1;
  ($chr1, $pos1, $id1, $ref1, $alt1) = split(/\t/, $arr1);

  foreach $arr2 (@arr2) {
    chomp $arr2;  
    ($chr2, $pos2, $id2, $ref2, $alt2) = split(/\s/, $arr2);

    {
      if (($pos1 eq $pos2 ) && ($chr1 eq $chr2 )) {
        print "$chr1\t$pos1\t$ref1\t$alt1\tYES\n";
      } else {
        print "$chr1\t$pos1\t$ref1\t$alt1\tNO\n"
      }  
    }   
  }
}  

最佳答案

您的代码相当复杂,所以我担心我没有时间理解它并纠正您做错的任何事情。

但是,我确实有时间展示我的解决方案(带评论):

#!/usr/bin/perl

# Always use these
use strict;
use warnings;

# Open file2...
open my $fh2, '<', 'file2' or die $!;

# ... and use its contents to construct a hash.
# The key of the hash is the line of data from the
# file (without the newline) and the value is the
# number 1.
# We can therefore use this hash to work out if a
# given line from file1 exists in file2.

my %file2 = map { chomp; $_ => 1 } <$fh2>;

# Open file1...
open my $fh1, '<', 'file1' or die $!;

# ... and process it a line at a time
while (<$fh1>) {
  # Remove the newline
  chomp;
  # Print the line
  print;
  # Find out if the line exists in file2
  # and print 'YES' or 'NO' as appropriate.
  print $file2{$_} ? ' YES' : ' NO';
  # Print a newline.
  print "\n";
}

更新:这是一个仅匹配输入数据的前两个字段的版本(考虑到示例输入,这应该不重要,但您的代码暗示这就是您想要匹配的内容) .

#!/usr/bin/perl

# Always use these
use strict;
use warnings;

# Open file2...
open my $fh2, '<', 'file2' or die $!;

# ... and use its contents to construct a hash.
# The key of the hash is the first two fields from
# the line of data from the file and the value is the
# number 1.
# We can therefore use this hash to work out if a
# given line from file1 exists in file2.

my %file2 = map { join(' ', (split)[0,1]) => 1 } <$fh2>;

# Open file1...
open my $fh1, '<', 'file1' or die $!;

# ... and process it a line at a time
while (<$fh1>) {
  # Remove the newline
  chomp;
  # Print the line
  print;
  # Find out if the line exists in file2
  # and print 'YES' or 'NO' as appropriate.
  print $file2{join ' ', (split)[0,1]} ? ' YES' : ' NO';
  # Print a newline.
  print "\n";
}

关于perl - 比较 2 个文件中的列并以与 file1 中相同的顺序打印匹配和不匹配的行并在匹配和不匹配的行末尾打印 YES/NO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68668243/

相关文章:

perl - 为什么不能识别 `cpanm`安装的模块?

arrays - Perl 中迭代循环并提取对或三元组的最佳方法是什么

windows - 如何在 Windows 上使用 Perl 优雅地打印 %z(时区)格式?

java - ORA-01795 : maximum number of expressions in a list is 1000 error with myibatis

php - 如何将 PHP 和 MySQL 与 while 和 foreach 循环一起使用?

java - 在java中将全名拆分为称呼,名字和姓氏

perl - 是否可以通过编程方式设置 $ 的前 7 位?

c# - 我可以将这个 do-while 循环重写为 foreach 循环吗?

python - 拆分 Pandas 列并对值求和

python - 基于 Pandas 中竖线分隔的列创建多个新行