【发布时间】:2013-08-23 15:38:07
【问题描述】:
我有两个包含多个列的 txt 文件。这是第一个文件 ($frequency) 的样子:
C1 C2 A a B b C c D d
text 1 0 1 0 0 0 0 0 0
text 2 1 0 5 4 0 0 0 0
text 3 0 0 0 0 10 11 3 6
text 4 1 0 9 4 0 2 0 0
text 5 5 3 0 0 6 7 4 0
所以 C2 包含从 1 到 20000 的所有位置。A-d 列包含所有等于或大于 0 的数值。
这是第二个文件 ($variants) 的样子
C1 C2 C3 C4
text 2 A D
text 4 B C
text 5 A B,D
这里的 C2 包含 1 到 20000 之间的一些值。C3 和 C4 包含 A-D 之间的字母(如表 1 中的列名,但都是大写字母)。我现在要做的是:将$variants中的C2中的值与$frequency中的C2中的值匹配,然后检查$variants的C3中的哪个字母,然后复制相应的值(所以正确的行并更正带有大写和小写字母的列)从$frequency 到$variants 中的两个新列。然后需要对 $variants 的 C4 执行相同操作。
编辑:有时$variants 中的 C4 也可能包含两个由“,”分隔的字母。对于这两个字母,来自$frequency 的值应该出现在输出中
根据这个例子,输出应该是这样的
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10
text 2 A D 1 0 0 0 empty
text 4 B C 9 4 0 2 empty
text 5 A B,D 5 3 0 0 4 0
我已经开始编写脚本,但我遇到了一些需要比较值和字母的问题。
这是我目前所拥有的:
my $table1 = prompt("Give the name of the file with variants:\n");
open(my $variants, '<',$table1) || die "Could not open file $table1 $!";
my $table2 = prompt("Give the name of the file with the frequencies: \n");
open(my $frequency, '<',$table2) || die "Could not open file $table2 $!";
my (@position, @A, @a, @B, @b, @C, @c, @D, @d); #instead of using hashes I was trying to put all the values in arrays, because I don't know how to hash multiple columns from a file.
while(<$frequency>){
my @column = split(/\t/); # split on tabs
$position[$_] .= "$column[1] "; # I want to assign the correct column values to the arrays
$Afor[$_] .= "$column[2] ";
$arev[$_] .= "$column[3] ";
$Bfor[$_] .= "$column[4] ";
$brev[$_] .= "$column[5] ";
$Cfor[$_] .= "$column[6] ";
$crev[$_] .= "$column[7] ";
$Dfor[$_] .= "$column[8] ";
$drev[$_] .= "$column[9] ";
}
while(<$variants>){
next if /^\s*#/; # skipping some lines
next if /^\s*"/;
chomp;
my ($chr, $pos, $refall, $altall) = split;
}
我不确定这是否是正确的方法,因为我现在不知道如何检查$frequencies 中的正确行和对应列。有人可以帮我吗?
【问题讨论】:
-
您试图解释应该如何创建输出,但是如果您可以发布一个带有输入文件和预期输出的 complete 示例以便我们进行测试,这会容易得多我们的解决方案。
-
@amon 我更改了输入输出示例以更好地反映现实
标签: perl