染色体グラフの連結グラフに番号をつける

  • 染色体グラフは複数のグラフの集まりである(ことが多い。血族婚などで一つにつながることもある)
  • 個々の連結グラフに番号をつけよう
SeparateGraphs<-function(hG){
	ns<-length(hG[,1])
	assigned<-rep(0,ns)
	cnt<-1
	for(i in 1:length(hG[,1])){
		if(hG[i,1]!=0){
			tmpself<-assigned[i]
			tmpp1<-assigned[hG[i,1]]
			tmpp2<-assigned[hG[i,2]]
			M<-max(tmpself,tmpp1,tmpp2)
			if(M==0){
				M<-cnt
				cnt<-cnt+1
			}
			assigned[c(i,hG[i,1],hG[i,2])]<-M
		}
	}
	#assigned
	G<-list()
	hG2<-cbind(1:ns,hG)
	for(i in 1:max(assigned)){
		G[[i]]<-hG2[assigned==i,]
	}
	G
}

SeparateGraphs(hG)
  • 次の場合は2つのグラフに分かれることがわかる
p<-matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  0, 0, 0, 0, 2, 2, 4, 4, 6,  6,  0,  0, 12, 13,
  0, 0, 0, 0, 1, 1, 3, 3, 7,  7,  0,  0, 11, 10,
  0, 1, 0, 1, 0, 1, 0, 1, 0,  0,  0,  1,  1,  1,
  3,1,1,1,3,1,1,1,1,2,1,1,2,1),
  ncol=5)
> SeparateGraphs(hG)
[[1]]
      [,1] [,2] [,3]
 [1,]    1    0    0
 [2,]    2    0    0
 [3,]    3    0    0
 [4,]    4    0    0
 [5,]    5    0    0
 [6,]    6    0    0
 [7,]    7    0    0
 [8,]    8    0    0
 [9,]    9    3    4
[10,]   10    1    2
[11,]   11    3    4
[12,]   12    1    2
[13,]   13    7    8
[14,]   14    5    6
[15,]   15    7    8
[16,]   16    5    6
[17,]   17   11   12
[18,]   18   13   14
[19,]   19   11   12
[20,]   20   13   14
[21,]   28   19   20

[[2]]
     [,1] [,2] [,3]
[1,]   21    0    0
[2,]   22    0    0
[3,]   23    0    0
[4,]   24    0    0
[5,]   25   23   24
[6,]   26   21   22
[7,]   27   25   26
  • 近親婚でグラフが1つになるのはこちら
p<-matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  0, 0, 0, 0, 2, 2, 4, 4, 6,  6,  4,  0, 12, 13,
  0, 0, 0, 0, 1, 1, 3, 3, 7,  7,  3,  0, 11, 10,
  0, 1, 0, 1, 0, 1, 0, 1, 0,  0,  0,  1,  1,  1,
  3,1,1,1,3,1,1,1,1,2,1,1,2,1),
  ncol=5)
NL<-1
ped<-MakePedigreeFromFamilyInfo(p)
plot(ped)

hG<-MakeHaplotypeGraph2(p)

SeparateGraphs<-function(hG){
	ns<-length(hG[,1])
	assigned<-rep(0,ns)
	cnt<-1
	for(i in 1:length(hG[,1])){
		if(hG[i,1]!=0){
			tmpself<-assigned[i]
			tmpp1<-assigned[hG[i,1]]
			tmpp2<-assigned[hG[i,2]]
			M<-max(tmpself,tmpp1,tmpp2)
			if(M==0){
				M<-cnt
				cnt<-cnt+1
				assigned[c(i,hG[i,1],hG[i,2])]<-M
			}else{
				M<-max(tmpself,tmpp1,tmpp2)
				trioID<-c(i,hG[i,1],hG[i,2])
				trio<-c(tmpself,tmpp1,tmpp2)
				for(j in 1:length(trio)){
					if(trio[j]!=0){
						assigned[which(assigned==trio[j])]<-M
					}else{
						assigned[trioID[j]]<-M
					}
				}
			}
			#print(assigned)
			
		}
	}
	#assigned
	G<-list()
	cnt<-1
	hG2<-cbind(1:ns,hG)
	for(i in 1:max(assigned)){
		if(length(which(assigned==i))!=0){
			G[[cnt]]<-hG2[assigned==i,]
			cnt<-cnt+1
		}
		
	}
	G
}

SeparateGraphs(hG)