首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从单独的列表中选择文本文件中的数据- perl或unix。

从单独的列表中选择文本文件中的数据- perl或unix。
EN

Stack Overflow用户
提问于 2013-03-08 14:08:03
回答 1查看 795关注 0票数 1

我有一个大的选项卡分隔文件,如下所示:

contig04733 contig00012 77 contig00546 contig01344 12 contig08943 contig00001 14 contig00765 contig03125 88 等

我有一个单独的制表符分隔文件,只有这些连体对的子集,如下所示:

contig04733 contig00012 contig08943 contig00001 等

我想将第一个文件中与第二个文件中列出的行对应的行提取到一个新文件中。在这个特定的数据集中,我认为在这两个文件中,每对应该是相同的。但也想知道是否说:

file1 contig08943 contig00001 14

但在file2中

contig00001 contig08943

我还是想要那个组合,也可以写点什么吗?

我的密码在下面。

代码语言:javascript
复制
use strict;
use warnings;

#open the contig pairs list
open (PAIRS, "$ARGV[0]") or die "Error opening the input file with contig pairs";

#hash to store contig IDs - I think?!
my $pairs;

#read through the pairs list and read into memory?
while(<PAIRS>){
    chomp $_; #get rid of ending whitepace
    $pairs->{$_} = 1;
}
close(PAIRS);

#open data file
open(DATA, "$ARGV[1]") or die "Error opening the sequence pairs file\n";
while(<DATA>){
    chomp $_;
    my ($contigs, $identity) = split("\t", $_);
    if (defined $pairs->{$contigs}) {
        print STDOUT "$_\n";
    }
}
close(DATA);
EN

回答 1

Stack Overflow用户

发布于 2013-03-08 16:48:09

尝试使用基于这两个键的散列哈希(在拆分之后)。

代码语言:javascript
复制
use strict;
use warnings;

#open the contig pairs list
open (PAIRS, "$ARGV[0]") or die "Error opening the input file with contig pairs";

#hash to store contig IDs - I think?!
#my $pairs;

#read through the pairs list and read into memory?
my %all_configs;
while(<PAIRS>){
    chomp $_; #get rid of ending whitepace
    my @parts = split("\t", $_); #split into ['contig04733', 'contig00012', 77]
    #store the entire row as a hash of hashes
    $all_configs{$parts[0]}{$parts[1]} = $_;
    #$pairs->{$_} = 1; 
}
close(PAIRS);

#open data file
open(DATA, "$ARGV[1]") or die "Error opening the sequence pairs file\n";
while(<DATA>){
    chomp $_;
    my ($contigs, $identity) = split("\t", $_);
    #see if we find a row, one way, or the other
    my $found_row = $all_configs{$contigs}{$identity} 
        || $all_configs{$identity}{$contigs};
    #if found, the split, and handle each part    
    if ($found_row) {
        my @parts = split("\t", $found_row);
        #same sequence as in first file
        my $modified_row  = $parts[0]."\t".$parts[1].(sqrt($parts[2]/100));
        #if you want to keep the same sequence as found in second file
        my $modified_row  = $contigs."\t".$identity.(sqrt($parts[2]/100));

        print STDOUT $found_row."\n"; #or
        print STDOUT $modified_row."\n";
    }
}
close(DATA);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15295831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档