这是主函数
clc; clear all; close all; %% 生成初始序列 sequenceOfLength = 20; sequenceOfPop = 4; sequence = produceSequence(sequenceOfLength,sequenceOfPop); %% 序列中植入modify l_merOfLength = 8; l_mer = produceSequence(l_merOfLength,1); sequence = plantModify(sequence,l_mer,l_merOfLength,sequenceOfLength); %% 查找最优 for kk = 1:100 tem = randperm(sequenceOfPop,1); tem_sequence = sequence(tem,:); ofMove_sequence = sequence([1:tem-1,tem+1:end],:); s = randperm(sequenceOfLength - l_merOfLength + 1,sequenceOfPop - 1); for ii = 1:sequenceOfPop - 1 l_mere(ii,:) = ofMove_sequence (ii,[s(ii):s(ii) + l_merOfLength-1]); end [p_matrix,best_mer,score] = profileMatrix(l_mere); p_most = 0; for i = 1 : sequenceOfLength - l_merOfLength + 1 temp_sequenceOfL_mer = tem_sequence(i:i + l_merOfLength - 1); p_m = []; for j = 1:l_merOfLength temp_s = temp_sequenceOfL_mer (j) ; switch temp_s case 'A' p_m = [p_m,p_matrix(1,j)]; case 'C' p_m = [p_m,p_matrix(2,j)]; case 'T' p_m = [p_m,p_matrix(3,j)]; case 'G' p_m = [p_m,p_matrix(4,j)]; end end p_m = cumprod(p_m); if p_m(l_merOfLength) > p_most p_most = p_m(l_merOfLength); p_bestOfL_mer = temp_sequenceOfL_mer; end end com_l_mer = [l_mere;p_bestOfL_mer] end
function [sequence] = produceSequence(s_length,t)
% s_length 输入序列的长度;
% t 输入序列的条数;
% sequence 返回序列数组;
% 本序列用数字来代表序列字母,其中用1,2,3,4代表A,T,C,G;
for i = 1:t
for j = 1:s_length
temp = randperm(4);
%sequence(i,j) = temp(1);
temp = temp(1);
switch temp
case 1
sequence(i,j) = 'A';
case 2
sequence(i,j) = 'T';
case 3
sequence(i,j) = 'C';
case 4
sequence(i,j) = 'G';
end
end
end
end
function [sequence] = plantModify(sequence,l_mer,l_mereOfLength,sequenceOfLength)
temp_max = sequenceOfLength - l_mereOfLength;
t = length(sequence(:,1));
temp = randperm(temp_max,t);
p_mutation = .7;
for i = 1:t
l_mer = l_mereMutation(l_mer,p_mutation,l_mereOfLength);
sequence(i,temp(i):temp(i) + l_mereOfLength - 1) = l_mer;
end
end
function [l_mer_new] = l_mereMutation(l_mer,p_mutation,l_meLength)
temp = rand;
if temp < p_mutation
temp_t = round(rand * 2);
i = 1;
while i <= temp_t
position = randperm(l_meLength,1);
temp_l_mer = produceSequence(1,1);
l_mer(position) = temp_l_mer;
i = i + 1;
end
end
l_mer_new = l_mer;
end
function [p_matrix,best_mer,score] = profileMatrix(l_mere)
t = length(l_mere(:,1));
l = length(l_mere(1,:));
p_matrix = zeros(4,l);
for i = 1:l
for j = 1:t
if l_mere(j,i) == 'A'
p_matrix(1,i) = p_matrix(1,i) + 1;
elseif l_mere(j,i) == 'C'
p_matrix(2,i) = p_matrix(2,i) + 1;
elseif l_mere(j,i) == 'T'
p_matrix(3,i) = p_matrix(3,i) + 1;
else
p_matrix(4,i) = p_matrix(4,i) + 1;
end
end
end
p_matrix(find(p_matrix == 0)) = .00001;
p_matrix = p_matrix/t;
%% 返回最大值
[max_,position] = max (p_matrix);
score = cumprod(max_);
score = score(1,end);
for i = 1:length(position)
if position(i) == 1
best_mer(i) = 'A';
elseif position(i) == 2;
best_mer(i) = 'C';
elseif position(i) == 3;
best_mer(i) = 'T';
else
best_mer(i) = 'G';
end
end
end