-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.R
79 lines (60 loc) · 2.06 KB
/
main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
print("---------------------------------------------------------Code Start")
library("GenomicSEM")
library("utils")
library("Matrix")
if (!dir.exists("log")) {
dir.create("log")
}
paths_corr <- "PATH_DIRECTORY_LOCATION_SUMSTATS" # EDIT WITH APPROPRIATE PATH
ld <- "PATH_DIRECTORY_LOCATION_LD" # EDIT WITH APPROPRIATE PATH
wld <- ld
corr <- read.csv("Correlation_input.csv")
corr_input <- paste0(paths_corr, corr$code)
corr_input <- c(corr_input)
missing_files <- corr_input[!file.exists(corr_input)]
if (length(missing_files) > 0) {
print("The following files do not exist:")
print(missing_files)
} else {
print("All files exist.")
}
corr$sampleprev <- as.character(corr$sampleprev)
corr$popprev <- as.character(corr$popprev)
corr$sampleprev <- gsub(",", ".", corr$sampleprev)
corr$popprev <- gsub(",", ".", corr$popprev)
corr$sampleprev <- as.numeric(corr$sampleprev)
corr$popprev <- as.numeric(corr$popprev)
first_trait_correlations <- numeric(length(corr_input) - 1)
first_trait_ses <- numeric(length(corr_input) - 1)
second_traits <- corr$trait[-1]
original_wd <- getwd()
for (i in 2:length(corr_input)) {
setwd("log")
log_file <- sprintf("ldsc_log_%d.log", i)
Correlation_output <- suppressMessages(
suppressWarnings(
ldsc(traits = corr_input[c(1, i)],
sample.prev = corr$sampleprev[c(1, i)],
population.prev = corr$popprev[c(1, i)],
ld = ld, wld = wld, stand = TRUE,
ldsc.log = log_file)
)
)
print(Correlation_output)
first_trait_correlations[i - 1] <- Correlation_output$S_Stand[1, 2]
variance <- Correlation_output$V_Stand[1, 2]
if (!is.null(variance)) {
first_trait_ses[i - 1] <- sqrt(abs(variance))
} else {
first_trait_ses[i - 1] <- NA
}
setwd(original_wd)
}
results_table <- data.frame(
TRAIT2 = second_traits,
First_Trait_Correlations = first_trait_correlations,
First_Trait_SEs = first_trait_ses
)
print(results_table)
write.csv(results_table, file = "results_table.csv", row.names = FALSE)
print("---------------------------------------------------------Code Finished")