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.