1

I'm trying to recreate a program I made with C++ into a C# windows form program I have the bulk of it done. There's one small thing that's keeping the program from working like it should.

My program is a bionformatics program that allows the user to either enter a string/sequence of DNA or RNA characters and the program converts it into the corresponding protein/amino acid and prints out an amino acid/protein for every codon the program sees. So if I input "AAA GGG CCC" it prints out "Lysine Glycine Proline".

This is the snippet of code I'm having trouble with in the C++ version

for (i=0; i<numberOfCodons;i++)
{
 endIndex=beginIndex+3;
 codon="";
 {
 //here is where I'm having the trouble converting this to C# and have it cout the write
 //way 
 codon.append(RNA.substr(beginIndex,endIndex-beginIndex));
 }
 for (k=0;k<64;k++)
 {
 if(codon==codons[k])
 {
 //here is where I'm having the trouble converting this to C# and have it cout the write way
 //like I metioned previously AAA GGG CCC couts Lysine Glycine Proline 
 protein.append(aminoAcids[k]); 
 } 
 }
 beginIndex+=3;
}
cout<<protein<<endl;
protein.clear();

here's what I have in c# so far

private void Tranlate_Click(object sender, EventArgs e)
{
 numberOfCodons = rnaLength / 3;
 beginIndex = 0;
 richTextBox2.AppendText("Total Number Of codons are: ");
 richTextBox2.AppendText(numberOfCodons.ToString());
 richTextBox2.AppendText("\n");
 for (i = 0; i < numberOfCodons; i++)
 {
 endIndex = beginIndex + 3;
 codon = ""; 
 {
 // these are the two possible conversions of the C++ code that dont work at all for me******
 // codon.AppendText(RNA.Substring(beginIndex, endIndex - beginIndex));
 codon=(RNA.Substring(beginIndex, endIndex - beginIndex));
 }
 for (k = 0; k < 64; k++)
 {
 if (codon == codons[k])
 {
 //supposed to print out all the coresponding amino acids from the array and it will only print out one amino acid (Lysine)*******
 //protein.AppendText(aminoAcids[k]);
 protein = (aminoAcids[k]);
 }
 }
 beginIndex += 3;
 }
 richTextBox2.AppendText(protein);
 richTextBox2.AppendText("\n");
 //protein.clear();
}

Why is it doing this and how can I fix it?

CodesInChaos
109k26 gold badges224 silver badges268 bronze badges
asked Oct 13, 2012 at 12:18

2 Answers 2

2

Try to change this line inside your loop

protein = (aminoAcids[k]); 

to

protein += (aminoAcids[k]); 

The C++ version loops for 64 times and append to a string, the C# version reinitialize the string every time and ends with the last match found by the comparison if (codon == codons[k]).

The same happens with the codon string
This reinitialize it in every loop

codon=(RNA.Substring(beginIndex, endIndex - beginIndex)); 

but, in this case, I'm not sure if it is right to build a single string and then check against a string array like codons[k]

PS. While they are ininfluent on the code generated, I find your liberal use of parenthesys a bit distracting. You could write aminoAcids[k]; and codon=RNA.Substring(beginIndex, endIndex - beginIndex);. This is (in my opinion) more clear.

answered Oct 13, 2012 at 12:30
Sign up to request clarification or add additional context in comments.

2 Comments

Also, consider using StringBuilder
@GiladNaaman, not sure if, in this case, the usage of StringBuilder is really effective. See here dotnetperls.com/stringbuilder-performance .
1

Your code is a nice candidate for LINQ which is widely used in C#. If I understood the logic correctly, you could write something like:

richTextBox2.AppendText(string.Join("\n",
 Enumerable.Range(0, numberOfCodons)
 .Select(i => RNA.Substring(i * 3, i * 3 + 3))
 .Where(c => codons.Contains(c))
 .Select(c => aminoAcids[codons.IndexOf(c)])
 .ToArray())
 );
answered Oct 13, 2012 at 13:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.