SearchMissingsMultiFamily<-function(pedigrees,genotypesFamily,Gpool,candidatesList,FamilyNames,IndNames,Alleles,Probs){
output<-list()
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){
ptemp<-MakePedigreeFromFamilyInfo(p)
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)
plot(ptemp,id=unlist(IndNs))
pdffile<-paste(FamilyNs,".pdf")
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]]]]
}
BaseMade<-FALSE
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,]
numtested<-length(ToTestCurrent)
tmpcandidates<-list()
if(numtested>0){
for(k in 1:numtested){
selectedToBeTested<-which(searched==ToTestCurrent[k])
tmpcandidates[[k]]<-candidates[[selectedToBeTested]]
}
}
tmpTestPattern<-MakeTestPattern(tmpcandidates)
for(k in 1:length(tmpTestPattern[,1])){
tmpG2<-G
tmpG2[ToTestCurrent,,]<-Gpool[tmpTestPattern[k,],,]
tttmpG<-tmpG2[,,1]
for(i in 2:length(tmpG2[1,1,])){
tttmpG<-cbind(tttmpG,tmpG2[,,i])
}
tmpPed2<-setMarkers(tmpPed, m=tttmpG)
mCout<-mendelianCheck(tmpPed2)
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)
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)
}
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]])
}