function [Sequence] = CB_gene_v5(Segments)
NumTrials = 10;
Seq = {};
L = zeros(NumTrials,1);
for q = 1:NumTrials
[Seq{q},L(q)] = CB_gene_v4b(Segments);
end
[value,Index]=min(L);
Sequence = Seq{Index(1)};
return;
function [Sequence,NewSeqLength] = CB_gene_v4b(Segments)
% Eliminate Duplicates
Segments = unique(Segments,'rows');
[rows,cols] = size(Segments);
[junk,remap] = sort(rand(rows,1));
Segments = Segments( remap,:); % randomize
maxlength = rows*cols;
Sequence = zeros(1,maxlength);
Temp = zeros(cols,rows);
Temp = Segments';
Sequence = Temp(:)';
SegPos = [1:cols:maxlength];
% Idea: Create a sparse matrix of dimension rows by rows*cols and an
% index variable that keeps track of where the Segs go.
for k = 1:rows
sparseM(k,[SegPos(k):(SegPos(k)+cols-1)]) = Segments(k,:);
end
sparseM = sparse( sparseM );
NumOverlapped = sum(sparseM>0);
SeqLength = sum(NumOverlapped>0);
NewSeqLength = SeqLength;
k = 0;
while (1)
k = mod(k,rows)+1;
indicies = [];
indicies = findstr(Segments(k,:),Sequence);
OrgPos = SegPos(k);
useful = find(indicies~=OrgPos);
if (length(indicies)>1)
% Move it
NewPos = indicies(useful(1)); % might want to try this with a random index??
% instead of the (1) first one
TempSeg = sparseM(k,[OrgPos:(OrgPos+cols-1)]);
sparseM(k,[OrgPos:(OrgPos+cols-1)]) = zeros(1,cols);
sparseM(k,[NewPos:(NewPos+cols-1)]) = TempSeg;
SegPos(k) = NewPos;
[SegPos,MapIndex] = sort(SegPos); % sort according to placement
Segments = Segments(MapIndex,:); % arrange rows
sparseM = sparseM(MapIndex,:);
% remove leading spaces
if (SegPos(1)~=1)
MoveBy = SegPos(1)-1;
SegPos = SegPos - MoveBy;
[A,B] = size(sparseM);
sparseM = sparseM(:,[(SegPos(1)+MoveBy):B]);
end
% remove spaces in the sequence
Spacing = diff(SegPos);
BadSpots = find(Spacing>cols);
if ~isempty(BadSpots)
SpaceSize = Spacing(BadSpots);
MoveBy = SpaceSize - cols;
for p = (BadSpots+1):rows
% store info
TempSeg = sparseM(p, [SegPos(p):(SegPos(p)+cols-1)]);
% zero old position
sparseM(p, [SegPos(p):(SegPos(p)+cols-1)]) = zeros(1,cols);
% store in new position
sparseM(p, ([SegPos(p):(SegPos(p)+cols-1)]-MoveBy)) = TempSeg;
end
SegPos((BadSpots+1):rows) = SegPos((BadSpots+1):rows) - MoveBy;
end
sparseM = sparse(sparseM); % clean up
NewNumOverlapped = sum(sparseM>0); % compute length
NewSeqLength = sum(NewNumOverlapped>0);
Sequence = sum(sparseM)./(NewNumOverlapped + ~(NewNumOverlapped));
TempSequence = zeros(1,NewSeqLength);
TempSequence = full(Sequence);
Sequence = TempSequence; %Sequence( find(Sequence) );%TempSequence(1:NewSeqLength);
end
if (k==rows) % Stop if there is minimal progress after trying all rows
if (SeqLength-NewSeqLength<4)
break
else
SeqLength = NewSeqLength; % update to track progress
end
end
end
return;
|