簡體   English   中英

搜索字符串中的序列。 脫氧核糖核酸

[英]Search a sequence in a string. DNA

我需要做一個程序,從3到字符串的大小分開,並與給定的相同字符串中的3個序列進行比較。 我要解釋一下。

用戶介紹此DNA字符串=“ACTGCGACGGTACGCTTCGACGTAG”例如。 我們從n = 3開始,這就是我們在DNA中進行比較的前三個字符。

第一個字符是:“ACT”,我們需要將它與其他三個序列進行比較,如[CTG,TGC,GCA ......直到結束]。

如果我們發現另一個等於“ACT”的序列,我們保存位置。 這是另一個例子:

DNA:“ACTGCGACGGTACGCTTCGACGTAG”,我們發現這個序列在他的位置:

  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數字是序列開始的位置:

ACTGCG ACG GT ACG CTTCG ACG TAG

你可以看到n = 3,當我們通過n = 3找到時,增量為1,變量傳遞給n = 4,直到n = DNA.size()。

我的問題是我有一個函數可以在DNA的一小部分序列中划分字符串,然后我執行push_back()來保存向量,然后我可以看到是否有更多序列,但我不知道我知道怎么能得到這個位置。

我可以使用庫算法,當然,在這個庫中有一個函數可以做到這一點,但我不太了解這個庫。

這是我的代碼:

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

怎么樣:

std::map< std::string, std::vectpr<int> > msvi;
std::size_t len = dna.size();
for(size_t from = 0; from < len; ++from) {
  for(size_t sz = 3; sz < len; ++sz) {
    msvi[ dna.substr(from, sz ].push_back(from);
  }
}

這將創建大小為3的所有字符串並將其保存在地圖中。

現場演示鏈接

僅打印具有2個或更多實例的項目


由於您不想使用std::map ,因此可以構建一個如C所示的本頁所示的trie。 將樹節點更改為:

struct tree_node {
  vector<int> starts;
  struct tree_node *children[26];  /* A to Z */
};

基於Mohit的答案,但重新使用指針,可以獲得更好的性能(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;
}

打印全部:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM