
다음의 쉘 스크립트를 도와주세요. 샘플(col2) 전체의 각 레인(col1)에 일관된 변수의 개수가 필요합니다. 예를 들어, 모든 샘플에 걸쳐 레인1 변수 1의 모든 값(col4)이 샘플이므로, 변수1은 일관성 변수에 포함됩니다. 마찬가지로 레인 2 변수 2와 3은 모두 일치하지 않습니다.
lane1 sample1 variable1 ab
lane1 sample2 variable1 ab
lane1 sample3 variable1 ab
lane1 sample1 variable2 cd
lane1 sample2 variable2 cd
lane1 sample3 variable2 cd
lane1 sample1 variable3 gh
lane1 sample2 variable3 ab
lane1 sample3 variable3 gh
lane2 sample1 variable1 ac
lane2 sample2 variable1 ac
lane2 sample3 variable1 ac
lane2 sample1 variable2 gt
lane2 sample2 variable2 gt
lane2 sample3 variable2 ac
lane2 sample1 variable3 ga
lane2 sample2 variable3 ga
lane2 sample3 variable3 ac
산출
세 가지 표본 전체에 걸쳐 일관성 있는 변수와 일관성 없는 변수의 수
#Consistent #Inconsistent
lane1 2 1
lane2 1 2
답변1
펄 솔루션:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
my %values;
while (<>) {
next if /^$/; # Skip empty lines
my ($lane, $sample, $var, $val) = split;
die "Duplicate $lane $sample $var\n" if $values{$lane}{$var}{$val}{$sample};
$values{$lane}{$var}{$val}{$sample} = 1;
}
my %results;
for my $lane (keys %values) {
for my $var (keys %{ $values{$lane} }) {
my $count = keys %{ $values{$lane}{$var} };
if (1 == $count) {
++$results{$lane}{consistent};
} else {
++$results{$lane}{inconsistent};
}
}
say join "\t", $lane, @{ $results{$lane} }{qw{ consistent inconsistent }};
}