2

So the title might be a little misleading, but hear me out. It's not as simple as I worded it in the title.

So I have a string say,

String dna="ACTG";

I have to convert said string into it's complement form. To complement said string, I have to replace all occurrences of "A" to "T", "C" to "G", "T" to "A" and "G" to "C". So the complement of the String should look like this:

String dnaComplement="TGAC";

How do I do this properly? E.G.

String temp = dna.Replace("A", "T");
temp = temp.Replace("T", "A");
temp = temp.Replace("C", "G");
temp = temp.Replace("G", "C");

This would have the output:

TCTC

Which is wrong. I'm a beginner at C# and I know a little about programming with it. I'm used to using java.

qwerty
  • 2,065
  • 2
  • 28
  • 39
Glynn Bacanto
  • 439
  • 2
  • 6
  • 12
  • How are you complements defined? i.e. do you have complemets for letters other than A, T, C, G defined as well? – Michal Hosala Nov 26 '14 at 09:37
  • 1
    And because you can not program a simple beginner job now is "advanced"? OMG. Get real, please. The question is valid, but it is not advanced at all. – TomTom Nov 26 '14 at 09:37
  • Michal, they are the base pairs that form DNA. So this alphabet only has 4 letters. The pairing is how DNA can be replicated during cell division. – Loofer Nov 26 '14 at 11:22

8 Answers8

4

Something like:

String dna="ACTG";
String dnaComplement = "";

foreach(char c in dna)
{
  if (c == 'A') dnaComplement += 'T';
  else if (c == 'C') dnaComplement += 'G';
 // and so long
}
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
2

You can use dictionary of complements and then use Linq Select:

var complements = new Dictionary<char, char>
    {
        { 'A', 'T' },
        { 'C', 'G' },
        { 'G', 'C' },
        { 'T', 'A' },
    };

var original = "TGAC";
var transformated = string.Concat(original.Select(c => (complements.ContainsKey(c)) ? complements[c] : c));
Michal Hosala
  • 5,570
  • 1
  • 22
  • 49
1

Change the characters first to something else.

String temp = dna.Replace("A", "t");
temp = temp.Replace("T", "A");
temp = temp.Replace("t", "T");
temp = temp.Replace("C", "g");
temp = temp.Replace("G", "C");
temp = temp.Replace("g", "G");

This code from you:

String temp = dna.Replace("A", "T"); // A's are changed to T...
temp = temp.Replace("T", "A"); // ... but your just obtained 'T's are changed back to A
// ...
Sjips
  • 1,248
  • 2
  • 11
  • 22
1

I would do this:

var complement = dna.Select(c =>
{
    switch (c)
    {
        case 'A': return 'T';
        case 'T': return 'A';
        case 'C': return 'G';
        case 'G': return 'C';
        default: throw new ArgumentException(); // or whatever
    }
});

EDIT: With this you get an IEnumerbale<char>. To transform this back to a string see this Answer.

Community
  • 1
  • 1
Fabian S.
  • 909
  • 6
  • 20
1

I would do this:

var complements = new Dictionary<char, char>
{
    { 'A', 'T' },
    { 'C', 'G' },
    { 'G', 'C' },
    { 'T', 'A' },
};

string dna = "ACTG";

string dnaComplement =
    new string(
        dna
            .ToCharArray()
            .Select(x => complements[x])
            .ToArray());
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

Replace with unique tokens so for example A-->l, T-->m C-->n G-->o. Now you can substitute for each token without changing any other - so L-->T etc.

Simon N
  • 27
  • 3
0

Each of the answers above is correct and solves your problem, but if you're going to do a lot of replacing frequently, consider using a StringBuilder. string.Replace() returns a new string instance every time you call it, the old instance becomes garbage and has to be collected. This can lead to a performance penalty for your app.

user2936023
  • 131
  • 1
  • 6
0

Like Michal Hosala said, you have to make clear what is the definition of the complement. I will assume that you split the string in 2 equal parts and the first char in the first string is the complement of the first char in the second string.

I also treated the case when you have an odd number of chars, the complement of "ACXTG" will be "TGXAC".

string dna = "ACTG";
string dnaComplement = "";

if (dna.Length % 2 == 1)
    dnaComplement += dna[dna.Length / 2];
for (int i = 0; i < dna.Length / 2; i++)
    dnaComplement = dna[dna.Length - i - 1] + dnaComplement + dna[i];
Trivinel
  • 1
  • 1