in reply to While loop problem with filereading

I will admit that I got a bit lost reading your code. I opened the file with the single points and made a hash table keyed on chromosome with values being the single point value and the text of the line. Then I went line by line thru the first file and for each chromosome range, and printed one line for each single point value that fit within the range. If a range line is seen more than once it is skipped.

Maybe this helps, maybe not. I am sure that I botched the terminology up somehow - you'll have to fix that in the program. But the number of levels of "while" is less in the approach below. Anyway for each range, you get one line of output for each of the single points that is within that range.

#usr/bin/perl -w use strict; use Data::Dump qw(pp); open (my $fileReg, '<', 'chromfile1.txt') or die "unable to open chrom +file1.txt $!\n"; open (my $fileBase, '<', 'chromfile2.txt') or die "unable to open chro +mfile2.txt $!\n"; my %region; while (<$fileBase>) { chomp; my ($chr, $base) = (split)[0,1]; push @{$region{$chr}}, [$base, $_]; } close $fileBase; my %seen; while (my $regionline = <$fileReg>) { chomp ($regionline); my ($base,$start,$end)= (split(/\s+/,$regionline))[0,1,2]; next if $seen{"$base-$start-$end"}++; next if !defined $region{$base}; foreach my $ptr (@{$region{$base}}) { print "$regionline $ptr->[1]\n" if ($ptr->[0]>= $start and $ptr- +>[0]<=$end); } } __END__ prints: chr1 100 159 0 chr1 104 104 0 0 + chr1 100 159 0 chr1 145 145 0 0 + chr1 200 260 0 chr1 205 205 0 0 + chr1 500 750 0 chr1 600 600 0 0 + chr3 450 700 0 chr3 500 500 0 0 + chr4 100 300 0 chr4 150 150 0 0 + chr4 100 300 0 chr4 175 175 0 0 + chr7 350 600 0 chr7 400 400 0 0 + chr7 350 600 0 chr7 550 550 0 0 + chr9 100 125 0 chr9 100 100 0 0 + chr11 679 687 0 chr11 680 680 0 0 + chr11 679 687 0 chr11 681 681 0 0 + chr22 100 200 0 chr22 105 105 0 0 + chr22 100 200 0 chr22 110 110 0 0 + chr22 300 400 0 chr22 350 350 0 0 + file1: chr1 100 159 0 chr1 100 159 0 chr1 200 260 0 chr1 500 750 0 chr3 450 700 0 chr4 100 300 0 chr4 100 300 0 chr7 350 600 0 chr7 350 600 0 chr9 100 125 0 chr11 679 687 0 chr11 679 687 0 chr22 100 200 0 chr22 100 200 0 chr22 300 400 0 file2: chr1 104 104 0 0 + chr1 145 145 0 0 + chr1 205 205 0 0 + chr1 600 600 0 0 + chr3 500 500 0 0 + chr4 150 150 0 0 + chr4 175 175 0 0 + chr7 400 400 0 0 + chr7 550 550 0 0 + chr9 100 100 0 0 + chr11 680 680 0 0 + chr11 681 681 0 0 + chr22 105 105 0 0 + chr22 110 110 0 0 + chr22 350 350 0 0 +

Replies are listed 'Best First'.
Re^2: While loop problem with filereading
by a217 (Novice) on Jun 23, 2011 at 21:22 UTC

    Wow! That's very elegant and simple. I initially thought using hash tables would be beneficial, but I thought that since there would be multiple chromosome numbers as the key that it wouldn't work.

    You've definitely pointed me in the right direction and I thank you for helping me with this problem that I got stuck on.

    One question regarding the Data::Dump. I don't have the module installed on my computer (and I'm not sure if its on the server I'm working on), but I #commented it out and the solution still worked. Just as a precaution, is the module necessary to produce the correct output? I only ask because this was a small sample input I'm working with but the actual data set is 15GB. I just want to make sure before I even attempt that beast.

    Thank you,

    a217

Re^2: While loop problem with filereading
by a217 (Novice) on Jun 23, 2011 at 21:24 UTC

    ^^Nevermind, I realized shortly after I posted that it is just to assist in the printing. Sorry about that.