TASK

Step One

The DNA molecule is a double-helix. You can think of this double helix as two parallel sequences of DNA with nucleotides or bases (A,C,G,Ts) on one strand matched with their associated nucleotide on the other strand according to the following rules:

A matched with T
C matched with G

For example, imagine we have removed the double-helix and show the two strands in parallel; notice that every base ‘A’ on one strand matches a ‘T’ on the other strand and that every ‘C’ on one strand is matched with a ‘G’ on the other strand:

Strand 1: Read left to right
A C G T C C C G G A

T G C A G G G C C T

Strand 2 : read right to left

Databases that store the DNA sequence of an organism typically only store one of the two strands of the DNA to save room. Since the two DNA strands are complements of one another, it is “easy” to get the second complementary strand if you have the original strand. If a base on strand 1 is an ‘A’, then the base on the other strand must be a ‘T’. Note however, that strand 1 begins at the left and is read from the left to the right: “ACGTC…” but strand #2 begins at the right and is read from the right to the left: “TCCGG…”. Because the strands are read in “reverse” order and they have bases that are “complements” of one another, the strands are commonly called “reverse complements”.

This exercise involves writing a C++ program to accept a file of DNA sequence via prompt input. The aim is to produce the reverse complement strand of DNA.

For example, if the starting file called ecoli.fna contained the sequence:

ACTTGGCC<newline>
GGTAC<newline>

your C++ program, when run as shown below, should produce the reverse complement of this sequence to standard output:

$ ./revComp
Enter Filename to read: ecoli.fna
GTACCGGCCAAGT

Notice that the reverse complement should be written to standard output without any newlines. You can assume that the input file will have only uppercase letters (A, C, G, or T) and will not have a starting header line.

To implement this, write a function called reverse and place its implementation in the source file dna.cpp and prototype in dna.h. The function should take an open input file stream and print the compliment to standard output.

Write a suitable main function (driver) in main.cpp which prompts the user for a filename, as illustrated above, opens the file and invokes the reverse function. Once complete the main function should close the stream. If for some reason the input file stream cannot be opened the main function should terminate with an error message. You can assume a DNA strand is no more than 3000 characters long.

Step Two

In the first step, your program computed the reverse compliment of the DNA strand. Add another function called write in the source file dna.cpp (along with appropriate prototype in dna.h) that writes the reverse compliment back to a file. The function just like reverse should take an open output file stream and write the reverse compliment to the file. You should modify the program so that main prompts for the filename to output the reverse compliment to. If for some reason the file cannot be written, print an appropriate error message. You need not write newlines out to the file.

An example of what should happen if everything work:

$ ./revComp
Enter Filename to read: ecoli.fna
GTACCGGCCAAGT
Enter Filename to write: ecoli-rev.fna
File written – done.

An example of what would happen on an error:

$ ./revComp
Enter Filename to read: ecoli.fna
GTACCGGCCAAGT
Enter Filename to write: ecoli-rev.fna
File can not be opened for writing – exiting.

My codes for main.cpp

#include <iostream>
#include <fstream>
#include <cstring>
#include "dna.h"

using namespace std;

void reverse( char x [3000],char y [10]);

int main()
{
    char words[3000];
    char filename[10];
    cout<<"Enter filename : ";
    cin>>filename;
    reverse(words,filename);

    return 0;
}

This is my code for dna.cpp

#include <iostream>
#include <fstream>
#include "dna.h"
using namespace std;

void reverse(char x[3000] , char y[10] )
{   
    int i = 0;
    ifstream infile;
    infile.open(y,ios::in);

    if(!infile)
    {
        cout<<"File not found "<<endl;
    }

    while(!infile.eof())
    {
        infile.getline(x,3000);
        i++;
        cout<<x<<endl;
    }

    infile.close();
    )
}

And lastly this is my header files

void reverse(char x[3000],char y[10])

Fa

Reply

l be appreciated, best with example codes and proper explanation to solve this question, ^_^!

re-read Stop 1 carefully, your reverse() function does not meet the stated specification.

The function should take an open input file stream

That means the function referse() should have this prototype:

void reverse(ifstream& infile);

Next: Unless you were told otherwise by your teacher you should use std::string instead of charcter arrays, using std::string simplifies everything because it is less error prone.

Your reverse function is incomplete, it does nothing with the data that is read from the file. After a line is read the function must compute the reverse complement and print it to stdout (the screen).

I am not allowed to use string a we do not learnt about it yet.btw i have edited my code to get the output but i have problems with the multi files like (dna.h and dna.cpp) , what kind of prototype and function i should have?

below is my main.cpp
Can i know what should be the content of my dna.h and dna.cpp?
Sample of codes would help.

#include <iostream>
#include <fstream>
#include <cstring>


using namespace std;


void reverse( char x [3000],char y [10] , char z [3000]);

int main()


    {


char str1[3000];
char str2[3000];
char filename[10];
cout<<"Enter filename : ";
cin>>filename;
reverse(str1,filename,str2);

return 0;


    }



void reverse(char x[3000] , char y[10] ,char z[3000])


    {   


    int i = 0;
    ifstream infile;
    infile.open(y,ios::in);

    if(!infile)
    {
        cout<<"File not found "<<endl;
    }

    while(!infile.eof())
    {
        infile>>x[i];
        i++;

    }
            cout<<x<<endl;
        for( int i = 0 ; i<16 ; i++)
    {
        if (x[i] == 'A')
        {
            z [i]= 'T';

        }

        if( x[i] == 'T')
        {
            z[i]='A';
        }

        if( x[i] == 'C')
        {
            z[i]='G';
        }

        if(x[i] == 'G' )
        {
            z[i]='C';
        }
    }

    cout<<endl;
    for(int i = 16 ; i>=0 ; i --)
    {   

        cout<<z[i];

    }

    infile.close();



    }

//End of program

this is the content in my text file

ACTTGGCC
GGTAC

so , if i compile the file that has the content above,i will get these output

Original output ( in a line) :ACTTGGCCGGTAC
After reverse compliment : GTACCGGCCAAGT

I guess i get the output right but how can i break them up into multiple files(dna.h and dna.cpp),im lacking experience as this is my first quention about multi source files, hope someone could help me out with it ^_^!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.