Before getting to my question, I feel compelled to make the following statements:
- I'm new to Perl.
- There is probably a better/easier way to do this.
- Thanks in advance for your help!
I have long text file with DNA pentamers (i.e. ATGCA) and associated data (number observed, expected probability, total number of pentamers in the sequence) for each pentamer. The pentamers are listed multiple times and I would like to create a way to combine all repeated entries.
I have written a script that creates new hashes for each value and successfully combines repeated entries.
Now what I would like to do is print out on one line the following:
Pentamer \t Probability \t totalCounts \t totalPentCount \n
(spaces added for readability)
I am at a total loss for what to do next and I would appreciate advice if I should give up and try another way or keep going with what I have...
#!/usr/bin/perl use strict; use warnings; #given a completed pentamer count file, sort pentamers, and consolidat +e duplicates my @data; my @currentPentLine; my $counter; my $pent; my $pentExpected; my $pentObserved; my $pentTotal; #read in input file, capturing pentamers and related info for each open(FREQ,"<testPentamerCounts.txt") or die "Cannot open FREQ output f +ile\n"; while (<FREQ>) { chomp; @currentPentLine = split("\t", $_); $pent = $currentPentLine[0]; $pentExpected = $currentPentLine[1]; $pentObserved = $currentPentLine[2]; $pentTotal = $currentPentLine[3]; #create array of hashes with data from input file $data[$counter]{'pent'} = $pent; $data[$counter]{'count'} = $pentObserved; $data[$counter]{'prob'} = $pentExpected; $data[$counter]{'pentTotal'} = $pentTotal; $counter++; } close FREQ; my $criteria = "pent"; my @sorted_pentamers = sort { $a->{$criteria} cmp $b->{$criteria} } @d +ata; my %totalProbability; my $probTotal; for $probTotal (@sorted_pentamers) { if (not exists $totalProbability{$probTotal->{pent}}) { $totalProbability{$probTotal->{pent}} = 0; } $totalProbability{$probTotal->{pent}} += $probTotal->{prob}; } my %totalCounts; my $countTotal; for $countTotal (@sorted_pentamers) { if (not exists $totalCounts{$countTotal->{pent}}) { $totalCounts{$countTotal->{pent}} = 0; } $totalCounts{$countTotal->{pent}} += $countTotal->{count}; } my %totalPentCounts; my $pentTotal; for $pentTotal (@sorted_pentamers) { if (not exists $totalPentCounts{$pentTotal->{pent}}) { $totalPentCounts{$pentTotal->{pent}} = 0; } $totalPentCounts{$pentTotal->{pent}} += $pentTotal->{pentTotal +}; } my @keysProb = keys %totalProbability; my @keysTotal = keys %totalCounts; print @keysProb, "\n"; print @keysTotal, "\n"; #print unique sequences to output file Is there some way I could access iterate through each pentamer as a ke +y in each of the hashes and print out each value? Thanks again!
In reply to Printing out multiple hashes at one time by cvillain
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |