Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found