{"id":744,"date":"2013-05-31T18:55:09","date_gmt":"2013-05-31T18:55:09","guid":{"rendered":"http:\/\/psyphi.net\/blog\/?p=744"},"modified":"2013-05-31T20:03:39","modified_gmt":"2013-05-31T20:03:39","slug":"random-sequence-mutator","status":"publish","type":"post","link":"https:\/\/psyphi.net\/blog\/2013\/05\/random-sequence-mutator\/","title":{"rendered":"Random Sequence Mutator"},"content":{"rendered":"<p>\n Here&#8217;s a handy one(ish)-liner to mutate an input sequence using Perl&#8217;s RegEx engine:\n<\/p>\n<pre><code>epiphyte:~ rmp$ perl -e '$seq=\"ACTAGCTACGACTAGCATCGACT\"; $mutants = [qw(A C T G)];\r\n  print \"$seq\\n\";\r\n  $seq =~ s{([ATGC])}{ rand() &lt; 0.5 ? $mutants-&gt;[int rand 4] : $1 }smiexg;\r\n  print \"$seq\\n\";'\r\nACTAGCTACGACTAGCATCGACT\r\nACAATCGCGGACCAGAATCTCTT<\/code><\/pre>\n<p>\nThis gives each base in $seq a 50% chance (rand() < 0.5) of mutating to something, but as the original base is in the available $mutants array it has a further 25% chance of changing to itself. If you wanted to improve it by excluding the original base for each mutation you might do something like:\n<\/p>\n<pre><code>epiphyte:~ rmp$ perl -e '$seq=\"ACTAGCTACGACTAGCATCGACT\"; $mutants = [qw(A C T G)];\r\n  $mutsize=scalar @{$mutants}; print \"$seq\\n\";\r\n  $seq =~ s{([ATGC])}{ rand() &lt; 0.5 ? [grep { $_ ne $1 } @{$mutants}]-&gt;[int rand $mutsize-1] : $1 }smiexg;\r\n  print \"$seq\\n\";'\r\nACTAGCTACGACTAGCATCGACT\r\nTGTAGATAATGTGATACGAGACT<\/code><\/pre>\n<\/p>\n<p>\nThis (quite inefficiently) builds an array of all available options from $mutants, excluding $1 the matched base at each position.<\/p>\n<p>Unrolling it and tidying it up a little for readability looks like this:\n<\/p>\n<pre><code>my $seq     = 'ACTAGCTACGACTAGCATCGACT';\r\nmy $mutants = [qw(A C T G)];\r\nmy $mutsize = scalar @{$mutants};\r\n\r\nprint \"$seq\\n\";\r\n\r\n$seq =~ s{([ATGC])}{\r\n   rand() &lt; 0.5\r\n    ?\r\n   [grep { $_ ne $1 } @{$mutants}]-&gt;[int rand $mutsize-1]\r\n    :\r\n   $1\r\n }smiexg;\r\n\r\nprint \"$seq\\n\";'<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a handy one(ish)-liner to mutate an input sequence using Perl&#8217;s RegEx engine: epiphyte:~ rmp$ perl -e &#8216;$seq=&#8221;ACTAGCTACGACTAGCATCGACT&#8221;; $mutants = [qw(A C T G)]; print &#8220;$seq\\n&#8221;; $seq =~ s{([ATGC])}{ rand() &lt; 0.5 ? $mutants-&gt;[int rand 4] : $1 }smiexg; print &#8220;$seq\\n&#8221;;&#8217; ACTAGCTACGACTAGCATCGACT ACAATCGCGGACCAGAATCTCTT This gives each base in $seq a 50% chance (rand() < 0.5) &hellip; <a href=\"https:\/\/psyphi.net\/blog\/2013\/05\/random-sequence-mutator\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Random Sequence Mutator&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[11],"tags":[934,935,778],"class_list":["post-744","post","type-post","status-publish","format-standard","hentry","category-programming","tag-mutation","tag-random","tag-sequence"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts\/744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/comments?post=744"}],"version-history":[{"count":7,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts\/744\/revisions"}],"predecessor-version":[{"id":751,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts\/744\/revisions\/751"}],"wp:attachment":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/media?parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/categories?post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/tags?post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}