Mercurial > repos > recetox > bioconductor_scp
comparison utils.r @ 0:cd2f3a280463 draft
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/bioconductor-scp commit a0a1a3de5dd24b2aabe96ec3d6f89acdcf5e462b
| author | recetox |
|---|---|
| date | Wed, 22 Jan 2025 07:44:00 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:cd2f3a280463 |
|---|---|
| 1 # Export intermediate results | |
| 2 # Function to export a single assay with metadata | |
| 3 export_assay_with_metadata <- function(qf, assay_name) { | |
| 4 # Extract assay data, row metadata, and col metadata | |
| 5 assay_data <- SummarizedExperiment::assay(qf[[assay_name]]) | |
| 6 row_metadata <- as.data.frame(SummarizedExperiment::rowData(qf[[assay_name]])) | |
| 7 col_metadata <- as.data.frame(SummarizedExperiment::colData(qf)) | |
| 8 # Combine row metadata with assay data | |
| 9 export_data <- cbind(RowNames = rownames(assay_data), row_metadata, as.data.frame(assay_data)) | |
| 10 # Save the table to a CSV file | |
| 11 output_file <- file.path("outputs", paste0(assay_name, "_export.txt")) | |
| 12 write.table(export_data, output_file, row.names = FALSE, sep = "\t", quote = F) | |
| 13 } | |
| 14 | |
| 15 # Export all assays | |
| 16 export_all_assays <- function(qf) { | |
| 17 # Get the names of all assays | |
| 18 # assay_names <- names(assays(qf)) | |
| 19 assay_names <- c("peptides", "peptides_norm", "peptides_log", "proteins", "proteins_norm", "proteins_imptd") | |
| 20 dir.create("outputs") | |
| 21 # Export each assay | |
| 22 for (assay_name in assay_names) { | |
| 23 export_assay_with_metadata(qf, assay_name) | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 # Plot the QC boxplots | |
| 28 create_boxplots <- function(scp, i, is_log2, name) { | |
| 29 sce <- scp[[i]] | |
| 30 assay_data <- as.data.frame(SummarizedExperiment::assay(sce)) |> | |
| 31 tibble::rownames_to_column("FeatureID") | |
| 32 col_data <- as.data.frame(SummarizedExperiment::colData(scp)) |> | |
| 33 tibble::rownames_to_column("SampleID") | |
| 34 long_data <- assay_data |> | |
| 35 tidyr::pivot_longer( | |
| 36 cols = -FeatureID, | |
| 37 names_to = "SampleID", | |
| 38 values_to = "Value" | |
| 39 ) | |
| 40 long_data <- long_data |> | |
| 41 dplyr::left_join(col_data, by = "SampleID") | |
| 42 if (is_log2 == TRUE) { | |
| 43 long_data$Value <- log2(long_data$Value) | |
| 44 } | |
| 45 long_data |> | |
| 46 dplyr::filter(Value != "NaN") |> | |
| 47 ggplot2::ggplot(ggplot2::aes(x = runCol, y = Value, fill = SampleType)) + | |
| 48 ggplot2::geom_boxplot() + | |
| 49 ggplot2::theme_bw() + | |
| 50 ggplot2::labs( | |
| 51 title = name, | |
| 52 x = "Run", | |
| 53 y = "Log2 intensity" | |
| 54 ) + | |
| 55 ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1)) | |
| 56 } | |
| 57 | |
| 58 # Heatmap | |
| 59 plot_heatmap <- function(scp, i) { | |
| 60 sce <- scp[[i]] | |
| 61 heatmap_mat <- as.matrix(SummarizedExperiment::assay(sce)) | |
| 62 heatmap_mat[is.na(heatmap_mat)] <- 0 | |
| 63 heatmap_bin <- ifelse(heatmap_mat > 0, 1, 0) | |
| 64 colnames(heatmap_bin) <- gsub("Reporter.intensity.", "", colnames(heatmap_bin)) | |
| 65 heatmap(heatmap_bin, scale = "none", col = c("white", "black"), labRow = FALSE, margins = c(10, 5), cexCol = 0.5) | |
| 66 } |
