I am a novice and would be grateful for your help. I tried to write a piece of code to compare two files which look like
File 1File 2['CHROM', 'POS', 'REF', 'ALT', 'LIST', 'SAMPLE_1A', 'SAMPLE_2A', 'SAMP +LE_3A'] ['M, '16', 'T', 'C', 'C', 'REF', 'C', 'REF'] ['M', '381', 'T', 'A', 'A', T', 'REF', 'REF'] ['M', '529', 'A', 'G', 'G', 'REF', 'G', 'REF']
and
['CHROM', 'POS', 'REF', 'ALT', 'LIST', 'SAMPLE_1B', 'SAMPLE_2B', 'SAMP +LE_3B'] ['M', '16', 'T', 'C', 'C', 'C', 'REF', 'REF'] ['M', '381', 'T', 'A', 'A', 'A', 'REF', 'REF'] ['M', '528', 'A', 'G', 'G', 'REF', 'REF', 'REF']
I am trying to write a code which prints out file 1 again but if the sample value is not 'REF', looks up file 2. If the corresponding file 2 value is 'REF' then print the original value appearing in file 1. If the corresponding value in file 1 is not 'REF' then print the value we find in file 2. I also wish to ignore positions in file 2 not present in file 1. Using the examples above, my output for the first two rows should look like
['CHROM', 'POS', 'REF', 'ALT', 'LIST', 'SAMPLE_1A', 'SAMPLE_2A', 'SAMP +LE_3A'] ['M, '16', 'T', 'C', 'C', 'REF', 'C', 'REF'] ['M', '381', 'T', 'A', 'A', 'A', 'REF', 'REF']
The code I have written so far is:
#!/usr/bin/local/perl use strict; use warnings; my %HashRef = (); my $File1 = 'blah.txt; my $File2 = 'moreblah.txt'; my $outfile = 'blank.txt'; open FILE2, "< $File2" or die "could not open tumour file...\n"; open FILE1, "< $File1" or die "could not open host file...\n"; open( my $out_fh, '>', $outfile ) or die "$!"; while (my $cols = <FILE2> { chomp $cols; my @values = split ',', $cols; for my $i(5..$#values) { push( @{$HashRef{ $values[2] }}, $values[$i]); } } while (my $cols = <FILE1>) { chomp $cols; my @values = split ',', $cols; my @newarray = ($values[0], $values[1], $values[2], $values[3], $v +alues[ 4]); for my $j(5..$#values) { if($values[$j] =~ m/'REF'/) { push(@newarray, $values[$j]); } elsif( $HashRef{ $values[$j]} =~ m/'REF'/) { push(@newarray, $values[$j]); } else { push(@newarray, " REF "); } } say $out_fh @newarray; } close $out_fh; close file1; close file2;
I keep getting the error "Use of uninitialized value within %HashRef in pattern match (m//) at test.pl line 45, <FILE1> line 27". I tried storing the columns I wanted from file 2 in a hash in order to look them up but it just doesn't seem to be working. I have tried everything I can think of but at this stage I am just running into a brick wall. Please help! Any and all suggestions/corrections/criticisms are welcome!
In reply to Can't access data stored in Hash - help! by corcra
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |