0

The genetic code is the set of rules used by living cells to translate information encoded within genetic material (DNA or mRNA sequences of nucleotide triplets, or codons) into proteins. The genetic code is highly similar among all organisms and can be expressed in a simple table with 64 entries. A three-nucleotide codon in a nucleic acid sequence specifies a single amino acid. The vast majority of genes are encoded with a single scheme often referred to as the genetic code (refer to the codon table). Attached to this assignment, you will find a text file named “mouse.dat” that contains the complete genome of a mouse. Write a program to read in the DNA sequence from the file, calculate the frequency of each codon in the codon table, and print out the result as a number and a percentage. (a) Write a serial code for the solution "Normal Code by using C++ Language".

When I compiled the above code I got the below error message "Unable to open file mouse.dat: No such file or directory"

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    std::vector<string> codons = { "ttt" }; // Better always initialize any variable or array or objects to zero or NULL or empty string.
    codons.push_back("ttc"); // { "ttt", "ttc"
    codons.push_back("tta"); // { "ttt", "ttc", "tta"
    codons.push_back("ttg"); // { "ttt", "ttc", "tta", ...
    codons.push_back("tct");
    codons.push_back("tcc");
    codons.push_back("tca");
    codons.push_back("tcg");
    codons.push_back("tat");
    codons.push_back("tac");
    codons.push_back("taa");
    codons.push_back("tag");
    codons.push_back("tgt");
    codons.push_back("tgc");
    codons.push_back("tga");
    codons.push_back("tgg");
    codons.push_back("ctt");
    codons.push_back("ctc");
    codons.push_back("cta");
    codons.push_back("ctg");
    codons.push_back("cct");
    codons.push_back("ccc");
    codons.push_back("cca");
    codons.push_back("ccg");
    codons.push_back("cat");
    codons.push_back("cac");
    codons.push_back("caa");
    codons.push_back("cag");
    codons.push_back("cgt");
    codons.push_back("cgc");
    codons.push_back("cga");
    codons.push_back("cgg");
    codons.push_back("att");
    codons.push_back("atc");
    codons.push_back("ata");
    codons.push_back("atg");
    codons.push_back("act");
    codons.push_back("acc");
    codons.push_back("aca");
    codons.push_back("acg");
    codons.push_back("aat");
    codons.push_back("aac");
    codons.push_back("aaa");
    codons.push_back("aag");
    codons.push_back("agt");
    codons.push_back("agc");
    codons.push_back("aga");
    codons.push_back("agg");
    codons.push_back("gtt");
    codons.push_back("gtc");
    codons.push_back("gta");
    codons.push_back("gtg");
    codons.push_back("gct");
    codons.push_back("gcc");
    codons.push_back("gca");
    codons.push_back("gcg");
    codons.push_back("gat");
    codons.push_back("gac");
    codons.push_back("gaa");
    codons.push_back("gag");
    codons.push_back("ggt");
    codons.push_back("ggc");
    codons.push_back("gga");
    codons.push_back("ggg"); // // { "ttt", "ttc", "tta", ..., "ggg"}
    // codons.size() is 64
    vector<int> counts(64, 0);
    string line = ""; // Always initialize.
    // int numberOfLines=0; // warning: unused variable numberOfLines
    char my_character = '\0'; // Always initialize.
    for (int indx = 0; 64 > indx; indx++) // Better compare using "number comparison variable" way
    {
        string codon_req = codons[indx];
        ifstream myfile("mouse.dat");
        if (myfile.is_open())
        {
            int cnt = 0, ans = 0;
            while (!myfile.eof())
            {
                myfile.get(my_character);
                // If number of count "cnt" becomes 3 reinitialize that to zero.
                // and increase "ans" count
                if (3 == cnt)
                {
                    ans++;
                    cnt = 0;
                }
                if ('\n' == my_character)
                {
                    continue;
                }
                // Here comparison is not done sequential
                // Search if first charater (example t at ttt) is present
                // increase cnt
                // Next time if it is not present
                // compare until we find next letter t
                // if found increase cnt at that time.
                // Hence ans count is more greater than expected count on word ttt
                // NOT SURE ON YOUR PROJECT REQUIREMENT.
                if (my_character == (char)codon_req[cnt])
                {
                    cnt++;
                }
            }
            myfile.close();
            counts[indx] = ans;
        }
        else
        {
            perror("Unable to open file mouse.dat");
            exit(1);
        }
    }
    for (int indx = 0; 64 > indx; indx++) //// Better compare using "number comparison variable" way
    {
        cout << "Before counts[indx] " << counts[indx] << "\n";
        codons[indx] = codons[indx] + " " + to_string(counts[indx]);
        cout << "After  counts[indx] " << counts[indx] << "\n";
    }
    ofstream newFile("results.txt");
    if (newFile.fail())
    {
        perror("Opening results.txt file failed");
        exit(2);
    }
    else
    {
        for (int indx = 0; 64 > indx; indx++) /// Better compare using "number comparison variable" way
        {
            newFile << codons[indx] << '\n';
        }
        newFile.close();
    }
    return 0;
}

END

yazanjs
  • 1
  • 2
  • Do I have to save the file mouse.dat in specific location or no needs – yazanjs Mar 10 '20 at 22:38
  • `mouse.dat` should be in the folder where your program is run. – Yksisarvinen Mar 10 '20 at 22:46
  • I'm using MS Visual Studio 2019 on windows , Please let me know the location for program – yazanjs Mar 10 '20 at 22:53
  • From the comments you seem to understand that you can `std::vector codons = { "ttt" , "ttc", "tta", ... };` and save a buttload of typing, so I'm curious. Why did you set up `codons` the hard way? – user4581301 Mar 10 '20 at 22:56
  • Recommended reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). If it doesn't save you any debugging now, it will later. – user4581301 Mar 10 '20 at 22:57
  • Some reading on finding the directory Visual Studio is running your program from: [C++ Visual Studio Current Working Directory](https://stackoverflow.com/questions/19691058/c-visual-studio-current-working-directory) and [Visual Studio working directory](https://stackoverflow.com/questions/23458292/visual-studio-working-directory) – user4581301 Mar 10 '20 at 23:03
  • After I copied mouse.dat file to project folder, the code is working and I can read the data from file, Now I need assistance to customize the code to print out the result as a number and a percentage for each codon from results.txt (which already generated by code and includes the frequency number for each codon) – yazanjs Mar 10 '20 at 23:07
  • Any update how to customize code to print frequency number and percentage – yazanjs Mar 11 '20 at 14:20

0 Answers0