Curious if I can get a little help here. I’m a perl newbie, and can’t figure out how to convert the following code into something a bit more useful for my analysis.
This code presently takes the 1st and 4th column from a user supplied list of data files and puts them together.
What I’d like my code to do, for each row of the “current output” generated by this code (see below), is make a sum of these 4th column values (filea, fileb, filec). Not quite sure how to implement this…
Current Output:
filea fileb filec
entrya | 0 |10.2 | 0
entryb | 0 | 0.0 | 1
entryc | 8 | 57.0| 46
desired output
sum
entrya | 10.2
entryb | 1
entryc | 111
current code looks like this:
main: {
my %data;
foreach my $file (@rsem_files) {
open (my $fh, $file) or die "Error, cannot open file $file";
my $header = <$fh>; # ignore it
while (<$fh>) {
chomp;
my @x = split(/\t/);
my $acc = $x[0];
my $count = $x[4];
$data{$acc}->{$file} = $count;
}
close $fh;
}
my @filenames = @rsem_files;
foreach my $file (@filenames) {
$file = basename($file);
}
print join("\t", "", @filenames) . "\n";
foreach my $acc (keys %data) {
print "$acc";
foreach my $file (@rsem_files) {
my $count = $data{$acc}->{$file};
unless (defined $count) {
$count = "NA";
}
print "\t$count";
}
print "\n";
}
exit(0);
}
Alter the
@rsemfilesloop: