将一个列表与另一个列表匹配 [英] match one list to another

查看:53
本文介绍了将一个列表与另一个列表匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 txt 文件,其中的数据看起来像这样(测试):

I have a txt file with data that looks like this (TEST):

chr1_10524
chr1_10525
chr1_10562
chr1_8383722
chr1_201327234
chr2_123123

另一个 txt 文件的数据看起来像这样(数据库):

And another txt file with data that looks like this (DATABASE):

chrom chromStart chromEnd name
chr1 67071812 67170812 13_Heterochrom/lo
chr1 201326377 201330777 13_Heterochrom/lo
chr1 8383613 8389213 12_Repressed
chr2 120000 130000 1_Active Promoter

我希望获得一个输出文件,其中 TEST 与 DATABASE 匹配,给出如下内容:

I wish to obtain an output file where the TEST is matched with DATABASE giving something like this:

chr1_8383722 12_Repressed
chr1_201327234 13_Heterochrom/lo
chr2_123123 1_Active Promoter

这可以在 perl 上完成吗?谢谢!

Can this be done on perl? Thank you!

推荐答案

试试这个:

#!/usr/bin/perl

use warnings;
use strict;

open(my $db, "<", "database.txt") or die "Cannot open < database.txt: $!";
open(my $tst, "<", "test.txt") or die "Cannot open < test.txt: $!";

my @database;

while (<$db>) {
    chomp;
    my @fields = split;
    push @database, \@fields;
}

while (my $line = <$tst>) {
    chomp($line);
    my ($chr, $pos) = split /_/, $line;
    # There is not unique key can be used to detect whether an entry is exist
    # in the database.
    foreach my $entry (@database) {
        if ($chr eq $entry->[0] && $entry->[1] <= $pos && $pos <= $entry->[2]) {
            print "$line $entry->[3]\n";
        }
    }
}

这篇关于将一个列表与另一个列表匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆