Reorder Matrix
CH slack, Dec 30th 2020 - Jan 12th 2020)
CH slack, Dec 30th 2020 - Jan 12th 2020)
Please also help with this task, written here
"/space/chen-syn01/1/data/chen/gwas_gclust/genesis/ABCD/8.reorder_cor_mat.R"
Original instructions in the code kept, see the comments.
Stored on server: /space/chen-syn01/1/data/cinliu/data/2020archive/reorder_cor
8.reorder_cor_mat_edit1.R
rm(list=ls())
library(graph4lg)
library(reshape2)
dir <- "/space/chen-syn01/1/data/chen/gwas_gclust/genesis/ABCD/"
pheno_dir <- "/space/chen-syn01/1/data/cmakowski/ABCD/GCLUST/GWAS/Science_revisions/"
load(paste0(dir,"GRM_adj.RData"))
IID <- read.table(paste0(pheno_dir,"/genotype_QCed/ABCD_ALL.fam"),sep=" ",h=F)
#reorder
matrix <- as.matrix(GRM_adj) #before reordering
order1 <- IID$V1
reordered_GRM_adj <- reorder_mat(mat = matrix, order = order1) # using graph4lg package
#convert the correlation matrix format into a table format:
####SAVING THE ENTRIE MATRIX TO DATATABLE ####
#data <- melt(mat, value.name = "grm", varnames=c('ID1', 'ID2')) #reshape2 package
#write.csv(data,"all_melt.csv", row.names = FALSE) #save a copy of all
#generate one table from upper triangular and one from lower triangular part.
###LOWER ONLY ####
lower<-reordered_GRM_adj
lower[upper.tri(reordered_GRM_adj, diag=TRUE)] <- NA
lower_melt <- melt(lower)
lower_melt <- na.omit(lower_melt)
colnames(lower_melt) <- c('ID1', 'ID2', "grm")
write.csv(lower_melt,"lower_table_format.csv", row.names = FALSE)
###UPPER ONLY ####
upper<-reordered_GRM_adj
upper[lower.tri(reordered_GRM_adj)] <- NA
upper_melt <- melt(upper)
upper_melt <- na.omit(upper_melt)
colnames(upper_melt) <- c('ID1', 'ID2', "grm")
write.csv(upper_melt,"upper_table_format.csv", row.names = FALSE)
Hello Nini, we need your help on a task. please see the same file with the instruction from Line 30
"/space/chen-syn01/1/data/chen/gwas_gclust/genesis/ABCD/8.reorder_cor_mat.R"
8.reorder_cor_mat_edit2.R
rm(list=ls())
dir <- "/space/chen-syn01/1/data/chen/gwas_gclust/genesis/ABCD/"
pheno_dir <- "/space/chen-syn01/1/data/cmakowski/ABCD/GCLUST/GWAS/Science_revisions/"
#use the subject list FILE1 below and find their grm values in the matrix above, GRM_adj
load(paste0(dir,"GRM_adj.RData"))
#FILE1: /space/chen-syn01/1/data/cmakowski/ABCD/ABCD_zygozygosity.2020_04_19.check.csv # need to read this file into R
FILE1 <- read.csv("~/Desktop/2020lab/plots/CH/ABCD_zygozygosity.2020_04_19.check.csv") #UPDATE
df <- FILE1[,1:2]
df$grm <- NA
#loop will put NA in location where no matches are found
for (i in 1:nrow(df)) {
x <- paste0(GRM_adj[colnames(GRM_adj) == FILE1$IID1[i] , rownames(GRM_adj) == FILE1$IID2[i]])
if (identical(x, character(0)) == TRUE) {
df$grm[i] <- NA
}else{
df$grm[i] <- x
}
}
write.csv(df,"output.csv", row.names = FALSE)