This program takes a simple nucleotide sequence and finds the most common k-mers in the sequence, as determined by the supplied dataset (see below). The goal of the program is to find the origin of replication of a DNA sequence, as it is presumably the most common sequence, or in other words, the substring with the most occurrences.#include <iostream>#include <string>#include <fstream>#include <stdlib.h>#include <map>//-------------------------------------------//FUNCTION PROTYPES//Dataset files include k in them//void chooseK();//Open file and read sequencevoid enterFileToOpen();void openFile();void readFile();//Generate substrings and save them to filevoid enterOFileName();void openOFile();void printSubstrToFile();//Count substrings and determine modal substrings//Step 1: Open substring output file//Step 2: Read and aggregate substringsvoid setOutPutReadFiletoOutputFile();void openOutputFile();void countSubStrings();//---------------------------------//VARIABLESstd::string sequence;int k;int kmerCount;int i=0;std::string subStrPrint;std::ifstream inFile;std::string fileName;std::ofstream outFile;std::string ofileName;std::ifstream outputReadFile;std::string outputReadFileName;std::map<std::string, int> subSequenceCountMap;std::string stringMapElement;//------------------------------------------int main(){    enterFileToOpen();    openFile();    readFile();    enterOFileName();    openOFile();    printSubstrToFile();    setOutPutReadFiletoOutputFile();    openOutputFile();    countSubStrings();    inFile.close();    outFile.close();    return 0;}void enterFileToOpen(){    std::cout << Please enter file to open: (remember to include full filepath if file is not located within this program's directory) << std::endl;    std::cin >> fileName;    std::cout << File:  << fileName << std::endl;}void openFile(){    inFile.open(fileName.c_str());    //Check for Error during file open process    if(inFile.fail())    {        std::cerr << Error during Open File Process << std::endl;        exit(1);    }}void readFile(){    inFile >> sequence >> k;    std::cout << \nNucleotide sequence:  << sequence << \n << std::endl;    std::cout << k =  << k << std::endl;}void enterOFileName(){    std::cout << Please enter name for nucleotide subsequence output file: ;    std::cin >> ofileName;    std::cout << Nucleotide subsequence output file:  << ofileName << std::endl;}void openOFile(){    outFile.open(ofileName.c_str());}void printSubstrToFile(){    //variables declared globally: kmerCount, i, subStrPrint    kmerCount = sequence.length()-k;    i = 0;    for(i=0; i<kmerCount; i++)    {        subStrPrint = sequence.substr(i,k);        outFile << subStrPrint << std::endl;        std::cout << Iteration  << i+1 << : sub-sequence  << subStrPrint << std::endl;    }    outFile.close();    inFile.close();}void setOutPutReadFiletoOutputFile(){    //No need to specify file path for read file, since we just created it    outputReadFileName = ofileName;}void openOutputFile(){    inFile.open(outputReadFileName.c_str());    //Check for Errors    if(inFile.fail())    {        std::cerr << Error during File Open << std::endl;        exit(1);    }    else    {        std::cout << \nFile opened successfully\n << std::endl;    }}void countSubStrings(){    i = 0;    for(i = 0; i < kmerCount; i++)    {        inFile >> stringMapElement;        if(subSequenceCountMap.find(stringMapElement)!=subSequenceCountMap.end())        {            subSequenceCountMap[stringMapElement]++;        }        else        {            subSequenceCountMap[stringMapElement]=1;        }    }    for (std::map<std::string, int>::iterator itr = subSequenceCountMap.begin(); itr!=subSequenceCountMap.end(); ++itr) //changed const_iterator to iterator, cbegin to begin, cend to end    {        if(itr->second >1)        {            std::cout << itr->first <<  occurs  << itr->second <<  times << std::endl;        }        else        {            std::cout << itr->first <<  occurs  << itr->second <<  time << std::endl;        }    }}Sample dataset:TGAATAGGAATCCCAACGTCTTGTGAATAGGAATCCCAAGAATCCCAAGAATCCCAAGAATCCCAAGAATCCCAACGTCTTGTGCAAGGTGCAAGGGAATCCCAAGAATCCCAACGTCTTGTGCAAGGGAATCCCAACGTCTTGTGGCCTAGAATCCCAACGTCTTGGAATCCCAACGTCTTGGAATCCCAATGCAAGGGAATCCCAACGTCTTGTGCAAGGTGCAAGGTGAATAGCGTCTTGGAATCCCAATGCAAGGTGGCCTATGCAAGGTGAATAGTGAATAGTGGCCTATGGCCTATGCAAGGTGCAAGGTGAATAGTGCAAGGGAATCCCAAGAATCCCAATGAATAGGAATCCCAAGAATCCCAATGGCCTATGGCCTACGTCTTGTGCAAGGTGGCCTACGTCTTGGAATCCCAATGCAAGGGAATCCCAATGAATAGTGCAAGGGAATCCCAAGAATCCCAATGCAAGGTGGCCTATGCAAGGCGTCTTGCGTCTTGTGGCCTATGCAAGGGAATCCCAATGCAAGGGAATCCCAATGCAAGGTGCAAGGTGCAAGGGAATCCCAACGTCTTGGAATCCCAACGTCTTGCGTCTTGGAATCCCAAGAATCCCAATGGCCTACGTCTTGGAATCCCAATGCAAGGTGAATAGTGAATAGGAATCCCAATGGCCTACGTCTTGTGGCCTATGAATAGTGAATAGTGGCCTAGAATCCCAATGAATAGGAATCCCAATGGCCTACGTCTTGTGCAAGGTGCAAGGTGGCCTACGTCTTGCGTCTTGGAATCCCAAGAATCCCAATGCAAGGTGGCCTATGGCCTATGAATAGTGCAAGGGAATCCCAACGTCTTGCGTCTTGCGTCTTGGAATCCCAA12When this file is entered into the program, the program will generate every possible substring of length 12 and calculate the occurrence of each distinct one.Sample data set output:AACGTCTTGCGT occurs 2 timesAACGTCTTGGAA occurs 3 timesAACGTCTTGTGA occurs 1 timeAACGTCTTGTGC occurs 3 timesAACGTCTTGTGG occurs 1 timeAAGAATCCCAAC occurs 2 timesAAGAATCCCAAG occurs 3 timesAAGAATCCCAAT occurs 5 timesAAGGCGTCTTGC occurs 1 timeAAGGGAATCCCA occurs 10 timesAAGGTGAATAGC occurs 1 timeAAGGTGAATAGT occurs 3 timesAAGGTGCAAGGG occurs 2 timesAAGGTGCAAGGT occurs 4 timesAAGGTGGCCTAC occurs 2 timesAAGGTGGCCTAT occurs 3 timesAATAGCGTCTTG occurs 1 timeAATAGGAATCCC occurs 5 timesAATAGTGAATAG occurs 3 timesAATAGTGCAAGG occurs 3 timesAATAGTGGCCTA occurs 2 timesAATCCCAACGTC occurs 10 timesAATCCCAAGAAT occurs 10 timesAATCCCAATGAA occurs 3 timesAATCCCAATGCA occurs 8 timesAATCCCAATGGC occurs 4 timesAATGAATAGGAA occurs 2 timesAATGAATAGTGC occurs 1 timeAATGCAAGGGAA occurs 3 timesAATGCAAGGTGA occurs 1 timeAATGCAAGGTGC occurs 1 timeAATGCAAGGTGG occurs 3 timesAATGGCCTACGT occurs 3 timesAATGGCCTATGG occurs 1 timeACGTCTTGCGTC occurs 3 timesACGTCTTGGAAT occurs 5 timesACGTCTTGTGAA occurs 1 timeACGTCTTGTGCA occurs 5 timesACGTCTTGTGGC occurs 2 timesAGAATCCCAACG occurs 3 timesAGAATCCCAAGA occurs 3 timesAGAATCCCAATG occurs 6 timesAGCGTCTTGGAA occurs 1 timeAGGAATCCCAAC occurs 1 timeAGGAATCCCAAG occurs 2 timesAGGAATCCCAAT occurs 2 timesAGGCGTCTTGCG occurs 1 timeAGGGAATCCCAA occurs 10 timesAGGTGAATAGCG occurs 1 timeAGGTGAATAGTG occurs 3 timesAGGTGCAAGGGA occurs 2 timesAGGTGCAAGGTG occurs 4 timesAGGTGGCCTACG occurs 2 timesAGGTGGCCTATG occurs 3 timesAGTGAATAGGAA occurs 1 timeAGTGAATAGTGG occurs 2 timesAGTGCAAGGGAA occurs 3 timesAGTGGCCTAGAA occurs 1 timeAGTGGCCTATGG occurs 1 timeATAGCGTCTTGG occurs 1 timeATAGGAATCCCA occurs 5 timesATAGTGAATAGG occurs 1 timeATAGTGAATAGT occurs 2 timesATAGTGCAAGGG occurs 3 timesATAGTGGCCTAG occurs 1 timeATAGTGGCCTAT occurs 1 timeATCCCAACGTCT occurs 10 timesATCCCAAGAATC occurs 10 timesATCCCAATGAAT occurs 3 timesATCCCAATGCAA occurs 8 timesATCCCAATGGCC occurs 4 timesATGAATAGGAAT occurs 2 timesATGAATAGTGAA occurs 1 timeATGAATAGTGCA occurs 2 timesATGCAAGGCGTC occurs 1 timeATGCAAGGGAAT occurs 4 timesATGCAAGGTGAA occurs 2 timesATGCAAGGTGCA occurs 2 timesATGCAAGGTGGC occurs 3 timesATGGCCTACGTC occurs 4 timesATGGCCTATGAA occurs 1 timeATGGCCTATGCA occurs 1 timeATGGCCTATGGC occurs 1 timeCAACGTCTTGCG occurs 2 timesCAACGTCTTGGA occurs 3 timesCAACGTCTTGTG occurs 5 timesCAAGAATCCCAA occurs 10 timesCAAGGCGTCTTG occurs 1 timeCAAGGGAATCCC occurs 10 timesCAAGGTGAATAG occurs 4 timesCAAGGTGCAAGG occurs 6 timesCAAGGTGGCCTA occurs 5 timesCAATGAATAGGA occurs 2 timesCAATGAATAGTG occurs 1 timeCAATGCAAGGGA occurs 3 timesCAATGCAAGGTG occurs 5 timesCAATGGCCTACG occurs 3 timesCAATGGCCTATG occurs 1 timeCCAACGTCTTGC occurs 2 timesCCAACGTCTTGG occurs 3 timesCCAACGTCTTGT occurs 5 timesCCAAGAATCCCA occurs 10 timesCCAATGAATAGG occurs 2 timesCCAATGAATAGT occurs 1 timeCCAATGCAAGGG occurs 3 timesCCAATGCAAGGT occurs 5 timesCCAATGGCCTAC occurs 3 timesCCAATGGCCTAT occurs 1 timeCCCAACGTCTTG occurs 10 timesCCCAAGAATCCC occurs 10 timesCCCAATGAATAG occurs 3 timesCCCAATGCAAGG occurs 8 timesCCCAATGGCCTA occurs 4 timesCCTACGTCTTGC occurs 1 timeCCTACGTCTTGG occurs 2 timesCCTACGTCTTGT occurs 3 timesCCTAGAATCCCA occurs 2 timesCCTATGAATAGT occurs 2 timesCCTATGCAAGGC occurs 1 timeCCTATGCAAGGG occurs 1 timeCCTATGCAAGGT occurs 2 timesCCTATGGCCTAC occurs 1 timeCCTATGGCCTAT occurs 2 timesCGTCTTGCGTCT occurs 5 timesCGTCTTGGAATC occurs 9 timesCGTCTTGTGAAT occurs 1 timeCGTCTTGTGCAA occurs 5 timesCGTCTTGTGGCC occurs 3 timesCTACGTCTTGCG occurs 1 timeCTACGTCTTGGA occurs 2 timesCTACGTCTTGTG occurs 3 timesCTAGAATCCCAA occurs 2 timesCTATGAATAGTG occurs 2 timesCTATGCAAGGCG occurs 1 timeCTATGCAAGGGA occurs 1 timeCTATGCAAGGTG occurs 2 timesCTATGGCCTACG occurs 1 timeCTATGGCCTATG occurs 2 timesCTTGCGTCTTGC occurs 1 timeCTTGCGTCTTGG occurs 3 timesCTTGCGTCTTGT occurs 1 timeCTTGGAATCCCA occurs 9 timesCTTGTGAATAGG occurs 1 timeCTTGTGCAAGGG occurs 1 timeCTTGTGCAAGGT occurs 4 timesCTTGTGGCCTAG occurs 1 timeCTTGTGGCCTAT occurs 2 timesGAATAGCGTCTT occurs 1 timeGAATAGGAATCC occurs 5 timesGAATAGTGAATA occurs 3 timesGAATAGTGCAAG occurs 3 timesGAATAGTGGCCT occurs 2 timesGAATCCCAACGT occurs 10 timesGAATCCCAAGAA occurs 10 timesGAATCCCAATGA occurs 3 timesGAATCCCAATGC occurs 8 timesGAATCCCAATGG occurs 4 timesGCAAGGCGTCTT occurs 1 timeGCAAGGGAATCC occurs 10 timesGCAAGGTGAATA occurs 4 timesGCAAGGTGCAAG occurs 6 timesGCAAGGTGGCCT occurs 5 timesGCCTACGTCTTG occurs 6 timesGCCTAGAATCCC occurs 2 timesGCCTATGAATAG occurs 2 timesGCCTATGCAAGG occurs 4 timesGCCTATGGCCTA occurs 3 timesGCGTCTTGCGTC occurs 2 timesGCGTCTTGGAAT occurs 4 timesGCGTCTTGTGGC occurs 1 timeGGAATCCCAACG occurs 7 timesGGAATCCCAAGA occurs 7 timesGGAATCCCAATG occurs 9 timesGGCCTACGTCTT occurs 6 timesGGCCTAGAATCC occurs 2 timesGGCCTATGAATA occurs 2 timesGGCCTATGCAAG occurs 4 timesGGCCTATGGCCT occurs 3 timesGGCGTCTTGCGT occurs 1 timeGGGAATCCCAAC occurs 4 timesGGGAATCCCAAG occurs 3 timesGGGAATCCCAAT occurs 3 timesGGTGAATAGCGT occurs 1 timeGGTGAATAGTGA occurs 2 timesGGTGAATAGTGC occurs 1 timeGGTGCAAGGGAA occurs 2 timesGGTGCAAGGTGA occurs 2 timesGGTGCAAGGTGC occurs 1 timeGGTGCAAGGTGG occurs 1 timeGGTGGCCTACGT occurs 2 timesGGTGGCCTATGC occurs 2 timesGGTGGCCTATGG occurs 1 timeGTCTTGCGTCTT occurs 5 timesGTCTTGGAATCC occurs 9 timesGTCTTGTGAATA occurs 1 timeGTCTTGTGCAAG occurs 5 timesGTCTTGTGGCCT occurs 3 timesGTGAATAGCGTC occurs 1 timeGTGAATAGGAAT occurs 2 timesGTGAATAGTGAA occurs 2 timesGTGAATAGTGCA occurs 1 timeGTGAATAGTGGC occurs 2 timesGTGCAAGGGAAT occurs 6 timesGTGCAAGGTGAA occurs 2 timesGTGCAAGGTGCA occurs 4 timesGTGCAAGGTGGC occurs 2 timesGTGGCCTACGTC occurs 2 timesGTGGCCTAGAAT occurs 2 timesGTGGCCTATGAA occurs 1 timeGTGGCCTATGCA occurs 3 timesGTGGCCTATGGC occurs 2 timesTACGTCTTGCGT occurs 1 timeTACGTCTTGGAA occurs 2 timesTACGTCTTGTGC occurs 2 timesTACGTCTTGTGG occurs 1 timeTAGAATCCCAAC occurs 1 timeTAGAATCCCAAT occurs 1 timeTAGCGTCTTGGA occurs 1 timeTAGGAATCCCAA occurs 5 timesTAGTGAATAGGA occurs 1 timeTAGTGAATAGTG occurs 2 timesTAGTGCAAGGGA occurs 3 timesTAGTGGCCTAGA occurs 1 timeTAGTGGCCTATG occurs 1 timeTATGAATAGTGA occurs 1 timeTATGAATAGTGC occurs 1 timeTATGCAAGGCGT occurs 1 timeTATGCAAGGGAA occurs 1 timeTATGCAAGGTGA occurs 1 timeTATGCAAGGTGC occurs 1 timeTATGGCCTACGT occurs 1 timeTATGGCCTATGA occurs 1 timeTATGGCCTATGC occurs 1 timeTCCCAACGTCTT occurs 10 timesTCCCAAGAATCC occurs 10 timesTCCCAATGAATA occurs 3 timesTCCCAATGCAAG occurs 8 timesTCCCAATGGCCT occurs 4 timesTCTTGCGTCTTG occurs 5 timesTCTTGGAATCCC occurs 9 timesTCTTGTGAATAG occurs 1 timeTCTTGTGCAAGG occurs 5 timesTCTTGTGGCCTA occurs 3 timesTGAATAGCGTCT occurs 1 timeTGAATAGGAATC occurs 5 timesTGAATAGTGAAT occurs 3 timesTGAATAGTGCAA occurs 3 timesTGAATAGTGGCC occurs 2 timesTGCAAGGCGTCT occurs 1 timeTGCAAGGGAATC occurs 10 timesTGCAAGGTGAAT occurs 4 timesTGCAAGGTGCAA occurs 6 timesTGCAAGGTGGCC occurs 5 timesTGCGTCTTGCGT occurs 1 timeTGCGTCTTGGAA occurs 3 timesTGCGTCTTGTGG occurs 1 timeTGGAATCCCAAC occurs 2 timesTGGAATCCCAAG occurs 2 timesTGGAATCCCAAT occurs 4 timesTGGCCTACGTCT occurs 6 timesTGGCCTAGAATC occurs 2 timesTGGCCTATGAAT occurs 2 timesTGGCCTATGCAA occurs 4 timesTGGCCTATGGCC occurs 3 timesTGTGAATAGGAA occurs 1 timeTGTGCAAGGGAA occurs 1 timeTGTGCAAGGTGC occurs 3 timesTGTGCAAGGTGG occurs 1 timeTGTGGCCTAGAA occurs 1 timeTGTGGCCTATGA occurs 1 timeTGTGGCCTATGC occurs 1 timeTTGCGTCTTGCG occurs 1 timeTTGCGTCTTGGA occurs 3 timesTTGCGTCTTGTG occurs 1 timeTTGGAATCCCAA occurs 8 timesTTGTGAATAGGA occurs 1 timeTTGTGCAAGGGA occurs 1 timeTTGTGCAAGGTG occurs 4 timesTTGTGGCCTAGA occurs 1 timeTTGTGGCCTATG occurs 2 times
Reading string from a text file and returning the number of occurrences of each substring of length k
c++;strings;c++11;bioinformatics
Code ReviewReplace:    if(subSequenceCountMap.find(stringMapElement)!=subSequenceCountMap.end())    {        subSequenceCountMap[stringMapElement]++;    }    else    {        subSequenceCountMap[stringMapElement]=1;    }With:    subSequenceCountMap[stringMapElement]++;This is because operator[] will automatically insert the element 0 if it does not already exist before returning a reference. Thus if you just increment the value you get the correct result.I don't see the point of using an intermediate file to store all the sub strings. Just put them dirextly into the map.Replace:for(i=0; i<kmerCount; i++){    subStrPrint = sequence.substr(i,k);    outFile << subStrPrint << std::endl;    std::cout << Iteration  << i+1 << : sub-sequence  << subStrPrint << std::endl;}With:for(i=0; i<kmerCount; i++){    subSequenceCountMap[sequence.substr(i,k)]++;}Not sure why you need this test:    if(itr->second >1)    {        std::cout << itr->first <<  occurs  << itr->second <<  times << std::endl;    }    else    {        std::cout << itr->first <<  occurs  << itr->second <<  time << std::endl;    }Both sides of the if statement are exactly the same. Remove all the ones that have only happened once by removing the whole else block!Ohh. I see it now. The extra s on the end of time. Yea. If this were a million dollar project maybe I would go to the extra effort of pluralization. But I would do it simpler.    std::cout << itr->first <<  occurs               << itr->second <<  time << ((itr->second > 1)?s:)              << \n;Design:Don't use global variables.//---------------------------------//VARIABLESstd::string sequence;int k;int kmerCount;int i=0;std::string subStrPrint;std::ifstream inFile;std::string fileName;std::ofstream outFile;std::string ofileName;std::ifstream outputReadFile;std::string outputReadFileName;std::map<std::string, int> subSequenceCountMap;std::string stringMapElement;Have local variables and pass them as parameters to functions and get results as a return value from the function.Loading the whole gene into memory seems like it could be very expensive. Especially since you only need the k characters at a time. Why not load the first k characters (that's your first string). Then move everything down once then read one more character to the end.ErrorYou have an off by one error here: kmerCount = sequence.length()-k;Your code reports: TTGGAATCCCAA occurrs 8 timesBut if you manually count it. You will find that it happens 9 times.Writting in C++ styleI would have used an object to encapsulate the gene fragment.#include <iostream>#include <fstream>#include <map>#include <string>class Gene{    std::istream&   in;    std::string     section;    public:        Gene(std::istream& in, std::size_t size)            : in(in)        {            section.resize(size);            in.read(&section[0], size);        }        bool more()        {            return in.good();        }        void next()        {            std::move(std::begin(section) + 1, std::end(section), std::begin(section));            in.read(&section.back(), 1);        }        operator std::string const&()        {            return section;        }};int main(){    std::string fileName;    std::size_t size;    std::cout << Enter File Name with Gene and size\n;    if (!(std::cin >> fileName >> size))    {        std::cerr << Failed to read info\n;        return 1;    }    std::ifstream   file(fileName);    if (!file)    {        std::cerr << Failed to open file\n;        return 1;    }    std::map<std::string, int>  count;    for(Gene gene(file, size);gene.more();gene.next())    {        ++count[gene];    }    for(auto const& item: count)    {        if (item.second > 1)        {            std::cout << item.first <<   << item.second << \n;        }    }}