Hello Monks,
I have created a hash (from a given number of files), which has the following keys : values associated with it:
aw1:10 qs2:20 dd3:30 de4:10 hg5:30 dfd6:20 gf4:20 hgh5:30 hgy3:10 and so on...
There are only 3 values (10,20,30), for this hash. Furthermore, I have a file - "Sample.fa" (with header information) like:
>aw1 ATGCTAGATGCTAGCTAGCTAGCACTGAT CGATGCTAGCGTAGTCAGCTGATGCTGTA CGATGCTAGTCGTACG >qs2 CGAGCTAGTCGTAGTCGTGATGCTGATTA CGATGCTAGTCGTAGCTAGCTGATGCTGC CGATGCTAGTCGTAGTC >dd3 CGTAGTCGTAGTCGTAGTCGATGCTGATG GCTAGTCGATGCTAGCTAGTCGATGCTGG CGATGCTGAT >de4 CGTAGTCGTAGTCGTACGTAGTCGTGAGT CGATTATTTAGGAGGGACAAGGATAGTA >hg5 CGTAGTCGTAGTCTAGTCGTGATGCTAGA >dfd6 CGATGCTACGTACGTAGTCAGTCGTGATG AATTAGAGCAGATAGAGGGGGAAAGGGTT AAACCCC >gf4 CGTAGTCAGTCTAGCTGATGTCGATGCTG >hgh5 CATGCTAGTCGTAGTCGTAGTCGATGCTT TTTTAAGGGAACCCCC >hgy3 CCCCGGGTTTGGGAAAAGGGGGGGGATAG
I want to write the corresponding header and seq from Sample.fa, to 3 files (10.txt, 20.txt, 30.txt -- according to the hash key and value). Following is a snippet from my code :
open (FILE1, ">10.txt"); open (FILE2, ">20.txt"); open (FILE3, ">30.txt"); my @files = glob('Data/*.fa'); my %hash = &readFile(); foreach my $f(@files){ open FILE, $f or die "Cannot open : $!\n"; while (my $line = <FILE>){ chomp $line; #print "$line\n"; if($line =~ /^>/){ my @head = split (/ /, $line); my $name = substr($line, 1); foreach my $key(keys %hash){ if($key eq $name){ if($hash{$key} == 10){ select FILE1; } if($hash{$key} == 20){ select FILE2; } if($hash{$key} == 30){ select FILE3; } } } } } }
Can someone point me in the right direction?
Thanks!
In reply to Compare hash with arrays and print by ad23
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |