mercredi 25 mars 2015

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;
}

Aucun commentaire:

Enregistrer un commentaire