如何匹配 tab-separated 矩阵文件中的字符串列表

How to match a list of strings in a tab-separated matrix file

我有两个文件:

  1. 包含 miRNA 目标列表的文本文件。
  2. 具有 miRNA 作为 header 及其相应目标的文本文件。 两个文件中都有共同的目标。

示例文件 1.

CDK6
CCNE1
CDC25A
STAT5A
RAVER2
RHOC
KLK10
BDNF
NFKB1
CXCR4
ROCK1
HUWE1

示例文件2是矩阵如下

hsa-miR-29a-3p  hsa-miR-15b-5p  hsa-let-7c-5p   hsa-miR-222-3p  hsa-miR-99a-5p  hsa-miR-493-3p
CDK6    CCNE1   CDC25A  STAT5A  RAVER2  RHOC
RAN RECK    TGFBR1  CDKN1B  FGFR3   MAP2K7
BACE1   BCL2    TRIM71  BCL2L11 SERPINE1    FZD4
DNMT3A  CCND1   HMGA2   SOD2    IGF1R   HNRNPU
DNMT3B  VEGFA   MYC MMP1    MTOR    ABHD2
COL4A2  EIF4A1  DICER1  FOXO3   AGO2    SRSF11

所需的输出文件是:

CDK6      1  0  0  0  0  0
CCNE1     0  1  0  0  0  0
CDC25A    0  0  1  0  0  0

open (FH,"sample_down.txt");
open (FILE,"sample_ref.txt");
while (<FH>) {
    chomp;
    $_ =~ s/\r//;
    if (/^hsa/) {
           $header=$_;
           @head=split (/\t/,$header);
           for (@head) {
               $h=$_;
               $hash1{$h}="$h";
           }
    } else {
        @target=split (/\t/,$_);
        for (@target) {
            $t=$_;
            $hash{$h}{$t}="$t";
        }
    }
}
while (<FILE>) {
    chomp;
    $_ =~ s/\r//;
    $ref =$_;
    foreach $v (keys %hash) {
             print "$v\n";
             if (exists $hash{$h}{$ref}) {
                 #print "$ref\t11\n";
             } else {
                 #print "$ref\t00000\n";
             }
    }
}

如果有人能帮助我,那就太好了。

您可以使用 :crlf 图层,这样您就不必明确删除 \r

首先,阅读第二个文件,记住每个目标是在哪里找到的。然后,读取第一个文件并为每个目标打印记住的信息。

#! /usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my $target_file = shift;
my $miRNA_file  = shift;

open my $miRNA_fh, '<:crlf', $miRNA_file or die $!;
my @header = split ' ', <$miRNA_fh>;
my %found;
while (<$miRNA_fh>) {
    chomp;
    my @t = split;
    for my $i (0 .. $#t) {
        $found{ $t[$i] }[$i] = 1;
    }
}

open my $target_fh, '<:crlf', $target_file or die $!;
while (my $t = <$target_fh>) {
    chomp $t;
    say join "\t", $t, map $found{$t}[$_] // 0, 0 .. $#header;
}

从您的数据来看,同一行或同一列中似乎不能有多个 1。我的代码是通用的,即不适用此限制。应用它可能会大大改变代码。