-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plots are generated with plotly now and some minor changes
- Loading branch information
fawaz-dabbaghieh
committed
Feb 2, 2018
1 parent
bb7881e
commit e0162a8
Showing
7 changed files
with
201 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
plotly_pca <- function(experiments_info_meta,filtered_score_matrix, project,type_of_score, | ||
color_by = "biosource_name", | ||
epigenetic_mark = "No Epigenetic mark selected", | ||
first_pc="1", | ||
second_pc="2", | ||
show_legend = T){ | ||
|
||
#calculating PCA | ||
pca <- prcomp(filtered_score_matrix, center = TRUE, scale. = TRUE) | ||
|
||
#preparing the plot data by taking the PCAs and adding metadata | ||
plot.data <- as.data.frame(pca$rotation) %>% | ||
tibble::rownames_to_column(var = "experiment") %>% | ||
dplyr::left_join(experiments_info_meta, by=c("experiment")) | ||
|
||
# #Getting colour pallet | ||
# colourCount <- 9 | ||
# getPalette <- colorRampPalette(brewer.pal(colourCount, "Set1")) | ||
|
||
if(project == "DEEP"){ | ||
label <- "DEEP_SAMPLE_ID" | ||
hover = ~paste("Sample ID: ", DEEP_SAMPLE_ID, | ||
'</br>Biosource Name: ', biosource_name) | ||
}else{ | ||
label <- "experiment" | ||
hover = ~paste("Sample ID: ", experiment, | ||
'</br>Biosource Name: ', biosource_name) | ||
} | ||
|
||
x_lab <- paste0(paste0("PC", first_pc," ", "("), | ||
round(pca$sdev[as.integer(first_pc)]^2/sum(pca$sdev^2), 2) * 100, "%)") | ||
|
||
y_lab <- paste0(paste0("PC", second_pc," ", "("), | ||
round(pca$sdev[as.integer(second_pc)]^2/sum(pca$sdev^2), 2) * 100, "%)") | ||
|
||
browser() | ||
p <- | ||
plot.data %>% | ||
arrange(plot.data[,color_by]) %>% | ||
plot_ly(x = as.formula(paste0("~","PC", first_pc)), | ||
text = hover, | ||
color = as.formula(paste0("~",color_by)), | ||
legendgroup = as.formula(paste0("~",color_by)), | ||
colors = brewer.pal(9, "Set1"), | ||
marker = list(size = 17.5)) %>% | ||
add_markers(y = as.formula(paste0("~","PC", second_pc)), showlegend = show_legend) %>% | ||
layout(title = paste("2 PCs plot", type_of_score, epigenetic_mark), | ||
yaxis = list(title = y_lab, zeroline = FALSE), | ||
xaxis = list(title = x_lab, zeroline = FALSE)) | ||
|
||
return(p) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
supervised_sva_batch_effect <- function(filtered_score_matrix, | ||
adjustment_var, | ||
interest_var){ | ||
|
||
|
||
# filtered_matrix <- filtered_score_matrix$data | ||
metadata <- attr(filtered_score_matrix, "meta") | ||
|
||
#validation of the inptus | ||
#The variable selected should have more than 1 level | ||
if(adjustment_var == ""){ | ||
adjustment_var = NULL | ||
}else{ | ||
for (adj_var in adjustment_var){ | ||
|
||
validate( | ||
need(!anyNA(metadata[,adj_var]), message = paste(adj_var, "has NAs and cannot be used to make the model")) | ||
) | ||
validate( | ||
need(nlevels(metadata[,adj_var]) > 1, message = paste(adj_var,"has less than 2 level", | ||
"check levels using the pie chart")) | ||
) | ||
} | ||
} | ||
|
||
if(interest_var == ""){ | ||
validate( | ||
need(FALSE, message = "You need to choose a variable of interest for the full model in SVA") | ||
) | ||
}else{ | ||
for (inter_var in interest_var){ | ||
|
||
validate( | ||
need(!anyNA(metadata[,inter_var]), message = paste(inter_var, "has NAs and cannot be used for the model")) | ||
) | ||
validate( | ||
need(nlevels(metadata[,inter_var]) > 1, message = paste(inter_var,"has less than 2 level", | ||
"check levels using the pie chart")) | ||
) | ||
} | ||
} | ||
|
||
if(is.null(adjustment_var)){ | ||
#No interest variable, mod0 is the intercept, full mod is the interest_var | ||
mod0 <- model.matrix(~1, data = metadata) | ||
mod <- model.matrix(as.formula(paste0("~", paste(interest_var, collapse = "+"))), | ||
data = metadata) | ||
|
||
}else{ | ||
mod0 <- model.matrix(as.formula(paste0("~", paste(adjustment_var, collapse = "+"))), | ||
data = metadata) | ||
|
||
mod <- model.matrix(as.formula(paste0("~", paste( | ||
paste(interest_var, collapse = " + "),"+", paste(adjustment_var, collapse = " + ") | ||
) | ||
)),data = metadata) | ||
} | ||
n.sv <- num.sv(filtered_score_matrix, mod, method = "leek") | ||
showNotification(paste("The number of latent factors estimated is", n.sv), duration = 3) | ||
|
||
sva_object <- sva(filtered_score_matrix, mod, mod0, n.sv = n.sv) | ||
|
||
batch_adjusted_matrix <- sva_object$sv | ||
|
||
attr(batch_adjusted_matrix, "meta") <- metadata | ||
|
||
return(batch_adjusted_matrix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters