Search code examples
c++stringalgorithmsearchdna-sequence

Search a sequence in a string. DNA


I need to do a program that separate from 3 to the size of a string and compare to the others sequences of 3 in the same string given. I'm going to explain it.

User introduce this DNA string = "ACTGCGACGGTACGCTTCGACGTAG" For example. We start with n = 3, this is, we take the first three caracters for comparing in the DNA.

The first characters are: "ACT", and we need to compare it with the other sequences of three, like, [CTG,TGC,GCA... until the end].

If we find another sequence equal to "ACT", we save the position. Here is another example:

DNA: "ACTGCGACGGTACGCTTCGACGTAG" and we find this sequences in his positions:

  1. ACG: 7 - 12 - 20
  2. CGA: 5 - 18
  3. GAC: 6 - 19
  4. GTA: 10 - 22
  5. CGAC: 5 - 18
  6. GACG: 6 - 19
  7. CGACG: 5 - 18 The number is the position of the start of the sequence:

ACTGCGACGGTACGCTTCGACGTAG

You can see that the n = 3, increment in 1 when the we end to find by n = 3, the variable pass to n=4, until n = DNA.size().

My problem is that i have one function for divide the string in a little sequences of the DNA, and I do a push_back() for saving in the vector, and then I can see if there is more sequences or not, but i don't know how can i get the position.

I can use the library algorithm, and for sure, in this library there is a function that do this but i don't know so much this library.

Here is my code:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

const string DNA = "ACTGCGACGGTACGCTTCGACGTAG";
const size_t taille = DNA.size();

size_t m = 3;
vector<string> v;

/*
struct DNA{
    const string dna;  // chaine saisie pour l'utilisateur
    size_t taille;  // Taille de la chaine
    string chaine;  // Chaine à chercher
};
*/

// what kind of structs can i create? for me it's stupid to make any struct in this program.

bool checkDNA(string &s);
string takeStrings(const string &s,size_t i, size_t m);
void FindSequenceDNA(vector<string>&s,string sq);
size_t incrementValue(size_t &m);



int main(){

    string DNAuser;
    cout << "Introduce the DNA: ";
    cin >> DNAuser;

    bool request;
    cout << boolalpha;
    request = DNAuser.find_first_not_of("AGCT");
    cout << request << endl;

    vector<string> vectorSq;
    size_t auxiliar = 0;
    string r;
    size_t ocurrencies = DNA.size()-2;
    cout << "DNA: " << DNA << endl;
    while(auxiliar<ocurrencies){        // This gonna be works with the ocurriences, from 1 to end.
        r = takeStrings(DNA,auxiliar,auxiliar+m);
        auxiliar++;
        if(r.size()==m){
            vectorSq.push_back(r);
        }
    }

    // string res = takeStrings(DNA,0,3);
    // cout << "res: " << res << endl;
    // cout << "Printing vector: " << endl;

    // I just need to find the other, the practice is almost done.

    for(size_t i = 0; i< vectorSq.size(); i++){
        cout << vectorSq[i] << endl;
    }

    return 0;

}


string takeStrings(const string &s,size_t i, size_t m){
    string result;
    size_t aux=i;
    if(s.size()==0){
        cout << "String is empty." << endl;
    }
    else{
        for(;i<s.size()&&i!=m;i++){
            result+=s[i];
            aux++;
        }

    }
    return result;
}

void FindSequenceDNA(vector<string>&s,string sq){
    if(s.size()==0){
        cout << "DNA invalid." << endl;
    }
    else{
        for(size_t i=0;i<s.size();i++){
            if(sq==s[i]){
                cout << "function: " << endl;
                cout << s[i] << endl; // I need to calculate the real position in the string, not in the vector
            }
        }
    }

}

bool checkDNA(string &s){
    bool res;
    if(s.size()==0 || s.size()<3){
        cout << "DNA invalid" << endl;
    }
    else{
        for(size_t i=0;i<s.size();i++){
            if(s[i]=='A' || s[i]=='C' || s[i]=='G' || s[i]=='T')
            {
                res = true;
            }
            else{
                res= false;
            }
        }
    }
    return res;
}

size_t incrementValue(size_t &m){
    if(m<DNA.size()){
        m++;
    }
    return m;
}

Solution

  • Based on Mohit's answer but re-uses pointers to possibly, get better performance (vs string.substr)

    #include <iostream>
    #include <cstring>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    static const char* DNAdata = "ACTGCGACGGTACGCTTCGACGTAG";
    static const size_t len = strlen(DNAdata);
    
    vector< vector< string > > uniqueKeys(len);
    vector< vector< vector<size_t> > > locations(len);
    
    
    void saveInfo(const char* str, size_t n, size_t loc) {
       vector<string>& keys = uniqueKeys[n-1];
       vector<vector<size_t> >& locs = locations[n-1];
    
       bool found = false;
       for (size_t i=0; i<keys.size(); ++i) {
          if (keys[i] == str) {
         locs[i].push_back(loc);
         found = true;
         break;
          }
       }
       if (!found) {
          vector<size_t> newcont;
          newcont.push_back(loc);
          keys.push_back(str);
          locs.push_back(newcont);
       }
    }
    
    void printInfo(const char* str) {
       cout << str << endl;
       size_t len = strlen(str);
       vector<string>& keys = uniqueKeys[len-1];
       vector<vector<size_t> >& locs = locations[len-1];
       for (size_t i=0; i<keys.size(); ++i) {
          if (keys[i] == str) {
         vector<size_t>& l = locs[i];
         vector<size_t>::iterator iter = l.begin();
         for (; iter != l.end(); ++iter) {
            cout << *iter << endl;
         }
    
         break;
          }
       }
    }
    
    int main() {
       char* DNA = new char[len+1];
       strcpy(DNA, DNAdata);
       char* end = DNA+len;
       char* start = DNA;
       for (size_t n =3; n<=len; ++n) {
          size_t loc = 0;
          char* p = start;   
          char* e = p+n;
          while (e <= end) {     
         char save = *e;
         *e = 0;
         saveInfo(p++, n, loc++);
         *e = save;
         ++e;
          }
       }
       delete[] DNA;
    
       printInfo("GTA");
       printInfo("ACTGCGACGGTACGCTTCGACGTA");
    
       return 0;
    }
    

    To print all:

    void printAll() {
       for (size_t n=3; n<=len; ++n) {
          cout << "--> " << n << " <--" << endl;
          vector<string>& keys = uniqueKeys[n-1];
          vector<vector<size_t> >& locs = locations[n-1];
          for (size_t i=0; i<keys.size(); ++i) {
         cout << keys[i] << endl;
         vector<size_t>& l = locs[i];
         vector<size_t>::iterator iter = l.begin();
         for (; iter != l.end(); ++iter) {
            cout << *iter << endl;
         }
          }
       }
    }