Added source Matlab code for reference

This commit is contained in:
Waldir Leoncio 2019-12-16 16:47:21 +01:00
parent b8af977117
commit b5d99903d2
186 changed files with 61405 additions and 1 deletions

View file

@ -0,0 +1,82 @@
function changes = calcLogmlChanges(inds, cqData, nCqCodes, spData, nSpCodes, locCliques, locSeparators, logml)
% compute the logml change if the given inds are moved to another cluster
% the input inds are supposed to come from the same cluster
% changes is a npops*1 vector
% Lu Cheng, 15.12.2012
global CQ_COUNTS; global SUM_CQ_COUNTS;
global SP_COUNTS; global SUM_SP_COUNTS;
global PARTITION;
global LOC_CQ_COUNTS;
global LOC_SP_COUNTS;
npops = size(CQ_COUNTS,3);
changes = zeros(npops,1);
indsToBeMoved = inds;
if isempty(indsToBeMoved), return, end
i1 = PARTITION(indsToBeMoved(1));
[diffCqCounts diffCqSumCounts]= computeDiffInCounts(indsToBeMoved, cqData, nCqCodes);
[diffSpCounts diffSpSumCounts]= computeDiffInCounts(indsToBeMoved, spData, nSpCodes);
diffLocCqCounts = computeDiffInCliqCounts(locCliques, indsToBeMoved);
diffLocSpCounts = computeDiffInCliqCounts(locSeparators, indsToBeMoved);
CQ_COUNTS(:,:,i1) = CQ_COUNTS(:,:,i1) - diffCqCounts;
SP_COUNTS(:,:,i1) = SP_COUNTS(:,:,i1) - diffSpCounts;
SUM_CQ_COUNTS(:,i1) = SUM_CQ_COUNTS(:,i1) - diffCqSumCounts;
SUM_SP_COUNTS(:,i1) = SUM_SP_COUNTS(:,i1) - diffSpSumCounts;
LOC_CQ_COUNTS(:,i1) = LOC_CQ_COUNTS(:,i1) - diffLocCqCounts;
LOC_SP_COUNTS(:,i1) = LOC_SP_COUNTS(:,i1) - diffLocSpCounts;
% PARTITION(inds) = -1;
updateLogmlTable(i1);
for i2 = 1:npops
if i2 ~= i1
CQ_COUNTS(:,:,i2) = CQ_COUNTS(:,:,i2) + diffCqCounts;
SP_COUNTS(:,:,i2) = SP_COUNTS(:,:,i2) + diffSpCounts;
SUM_CQ_COUNTS(:,i2) = SUM_CQ_COUNTS(:,i2) + diffCqSumCounts;
SUM_SP_COUNTS(:,i2) = SUM_SP_COUNTS(:,i2) + diffSpSumCounts;
LOC_CQ_COUNTS(:,i2) = LOC_CQ_COUNTS(:,i2) + diffLocCqCounts;
LOC_SP_COUNTS(:,i2) = LOC_SP_COUNTS(:,i2) + diffLocSpCounts;
% PARTITION(inds) = i2;
updateLogmlTable(i2);
logml_new = computeTotalLogml();
changes(i2) = logml_new - logml;
CQ_COUNTS(:,:,i2) = CQ_COUNTS(:,:,i2) - diffCqCounts;
SUM_CQ_COUNTS(:,i2) = SUM_CQ_COUNTS(:,i2) - diffCqSumCounts;
SP_COUNTS(:,:,i2) = SP_COUNTS(:,:,i2) - diffSpCounts;
SUM_SP_COUNTS(:,i2) = SUM_SP_COUNTS(:,i2) - diffSpSumCounts;
LOC_CQ_COUNTS(:,i2) = LOC_CQ_COUNTS(:,i2) - diffLocCqCounts;
LOC_SP_COUNTS(:,i2) = LOC_SP_COUNTS(:,i2) - diffLocSpCounts;
% PARTITION(inds) = -1;
updateLogmlTable(i2);
end
end
CQ_COUNTS(:,:,i1) = CQ_COUNTS(:,:,i1) + diffCqCounts;
SUM_CQ_COUNTS(:,i1) = SUM_CQ_COUNTS(:,i1) + diffCqSumCounts;
SP_COUNTS(:,:,i1) = SP_COUNTS(:,:,i1) + diffSpCounts;
SUM_SP_COUNTS(:,i1) = SUM_SP_COUNTS(:,i1) + diffSpSumCounts;
LOC_CQ_COUNTS(:,i1) = LOC_CQ_COUNTS(:,i1) + diffLocCqCounts;
LOC_SP_COUNTS(:,i1) = LOC_SP_COUNTS(:,i1) + diffLocSpCounts;
% PARTITION(inds) = i1;
updateLogmlTable(i1);
%---------------------------------------------------------------------

View file

@ -0,0 +1,18 @@
function clearGlobalVars
% Lu Cheng, 15.12.2012
global COUNTS; COUNTS = [];
global SUMCOUNTS; SUMCOUNTS = [];
global PARTITION; PARTITION = [];
global LOGML_TABLE; LOGML_TABLE = [];
global ADDITION_DIFFERENCE; ADDITION_DIFFERENCE = [];
global REMOVAL_DIFFERENCE; REMOVAL_DIFFERENCE = [];
global JOIN_DIFFERENCE; JOIN_DIFFERENCE = [];
global CQ_COUNTS; CQ_COUNTS = [];
global SP_COUNTS; SP_COUNTS = [];
global SUM_CQ_COUNTS; SUM_CQ_COUNTS = [];
global SUM_SP_COUNTS; SUM_SP_COUNTS = [];
global CQ_PRIOR; CQ_PRIOR = [];
global SP_PRIOR; SP_PRIOR = [];
global LOC_SP_COUNTS; LOC_SP_COUNTS = [];
global LOC_CQ_COUNTS; LOC_CQ_COUNTS = [];

View file

@ -0,0 +1,52 @@
function T = cluster_own(Z,nclust)
% search down the dendogram from the root, until nclust clusters are found
% comments added by Lu Cheng
% 04.01.2011
maxclust = nclust;
% Start of algorithm
m = size(Z,1)+1;
T = zeros(m,1);
% maximum number of clusters based on inconsistency
if m <= maxclust
T = (1:m)';
elseif maxclust==1
T = ones(m,1);
else
clsnum = 1;
for k = (m-maxclust+1):(m-1)
i = Z(k,1); % left tree
if i <= m % original node, no leafs
T(i) = clsnum;
clsnum = clsnum + 1;
elseif i < (2*m-maxclust+1) % created before cutoff, search down the tree
T = clusternum(Z, T, i-m, clsnum);
clsnum = clsnum + 1;
end
i = Z(k,2); % right tree
if i <= m % original node, no leafs
T(i) = clsnum;
clsnum = clsnum + 1;
elseif i < (2*m-maxclust+1) % created before cutoff, search down the tree
T = clusternum(Z, T, i-m, clsnum);
clsnum = clsnum + 1;
end
end
end
function T = clusternum(X, T, k, c)
m = size(X,1)+1;
while(~isempty(k))
% Get the children of nodes at this level
children = X(k,1:2);
children = children(:);
% Assign this node number to leaf children
t = (children<=m);
T(children(t)) = c;
% Move to next level
k = children(~t) - m;
end

View file

@ -0,0 +1,27 @@
function [cliqcounts, sepcounts] = computeCounts(cliques, separators, npops)
global PARTITION;
ncliq = size(cliques,1);
nsep = size(separators,1);
cliqPartition = zeros(ncliq, size(cliques,2));
sepPartition = zeros(nsep, size(separators, 2));
apuCliq = find(cliques > 0);
apuSep = find(separators > 0);
cliqPartition(apuCliq) = PARTITION(cliques(apuCliq));
sepPartition(apuSep) = PARTITION(separators(apuSep));
cliqcounts = zeros(ncliq, npops);
for i = 1:npops
cliqcounts(:,i) = sum(cliqPartition == i, 2);
end
sepcounts = zeros(nsep, npops);
for i = 1:npops
sepcounts(:,i) = sum(sepPartition == i, 2);
end
%-------------------------------------------------------------------------

View file

@ -0,0 +1,19 @@
function diffInCliqCounts = computeDiffInCliqCounts(cliques, inds)
% Laskee muutoksen CLIQCOUNTS:ssa (tai SEPCOUNTS:ssa, jos syötteen?
% separators) kun yksilöt inds siirretään.
% diffInCliqcounts on ncliq*1 taulu, joka on CLIQCOUNTS:n sarakkeesta josta
% yksilöt inds siirretään ja lisättäv?sarakkeeseen, johon yksilöt
% siirretään.
% taken from spatial model of Jukka Siren's code
% Lu Cheng
% 15.12.2012
ncliq = size(cliques,1);
diffInCliqCounts = zeros(ncliq,1);
ninds = length(inds);
for i = 1:ninds
ind = inds(i);
rivit = sum((cliques == ind),2);
diffInCliqCounts = diffInCliqCounts + rivit;
end

View file

@ -0,0 +1,7 @@
function [counts sumcounts] = computeDiffInCounts(rows, data, nLetters)
% calculate the counts of the given rows of the data (ninds*nLoci)
% nLetters is the maximum number of different symbols over all loci
% Lu Cheng, 25.05.2011
counts = histc(data(rows,:),1:nLetters,1);
sumcounts = sum(counts,1)';

View file

@ -0,0 +1,42 @@
function logml = computeTotalLogml
% compute the log marginal likelihood of the data
% Lu Cheng, 15.12.2012
global LOGML_TABLE;
global LOC_CQ_COUNTS;
global LOC_SP_COUNTS;
notEmpty = any(LOC_CQ_COUNTS,1);
npops = length(find(notEmpty == 1));
% the following codes added by Lu Cheng, 15.12.2012
% some lines might all be zero if some sequence is deleted
tmpIndsCq = find(any(LOC_CQ_COUNTS,2));
tmpIndsSp = find(any(LOC_SP_COUNTS,2));
locCqCounts = LOC_CQ_COUNTS(tmpIndsCq,notEmpty);
locSpCounts = LOC_SP_COUNTS(tmpIndsSp,notEmpty);
sumcliq=sum(locCqCounts, 2);
sumsep=sum(locSpCounts, 2);
ncliq = length(tmpIndsCq);
nsep = length(tmpIndsSp);
cliqsizes = sum(locCqCounts, 2)';
sepsizes = sum(locSpCounts, 2)';
cliqsizes = min([cliqsizes; npops*ones(1,ncliq)])';
sepsizes = min([sepsizes; npops*ones(1,nsep)])';
klikkitn = sum(sum(gammaln(locCqCounts + repmat(1./cliqsizes, [1 npops])))) ...
- sum(npops*(gammaln(1./cliqsizes))) ...
- sum(gammaln(sumcliq + 1));
septn = sum(sum(gammaln(locSpCounts + repmat(1./sepsizes, [1 npops])))) ...
- sum(npops*(gammaln(1./sepsizes))) ...
- sum(gammaln(sumsep + 1));
spatialPrior = (klikkitn - septn);
logml = sum(LOGML_TABLE) + spatialPrior;

View file

@ -0,0 +1,17 @@
function [sumcounts, counts] = initialCounts2(partition, data, npops, nLetters)
% initialize counts and sumcounts for the initial partition
% npops: number of populations in the partition
% nLetters: the maximum number of different symbols over all loci
% Lu Cheng, 25.05.2011
[nSeq nLoci] = size(data);
counts = zeros(nLetters,nLoci,npops);
sumcounts = zeros(nLoci,npops);
for i=1:npops
inds = (partition==i);
counts(:,:,i) = histc(data(inds,:),1:nLetters,1);
sumcounts(:,i) = sum(counts(:,:,i),1);
end

View file

@ -0,0 +1,366 @@
function [partition, logml, partitionSummary, logmldiff] = model_search_parallel(c, partition, orig_dist, roundTypes)
% This function clusters DNA alignment using "codon" model in Corander and Tang's
% paper: Bayesian analysis of population structure based on linked
% molecular information (2007), Mathematical Biosciences
% c: preprocessed data for the sequence alignment
% partition: initial partition of the individuals
% origdist: hamming distance between individuals, indexed by
% (1,2)(1,3)(14)...(2,3)(2,4).....(3,4)...(n-1,n)
% roundTypes: array of operation types
% Lu Cheng
% 15.12.2012
interactive = false;
global PARTITION;
global CQ_COUNTS;global SUM_CQ_COUNTS;
global SP_COUNTS;global SUM_SP_COUNTS;
global CQ_PRIOR; global SP_PRIOR;
global LOGML_TABLE;
global ADDITION_DIFFERENCE;
global REMOVAL_DIFFERENCE;
global JOIN_DIFFERENCE;
global LOC_SP_COUNTS;
global LOC_CQ_COUNTS;
clearGlobalVars;
nPOPS = length(unique(partition));
% PRIOR VALUES:
CQ_PRIOR = c.cqPrior;
SP_PRIOR = c.spPrior;
% Initialize PARTITION, **_COUNTS, SUM_**_COUNTS, alnMat
[sumCqCounts, cqCounts] = initialCounts2(partition, c.cqData, nPOPS, c.nMaxCqCodes);
[sumSpCounts, spCounts] = initialCounts2(partition, c.spData, nPOPS, c.nMaxSpCodes);
CQ_COUNTS = cqCounts; SUM_CQ_COUNTS = sumCqCounts;
SP_COUNTS = spCounts; SUM_SP_COUNTS = sumSpCounts;
PARTITION = partition;
[cliqcounts, sepcounts] = computeCounts(c.locCliques, c.locSeparators, nPOPS);
LOC_CQ_COUNTS = cliqcounts;
LOC_SP_COUNTS = sepcounts;
% alnMat = c.alnMat;
partitionSummary = -Inf*ones(30,2,nPOPS); % Tiedot 30 parhaasta partitiosta (npops ja logml)
partitionSummary(:,1,:) = zeros(30,1,nPOPS);
worstLogml = -Inf*ones(1, nPOPS); worstIndex = ones(1, nPOPS);
clear partition cqCounts sumCqCounts spCounts sumSpCounts
% Initialize LOGML_TABLE:
nINDS = c.nSeq;
LOGML_TABLE = zeros(nPOPS,1);
updateLogmlTable(1:nPOPS);
REMOVAL_DIFFERENCE = zeros(nINDS,1);
REMOVAL_DIFFERENCE(:,:) = nan;
ADDITION_DIFFERENCE = zeros(nINDS,nPOPS);
ADDITION_DIFFERENCE(:,:) = nan;
JOIN_DIFFERENCE = zeros(nPOPS, nPOPS);
JOIN_DIFFERENCE(:,:) = nan;
% ***********Doc:********************
% REMOVAL_DIFFERENCE(ind) tells the change in logml if ind is removed from
% its cluster. nan, if the cluster has changed, since the value was last
% calculated.
%
% ADDITION_DIFFERENCE(ind, pop) tells the change in logml if ind is added
% to cluster pop. nan, if the cluster has changed since the value was last
% calculated. Always nan, if pop is ind's own cluster.
%
% JOIN_DIFFERENCE(pop1,pop2) = tells the change in logml if pop1 and pop2
% are combined. nan, if either cluster has changed since the value was last
% calculated.
% ***********Doc end*****************
logml = computeTotalLogml;
disp('The beginning:');
% disp(['Partition: ' num2str(PARTITION')]);
disp(['Nclusters: ' num2str(length(unique(PARTITION)))]);
disp(['Log(ml*prior): ' num2str(logml)]);
disp(' ');
nnotEmptyPops = length(unique(PARTITION));
if logml>worstLogml(nnotEmptyPops);
[partitionSummary(:,:,nnotEmptyPops), added] = addToSummary(logml, ...
partitionSummary(:,:,nnotEmptyPops), worstIndex(nnotEmptyPops));
if (added==1)
[worstLogml(nnotEmptyPops), worstIndex(nnotEmptyPops)] = ...
min(partitionSummary(:,2,nnotEmptyPops));
end
end
% START SEARCH OF THE BEST PARTITION:
vipu = zeros(1,14);
if interactive
roundTypes = input('Input steps: ');
if ischar(roundTypes), roundTypes = str2num(roundTypes); end
end
ready = 0;
while ready ~= 1
% disp(['Performing steps: ' num2str(roundTypes)]);
for n = 1:length(roundTypes)
round = roundTypes(n);
moveCounter = 0;
if round==1 && vipu(1)==0 % move an individual to another population
% inds = randperm(nINDS);
inds = getMoveInds(orig_dist,nINDS); % get inds to be moved
for ind = inds(:)'
update_difference_tables(ind, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators, logml);
tmpDiff = REMOVAL_DIFFERENCE(ind) + ADDITION_DIFFERENCE(ind,:);
tmpDiff(PARTITION(ind)) = 0;
[maxChange, maxIndex] = max(tmpDiff);
if maxChange>1e-5
updateGlobalVariables(ind, maxIndex, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators);
% fprintf('moving from %d to %d.\n',PARTITION(ind),maxIndex)
logml = computeTotalLogml();
moveCounter = moveCounter+1;
vipu = zeros(1,14);
nnotEmptyPops = length(unique(PARTITION));
if logml>worstLogml(nnotEmptyPops);
[partitionSummary(:,:,nnotEmptyPops), added] = addToSummary(logml, ...
partitionSummary(:,:,nnotEmptyPops), worstIndex(nnotEmptyPops));
if (added==1)
[worstLogml(nnotEmptyPops), worstIndex(nnotEmptyPops)] = ...
min(partitionSummary(:,2,nnotEmptyPops));
end
end
end
end
if moveCounter==0, vipu(1)=1; end
disp(['Step 1: ' num2str(moveCounter) ' individuals were moved.']);
elseif round==2 && vipu(2)==0 % join two populations
update_join_difference(c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators, logml);
[maxChange, aux] = max(JOIN_DIFFERENCE(:));
[i1, i2] = ind2sub([nPOPS,nPOPS],aux);
if maxChange>1e-5
tmpInds = find(PARTITION==i1);
updateGlobalVariables(tmpInds, i2, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators);
logml = computeTotalLogml;
disp(['Step 2: Clusters ' num2str(i1) ' and ' num2str(i2) ' combined.']);
vipu = zeros(1,14);
nnotEmptyPops = length(unique(PARTITION));
if logml>worstLogml(nnotEmptyPops);
[partitionSummary(:,:,nnotEmptyPops), added] = addToSummary(logml, ...
partitionSummary(:,:,nnotEmptyPops), worstIndex(nnotEmptyPops));
if (added==1)
[worstLogml(nnotEmptyPops), worstIndex(nnotEmptyPops)] = ...
min(partitionSummary(:,2,nnotEmptyPops));
end
end
else
disp('Step 2: no changes.');
vipu(2)=1;
end
elseif ismember(round, 3:4) && vipu(round)==0 % Split a population, and move one subpopulation to another population
pops = randperm(nPOPS);
splitFlags = zeros(nPOPS,1);
for pop = pops(:)'
maxChange = 0;
indsToBeMoved = [];
inds2 = find(PARTITION==pop);
ninds2 = length(inds2);
if ninds2>4
if round==3
dist3 = getDistance(inds2, orig_dist, nINDS);
npops2 = min(20, floor(ninds2 / 5)); %Moneenko osaan jaetaan
elseif round==4
dist3 = getDistance(inds2, orig_dist, nINDS);
npops2 = 2;
end
Z3 = linkage(dist3);
T3 = cluster_own(Z3, npops2);
for i = 1:npops2
indsX = inds2(T3==i); indsX = indsX';
tmpChanges = calcLogmlChanges(indsX, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators, logml);
[tmpMaxChange, tmpMaxPop] = max(tmpChanges);
if tmpMaxChange>maxChange
maxChange = tmpMaxChange;
% i1 = pop;
i2 = tmpMaxPop;
indsToBeMoved = indsX;
end
end
if maxChange>1e-5
updateGlobalVariables(indsToBeMoved, i2, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators);
logml = computeTotalLogml;
splitFlags(pop)=1;
nnotEmptyPops = length(unique(PARTITION));
if logml>worstLogml(nnotEmptyPops);
[partitionSummary(:,:,nnotEmptyPops), added] = addToSummary(logml, ...
partitionSummary(:,:,nnotEmptyPops), worstIndex(nnotEmptyPops));
if (added==1)
[worstLogml(nnotEmptyPops), worstIndex(nnotEmptyPops)] = ...
min(partitionSummary(:,2,nnotEmptyPops));
end
end
end
end
end
if any(splitFlags)
disp(['Step ' num2str(round) ': ' num2str(sum(splitFlags)) ' populations were split.']);
vipu = zeros(1,14);
else
disp(['Step ' num2str(round) ': no changes.']);
vipu(round)=1;
end
end
end
if interactive
roundTypes = input('Input extra steps: ');
if ischar(roundTypes), roundTypes = str2num(roundTypes); end
else
roundTypes = [];
end
if isempty(roundTypes)
ready = 1;
end
end
%disp(' ');
disp('BEST PARTITION: ');
%disp(['Partition: ' num2str(PARTITION')]);
disp(['Nclusters: ' num2str(length(unique(PARTITION)))]);
disp(['Log(ml): ' num2str(logml)]);
disp(' ');
nPOPS= rmEmptyPopulation(c.locCliques, c.locSeparators);
ADDITION_DIFFERENCE(:) = NaN;
REMOVAL_DIFFERENCE(:) = NaN;
logmldiff = zeros(nINDS,nPOPS); % the change of logml if individual i is moved to group j
for i=1:nINDS
update_difference_tables(i, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators, logml);
logmldiff(i,:) = REMOVAL_DIFFERENCE(i)+ADDITION_DIFFERENCE(i,:);
if all(isnan(logmldiff(i,:)))
keyboard
end
end
logmldiff(isnan(logmldiff))=0;
partition = PARTITION;
%----------------------------------------------------------------------------
function [dist2, dind1, dind2] = getDistance(inds2, dist_orig, ninds)
% pick out the distrances between samples in "inds2" from "dist_orig"
% dist_orig specifies the distances of (1,2),(1,3),(1,4)......(ninds-1,ninds)
% Lu Cheng, 22.06.2011
if ~issorted(inds2)
error('inds2 is not in ascending order!');
end
ninds2 = length(inds2);
apu = zeros(nchoosek(ninds2,2),2);
irow = 1;
for i=1:ninds2-1
for j=i+1:ninds2
apu(irow, 1) = inds2(i);
apu(irow, 2) = inds2(j);
irow = irow+1;
end
end
dind1 = apu(:,1);
dind2 = apu(:,2);
apu = (apu(:,1)-1).*ninds - apu(:,1) ./ 2 .* (apu(:,1)-1) + (apu(:,2)-apu(:,1));
dist2 = dist_orig(apu);
%---------------------------------------------------------------
function inds = getMoveInds(dist_orig, ninds)
% get individual indexs to be moved to another cluster
% we always take the 30% individuals of each cluster which are most distant
% to each other
% Lu Cheng, 25.05.2011
global PARTITION;
pops = unique(PARTITION);
inds = [];
for tmpPop = pops(:)'
tmpInds = find(PARTITION == tmpPop)';
if(length(tmpInds)<20)
inds = [inds tmpInds(:)']; %#ok<AGROW>
continue;
end
[tmpDist, dind1, dind2] = getDistance(tmpInds,dist_orig,ninds);
tmpSDist = sort(tmpDist,'Descend');
tmpInds2 = find(tmpDist>tmpSDist(round(length(tmpSDist)*0.3)));
tmpInds3 = union(unique(dind1(tmpInds2)), unique(dind2(tmpInds2)));
inds = [inds tmpInds3(:)']; %#ok<AGROW>
end
% ------------------------------------------------------------
function [partitionSummary, added] = addToSummary(logml, partitionSummary, worstIndex)
% Tiedetään, ett?annettu logml on isompi kuin huonoin arvo
% partitionSummary taulukossa. Jos partitionSummary:ss?ei viel?ole
% annettua logml arvoa, niin lisätään worstIndex:in kohtaan uusi logml ja
% nykyist?partitiota vastaava nclusters:in arvo. Muutoin ei tehd?mitään.
global PARTITION;
apu = find(abs(partitionSummary(:,2)-logml)<1e-5);
if isempty(apu)
% Nyt löydetty partitio ei ole viel?kirjattuna summaryyn.
npops = length(unique(PARTITION));
partitionSummary(worstIndex,1) = npops;
partitionSummary(worstIndex,2) = logml;
added = 1;
else
added = 0;
end

View file

@ -0,0 +1,385 @@
function [partition, logml, partitionSummary, logmldiff] = model_search_pregroup(c, pgPart, pgDist, roundTypes, nMaxPops)
% This function clusters DNA alignment using "codon" model in Corander and Tang's
% paper: Bayesian analysis of population structure based on linked
% molecular information (2007), Mathematical Biosciences
% c: preprocessed data for the sequence alignment
% pgPart: partition which assign sequences to pregroups
% pgDist: distances between the pregroups
% (1,2)(1,3)(1,4)...(2,3)(2,4).....(3,4)...(n-1,n)
% roundTypes: array of operation types
% Lu Cheng
% 21.03.2012
interactive = false;
global PARTITION;
global CQ_COUNTS;global SUM_CQ_COUNTS;
global SP_COUNTS;global SUM_SP_COUNTS;
global CQ_PRIOR; global SP_PRIOR;
global LOGML_TABLE;
global ADDITION_DIFFERENCE;
global REMOVAL_DIFFERENCE;
global JOIN_DIFFERENCE;
global LOC_SP_COUNTS;
global LOC_CQ_COUNTS;
clearGlobalVars;
nINDS = c.nSeq;
nPOPS = nMaxPops;
% load pregroup information
nPregroup = length(unique(pgPart));
if nPregroup<nMaxPops
error('#pregroup: %d, nMaxPops: %d. Number of pregroups should be higher than maximum number of population. \n',nPregroup,nMaxPops);
end
pregroups = cell(nPregroup,1);
pgSize = zeros(nPregroup,1);
for i=1:nPregroup
pregroups{i} = find(pgPart==i);
pgSize(i) = length(pregroups{i});
end
pgZ = linkage(pgDist(:)','complete');
initPart = cluster(pgZ,'maxclust',nPOPS);
partition = zeros(nINDS,1);
for i=1:nPregroup
partition(pregroups{i}) = initPart(i);
end
clear i pgZ initPart
% PRIOR VALUES:
CQ_PRIOR = c.cqPrior;
SP_PRIOR = c.spPrior;
% Initialize PARTITION, **_COUNTS, SUM_**_COUNTS, alnMat
[sumCqCounts, cqCounts] = initialCounts2(partition, c.cqData, nPOPS, c.nMaxCqCodes);
[sumSpCounts, spCounts] = initialCounts2(partition, c.spData, nPOPS, c.nMaxSpCodes);
CQ_COUNTS = cqCounts; SUM_CQ_COUNTS = sumCqCounts;
SP_COUNTS = spCounts; SUM_SP_COUNTS = sumSpCounts;
PARTITION = partition;
[cliqcounts, sepcounts] = computeCounts(c.locCliques, c.locSeparators, nPOPS);
LOC_CQ_COUNTS = cliqcounts;
LOC_SP_COUNTS = sepcounts;
partitionSummary = -Inf*ones(30,2,nPOPS); % Tiedot 30 parhaasta partitiosta (npops ja logml)
partitionSummary(:,1,:) = zeros(30,1,nPOPS);
worstLogml = -Inf*ones(1, nPOPS); worstIndex = ones(1, nPOPS);
clear partition cqCounts sumCqCounts spCounts sumSpCounts;
% Initialize LOGML_TABLE:
nINDS = c.nSeq;
LOGML_TABLE = zeros(nPOPS,1);
updateLogmlTable(1:nPOPS);
REMOVAL_DIFFERENCE = zeros(nINDS,1);
REMOVAL_DIFFERENCE(:,:) = nan;
ADDITION_DIFFERENCE = zeros(nINDS,nPOPS);
ADDITION_DIFFERENCE(:,:) = nan;
JOIN_DIFFERENCE = zeros(nPOPS, nPOPS);
JOIN_DIFFERENCE(:,:) = nan;
% ***********Doc:********************
% REMOVAL_DIFFERENCE(ind) tells the change in logml if ind is removed from
% its cluster. nan, if the cluster has changed, since the value was last
% calculated.
%
% ADDITION_DIFFERENCE(ind, pop) tells the change in logml if ind is added
% to cluster pop. nan, if the cluster has changed since the value was last
% calculated. Always nan, if pop is ind's own cluster.
%
% JOIN_DIFFERENCE(pop1,pop2) = tells the change in logml if pop1 and pop2
% are combined. nan, if either cluster has changed since the value was last
% calculated.
% ***********Doc end*****************
logml = computeTotalLogml;
disp('The beginning:');
% disp(['Partition: ' num2str(PARTITION')]);
disp(['Nclusters: ' num2str(length(unique(PARTITION)))]);
disp(['Log(ml*prior): ' num2str(logml)]);
disp(' ');
nnotEmptyPops = length(unique(PARTITION));
if logml>worstLogml(nnotEmptyPops);
[partitionSummary(:,:,nnotEmptyPops), added] = addToSummary(logml, ...
partitionSummary(:,:,nnotEmptyPops), worstIndex(nnotEmptyPops));
if (added==1)
[worstLogml(nnotEmptyPops), worstIndex(nnotEmptyPops)] = ...
min(partitionSummary(:,2,nnotEmptyPops));
end
end
% START SEARCH OF THE BEST PARTITION:
vipu = zeros(1,14);
if interactive
roundTypes = input('Input steps: ');
if ischar(roundTypes), roundTypes = str2num(roundTypes); end
end
ready = 0;
while ready ~= 1
% disp(['Performing steps: ' num2str(roundTypes)]);
for n = 1:length(roundTypes)
round = roundTypes(n);
moveCounter = 0;
if round==1 && vipu(1)==0 % move an individual to another population
pgInds = getMoveInds(pgPart,pgDist,nPregroup); % get pregroup inds to be moved
for pgind = pgInds(:)'
% inds = cell2mat(pregroups(pgInds));
tmpInds = pregroups{pgind};
tmpChanges = calcLogmlChanges(tmpInds, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators, logml);
[maxChange, maxIndex] = max(tmpChanges);
if maxChange>1e-5
updateGlobalVariables(tmpInds, maxIndex, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes,c.locCliques, c.locSeparators);
% fprintf('moving from %d to %d.\n',PARTITION(ind),maxIndex)
logml = computeTotalLogml();
moveCounter = moveCounter+length(pgInds);
vipu = zeros(1,14);
nnotEmptyPops = length(unique(PARTITION));
if logml>worstLogml(nnotEmptyPops);
[partitionSummary(:,:,nnotEmptyPops), added] = addToSummary(logml, ...
partitionSummary(:,:,nnotEmptyPops), worstIndex(nnotEmptyPops));
if (added==1)
[worstLogml(nnotEmptyPops), worstIndex(nnotEmptyPops)] = ...
min(partitionSummary(:,2,nnotEmptyPops));
end
end
end
end
if moveCounter==0, vipu(1)=1; end
disp(['Step 1: ' num2str(moveCounter) ' pregroups were moved.']);
elseif round==2 && vipu(2)==0 % join two populations
update_join_difference(c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators, logml);
[maxChange, aux] = max(JOIN_DIFFERENCE(:));
[i1, i2] = ind2sub([nPOPS,nPOPS],aux);
if maxChange>1e-5
tmpInds = find(PARTITION==i1);
updateGlobalVariables(tmpInds, i2, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators);
logml = computeTotalLogml;
disp(['Step 2: Clusters ' num2str(i1) ' and ' num2str(i2) ' combined.']);
vipu = zeros(1,14);
nnotEmptyPops = length(unique(PARTITION));
if logml>worstLogml(nnotEmptyPops);
[partitionSummary(:,:,nnotEmptyPops), added] = addToSummary(logml, ...
partitionSummary(:,:,nnotEmptyPops), worstIndex(nnotEmptyPops));
if (added==1)
[worstLogml(nnotEmptyPops), worstIndex(nnotEmptyPops)] = ...
min(partitionSummary(:,2,nnotEmptyPops));
end
end
else
disp('Step 2: no changes.');
vipu(2)=1;
end
elseif ismember(round, 3:4) && vipu(round)==0 % Split a population, and move one subpopulation to another population
pops = randperm(nPOPS);
splitFlags = zeros(nPOPS,1);
for pop = pops(:)'
maxChange = 0;
indsToBeMoved = [];
inds2 = find(PARTITION==pop);
pgInds2 = unique(pgPart(inds2));
nPgInds2 = length(unique(pgPart(inds2)));
if nPgInds2>4
if round==3
dist3 = getDistance(pgInds2,pgDist,nPregroup);
npops2 = min(20, floor(nPgInds2 / 5));
elseif round==4
dist3 = getDistance(pgInds2,pgDist,nPregroup);
npops2 = 2;
end
Z3 = linkage(dist3(:)','complete');
T3 = cluster(Z3, 'maxclust', npops2);
for i = 1:npops2
indsX = pgInds2(T3==i);
indsX = cell2mat(pregroups(indsX));
tmpChanges = calcLogmlChanges(indsX, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators, logml);
[tmpMaxChange, tmpMaxPop] = max(tmpChanges);
if tmpMaxChange>maxChange
maxChange = tmpMaxChange;
% i1 = pop;
i2 = tmpMaxPop;
indsToBeMoved = indsX;
end
end
if maxChange>1e-5
updateGlobalVariables(indsToBeMoved, i2, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators);
logml = computeTotalLogml;
splitFlags(pop)=1;
nnotEmptyPops = length(unique(PARTITION));
if logml>worstLogml(nnotEmptyPops);
[partitionSummary(:,:,nnotEmptyPops), added] = addToSummary(logml, ...
partitionSummary(:,:,nnotEmptyPops), worstIndex(nnotEmptyPops));
if (added==1)
[worstLogml(nnotEmptyPops), worstIndex(nnotEmptyPops)] = ...
min(partitionSummary(:,2,nnotEmptyPops));
end
end
end
end
end
if any(splitFlags)
disp(['Step ' num2str(round) ': ' num2str(sum(splitFlags)) ' populations were split.']);
vipu = zeros(1,14);
else
disp(['Step ' num2str(round) ': no changes.']);
vipu(round)=1;
end
end
end
if interactive
roundTypes = input('Input extra steps: ');
if ischar(roundTypes), roundTypes = str2num(roundTypes); end
else
roundTypes = [];
end
if isempty(roundTypes)
ready = 1;
end
end
% disp(' ');
disp('BEST PARTITION: ');
% disp(['Partition: ' num2str(PARTITION')]);
disp(['Nclusters: ' num2str(length(unique(PARTITION)))]);
disp(['Log(ml): ' num2str(logml)]);
disp(' ');
nPOPS = rmEmptyPopulation(c.locCliques, c.locSeparators);
logmldiff = zeros(nPregroup,nPOPS); % the change of logml if pregroup i is moved to group j
for i=1:nPregroup
tmpInds = pregroups{i};
tmpChanges = calcLogmlChanges(tmpInds, c.cqData, c.nMaxCqCodes, ...
c.spData, c.nMaxSpCodes, c.locCliques, c.locSeparators, logml);
logmldiff(i,:) = tmpChanges';
end
logmldiff(isnan(logmldiff))=0;
partition = zeros(nPregroup,1);
for i=1:nPregroup
partition(i)=unique(PARTITION(pgPart==i));
end
%----------------------------------------------------------------------------
function [dist2, dind1, dind2] = getDistance(inds2, origDist, ninds)
% pick out the distrances between samples in "inds2" from "origDist"
% origDist specifies the distances of (1,2),(1,3),(1,4)......(ninds-1,ninds)
% Lu Cheng, 22.06.2011
if ~issorted(inds2)
error('inds2 is not in ascending order!');
end
ninds2 = length(inds2);
apu = zeros(nchoosek(ninds2,2),2);
irow = 1;
for i=1:ninds2-1
for j=i+1:ninds2
apu(irow, 1) = inds2(i);
apu(irow, 2) = inds2(j);
irow = irow+1;
end
end
dind1 = apu(:,1);
dind2 = apu(:,2);
apu = (apu(:,1)-1).*ninds - apu(:,1) ./ 2 .* (apu(:,1)-1) + (apu(:,2)-apu(:,1));
dist2 = origDist(apu);
%---------------------------------------------------------------
function inds = getMoveInds(pgPart, pgDist, nPregroup)
% get pregroup indexs to be moved to another cluster
% we always take the 35% pregroups of each cluster which are most distant
% to each other
% Lu Cheng, 22.06.2011
global PARTITION;
pops = unique(PARTITION);
inds = [];
for tmpPop = pops(:)'
tmpInds = unique(pgPart(PARTITION==tmpPop));
if(length(tmpInds)<20)
inds = [inds tmpInds(:)']; %#ok<AGROW>
continue;
end
[tmpDist, dind1, dind2] = getDistance(tmpInds,pgDist,nPregroup);
tmpVal = quantile(tmpDist,0.65);
tmpInds2 = find(tmpDist>tmpVal);
tmpInds3 = union(unique(dind1(tmpInds2)), unique(dind2(tmpInds2)));
inds = [inds tmpInds3(:)']; %#ok<AGROW>
end
% ------------------------------------------------------------
function [partitionSummary, added] = addToSummary(logml, partitionSummary, worstIndex)
% Tiedetään, ett?annettu logml on isompi kuin huonoin arvo
% partitionSummary taulukossa. Jos partitionSummary:ss?ei viel?ole
% annettua logml arvoa, niin lisätään worstIndex:in kohtaan uusi logml ja
% nykyist?partitiota vastaava nclusters:in arvo. Muutoin ei tehd?mitään.
global PARTITION;
apu = find(abs(partitionSummary(:,2)-logml)<1e-5);
if isempty(apu)
% Nyt löydetty partitio ei ole viel?kirjattuna summaryyn.
npops = length(unique(PARTITION));
partitionSummary(worstIndex,1) = npops;
partitionSummary(worstIndex,2) = logml;
added = 1;
else
added = 0;
end

View file

@ -0,0 +1,97 @@
function c = preprocAln(alnMat)
% This function preprocess the alignment matrix to cliques and separators
% Lu Cheng, 24.05.2011
[nSeq nLoci] = size(alnMat);
alnCell = mat2cell(alnMat,nSeq,ones(1,nLoci));
arrUniqBase = cellfun(@unique,alnCell,'UniformOutput',false); % unique base at each loci
arrUniqBaseNum = cellfun(@length,arrUniqBase);
arrCqNum = arrUniqBaseNum(1:end-2).*arrUniqBaseNum(2:end-1).*arrUniqBaseNum(3:end);
arrSpNum = arrUniqBaseNum(2:end-2).*arrUniqBaseNum(3:end-1);
nMaxCqCodes = max(arrCqNum);
nMaxSpCodes = max(arrSpNum);
cqCodes = cellfun(@myProd,arrUniqBase(1:end-2),arrUniqBase(2:end-1),arrUniqBase(3:end), ...
'UniformOutput',false);
spCodes = cellfun(@myProd,arrUniqBase(2:end-2),arrUniqBase(3:end-1), ...
'UniformOutput',false);
cqData = zeros(nSeq,length(cqCodes));
spData = zeros(nSeq,length(spCodes));
cqCounts = zeros(nMaxCqCodes,length(cqCodes));
spCounts = zeros(nMaxSpCodes,length(spCodes));
cqPrior = ones(nMaxCqCodes,length(cqCodes));
spPrior = ones(nMaxSpCodes,length(spCodes));
for i=1:nLoci-2
nCodeTmp = size(cqCodes{i},1);
for j=1:nCodeTmp
tmpInds = ismember(alnMat(:,i:i+2),cqCodes{i}(j,:),'rows');
cqData(tmpInds,i) = j;
cqCounts(j,i) = sum(tmpInds);
end
cqPrior(1:nCodeTmp,i) = 1/nCodeTmp;
if i==1
continue;
end
k=i-1;
nCodeTmp = size(spCodes{k},1);
for j=1:nCodeTmp
tmpInds = ismember(alnMat(:,i:i+1),spCodes{k}(j,:),'rows');
spData(tmpInds,k) = j;
spCounts(j,k) = sum(tmpInds);
end
spPrior(1:nCodeTmp,k) = 1/nCodeTmp;
end
c.nSeq = nSeq;
% c.alnMat = alnMat;
c.arrUniqBase = arrUniqBase;
c.arrUniqBaseNum = arrUniqBaseNum;
c.nMaxCqCodes = nMaxCqCodes;
c.nMaxSpCodes = nMaxSpCodes;
c.cqCodes = cqCodes;
c.spCodes = spCodes;
c.cqData = cqData;
c.spData = spData;
c.cqCounts = cqCounts;
c.spCounts = spCounts;
c.cqPrior = cqPrior;
c.spPrior = spPrior;
function y = myProd(varargin)
% calculate the cartesian product for the input
% Lu Cheng, 24.05.2011
if nargin==2
set1 = varargin{1};
set2 = varargin{2};
[t1 t2] = meshgrid(set1,set2);
y = [t1(:) t2(:)];
elseif nargin==3
set1 = varargin{1};
set2 = varargin{2};
set3 = varargin{3};
[t1 t2 t3] = meshgrid(set1,set2,set3);
y = [t1(:) t2(:) t3(:)];
else
y = [];
end

View file

@ -0,0 +1,41 @@
function [npops notEmpty] = rmEmptyPopulation(locCliques,locSeparators)
% remove empty populations from CQ_COUNTS and SUM_CQ_COUNTS, SP_COUNTS,
% SUM_SP_COUNTS
% update PARTITION
% Lu Cheng, 15.12.2012
global CQ_COUNTS; global SUM_CQ_COUNTS;
global SP_COUNTS; global SUM_SP_COUNTS;
global PARTITION;
global LOGML_TABLE;
global ADDITION_DIFFERENCE;
global JOIN_DIFFERENCE;
global LOC_CQ_COUNTS;
global LOC_SP_COUNTS;
notEmpty = find(any(SUM_CQ_COUNTS,1) & any(SUM_SP_COUNTS,1));
CQ_COUNTS = CQ_COUNTS(:,:,notEmpty);
SP_COUNTS = SP_COUNTS(:,:,notEmpty);
SUM_CQ_COUNTS = SUM_CQ_COUNTS(:,notEmpty);
SUM_SP_COUNTS = SUM_SP_COUNTS(:,notEmpty);
LOGML_TABLE = LOGML_TABLE(notEmpty);
ADDITION_DIFFERENCE = ADDITION_DIFFERENCE(:,notEmpty);
JOIN_DIFFERENCE = JOIN_DIFFERENCE(notEmpty,notEmpty);
for i=1:length(notEmpty)
apu = (PARTITION==notEmpty(i));
PARTITION(apu)=i;
end
npops = length(notEmpty);
[cliqcounts, sepcounts] = computeCounts(locCliques, locSeparators, npops);
LOC_CQ_COUNTS = cliqcounts;
LOC_SP_COUNTS = sepcounts;

View file

@ -0,0 +1,61 @@
function updateGlobalVariables(inds, i2, cqData, nCqCodes, spData, nSpCodes, locCliques, locSeparators)
% this function moves the samples specified by "inds" to cluser i2
% then update all the global variables, "inds" are supposed to come from the
% same cluster
% Lu Cheng, 15.12.2012
global PARTITION;
global CQ_COUNTS; global SUM_CQ_COUNTS;
global SP_COUNTS; global SUM_SP_COUNTS;
global ADDITION_DIFFERENCE;
global REMOVAL_DIFFERENCE;
global JOIN_DIFFERENCE;
global LOC_SP_COUNTS;
global LOC_CQ_COUNTS;
i1 = PARTITION(inds(1));
PARTITION(inds)=i2;
[diffCqCounts diffCqSumCounts]= computeDiffInCounts(inds, cqData, nCqCodes);
[diffSpCounts diffSpSumCounts]= computeDiffInCounts(inds, spData, nSpCodes);
diffLocCqCounts = computeDiffInCliqCounts(locCliques, inds);
diffLocSpCounts = computeDiffInCliqCounts(locSeparators, inds);
CQ_COUNTS(:,:,i1) = CQ_COUNTS(:,:,i1) - diffCqCounts;
SP_COUNTS(:,:,i1) = SP_COUNTS(:,:,i1) - diffSpCounts;
SUM_CQ_COUNTS(:,i1) = SUM_CQ_COUNTS(:,i1) - diffCqSumCounts;
SUM_SP_COUNTS(:,i1) = SUM_SP_COUNTS(:,i1) - diffSpSumCounts;
LOC_CQ_COUNTS(:,i1) = LOC_CQ_COUNTS(:,i1) - diffLocCqCounts;
LOC_SP_COUNTS(:,i1) = LOC_SP_COUNTS(:,i1) - diffLocSpCounts;
CQ_COUNTS(:,:,i2) = CQ_COUNTS(:,:,i2) + diffCqCounts;
SP_COUNTS(:,:,i2) = SP_COUNTS(:,:,i2) + diffSpCounts;
SUM_CQ_COUNTS(:,i2) = SUM_CQ_COUNTS(:,i2) + diffCqSumCounts;
SUM_SP_COUNTS(:,i2) = SUM_SP_COUNTS(:,i2) + diffSpSumCounts;
LOC_CQ_COUNTS(:,i2) = LOC_CQ_COUNTS(:,i2) + diffLocCqCounts;
LOC_SP_COUNTS(:,i2) = LOC_SP_COUNTS(:,i2) + diffLocSpCounts;
updateLogmlTable([i1 i2]);
REMOVAL_DIFFERENCE(PARTITION==i1) = nan;
REMOVAL_DIFFERENCE(PARTITION==i2) = nan;
ADDITION_DIFFERENCE(:,[i1 i2]) = nan;
JOIN_DIFFERENCE(:,i2) = nan;
JOIN_DIFFERENCE(i2,:) = nan;
if ~any(PARTITION==i1)
% i1 became empty
JOIN_DIFFERENCE(:,i1) = 0;
JOIN_DIFFERENCE(i1,:) = 0;
JOIN_DIFFERENCE(i1,i1) = nan;
else
JOIN_DIFFERENCE(:,i1) = nan;
JOIN_DIFFERENCE(i1,:) = nan;
end

View file

@ -0,0 +1,37 @@
function updateLogmlTable(pops)
% Updates global variables LOGML_TABLE, npops*1 array, logml values for
% each population given in "pops"
% After the updates, the values are based on the current values of the
% global variables CQ_COUNTS, SUM_CQ_COUNTS, SP_COUNTS, SUM_SP_COUNTS
% Lu Cheng, 25.05.2011
global CQ_COUNTS; global SUM_CQ_COUNTS;
global SP_COUNTS; global SUM_SP_COUNTS;
global CQ_PRIOR; global SP_PRIOR;
global LOGML_TABLE;
tmpN = length(pops);
tmpCqPrior = repmat(CQ_PRIOR,[1 1 tmpN]);
tmpSpPrior = repmat(SP_PRIOR,[1 1 tmpN]);
term1 = 0-gammaln(1+SUM_CQ_COUNTS(:,pops));
term2 = sum(gammaln(tmpCqPrior+CQ_COUNTS(:,:,pops))-gammaln(tmpCqPrior) , 1);
if length(pops) > 1
term2 = squeeze(term2);
else
term2 = term2';
end
term3 = 0-gammaln(1+SUM_SP_COUNTS(:,pops));
term4 = sum(gammaln(tmpSpPrior+SP_COUNTS(:,:,pops))-gammaln(tmpSpPrior) , 1);
if length(pops) > 1
term4 = squeeze(term4);
else
term4 = term4';
end
LOGML_TABLE(pops) = sum(term1+term2) - sum(term3+term4);
%----------------------------------------------------------------------

View file

@ -0,0 +1,92 @@
function update_difference_tables(ind, cqData, nCqLetter, ...
spData, nSpLetter, locCliques, locSeparators,logml)
% update ADDITION_DIFFERENCE and REMOVAL_DIFFERENCE
% Lu Cheng, 15.12.2012
global CQ_COUNTS; global SUM_CQ_COUNTS;
global SP_COUNTS; global SUM_SP_COUNTS;
global PARTITION;
global ADDITION_DIFFERENCE;
global REMOVAL_DIFFERENCE;
global LOC_CQ_COUNTS;
global LOC_SP_COUNTS;
rem_old = REMOVAL_DIFFERENCE;
add_old = ADDITION_DIFFERENCE;
[diffCqCounts diffCqSumCounts] = computeDiffInCounts(ind, cqData, nCqLetter);
[diffSpCounts diffSpSumCounts] = computeDiffInCounts(ind, spData, nSpLetter);
diffLocCqCounts = computeDiffInCliqCounts(locCliques, ind);
diffLocSpCounts = computeDiffInCliqCounts(locSeparators, ind);
i1 = PARTITION(ind);
if isnan(rem_old(ind))
% Update removal difference for the individual:
% note that we did NOT add the removed item to other clusters
CQ_COUNTS(:,:,i1) = CQ_COUNTS(:,:,i1) - diffCqCounts;
SP_COUNTS(:,:,i1) = SP_COUNTS(:,:,i1) - diffSpCounts;
SUM_CQ_COUNTS(:,i1) = SUM_CQ_COUNTS(:,i1) - diffCqSumCounts;
SUM_SP_COUNTS(:,i1) = SUM_SP_COUNTS(:,i1) - diffSpSumCounts;
LOC_CQ_COUNTS(:,i1) = LOC_CQ_COUNTS(:,i1) - diffLocCqCounts;
LOC_SP_COUNTS(:,i1) = LOC_SP_COUNTS(:,i1) - diffLocSpCounts;
% PARTITION(ind) = -1;
updateLogmlTable(i1);
logml_new = computeTotalLogml();
rem_old(ind) = logml_new-logml;
CQ_COUNTS(:,:,i1) = CQ_COUNTS(:,:,i1) + diffCqCounts;
SP_COUNTS(:,:,i1) = SP_COUNTS(:,:,i1) + diffSpCounts;
SUM_CQ_COUNTS(:,i1) = SUM_CQ_COUNTS(:,i1) + diffCqSumCounts;
SUM_SP_COUNTS(:,i1) = SUM_SP_COUNTS(:,i1) + diffSpSumCounts;
LOC_CQ_COUNTS(:,i1) = LOC_CQ_COUNTS(:,i1) + diffLocCqCounts;
LOC_SP_COUNTS(:,i1) = LOC_SP_COUNTS(:,i1) + diffLocSpCounts;
% PARTITION(ind) = i1;
updateLogmlTable(i1);
end
new_pops = isnan(add_old(ind,:));
new_pops(i1) = 0; % Own cluster needs never be calculated.
new_pops = find(new_pops);
for i2 = new_pops(:)'
% Update addition differences for the individual:
% note that we did NOT remove the item
CQ_COUNTS(:,:,i2) = CQ_COUNTS(:,:,i2) + diffCqCounts;
SP_COUNTS(:,:,i2) = SP_COUNTS(:,:,i2) + diffSpCounts;
SUM_CQ_COUNTS(:,i2) = SUM_CQ_COUNTS(:,i2) + diffCqSumCounts;
SUM_SP_COUNTS(:,i2) = SUM_SP_COUNTS(:,i2) + diffSpSumCounts;
LOC_CQ_COUNTS(:,i2) = LOC_CQ_COUNTS(:,i2) + diffLocCqCounts;
LOC_SP_COUNTS(:,i2) = LOC_SP_COUNTS(:,i2) + diffLocSpCounts;
% PARTITION(ind) = i2;
updateLogmlTable(i2);
logml_new = computeTotalLogml();
add_old(ind,i2) = logml_new - logml;
CQ_COUNTS(:,:,i2) = CQ_COUNTS(:,:,i2) - diffCqCounts;
SP_COUNTS(:,:,i2) = SP_COUNTS(:,:,i2) - diffSpCounts;
SUM_CQ_COUNTS(:,i2) = SUM_CQ_COUNTS(:,i2) - diffCqSumCounts;
SUM_SP_COUNTS(:,i2) = SUM_SP_COUNTS(:,i2) - diffSpSumCounts;
LOC_CQ_COUNTS(:,i2) = LOC_CQ_COUNTS(:,i2) - diffLocCqCounts;
LOC_SP_COUNTS(:,i2) = LOC_SP_COUNTS(:,i2) - diffLocSpCounts;
% PARTITION(ind) = i1;
updateLogmlTable(i2);
end
REMOVAL_DIFFERENCE = rem_old;
ADDITION_DIFFERENCE = add_old;
%---------------------------------------------------------------------

View file

@ -0,0 +1,83 @@
function update_join_difference(cqData, nCqCodes, spData, nSpCodes, locCliques, locSeparators, logml)
% update JOIN_DIFFERENCE
% Lu Cheng, 15.12.2012
global CQ_COUNTS; global SUM_CQ_COUNTS;
global SP_COUNTS; global SUM_SP_COUNTS;
global PARTITION;
global JOIN_DIFFERENCE;
global LOC_CQ_COUNTS;
global LOC_SP_COUNTS;
npops = size(CQ_COUNTS,3);
for i1 = 1:npops-1
indsToBeMoved = find(PARTITION==i1);
if isempty(indsToBeMoved)
% Cluster i1 is empty
JOIN_DIFFERENCE(i1,(i1+1):npops) = 0;
JOIN_DIFFERENCE((i1+1):npops,i1) = 0;
else
[diffCqCounts diffCqSumCounts] = computeDiffInCounts(indsToBeMoved, cqData, nCqCodes);
[diffSpCounts diffSpSumCounts] = computeDiffInCounts(indsToBeMoved, spData, nSpCodes);
diffLocCqCounts = computeDiffInCliqCounts(locCliques, indsToBeMoved);
diffLocSpCounts = computeDiffInCliqCounts(locSeparators, indsToBeMoved);
unknown_pops = find(isnan(JOIN_DIFFERENCE(i1,(i1+1):end)));
unknown_pops = unknown_pops+i1;
CQ_COUNTS(:,:,i1) = CQ_COUNTS(:,:,i1) - diffCqCounts;
SUM_CQ_COUNTS(:,i1) = SUM_CQ_COUNTS(:,i1) - diffCqSumCounts;
SP_COUNTS(:,:,i1) = SP_COUNTS(:,:,i1) - diffSpCounts;
SUM_SP_COUNTS(:,i1) = SUM_SP_COUNTS(:,i1) - diffSpSumCounts;
LOC_CQ_COUNTS(:,i1) = LOC_CQ_COUNTS(:,i1) - diffLocCqCounts;
LOC_SP_COUNTS(:,i1) = LOC_SP_COUNTS(:,i1) - diffLocSpCounts;
% PARTITION(indsToBeMoved) = -1;
updateLogmlTable(i1);
for i2 = unknown_pops
CQ_COUNTS(:,:,i2) = CQ_COUNTS(:,:,i2) + diffCqCounts;
SUM_CQ_COUNTS(:,i2) = SUM_CQ_COUNTS(:,i2) + diffCqSumCounts;
SP_COUNTS(:,:,i2) = SP_COUNTS(:,:,i2) + diffSpCounts;
SUM_SP_COUNTS(:,i2) = SUM_SP_COUNTS(:,i2) + diffSpSumCounts;
LOC_CQ_COUNTS(:,i2) = LOC_CQ_COUNTS(:,i2) + diffLocCqCounts;
LOC_SP_COUNTS(:,i2) = LOC_SP_COUNTS(:,i2) + diffLocSpCounts;
% PARTITION(indsToBeMoved) = i2;
updateLogmlTable(i2);
logml_new = computeTotalLogml();
JOIN_DIFFERENCE(i1,i2) = logml_new-logml;
JOIN_DIFFERENCE(i2,i1) = logml_new-logml;
CQ_COUNTS(:,:,i2) = CQ_COUNTS(:,:,i2) - diffCqCounts;
SUM_CQ_COUNTS(:,i2) = SUM_CQ_COUNTS(:,i2) - diffCqSumCounts;
SP_COUNTS(:,:,i2) = SP_COUNTS(:,:,i2) - diffSpCounts;
SUM_SP_COUNTS(:,i2) = SUM_SP_COUNTS(:,i2) - diffSpSumCounts;
LOC_CQ_COUNTS(:,i2) = LOC_CQ_COUNTS(:,i2) - diffLocCqCounts;
LOC_SP_COUNTS(:,i2) = LOC_SP_COUNTS(:,i2) - diffLocSpCounts;
% PARTITION(indsToBeMoved) = -1;
updateLogmlTable(i2);
end
CQ_COUNTS(:,:,i1) = CQ_COUNTS(:,:,i1) + diffCqCounts;
SUM_CQ_COUNTS(:,i1) = SUM_CQ_COUNTS(:,i1) + diffCqSumCounts;
SP_COUNTS(:,:,i1) = SP_COUNTS(:,:,i1) + diffSpCounts;
SUM_SP_COUNTS(:,i1) = SUM_SP_COUNTS(:,i1) + diffSpSumCounts;
LOC_CQ_COUNTS(:,i1) = LOC_CQ_COUNTS(:,i1) + diffLocCqCounts;
LOC_SP_COUNTS(:,i1) = LOC_SP_COUNTS(:,i1) + diffLocSpCounts;
% PARTITION(indsToBeMoved) = i1;
updateLogmlTable(i1);
end
end