I am trying to using a for loop and if else statements to extract kin relationships from a matrix which has rows representing an unordered mix or related and unrelated individuals. There are identical twins, fraternal twins, other non-twin siblings and otherwise unrelated individuals.
Each individual's data is stored in a separate row, with columns for the subject IDs, parent IDs, gender, and so on. However, existing R packages all require there to be data rows for the parents in order to properly construct a pedigree tree. I created a new matrix, with rows for the parents that only contain their parent ID as the subject ID and I was able to use this new data matrix to calculate a kinship matrix using the package "kinship2".
However, I can't figure out how to calculate the coefficients relatedness (1 for identical twin/self, 0.5 for fraternal twin/full sibling, 0.25 for half-sibling, 0 for unrelated) and populate a vector of family assignments (i.e. a unique identifier for each family). I need both of these vectors in order to properly conduct heritability analyses.
Can you please let me know how you think I can write code in R to do this/tell me how I can fix the code I already wrote?
Here is a representative portion of the original matrix:
> pedigree.data$Subject[1:10]
[1] 100307 100408 101006 101107 101309 101410 101915 102008 102311 102816
> pedigree.data$Twin_Stat[1:10]
[1] "Twin" "Twin" "Twin" "NotTwin" "NotTwin" "Twin" "NotTwin" "NotTwin" "Twin" "Twin"
> pedigree.data$Zygosity[1:10]
[1] "NotMZ" "MZ" "MZ" "NotTwin" "NotTwin" "NotMZ" "NotTwin" "NotTwin" "MZ" "MZ"
> pedigree.data$Father_ID[1:10]
[1] 81352 81594 81149 81833 82248 82061 81841 81882 81543 81283
pedigree.data$Mother_ID[1:10]
[1] 51488 51730 51283 51969 52385 52198 51977 52018 51679 51418
And here is the code that I wrote to compute the coefficients:
Kin.Score <- matrix(0,1,542)
count <- 0
for(k in 1:(length(pedigree.data$Twin_Stat)-1)){
l <- k + 1
count <- count + 1
#while(pedigree.data$Mother_ID[k]==pedigree.data$Mother_ID[l]){
#while(count < 542)
if(pedigree.data$Twin_Stat[k]=="Twin" && pedigree.data$Twin_Stat[l]=="Twin" &&
pedigree.data$Zygosity[k]=="MZ" && pedigree.data$Zygosity[l]=="MZ" ){
Kin.Score[k] <- 0.5
Kin.Score[l] <- 0.5
}
else if(pedigree.data$Twin_Stat[k]=="Twin" && pedigree.data$Twin_Stat[l]=="Twin" &&
pedigree.data$Zygosity[k]=="DZ" && pedigree.data$Zygosity[l]=="DZ" ){
Kin.Score[k] <- 0.25
Kin.Score[l] <- 0.25
}
else if(pedigree.data$Twin_Stat[k]=="NotTwin" && pedigree.data$Twin_Stat[l]=="NotTwin" &&
pedigree.data$Mother_ID[k]==pedigree.data$Mother_ID[l] && Dads.Renamed[k]==Dads.Renamed[l]){
Kin.Score[k] <- 0.25
Kin.Score[l] <- 0.25
}
else if (pedigree.data$Twin_Stat[k]=="NotTwin" && pedigree.data$Twin_Stat[l]=="NotTwin" &&
pedigree.data$Mother_ID[k]==pedigree.data$Mother_ID[l] && Dads.Renamed[k]!=Dads.Renamed[l]){
Kin.Score[k] <- 0.125
Kin.Score[l] <- 0.125
}
else if(pedigree.data$Twin_Stat[k]=="NotTwin" && pedigree.data$Twin_Stat[l]=="NotTwin" &&
pedigree.data$Mother_ID[k]!=pedigree.data$Mother_ID[l] && Dads.Renamed[k]!=Dads.Renamed[l]){
Kin.Score[k] <- 0
Kin.Score[l] <- 0
}
}
#}
Part of the problem I am having is that I don't know how to tell the loop to identify family members and assign the appropriate value for each family member.
I would really appreciate any advice or suggestions.
Thanks a lot! -Craig.
Aucun commentaire:
Enregistrer un commentaire