0

Is there a way to use a javascript str.replace with a regular expression (RegEx) only to convert a DNA string to its (reverse) complement sequence?

This is a follow up to another question I asked about DNA complementary sequence conversion here. I am posting this separately because I am trying to learn more about Regular Expressions and how javascript works with them.

I found an answer to a similar question in JavaScript Regular Expression Replace Multiple Letters, but that question wasn't specifically asking for a RegEx and replacement string in lieu of a function as I am here.

Given a typical sequence of DNA bases as a string, the complement can be found by reversing the string, then performing the mapping of each base to its most energetically favorable hydrogen-bonding partner (A->T, T->A, C->G, G->C). For example, the complement of CTTAACCAGCGGACACGGGCTTGGC is GCCAAGCCCGTGTCCGCTGGTTAAG.

Here is one such algorithm in javascript given the reversed sequence reverseSeq:

complementSeq=reverseSeq.replace([/[ATCG]/g], function (match, p1, p2, p3, p4) {
            if (p1)
                return 'T';
            else if (p2)
                return 'A';
            else if (p3)
                return 'G';
            else if (p4)
                return 'C';
        }
        );

This script uses a function as the second argument to .replace. Could the same result be achieved with a new string as the call to .replace? If not, why not? Here is some documentation on string.replace and the syntax for using matches in the new string argument. I looked into conditional RegEx but don't see how it could be used for this purpose, i.e. telling .replace to use a T when it matches an A, etc.

Jesse W. Collins
  • 139
  • 1
  • 1
  • 12
  • You did not get an answer the last time you asked this? – epascarello Oct 27 '15 at 19:01
  • I got some answers to the other question I asked, which is basically what is the most efficient javascript algorithm for complement DNA finding. None of these produced or seemed to consider the second question I asked about using RegEx with a replacement string in particular, so I separated the 2 questions. – Jesse W. Collins Oct 27 '15 at 19:06
  • I actually did just find an answer to this question in another question though: http://stackoverflow.com/a/33288625/895065. Maybe some of these similar questions should be merged or deleted/edited? – Jesse W. Collins Oct 27 '15 at 19:08
  • 1
    @jwco -- the "duplicate" question does not request reversal of the string... this is a different question IMO. It asks (maybe not clearly enough??) "given `GCAGGACTTTTTA`, can you provide the reversal _and complement_ string (`TAAAAAGTCCTGC`), using only string.replace with regex (_not a function_) -- I think the answer would be no - you must use a function (or other algorithm) to reverse the string. – Code Jockey Oct 27 '15 at 20:18
  • Well, you would just replace the characters with their complimenting character, then reverse the string. See [this demo](http://jsbin.com/rudakeqofi/1/edit?js,console) –  Oct 27 '15 at 21:01
  • A simple for loop would probably be much faster. – royhowie Oct 27 '15 at 22:19
  • @CodeJockey This is not to object to your hypothesis (clearly you know way more about RegEx than me) but this article makes me wonder: http://stackoverflow.com/questions/3693698/how-does-this-regex-replacement-reverse-a-string – Jesse W. Collins Oct 27 '15 at 22:30
  • Hey, my demo is RegEx-ish. I don't understand what more you want, the demo provides the correct output does it not? –  Oct 27 '15 at 22:32
  • @TinyGiant - that is essentially in his other (linked) question. Using a map of replacements, and splitting/reversing/joining the string. Also, you can't get credit (if that's what you're after) by leaving a comment - only an answer - and this question was closed as a duplicate (though I still disagree, my voice is only so loud [and that's just fine]) – Code Jockey Oct 28 '15 at 13:03
  • @jwco -- though I haven't personally confirmed that the linked algorithm works, and though it does _appear_ to produce a single regex string that is then used for reversal, I can see that it produces a regex expression which uses lookbehind. JavaScript (ECMAScript) regular expressions do not support lookbehind, so... I still think it's not possible with pure JS regex, but I COULD(?) indeed be wrong? It'll be nasty and probably need a few blog posts to go through, though, I predict :-) – Code Jockey Oct 28 '15 at 13:09
  • @jwco -- upon closer inspection, I notice also that both look behinds in the final expression are variable-length, at least one of which looks necessary for the reversal functionality. among the languages that support look-behind, only a few support variable length look-behind. Java, last I checked, does not support non-finite-variable-length lookbehind, so I wonder if that example will run(?!) maybe recent versions? – Code Jockey Oct 28 '15 at 13:34
  • @CodeJockey 1) I know, and 2) I've got 7k+ rep, so yeah I know. –  Oct 28 '15 at 13:40

0 Answers0