Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (16)
    As of 2014-08-01 15:08 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Who would be the most fun to work for?















      Results (26 votes), past polls