Need help with a DNA to mRNA transcription code.

I have a project where i have to read a DNA sequence from a file and print to the console its mRNA counterpart. Here is the question:

Your task is to write a program called transcriptase.cpp that reads a text file called dna.txt that contains one DNA strand per line, which looks as follows:

AAGATGCCG
ATGCCGTAAGATGCGGTAAGATGC
CCGTAAGATGCCGTA
. . .
and outputs to the console (terminal) the corresponding mRNA strands. Each output line must contain exactly one mRNA strand. This is a sample output of the program:

$ ./transcriptase
UUGUACGGC
UACGGCAUUCUACGCCAUUCUACG
GGCAUUCUACGGCAU
. . .

The best way to do this is in two steps. First create a function that gives the complement of a base, and then write another function that uses it iteratively over a whole strand. For example, we could have char DNAbase_to_mRNAbase(char) to return the complement of a base and string DNA_to_mRNA(string) that uses it for each base in the strand. Note that the output must be in capital letters, regardless of how the input is formatted. To do this, you may include the <cstdlib> and use int toupper(int c), which returns the upper case of any alpha character passed to it.

I have some code, but I am very confused on how to approach this question and how to properly put into syntax the solution.

#include <iostream>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <climits>
#include <string>
using namespace std;

char setRNA(char base);
string newStrand(string strand);

int main()
{
char pair;
string newPair;
ifstream fin("dna.txt"); //Read the file
if (fin.fail())
{
cerr << "File cannot be read, opened, or does not exist.\n";
exit(1);
}
string strand;
while(getline(fin, strand)) //Reads file line by line
{
cout << newStrand(strand);
}

fin.close();
}

char setRNA(char base) //Function to convert the DNA bases to mRNA bases.
{
char mRNA;
base = toupper(base); //If the character is something, change to its compliment
if (base == 'A')
{
mRNA == 'U';
}
if (base == 'T')
{
mRNA == 'A';
}
if (base == 'G')
{
mRNA == 'C';
}
if (base == 'G')
{
mRNA == 'C';
}
return base;
}

string newStrand(string strand) //Function to print the new mRNA strand.
{
string newRNA = " ";
char nucleotide;
int s = strand.length();
for (int i = s; i > 0; i--) //For loop to put the changed bases into one strand.
{
newRNA += setRNA(nucleotide);
}
return newRNA;
}

Any help will be very much appreciated
Last edited on
Please edit your post to put [code][/code] tags around the code.

> mRNA == 'C';
You're mixing up comparison(==) with assignment (=).

> return base;
return mRNA;
would be a better idea.
GNU coreutils has command 'tr'. If you call it:
tr AUCGaucg TAGCTAGC < dna.txt

you should get same result as with your program.

The tr knows nothing about DNA or RNA; it simply manipulates characters.

The instructions are verbose, but the core logic is:
FOR EACH character (c) in input
  show complement of c

The complement in turn:
IF c is one of the bases
THEN c = complement of c

return c

You can use if .. else if .. or switch. (There are fancier ways too, but they are overkill for four base problem.)
Topic archived. No new replies allowed.