#!/usr/bin/perl -w if (@ARGV != 1){ print "USAGE: ./parse-counts.pl file\n"; exit(-1); } $mutfile = $ARGV[0]; %hash = (); open(INPUTR,"<$mutfile") || die "Can't open \$mutfile for reading. \n"; while($line=){ chomp $line; @toks = split(/\t/,$line); if ($toks[0] =~ /^Gname/){ $k = 0; # loop over the header row to get the unique "Gname"s @header = split(/\t/,$line); for $j (1..@toks-2){ $i = $j+1; if ($header[$i] ne $header[$j]){ $k++; $name[$k] = $header[$j]; } } for $i (0..$k){ $hash{$toks[0]}{$name[$k]} = $name[$k]; } } else { $k = 0; for $j (1..@toks-2){ $i = $j+1; if ($header[$i] ne $header[$j]){ $k++; $hash{$toks[0]}{$name[$k]} = 0; if ($toks[$j] =~ /M/){ $hash{$toks[0]}{$name[$k]} = 1; } } } } } close(INPUTR); $outdata = $mutfile; $outdata =~ /(.+).txt/; $outdata = $1."-COUNTS.txt"; open(OUTD,">$outdata"); foreach $idname (sort keys %hash){ if ($idname =~ /^Gname/){ print OUTD $idname; foreach $gid (sort keys %{$hash{$idname}}){ print OUTD "\t".$hash{$idname}{$gid}; } print OUTD "\n"; } } foreach $idname (sort keys %hash){ if ($idname !~ /^Gname/){ print OUTD $idname; foreach $gid (sort keys %{$hash{$idname}}){ print OUTD "\t".$hash{$idname}{$gid}; } print OUTD "\n"; } } close(OUTD); print "Printing $outdata file done.\n";