list - 如何使用 perl 比较文本文件中两个不同数据库的每个元素?
问题描述
我正在尝试将一个列表(screens.txt )中第1列的每个元素与另一个列表( new_list.txt )中第1列的任何元素进行比较,如果匹配,则打印列表的整行(screens.txt)一个单独的文本文件(matched.txt)。我设法选择了正确的列,但我得到的输出是列表中的行(new_list.txt)而不是列表(screens.txt),并且只找到一个命中,所以看起来循环也有问题.
new_list.txt格式 => first_column->double_tab->the_rest
我是 perl 编程的新手。任何帮助将不胜感激!
这是我到目前为止所做的:
#!usr/bin/perl
use warnings;
$list = "new_list.txt";
$screens = "screens.txt";
$result = "matched.txt";
open (FA, "<$list") or die "Can't read source file $list: $!\n";
open (RES, ">$result") or die "Can't write on file $result: $!\n";
$n = 0;
$column = 10;
while ($line = <FA>) {
@description = split (' ', $line);
@ID = split ('\\t', $description[0]);
#print just first column from the list
# print "$ID[0]\n";
}
close (FA);
open (FA, "$screens") or die "Can't read source file $screens: $!\n";
while ($file = <FA>) {
@table = split (' ', $file);
@accession_no = split ('\ ', $table[0]);
# print the first column from the list
# print "$accession_no[0]\n";
}
open (FA, "<$list") or die "Can't read source file $list: $!\n";
while ($line = <FA>) {
print "$line\n";
@description = split (' ', $line);
@ID = split ('\\t', $description[0]);
if ($accession_no eq $ID[0]) {
$n = $n+1;
for ($i = 0; $i < $column; $i++) {
print RES "$file";
}
print "\n";
}
}
close (FA);
close (RES);
print "Hits found: $n\n";
这是 next_list.txt 的示例:Q9UKA8 RCAN3_HUMAN 0
Q9UKA8-2 RCAN3_HUMAN 0
Q9UKA8-3 RCAN3_HUMAN 0
Q9UKA8-4 RCAN3_HUMAN 0
Q9UKA8-5 RCAN3_HUMAN 0
Q9GZP0 PDGFD_HUMAN 0
这是screens.txt的输入文件:
Q9GZP0 GDLDLASEST 支架连接因子 B2 (SAF-B2) SAFB2
Q9UKA8-5 QKAFNSSSFN Ran GTPase 激活蛋白 1 (RanGAP1) RANGAP1
我有兴趣检查Q9GZP0和Q9UKA8-5(第一列)
来自screens.txt是在new_list.txt的第一列,如果他们
然后从screen.txt打印整行/行。
先感谢您!
解决方案
用块的力量过滤屏幕的最小代码map
#!/usr/bin/perl
use strict;
use warnings;
my $input1 = 'new_list.txt';
my $input2 = 'screens.txt';
my %seen;
open my $fh1, "< $input1"
or die "Couldn't open $input1";
map{ $seen{$1} = $2 if /(\S+)\s(.*)/ } <$fh1>;
close $fh1;
open my $fh2, "< $input2"
or die "Couldn't open $input2";
map{ print if /(\S+)\s+(.*)/ and $seen{$1} } <$fh2>;
close $fh2;
输入:new_list.txt
Q9UKA8 RCAN3_HUMAN 0
Q9UKA8-2 RCAN3_HUMAN 0
Q9UKA8-3 RCAN3_HUMAN 0
Q9UKA8-4 RCAN3_HUMAN 0
Q9UKA8-5 RCAN3_HUMAN 0
Q9GZP0 PDGFD_HUMAN 0
输入:screens.txt
Q9GZP0 GDLDLASEST Scaffold attachment factor B2 (SAF-B2) SAFB2
Q9UKA8-5 QKAFNSSSFN Ran GTPase-activating protein 1 (RanGAP1) RANGAP1
输出:
Q9GZP0 GDLDLASEST Scaffold attachment factor B2 (SAF-B2) SAFB2
Q9UKA8-5 QKAFNSSSFN Ran GTPase-activating protein 1 (RanGAP1) RANGAP1
笔记:
Linux——使用命令使程序可执行chmod og+x program.pl
Windows——运行程序为perl program.pl
使用命令将输出重定向到文件中:
Linux -program.pl > matched.txt
窗户 -perl program.pl > matched.txt
推荐阅读
- python - Json_normalize 逐个字符地给出结果
- javascript - 隐藏数据表中最后一行的按钮 - JQuery
- flutter - 颤振材质图标减号
- android-studio - Android : 如何知道一个 android 应用程序是否有效运行(新手)
- javascript - 扩展捕获原型以进行新遗物跟踪
- dynamics-crm - D365 Online:自定义实体限制
- c# - 学习 C# 并尝试解决错误
- php - 仅在我的站点上更改服务器上的 PHP 版本
- angular - Primeng选择动态p-tabPanel
- c# - Parallel.ForEach 有条件