昨天用c++写了一个穷举法,来粘一下。
首先,数据是20*600的序列,植入模体为(9,2),因为是穷举法,太高的话效率太慢了。
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
int H_len(string a,string b,int l);//计算两个同长字符串的海明距离
int main()
{
//读取txt数据文件
ifstream ifile;
ifile.open("data.txt");
string line;
string s[20];
int panduan=0;
while (getline(ifile, line)) {
if(panduan%2==1){
s[(panduan-1)/2]=line;
//cout << s[(panduan-1)/2] << endl;
}
panduan = panduan + 1;
}
//对每个可能的组合做循环
string Letter("ACGT"); //定义字母表
int _len = 4; //定义字母表长度
int H_d = 2;
int LL = 9;
int n = 600;
int t = 20;
//string firstname(name.substr(0,4));
//cout << firstname << endl;
//为每一个可能的模体都做一次循环,在这里是4的9次方
for(int a = 0;a < _len;a++)
for(int b = 0;b < _len;b++)
for(int c = 0;c < _len;c++)
for(int d = 0;d < _len;d++)
for(int e = 0;e < _len;e++)
for(int f = 0;f < _len;f++)
for(int g = 0;g < _len;g++)
for(int h = 0;h < _len;h++)
for(int i = 0;i < _len;i++)
{
string t_moti;
t_moti = t_moti + Letter[a] + Letter[b] + Letter[c] + Letter[d] + Letter[e] + Letter[f] + Letter[g] + Letter[h] + Letter[i];
//cout<< t_moti <<endl;
int count_m = 0;
int sum = 0;
for(int j= 0;j<t;j++)
{
for(int k = 0;k < n - LL + 1 ;k++)
{
string cut = s[j].substr(k,LL);
if(H_len(cut,t_moti,LL)<H_d || H_len(cut,t_moti,LL)==H_d)
{
sum = sum + H_len(cut,t_moti,LL);
count_m = count_m + 1 ;
break;
}
}
if(count_m != j+1)
break;
}
if(count_m ==20)
cout<<t_moti<<" "<<sum<<endl;
}
return 0;
}
//计算两个同长字符串的海明距离
int H_len(string a,string b,int l)
{
int temp_d = 0;
for(int i = 0;i < l;i++)
{
if(a[i]!=b[i])
{
temp_d = temp_d + 1;
}
}
return temp_d;
}
运行一下,看一下结果。
跟植入的模体比较一下,AAAGTGAAC,发现是一致的,总体来看算法还是成功算出了模体,就是效率太慢,打算下一步实现PMSP算法,应该会提高效率不少。
继续加油!
PS:需要测试数据的可以私聊。