我做了散列哈希,其中文件的所有行根据其第5个字段的值排序到“主”哈希键。将每个散列键和它的值打印在新文件中?
%Tiles
有n个键,其中每个键是不同的$Tile_Number
。
%Tiles
的每个元素的值是对散列散列的引用,其中包含所有行,其中$Tile_Number
是当前散列键的编号。每个这些新密钥(行)的值仅为1.
$Tiles{$Tile_Number}{$Line}=1
,其中$Tiles{$Tile_Number}
有许多$Line=1
条目。
我想在单独的文件中打印每个$Tiles{$Tile_Number}
散列,最好在创建$Tile_Number
键时创建该文件,并且在添加每个新的$Tiles{$Tile_Number}{$Line}=1
以打印时节省内存。
最好的办法是不打印最终值(1),但我可以做到这一点。
我该如何让Perl为“主”散列中的每个键打开一个新文件并打印其所有键?
代码:
use strict;
use warnings;
my ($Line) = "";
my (@Alignment_Line) =();
my (%Tiles) =();
my $Huge_BAM_File= $ARGV[0] or die $USAGE;
open(HUGE_BAM_FILE,"< $Huge_BAM_File") || die "Sorry I couldn't open the INPUT file: $Huge_BAM_File !\n";
while(<HUGE_BAM_FILE>){
### Remove new line characters "\n"
### Split each line by "\t" and by ":" (for fields within READ ID FIELD)
chomp;
$Line = $_;
@Alignment_Line = split(/\t+|\:/, $Line);
my $Tile_Number = $Alignment_Line[4]
##########################################################
### Fill in hash of hashes %Tiles ###
### Key = $Tile_Number ###
### Second key is $Line ###
### and is filled with a 1 ###
### Each key contains all the alignments with that tile###
### number ###
##########################################################
$Tiles{$Tile_Number}{$Line} = 1;
##Here, I would like to write this new entry into the corresponding file,
and maybe remove it from the hash so the program doesn't run out of memory.
}
接近(HUGE_BAM_FILE); close(ALL_OUTPUTS_GENERATED);
您预计会有多少个$ Tile_Number值?对于每个值,$ Line有多少个值?我认为你寻求的答案取决于这些数字。另外,为什么您要节省内存,因为许多现代计算机都有可用的RAM? – AdrianHHH 2013-05-06 16:41:50
我期待96 $ Tile_Number,每个包含10-15百万个条目,并且为8个项目并行执行此操作。 – 2013-05-06 17:15:53