in reply to Re^3: how to sum over rows based on column headings in perl
in thread how to sum over rows based on column headings in perl
Actually, I think I did reply too fast! Obviously i don't understand map function the way you used it.
Could you explain your usage of map? E.g. I changed "M" to "MUTS" and the results are screwed up. Wherever you used "M", I changed it to "MUTS" and changed "M" in datafile to "MUTS" and I don't get the expected result.
map { $_ eq $label ? 'MUTS' : '_' } @headers[1..$#headers];
My modified code gives wrong results
#!/usr/bin/perl use strict; use warnings; use autodie; if (@ARGV != 1){ print "USAGE: ./parse-counts.pl file\n"; exit(1); } my $mutfile = $ARGV[0]; open(INPUTR,"<$mutfile") or die "Can't open \$mutfile for reading. \n" +; my (%counts, %unique, %masks); my ($headname, @unique) = grep !$unique{$_}++, my @headers = split /\t +/, <INPUTR>; # the basic syntax is @out = map { CODE } @in; for my $label ( @unique ) { $masks{$label} = join "\t", map { $_ eq $label ? 'MUTS' : '_' } @headers[1..$#headers]; } my $line; while($line=<INPUTR>) #while(<DATA>) { chomp $line; $line =~ s/\t/\t/g; # for uniform spacing my ($name, $letters) = split /\t/, $line, 2; $counts{$name}{$_} += ($masks{$_} | $letters) =~ /MUTS/ for @uniqu +e; print $name."\n"; print $letters."\n"; } print "$headname @unique\n"; print "$_ @{ $counts{$_} }{@unique}\n" for sort keys %counts; The output produced is: Gname G1 G2 G3 A 3 2 0 B 2 1 1 C 2 0 0 The modified datafiles is Gname G1 G1 G1 G1 G2 G2 G3 A W W MUTS W W W MUTS A W W W W W W W A W W W W W W W B W W W W W MUTS MUTS B MUTS W W W W MUTS MUTS C MUTS MUTS MUTS W W W W C MUTS W W MUTS MUTS W W
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: how to sum over rows based on column headings in perl
by Anonymous Monk on Jul 31, 2015 at 19:29 UTC | |
by angerusso (Novice) on Jul 31, 2015 at 19:58 UTC | |
by Anonymous Monk on Jul 31, 2015 at 20:09 UTC |