Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Replacing substrings within hash values

by BrowserUk (Patriarch)
on Mar 24, 2016 at 10:11 UTC ( [id://1158707]=note: print w/replies, xml ) Need Help??


in reply to Replacing substrings within hash values

Perhaps this will help? (Sorry, I'm too lazy to use Bio::*):

#! perl -slw use strict; use Inline::Files; use Data::Dump qw[ pp ]; my %seqs = map{ split "\n", $_ } <FASTA>; pp \%seqs; while( my( $seq, $pos, $chr, $rep ) = split ' ', <EDITS> ) { substr( $seqs{ '>' . $seq }, $pos-1, 1, $rep ); } pp \%seqs; __FASTA__ >I CATCAGTATAAAATGACTAGTAGCTAGATACCACAGATACGATACAACA >II TACCACAGATACGATACAACACATCAGTATAAAATGACTAGTAGCAGAC __EDITS__ I 2 A G I 4 C T I 5 A G I 7 T C II 1 T C II 2 A G II 3 C T II 5 A C II 8 G T II 10 T G

Output:

C:\test>1158701 { ">I" => "CATCAGTATAAAATGACTAGTAGCTAGATACCACAGATACGATACAACA", ">II" => "TACCACAGATACGATACAACACATCAGTATAAAATGACTAGTAGCAGAC", } { ">I" => "CGTTGGCATAAAATGACTAGTAGCTAGATACCACAGATACGATACAACA", ">II" => "CGTCCCATAGACGATACAACACATCAGTATAAAATGACTAGTAGCAGAC", }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Replacing substrings within hash values
by K_Edw (Beadle) on Mar 24, 2016 at 11:20 UTC
    Thanks! I'll have a go at understanding this. I've not come across 'map' before. It would also be good if somebody could help me understand what's wrong with my own code (for learning purposes), and also because I may wish to introduce more ways of editing such as deleting certain letters and thus needing to keep track of changes as it goes to ensure replacements still occur in the right place.

      The main problem with your code is that you must read the entire IN file for each sequence. This would work if you rewind (seek IN, 0, 0) the IN file at the end of the sequence loop. This approach is very inefficient. Run-time would become unacceptable for larger files.

      Your use of last is fine for sequence I. For all other sequences, it would exit the loop before it gets to the processing. Use next instead.

      Bill
        I see, thank you. The script is now working as desired after fixing the usage of substr, using next and the addition of seek(IN,0,0). Is there a more efficient way to accomplish this or is this an unavoidable consequence of the way the script is structured/written? My thought was that i'd only need to iterate over IN once as it's sorted and that once $F[0] no longer equalled the current key, it would begin reading IN where it left off for the next sequence before it terminated.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158707]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found