複数いる尤度解析対象者、複数いる候補者、これらを網羅的に組み合わせる

CalcLikeForCandidates<-function(p,G,candidates,Gcandidates,Alleles,Probs){
	# Gpoolの候補者が「ポピュレーションで観察される確率」をあらかじめ算出しよう
	
	LikeFromGenPop<-rep(0,length(Gpool2[,1,1]))
	for(i in 1:length(Alleles)){
		tmp<-OffspringGenotypeProb(Probs[[i]],Probs[[i]])
		for(j in 1:length(LikeFromGenPop)){
			tmpallele1<-which(Alleles[[i]]==Gpool2[j,1,i])
			tmpallele2<-which(Alleles[[i]]==Gpool2[j,2,i])
			tmp2<-log(tmp[min(tmpallele1,tmpallele2)[1],max(tmpallele1,tmpallele2)[1]],10)
			LikeFromGenPop[j]<-LikeFromGenPop[j]+tmp2
		}
	}
	
	# BaseLikeの計算(すべての被捜索者に候補者を当てはめない場合)
	BaseLike<-CalcLikeZ(p,G2,nucs,Alleles,Probs)
	
	SearchPattern<-MakeSearchPattern(searched)
	ret<-NULL
	ret2<-NULL
	ret3<-NULL
	counter<-1

	#SearchPattern<-2^(as.set(searched))
	for(i in 1:length(SearchPattern$X)){
	#for(i in SearchPattern){
		spx<-as.matrix(SearchPattern$X[[i]])
		
		for(j in 1:length(spx[,1])){
			ToTestCurrent<-spx[j,]
			tmpG2<-G
			#BaseLike<-CalcLogLikelihoodFamily(p,tmpG2,Alleles,Probs)$loglikesum
			#BaseLike<-CalcLogLikelihoodFamilyX(p,nucs,G,Alleles,Probs)$loglikesum
			#ret[[counter]]<-CalcLogLikelihoodFamily(p,tmpG2,Alleles,Probs)
			#ret2[[counter]]<-list(ToTestCurrent)
			#ret3[[counter]]<-list()
			#counter<-counter+1
			
			#numtested<-length(spx[j,])
			numtested<-length(ToTestCurrent)
			tmpcandidates<-list()
			if(numtested>0){
				for(k in 1:numtested){
					selectedToBeTested<-which(searched==ToTestCurrent[k])
					tmpcandidates[[k]]<-candidates[[selectedToBeTested]]
				}
			}

			#print("done for none")
			tmpTestPattern<-MakeTestPattern(tmpcandidates)
			for(k in 1:length(tmpTestPattern[,1])){
				#print("to be tested IDs in pedigree")
				#print(ToTestCurrent)
				#print("to be tested sampleIDs in pool")
				#print(tmpTestPattern[k,])

				tmpG2<-G
				tmpG2[ToTestCurrent,,]<-Gpool2[tmpTestPattern[k,],,]
				#print(tmpG2)
				LoutTmp<-CalcLikeZ(p,tmpG2,nucs,Alleles,Probs)
				#LoutTmp<-CalcLogLikelihoodFamily(p,tmpG2,Alleles,Probs)$loglikesum
				#print(LoutTmp)
				ret[[counter]]<-LoutTmp-BaseLike-sum(LikeFromGenPop[tmpTestPattern[k,]])
				ret2[[counter]]<-list(ToTestCurrent)
				ret3[[counter]]<-list(tmpTestPattern[k,])
				counter<-counter+1
			}

		}
	}
	list(LogLike=ret,SearchedID=ret2,CandidateID=ret3)

}
printTAPout2<-function(tap){
	n<-length(tap$LogLike)
	for(i in 1:n){
		if(tap$LogLike[[i]]!=-Inf){
			tmp<-paste("LogLike",tap$LogLike[[i]],"Prob",10^(tap$LogLike[[i]]),"SearchedID",c(tap$SearchedID[[i]]),"CandidateID",c(tap$CandidateID[[i]]))
			print(tmp)
		}
		
	}
}
clfcout<-CalcLikeForCandidates(p,G,candidates,Gcandidates,Alleles,Probs)

printTAPout2(clfcout)