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.

12
  • You did not get an answer the last time you asked this? Commented Oct 27, 2015 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. Commented Oct 27, 2015 at 19:06
  • I actually did just find an answer to this question in another question though: stackoverflow.com/a/33288625/895065. Maybe some of these similar questions should be merged or deleted/edited? Commented Oct 27, 2015 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. Commented Oct 27, 2015 at 20:18
  • Well, you would just replace the characters with their complimenting character, then reverse the string. See this demo Commented Oct 27, 2015 at 21:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.