AWallBuilder has asked for the wisdom of the Perl Monks concerning the following question:
I have 4 hashes for counting certain hits for many contigs and I would like to print the combined output as a table. For ex. $Hit1{$contigA}=4, $Hit2{$contigA}=5, $Hit4{$contigA}=1 I would like the following output:
contigA 4 5 0 1 contigB 10 1 1 0 etc.
looking for inspiration/help I found this subroutine zup. However, I am slightly confused by zup, and can't see how to extend it too hashes, also I would have to put in some check and if a count doesnt exist for that contig/Hit combo then make it zero.
any help is appreciated
##zup##my script so far#!/usr/bin/perl use strict; use warnings; no warnings qw /syntax/; sub zup { join "\n" => map {join " " => map {shift @$_} @_} @{$_ [0]} } my @array1 = qw /ab bc cd de/; my @array2 = qw /cc dd ee gg/; my @array3 = qw /12 34 56 78/; print zup \(@array1, @array2, @array3); print "\n";
use strict; use warnings; my $infile=$ARGV[0]; open (IN,$infile) or die "cannot open $infile"; my %count_Bact; my %count_undef; my %count_ProphVir; my %count_Euk; while (my $line=<IN>) { chomp $line; next if ($line =~/^#/) ; my ($geneid,$cons_flag)=split(/\t/,$line); my @geneid_columns=split(/_/,$geneid); my $contig=join("_",$geneid_columns[0],$geneid_columns[1],$gen +eid_columns[2],$geneid_columns[3]); if ($cons_flag eq "Bact"){ $count_Bact{$contig} ++; } if ($cons_flag eq "undef"){ $count_undef{$contig} ++; } if ($cons_flag eq "Proph_Vir" || $cons_flag eq "Vir" || $cons_ +flag eq "Proph"){ $count_ProphVir{$contig} ++; } if ($cons_flag eq "Euk"){ $count_Euk{$Euk} ++ ; } } close (IN);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: print multiple hashes as multiple columns
by johngg (Canon) on Jun 29, 2012 at 22:54 UTC | |
|
Re: print multiple hashes as multiple columns
by dulwar (Monk) on Jun 29, 2012 at 23:17 UTC | |
by AWallBuilder (Beadle) on Jul 04, 2012 at 08:58 UTC |