在字符串中搜索序列.脱氧核糖核酸
Search a sequence in a string. DNA
我需要做一个程序,将3分离为字符串大小,并与给定的同一字符串中的其他3序列进行比较。我来解释一下。
用户介绍这个DNA字符串="ACTGCGACGGTACGCTTCGACGTAG"例如。我们从n=3开始,也就是说,我们取DNA中的前三个特征进行比较。
第一个字符是:"ACT",我们需要将其与其他三个序列进行比较,如[CTG,TGC,GCA…直到最后]。
如果我们找到另一个等于"ACT"的序列,我们就保存这个位置。这里是另一个例子:
DNA:"ACTGCGACGGTACGCTTCGACGTAG",我们在他的位置上发现了这个序列:
- ACG:7-12-20
- CGA:5-18
- GAC:6-19
- GTA:10-22
- CGAC:5-18
- GACG:6-19
- CGACG:5-18数字是序列开始的位置:
ACTGCGACGGTAC GCTTCGACGTAG
你可以看到,当n=3,增量为1时,我们最终找到n=3,变量传递到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;
}
基于Mohit的答案,但可能会重新使用指针,以获得更好的性能(与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;
}
}
}
}
怎么样:
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
,因此可以构造一个trie,如本页所示,它是用C
编写的。将树节点更改为:
struct tree_node {
vector<int> starts;
struct tree_node *children[26]; /* A to Z */
};
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 在C++中搜索嵌套多映射值
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 为什么二进制搜索在我的测试中不起作用
- 正在尝试重载二进制搜索树分配运算符
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 向量上的线性搜索
- 如何在动态数组上使用搜索函数
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- cmake:添加要搜索头文件的目录
- 使用C++创建特殊的二叉搜索树
- 在C++的字符串中搜索和删除某些字符
- std::unordered_map 搜索算法是如何实现的?
- 使用不变量来确定二分搜索中的边界条件
- 二叉搜索如何比线性搜索更快?
- 按边长度递归搜索图中所有可行路径
- QStackWidget - 按名称搜索
- 在递归二叉搜索树中搜索
- 在字符串中搜索序列.脱氧核糖核酸