-
Notifications
You must be signed in to change notification settings - Fork 0
/
limma_fit.r
79 lines (61 loc) · 1.69 KB
/
limma_fit.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
suppressMessages(library(edgeR))
suppressMessages(library(limma))
library("optparse")
option_list = list(
make_option(
c("--input_h5ad"),
type="character",
),
make_option(
c("--design"),
type="character",
),
make_option(
c("--fit_output_path"),
type="character",
),
make_option(
c("--plot_output_path"),
type="character",
)
);
opt_parser = OptionParser(option_list=option_list);
opt = parse_args(opt_parser);
print(opt$input_h5ad)
print(opt$design)
print(opt$fit_output_path)
print(opt$plot_output_path)
cat("> Loading H5AD\\n")
ad <- anndata::read_h5ad(opt$input_h5ad)
row.names(ad$obs) <- row.names(ad$X)
counts = data.frame(t(ad$X))
row.names(counts) <- row.names(ad$var)
colnames(counts) <- row.names(ad$obs)
cat("> Normalizing\\n")
d0 <- DGEList(counts)
d0 <- calcNormFactors(d0)
make_mm <- function(formula_str, df){
# sets up the model matrix from the formula string and dataframe
for (column in colnames(df)){
assign(column, df[[column]])
}
mm <- model.matrix(eval(parse(text=formula_str)))
return(mm)
}
mm <- make_mm(opt$design, ad$obs)
if (!(length(opt$plot_output_path)==0)){
cat("> Generating voom plot\\n")
pdf(file = opt$plot_output_path, # The directory you want to save the file in
width = 4, # The width of the plot in inches
height = 4) # The height of the plot in inches
y <- voom(d0, mm, plot=TRUE)
dev.off()
} else{
y <- voom(d0, mm, plot=FALSE)
}
cat("> Fitting...\\n")
fit <- lmFit(y, mm)
print(colnames(fit$coefficients))
if(!(length(opt$fit_output_path)==0)){
saveRDS(fit, opt$fit_output_path)
}