Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ca66a27

Browse files
Merge pull request #3 from definecoder/jpd
Add files via upload
2 parents d28725f + 3916b8f commit ca66a27

File tree

4 files changed

+630
-633
lines changed

4 files changed

+630
-633
lines changed

‎api/code/6_volcano_highlight_m.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ create_volcano_plot <- function(data, gene_ids, log_fold_change_col = "log2FoldC
3636
) +
3737

3838
# Customize color palette for regulation categories
39-
scale_color_manual(values = c("Upregulated" = "red", "Downregulated" = "blue", "Not Significant" = "gray")) +
39+
scale_color_manual(values = c("Upregulated" = "#d84467", "Downregulated" = "#4682b4", "Not Significant" = "gray")) +
4040

4141
# Minimal theme
4242
theme_minimal() +

‎api/code/batch_effect_correction.R

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,87 @@
1+
12
batch_effect_correction <- function(input_file, output_dir, user_id) {
23
library(jsonlite)
34
library(sva) # For batch effect correction
4-
5+
56
tryCatch(
67
{
7-
# Read and preprocess data
8-
merged_df_data <- read.csv(input_file, header = TRUE, row.names = 1)
8+
# Read and preprocess data - preserve exact feature names
9+
merged_df_data <- read.csv(input_file, header = TRUE, row.names = 1, check.names=FALSE)
910
merged_df_data <- na.omit(merged_df_data)
11+
12+
# Ensure unique column names
13+
colnames(merged_df_data) <- make.unique(colnames(merged_df_data))
14+
15+
# Extract condition and expression matrix
1016
condition_info <- merged_df_data$condition
1117
data_t <- t(merged_df_data[, !(colnames(merged_df_data) %in% c("condition", "batch"))])
12-
sample_names <- colnames(data_t) # Save sample names for labeling
13-
18+
19+
# Save original feature names
20+
feature_names <- rownames(data_t)
21+
sample_names <- colnames(data_t)
22+
1423
# Batch effect correction with ComBat
1524
batch_info <- merged_df_data$batch
1625
data_combat <- ComBat(dat = as.matrix(data_t), batch = batch_info, par.prior = TRUE, prior.plots = FALSE)
17-
26+
27+
# Restore original feature names
28+
rownames(data_combat) <- feature_names
29+
1830
# Save corrected data
1931
output_file <- file.path(output_dir, paste0("batch_", basename(input_file)))
2032
data_corrected <- t(data_combat)
2133
data_corrected_with_condition <- cbind(condition = condition_info, data_corrected)
22-
write.csv(data_corrected_with_condition, output_file, row.names = TRUE)
23-
24-
# Save boxplots in multiple formats
25-
plot_formats <- c("png", "jpg", "tif", "pdf")
34+
35+
# Write CSV with proper quoting to preserve commas/spaces in feature names
36+
write.csv(
37+
data_corrected_with_condition,
38+
output_file,
39+
row.names = TRUE,
40+
quote = TRUE,
41+
na = "",
42+
fileEncoding = "UTF-8")
43+
44+
# Create boxplots in PDF and PNG formats only
45+
plot_formats <- c("pdf", "png")
2646
for (fmt in plot_formats) {
2747
file_name <- file.path(output_dir, paste0("batch_correction_boxplots.", fmt))
48+
49+
# Set up the plotting device
2850
if (fmt == "png") {
29-
png(file_name, width = 1200, height = 600)
30-
} else if (fmt == "jpg") {
31-
jpeg(file_name, width = 1200, height = 600)
32-
} else if (fmt == "tif") {
33-
tiff(file_name, width = 1200, height = 600)
34-
} else if (fmt == "pdf") {
51+
png(file_name, width = 1200, height = 600, res = 300)
52+
} else {
3553
pdf(file_name, width = 12, height = 6)
3654
}
55+
56+
# Create the plots
3757
par(mfrow = c(1, 2), mar = c(10, 5, 4, 2))
58+
59+
# Pre-correction plot
3860
boxplot(data_t,
39-
main = "Normalized Data", las = 2, col = "lightblue", outline = FALSE,
40-
ylab = "Expression Levels", cex.axis = 0.7, names = sample_names
41-
)
61+
main = "Before Batch Correction",
62+
las = 2,
63+
col = "lightblue",
64+
outline = FALSE,
65+
ylab = "Expression Levels",
66+
cex.axis = 0.7,
67+
names = sample_names)
68+
69+
# Post-correction plot
4270
boxplot(data_combat,
43-
main = "Batch Corrected Data", las = 2, col = "lightgreen",
44-
outline = FALSE, ylab = "Expression Levels", cex.axis = 0.7, names = sample_names
45-
)
71+
main = "After Batch Correction",
72+
las = 2,
73+
col = "lightgreen",
74+
outline = FALSE,
75+
ylab = "Expression Levels",
76+
cex.axis = 0.7,
77+
names = sample_names)
78+
4679
dev.off()
4780
}
48-
81+
4982
# Output completion message
5083
cat("Batch effect correction completed. Corrected data saved to:", output_file, "\n")
51-
cat("Boxplots saved in PNG, JPG, TIF, and PDF formats.\n")
84+
cat("Boxplots saved in PDF and PNG formats.\n")
5285
},
5386
error = function(e) {
5487
# Handle errors gracefully

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /