Zählen Sie Übereinstimmungen und Nichtübereinstimmungen, gruppieren Sie nach

Zählen Sie Übereinstimmungen und Nichtübereinstimmungen, gruppieren Sie nach

Bitte helfen Sie mit einem Shell-Skript des Folgenden. Ich muss die Anzahl der konsistenten Variablen in jeder Spur (Spalte 1) über alle Proben (Spalte 2) hinweg zählen. Da beispielsweise alle Werte (Spalte 4) der Spur 1, Variable 1, über alle drei Proben hinweg die Probe sind, wird Variable 1 zu einer konsistenten Variable gezählt. Ebenso sind die Variablen 2 und 3 der Spur 2 beide inkonsistent.

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

Ausgabe

Anzahl konsistenter und inkonsistenter Variablen in allen drei Stichproben

      #Consistent #Inconsistent
lane1  2             1
lane2  1             2

Antwort1

Perl-Lösung:

#!/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 }};
}

verwandte Informationen