-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall_packages.R
More file actions
226 lines (181 loc) · 7.13 KB
/
Copy pathinstall_packages.R
File metadata and controls
226 lines (181 loc) · 7.13 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# install_packages.R
#
# Purpose: Install all required R packages for T21-eQTL analysis
# Usage: Rscript install_packages.R
#
# This script will:
# 1. Check R version
# 2. Install CRAN packages
# 3. Install Bioconductor packages
# 4. Verify installations
# 5. Create renv.lock for reproducibility (optional)
#
# Author: Claude Code
# Date: 2025-11-11
cat("=== T21-eQTL Package Installation ===\n\n")
# =============================================================================
# STEP 1: Check R version
# =============================================================================
cat("Step 1: Checking R version...\n")
r_version <- R.version.string
cat(sprintf(" %s\n", r_version))
r_version_num <- as.numeric(R.version$major) +
as.numeric(R.version$minor) / 10
if (r_version_num < 4.0) {
warning("R version < 4.0 detected. Some packages may not work properly.")
cat(" Recommended: R >= 4.0\n")
} else {
cat(" R version OK\n")
}
# =============================================================================
# STEP 2: Define required packages
# =============================================================================
cat("\nStep 2: Defining required packages...\n")
# CRAN packages
cran_packages <- c(
# Data manipulation
"tidyverse", # Collection: dplyr, ggplot2, tidyr, readr, etc.
"data.table", # Fast data processing for large files
# Visualization
"ggplot2", # Plotting (included in tidyverse but listed explicitly)
"ggrepel", # Label positioning for plots
"ggalluvial", # Alluvial/Sankey diagrams
"patchwork", # Panel composition (script 05)
"RColorBrewer", # Color palettes
"pheatmap", # Heatmaps
"viridis", # Perceptually uniform color scales
# I/O
"arrow", # Reading GTEx parquet files (script 02)
# Utilities
"here" # Path management (optional but recommended)
)
# Bioconductor packages
bioc_packages <- c(
"DESeq2", # Differential expression analysis
"BiocManager" # Bioconductor package manager
)
cat(sprintf(" CRAN packages: %d\n", length(cran_packages)))
cat(sprintf(" Bioconductor packages: %d\n", length(bioc_packages)))
# =============================================================================
# STEP 3: Install BiocManager (required for Bioconductor)
# =============================================================================
cat("\nStep 3: Installing BiocManager...\n")
if (!require("BiocManager", quietly = TRUE)) {
install.packages("BiocManager", repos = "https://cloud.r-project.org")
cat(" BiocManager installed\n")
} else {
cat(" BiocManager already installed\n")
}
library(BiocManager)
# =============================================================================
# STEP 4: Install CRAN packages
# =============================================================================
cat("\nStep 4: Installing CRAN packages...\n")
for (pkg in cran_packages) {
cat(sprintf(" Checking %s...", pkg))
if (!require(pkg, character.only = TRUE, quietly = TRUE)) {
cat(" installing...")
tryCatch({
install.packages(pkg, repos = "https://cloud.r-project.org",
dependencies = TRUE)
cat(" SUCCESS\n")
}, error = function(e) {
cat(" FAILED\n")
cat(sprintf(" Error: %s\n", e$message))
})
} else {
cat(" already installed\n")
}
}
# =============================================================================
# STEP 5: Install Bioconductor packages
# =============================================================================
cat("\nStep 5: Installing Bioconductor packages...\n")
for (pkg in bioc_packages) {
if (pkg == "BiocManager") next # Already installed
cat(sprintf(" Checking %s...", pkg))
if (!require(pkg, character.only = TRUE, quietly = TRUE)) {
cat(" installing...")
tryCatch({
BiocManager::install(pkg, update = FALSE, ask = FALSE)
cat(" SUCCESS\n")
}, error = function(e) {
cat(" FAILED\n")
cat(sprintf(" Error: %s\n", e$message))
})
} else {
cat(" already installed\n")
}
}
# =============================================================================
# STEP 6: Verify installations
# =============================================================================
cat("\nStep 6: Verifying installations...\n")
all_packages <- c(cran_packages, bioc_packages)
failed_packages <- character()
for (pkg in all_packages) {
can_load <- require(pkg, character.only = TRUE, quietly = TRUE)
if (!can_load) {
failed_packages <- c(failed_packages, pkg)
cat(sprintf(" FAILED: %s\n", pkg))
}
}
if (length(failed_packages) == 0) {
cat(" All packages installed successfully!\n")
} else {
cat(sprintf("\n WARNING: %d packages failed to install:\n",
length(failed_packages)))
for (pkg in failed_packages) {
cat(sprintf(" - %s\n", pkg))
}
cat("\n Try installing failed packages manually:\n")
cat(sprintf(" install.packages(c(%s))\n",
paste0("\"", paste(failed_packages, collapse = "\", \""), "\"")))
}
# =============================================================================
# STEP 7: Print package versions
# =============================================================================
cat("\nStep 7: Package versions:\n")
for (pkg in all_packages) {
if (require(pkg, character.only = TRUE, quietly = TRUE)) {
version <- packageVersion(pkg)
cat(sprintf(" %-15s %s\n", pkg, version))
}
}
# =============================================================================
# STEP 8: Optional - Initialize renv for reproducibility
# =============================================================================
cat("\nStep 8: Optional renv initialization...\n")
cat(" To create reproducible environment, run:\n")
cat(" install.packages('renv')\n")
cat(" renv::init()\n")
cat(" renv::snapshot()\n")
cat("\n This creates renv.lock file tracking all package versions.\n")
# =============================================================================
# STEP 9: Save session info
# =============================================================================
cat("\nStep 9: Saving session info...\n")
if (!dir.exists("docs")) {
dir.create("docs", recursive = TRUE)
}
writeLines(capture.output(sessionInfo()),
"docs/package_installation_info.txt")
cat(" Saved: docs/package_installation_info.txt\n")
# =============================================================================
# Summary
# =============================================================================
cat("\n=== Installation Complete ===\n")
if (length(failed_packages) == 0) {
cat("All packages installed successfully!\n")
cat("You can now run the analysis scripts.\n")
} else {
cat("Some packages failed to install.\n")
cat("Please install them manually before running analysis.\n")
}
cat("\nNext steps - run the 00-05 pipeline:\n")
cat(" Rscript scripts/00_preprocess_data.R\n")
cat(" Rscript scripts/01_deseq2_analysis.R\n")
cat(" Rscript scripts/02_filter_genotypes.R\n")
cat(" Rscript scripts/03_t21_dosage_boxplots.R\n")
cat(" Rscript scripts/04_chr21_lane_assignment.R\n")
cat(" Rscript scripts/05_chr21_distribution_panel.R\n\n")