マルチ家族の情報を受け入れて処理・家族ごとに処理

# pedigreeごとに
# 家系情報
# 生存・協力者のジェノタイプ情報
# 被捜索者ごとに、身元不明者リスト中の候補者がリストアップされている
SearchMissingsMultiFamily<-function(pedigrees,genotypesFamily,Gpool,candidatesList,FamilyNames,IndNames,Alleles,Probs){
	output<-list()
	# Gpoolの候補者が「ポピュレーションで観察される確率」をあらかじめ算出しよう
	
	LikeFromGenPop<-rep(0,length(Gpool[,1,1]))
	for(i in 1:length(Alleles)){
		tmp<-OffspringGenotypeProb(Probs[[i]],Probs[[i]])
		for(j in 1:length(LikeFromGenPop)){
			tmpallele1<-which(Alleles[[i]]==Gpool[j,1,i])
			tmpallele2<-which(Alleles[[i]]==Gpool[j,2,i])
			tmp2<-log(tmp[min(tmpallele1,tmpallele2)[1],max(tmpallele1,tmpallele2)[1]],10)
			LikeFromGenPop[j]<-LikeFromGenPop[j]+tmp2
		}
	}

	for(ip in 1:length(pedigrees)){
		output[[ip]]<-SearchMissingsPerFamily(pedigrees[[ip]],genotypesFamily[[ip]],
		Gpool,candidatesList[[ip]],FamilyNames[[ip]],IndNames[[ip]],Alleles,Probs,LikeFromGenPop)
	}
	output
}




SearchMissingsPerFamily<-function(p,G,Gpool,candidatesL,FamilyNs,IndNs,Alleles,Probs,LikeFromGenPop){
	# 出力は家系ごと
	
	# 家系情報を遺伝系パッケージ用のオブジェクトにする
	# kinshipパッケージオブジェクト:家系図描図用
	ptemp<-MakePedigreeFromFamilyInfo(p)
	# paramlinkパッケージオブジェクト:メンデリアンチェック用
	tmpPed<-p[,1:5]
	tmpPed<-data.frame(ID=tmpPed[,1],FID=tmpPed[,3],MID=tmpPed[,2],SEX=tmpPed[,4]+1,AFF=tmpPed[,5]-1)
	tmpPed<-linkdat(tmpPed,model=1)

	# 家系図を描いてPDF出力する
	plot(ptemp,id=unlist(IndNs))
	pdffile<-paste(FamilyNs,".pdf")
	#print(IndNs)
	pdf(pdffile)
	plot(ptemp,id=unlist(IndNs))
	dev.off()
	
	# 核家族処理をする
	nucs<-subnucs(p)

	# 尤度を計算しよう
	searched<-which(p[,5]==2)
	candidates<-list()
	for(i in 1:length(searched)){
		candidates[[i]]<-candidatesL[[searched[[i]]]]
	}
	#print(candidates)
	# BaseLikeの計算(すべての被捜索者に候補者を当てはめない場合)は
	# 1チェックパターンでも「メンデリアンチェックを通ったら」実施する
	# そのためのチェッカー
	BaseMade<-FALSE
	#BaseLike<-CalcLikeZ(p,G,nucs,Alleles,Probs)
	
	SearchPattern<-MakeSearchPattern(searched)
	ret<-NULL
	ret2<-NULL
	ret3<-NULL
	counter<-1

	for(i in 1:length(SearchPattern$X)){
		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,,]<-Gpool[tmpTestPattern[k,],,]
				#print(tmpG2)
				# mendelian Check
				tttmpG<-tmpG2[,,1]
				for(i in 2:length(tmpG2[1,1,])){
					tttmpG<-cbind(tttmpG,tmpG2[,,i])
				}


				tmpPed2<-setMarkers(tmpPed, m=tttmpG)
				
				mCout<-mendelianCheck(tmpPed2)
				#print("mendelianChecked")
				#print(mCout)
				#print(length(mCout))
				if(length(mCout)!=0){
					ret[[counter]]<--Inf
					ret2[[counter]]<-list(ToTestCurrent)
					ret3[[counter]]<-list(tmpTestPattern[k,])
					counter<-counter+1
				}else{
					if(!BaseMade){
						BaseLike<-CalcLikeZ(p,G,nucs,Alleles,Probs)
						BaseMade<-TRUE
					}
					LoutTmp<-CalcLikeZ(p,tmpG2,nucs,Alleles,Probs)
					#LoutTmp<-CalcLogLikelihoodFamily(p,tmpG2,Alleles,Probs)$loglikesum
					#print(LoutTmp)
					# tmpTestPattern[k,]って変?
					ret[[counter]]<-LoutTmp-BaseLike-sum(LikeFromGenPop[tmpTestPattern[k,]])
					ret2[[counter]]<-list(ToTestCurrent)
					ret3[[counter]]<-list(tmpTestPattern[k,])
					counter<-counter+1
				}
				
			}

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


SMMFout<-SearchMissingsMultiFamily(pedigrees=pedigrees,genotypesFamily=genotypesFamily,Gpool=Gpool2,candidatesList=candidatesList,FamilyNames=FamilyNames,IndNames=IndNames,Alleles=Alleles,Probs=Probs)

for(ip in 1:length(pedigrees)){
	printTAPout2(SMMFout[[ip]])

}