我有一个表,它显示在一个OTU(行)中聚集了多少个ASV(列)。每个ASV由1的值表示。
#OTUID ASV_1 ASV_2 ASV_3 ASV_4 ASV_5 ASV_6 ASV_7 ASV_8 ASV_9 ASV_10
OTU1 1 0 0 1 0 0 0 0 0 1
OTU2 0 1 0 0 1 0 0 0 0 0
OTU3 0 0 0 0 0 1 0 1 1 0我想把这张表概述如下:
#OTUID ASVs
OTU1 ASV_1, ASV_4, ASV_10
OTU2 ASV_2, ASV_5
OTU3 ASV_6, ASV_8, ASV_9帮帮忙吧。
发布于 2019-09-06 05:02:26
下面的脚本假设您希望为每个输入行上的所有列(在第一个标题行之后)打印列名称,这些列的值 1。
#!/usr/bin/perl
use strict;
my @titles=();
while(<>) {
if ($. == 1) {
@titles = split; # get column titles
print "#OTUID\tASVs\n"; # print the new output header
next;
};
chomp;
my @F=split; # split the input line into fields, store in array @F
my @ASVs=(); # @ASV array holds the titles for each matching field.
foreach my $asv (1..$#F) {
push @ASVs, $titles[$asv] if ($F[$asv] == 1);
};
print "$F[0]\t", join(",", @ASVs), "\n";
}将其保存为,例如,alex.pl,使其与chmod +x alex.pl一起执行,并按如下方式运行:
$ ./alex.pl input.txt
#OTUID ASVs
OTU1 ASV_1,ASV_4,ASV_10
OTU2 ASV_2,ASV_5
OTU3 ASV_6,ASV_8,ASV_9发布于 2019-09-11 15:05:39
$ perl -lane '$,="\t";
$. == 1 and do{ $h{$_} = $F[$_] for 1..$#F; print $F[0], "ASVs"; next; };
print $F[0], join ", ", map { $h{$_} } grep { $F[$_] == 1 } 1..$#F;
' file<#>结果:
#OTUID ASVs
OTU1 ASV_1, ASV_4, ASV_10
OTU2 ASV_2, ASV_5
OTU3 ASV_6, ASV_8, ASV_9https://unix.stackexchange.com/questions/539295
复制相似问题