trouble appending select elements of an array to a string then printing in RichTextBo
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: trouble appending select elements of an array to a string then printing in RichTextBo

  1. #1
    Join Date
    Oct 2012
    Posts
    14

    trouble appending select elements of an array to a string then printing in RichTextBo

    I'm trying to recreate a program I made with C++ int a C# windows form program I have the bulk of it done but there's one small thing thats 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
    Code:
        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
    Code:
        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
    Code:
       
        protein.append(aminoAcids[k]);
    				
    			}
    			
    		}
    		beginIndex+=3;
    	}
    	cout<<protein<<endl;
    	protein.clear();
    heres what I have in c# so far
    Code:
        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******

    Code:
        // 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)*******
    Code:
                         
        //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???

    heres the C++ code in full
    Code:
    #include<iostream>
    #include <string>
    #include<fstream>
    using namespace std;
    
    int main()
    {
    	
    	int numberOfCodons;
    	int rnaLength;
    	int beginIndex;
    	int endIndex;
    	int i,j,k;
    	string codons[64] = {"UUU","UUC","UUA","UUG","UCU","UCC","UCA","UCG","UAU","UAC","UAA","UAG","UGU","UGC","UGA","UGG", 
    						 "CUU","CUC","CUA","CUG","CCU","CCC","CCA","CCG","CAU","CAC","CAA","CAG","CGU","CGC","CGA","CGG",
    						 "AUU","AUC","AUA","AUG","ACU","ACC","ACA","ACG","AAU","AAC","AAA","AAG","AGU","AGC","AGA","AGG",
    						 "GUU","GUC","GUA","GUG","GCU","GCC","GCA","GCG","GAU","GAC","GAA","GAG","GGU","GGC","GGA","GGG"};
    	string aminoAcids[64]={"Phenylalanine","Phenylalanine","Leucine","Leucine", "Serine","Serine","Serine","Serine","Tyrosine","Tyrosine", "Stop","Stop", "Cysteine","Cysteine","Stop", "Tryptophan",
    						   "Leucine","Leucine","Leucine","Leucine","Proline","Proline","Proline","Proline","Histidine","Histidine","Glutamine","Glutamine","Arginine","Arginine","Arginine","Arginine",
    						   "Isoleucine","Isoleucine","Isoleucine","Methionine","Threonine","Threonine","Threonine","Threonine","Asparagine","Asparagine","Lysine","Lysine","Serineine","Serineine","Arginine","Arginine",
    							"Valine","Valine","Valine","Valine","Alanine","Alanine","Alanine","Alanine","Aspartate","Aspartate","Glutamate","Glutamate","Glycine","Glycine","Glycine","Glycine"};
    	string codon="";
    	string protein="";
    	string RNA;
    	cout<<"   To begin translation    "<<endl;
    	cout<<"Please enter RNA sequence: "<<endl;
    
    	cin>>RNA;
    	cout<<endl;
    	rnaLength=RNA.length();
    	for(int i=0; i<RNA.length(); i+=3){
        
        cout<<RNA[i];
    	if(i+1 < RNA.length())
            cout<<RNA[i+1];
    	if(i+2 < RNA.length())
            cout<<RNA[i+2];
    		cout<<" ";
    }
    	cout<<endl;
    	numberOfCodons=rnaLength/3;
    	beginIndex=0;
    	cout<<"Total Number Of codons are: "<<numberOfCodons<<endl;
    	for (i=0; i<numberOfCodons;i++)
    	{
    		
    		endIndex=beginIndex+3;
    		codon="";
    		
    		{
    			codon.append(RNA.substr(beginIndex,endIndex-beginIndex));
    
    		}
    		for (k=0;k<64;k++)
    		{
    			if(codon==codons[k])
    			{
    				protein.append(aminoAcids[k]);
    				
    			}
    			
    		}
    		beginIndex+=3;
    	}
    	cout<<protein<<endl;
    	protein.clear();
    	
    	/*fstream file;//declaring an object called file (the object is like the file key)
    	file.open("IOResults.txt",ios::out);//file object used to open and create a file called IOResults (can be open and read from sub-function 2)
    	file<<RNA[i]<<endl;
    	file<<codon<<endl;
    	file<<numberOfCodons<<endl;
    	file<<protein<<endl;
    	file.close();//object used to close the file*/
    
    	system("PAUSE");
    		return 0;
    }
    heres the C# code in full
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    
    namespace bioinformaticsCal
    {
    
        public partial class Form1 : Form
        {
            int numberOfCodons;
    		int rnaLength;
    		int beginIndex;
    		int endIndex;
    		int i;
    		int j;
    		int k;
    		string[] codons = {"UUU","UUC","UUA","UUG","UCU","UCC","UCA","UCG","UAU","UAC","UAA","UAG","UGU","UGC","UGA","UGG", "CUU","CUC","CUA","CUG","CCU","CCC","CCA","CCG","CAU","CAC","CAA","CAG","CGU","CGC","CGA","CGG", "AUU","AUC","AUA","AUG","ACU","ACC","ACA","ACG","AAU","AAC","AAA","AAG","AGU","AGC","AGA","AGG", "GUU","GUC","GUA","GUG","GCU","GCC","GCA","GCG","GAU","GAC","GAA","GAG","GGU","GGC","GGA","GGG"};
    		string[] aminoAcids = {"Phenylalanine","Phenylalanine","Leucine","Leucine", "Serine","Serine","Serine","Serine","Tyrosine","Tyrosine", "Stop","Stop", "Cysteine","Cysteine","Stop", "Tryptophan", "Leucine","Leucine","Leucine","Leucine","Proline","Proline","Proline","Proline","Histidine","Histidine","Glutamine","Glutamine","Arginine","Arginine","Arginine","Arginine", "Isoleucine","Isoleucine","Isoleucine","Methionine","Threonine","Threonine","Threonine","Threonine","Asparagine","Asparagine","Lysine","Lysine","Serineine","Serineine","Arginine","Arginine", "Valine","Valine","Valine","Valine","Alanine","Alanine","Alanine","Alanine","Aspartate","Aspartate","Glutamate","Glutamate","Glycine","Glycine","Glycine","Glycine"};
    		string codon = "";
    		string protein = "";
    		string RNA;
    
            public Form1()
            {
                InitializeComponent();
            }
            
           List<sampleInfo> Strings = new List<sampleInfo>();  
            private void Form1_Load(object sender, EventArgs e)
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                if (!Directory.Exists(path + "\\Bioinformatics Data Files - Jason"))
                Directory.CreateDirectory(path + "\\Bioinformatics Data Files - Jason");
                if(!File.Exists(path + "\\Bioinformatics Data Files - Jason\\settings.xml"))
                File.Create((path + "\\Bioinformatics Data Files - Jason\\settings.xml"));
                File.Create((path + "\\Bioinformatics Data Files - Jason\\TextFiles.txt"));
                
            }
    
           
    
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog Fd = new OpenFileDialog();
                if (Fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    StreamReader Sr = new StreamReader(File.OpenRead(Fd.FileName));
                            RNA =Sr.ReadToEnd();               
                            rnaLength = RNA.Length;
                for (int i = 0; i < RNA.Length; i += 3)
                {
                    
                    richTextBox1.AppendText(RNA[i].ToString());
                    if (i + 1 < RNA.Length)
                    {
                     richTextBox1.AppendText(RNA[i + 1].ToString());
                    }
                    if (i + 2 < RNA.Length)
                    {
                     richTextBox1.AppendText(RNA[i + 2].ToString());
                    }
                    richTextBox1.AppendText(" ");
                }
               
                
             }
              
          }
            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 = "";
                    
                    {
                        //supposed to print all the coresponding codons in entirety***********************************************************
                       // 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************************************************
                            //protein.AppendText(aminoAcids[k]);
                            protein = (aminoAcids[k]);
                        }
    
                    }
                    beginIndex += 3;
                }
                richTextBox2.AppendText(protein);
                richTextBox2.AppendText("\n");
                //protein.clear();   
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
    
            }
    
            private void Reset(object sender, EventArgs e)
            {
               
            }
    
            private void richTextBox3_Load(object sender, EventArgs e)
            {
                string[] words =
    	    {
    		"To begin process ", "Either (A) Enter DNA or RNA sequence", "Or (B) open File dialog and select file" 
    		
    	    };
    
                for (int i = 0; i < words.Length; i++)
                {
                    string word = words[i];
    
                    {
    
                        richTextBox3.AppendText(word);
    
                        richTextBox3.AppendText("\r\n");
    
                        
                    }
    
                }
            }
    
        }
            class sampleInfo 
            {
                public string sampleSource
                {
                    get;
                    set;
                }
                public string sampleReference
                {
                    get;
                    set;
                }
                public string sampleType
                {
                    get;
                    set;
                }
                public string fileName
                {
                    get;
                    set;
    
                }
                public string fileContent
                {
                    get;
                    set;
                }
                public string conversionResults
                {
                    get;
                    set;
                }
                public string richTextBox3
                {
                    get;
                    set;
                }
            }
    }

  2. #2
    Join Date
    Jan 2010
    Posts
    1,099

    Re: trouble appending select elements of an array to a string then printing in RichTe

    I didn't try and run the code, but I think I know where's the problem.

    In C++ you're doing this:
    Code:
    codon.append(RNA.substr(beginIndex,endIndex-beginIndex));
    
    // and then later...
    
    protein.append(aminoAcids[k]);
    So, in the code above, you are appending the new string to the existing cumulative string.
    In C#, though, you're doing this:
    Code:
    codon=(RNA.Substring(beginIndex, endIndex - beginIndex));
    
    // and later...
    
    protein = (aminoAcids[k]);
    This simply replaces the entire string with a new one, which is why you end up with only one codon and one amino acid - the one which was the last in the sequence entered (right?).
    You can use the += operator, which will append the string instead.

    An expression of the form
    someString += otherString;
    is simply a shorthand for:
    someString = someString + otherString;


    So you can try this (BTW, the outer "(" and ")" are not required):
    Code:
    codon += RNA.Substring(beginIndex, endIndex - beginIndex);
    
    // and later...
    
    protein += aminoAcids[k];
    Or, if you need some kind of a separator in between:
    Code:
    codon += RNA.Substring(beginIndex, endIndex - beginIndex) + " ";
    
    // and later...
    
    protein += aminoAcids[k] + " ";
    Also, make sure that there aren't any more errors of the same sort in other parts of the program, since I may have missed them.

  3. #3
    Join Date
    Oct 2012
    Posts
    14

    Re: trouble appending select elements of an array to a string then printing in RichTe

    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center