Mercurial > repos > artbio > gsc_scran_normalize
comparison scran-normalize.R @ 1:fb2f1b8b0013 draft
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/gsc_scran_normalize commit e0357f07fdabee1ec6614aca6f7b51095111e0d5
| author | artbio |
|---|---|
| date | Sun, 04 Dec 2022 01:01:01 +0000 |
| parents | 252eded61848 |
| children | 6864acb21714 |
comparison
equal
deleted
inserted
replaced
| 0:252eded61848 | 1:fb2f1b8b0013 |
|---|---|
| 1 # load packages that are provided in the conda env | 1 # load packages that are provided in the conda env |
| 2 options( show.error.messages=F, | 2 options(show.error.messages = FALSE, |
| 3 error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } ) | 3 error = function() { |
| 4 cat(geterrmessage(), file = stderr()) | |
| 5 q("no", 1, FALSE) | |
| 6 } | |
| 7 ) | |
| 4 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") | 8 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") |
| 5 warnings() | 9 warnings() |
| 6 | 10 |
| 7 library(optparse) | 11 library(optparse) |
| 8 library(scran) | 12 library(scran) |
| 9 | 13 |
| 10 #Arguments | 14 # Arguments |
| 11 option_list = list( | 15 option_list <- list( |
| 12 make_option( | 16 make_option( |
| 13 c("-d", "--data"), | 17 c("-d", "--data"), |
| 14 default = NA, | 18 default = NA, |
| 15 type = 'character', | 19 type = "character", |
| 16 help = "Input file that contains count values to transform" | 20 help = "Input file that contains count values to transform" |
| 17 ), | 21 ), |
| 18 make_option( | 22 make_option( |
| 19 c("-s", "--sep"), | 23 c("-s", "--sep"), |
| 20 default = '\t', | 24 default = "\t", |
| 21 type = 'character', | 25 type = "character", |
| 22 help = "File separator [default : '%default' ]" | 26 help = "File separator [default : '%default' ]" |
| 23 ), | 27 ), |
| 24 make_option( | 28 make_option( |
| 25 "--cluster", | 29 "--cluster", |
| 26 default=FALSE, | 30 default = FALSE, |
| 27 action="store_true", | 31 action = "store_true", |
| 28 type = 'logical', | 32 type = "logical", |
| 29 help = "Whether to calculate the size factor per cluster or on all cell" | 33 help = "Whether to calculate the size factor per cluster or on all cell" |
| 30 ), | 34 ), |
| 31 make_option( | 35 make_option( |
| 32 c("-m", "--method"), | 36 c("-m", "--method"), |
| 33 default = 'hclust', | 37 default = "hclust", |
| 34 type = 'character', | 38 type = "character", |
| 35 help = "The clustering method to use for grouping cells into cluster : hclust or igraph [default : '%default' ]" | 39 help = "The clustering method to use for grouping cells into cluster : hclust or igraph [default : '%default' ]" |
| 36 ), | 40 ), |
| 37 make_option( | 41 make_option( |
| 38 "--size", | 42 "--size", |
| 39 default = 100, | 43 default = 100, |
| 40 type = 'integer', | 44 type = "integer", |
| 41 help = "Minimal number of cells in each cluster : hclust or igraph [default : '%default' ]" | 45 help = "Minimal number of cells in each cluster : hclust or igraph [default : '%default' ]" |
| 42 ), | 46 ), |
| 43 make_option( | 47 make_option( |
| 44 c("-o", "--out"), | 48 c("-o", "--out"), |
| 45 default = "res.tab", | 49 default = "res.tab", |
| 46 type = 'character', | 50 type = "character", |
| 47 help = "Output name [default : '%default' ]" | 51 help = "Output name [default : '%default' ]" |
| 48 ) | 52 ) |
| 49 ) | 53 ) |
| 50 | 54 |
| 51 opt = parse_args(OptionParser(option_list = option_list), | 55 opt <- parse_args(OptionParser(option_list = option_list), |
| 52 args = commandArgs(trailingOnly = TRUE)) | 56 args = commandArgs(trailingOnly = TRUE)) |
| 53 | 57 |
| 54 if (opt$sep == "tab") {opt$sep = "\t"} | 58 if (opt$sep == "tab") { |
| 59 opt$sep <- "\t" | |
| 60 } | |
| 55 | 61 |
| 56 data = read.table( | 62 data <- read.table( |
| 57 opt$data, | 63 opt$data, |
| 58 check.names = FALSE, | 64 check.names = FALSE, |
| 59 header = TRUE, | 65 header = TRUE, |
| 60 row.names = 1, | 66 row.names = 1, |
| 61 sep = opt$sep | 67 sep = opt$sep |
| 62 ) | 68 ) |
| 63 | 69 |
| 64 ## Import data as a SingleCellExperiment object | 70 ## Import data as a SingleCellExperiment object |
| 65 sce <- SingleCellExperiment(list(counts=as.matrix(data))) | 71 sce <- SingleCellExperiment(list(counts = as.matrix(data))) |
| 66 | 72 |
| 67 | 73 if (opt$cluster) { |
| 68 if(opt$cluster){ | |
| 69 clusters <- quickCluster(sce, min.size = opt$size, method = opt$method) | 74 clusters <- quickCluster(sce, min.size = opt$size, method = opt$method) |
| 70 | 75 |
| 71 ## Compute sum factors | 76 ## Compute sum factors |
| 72 sce <- computeSumFactors(sce, cluster = clusters) | 77 sce <- computeSumFactors(sce, cluster = clusters) |
| 73 } else { | 78 } else { |
| 76 sce <- computeSumFactors(sce) | 81 sce <- computeSumFactors(sce) |
| 77 } | 82 } |
| 78 | 83 |
| 79 sce <- normalize(sce) | 84 sce <- normalize(sce) |
| 80 | 85 |
| 81 logcounts <- data.frame(genes = rownames(sce), round(logcounts(sce), digits=5), check.names = F) | 86 logcounts <- data.frame(genes = rownames(sce), round(logcounts(sce), digits = 5), check.names = FALSE) |
| 82 | 87 |
| 83 | 88 |
| 84 write.table( | 89 write.table( |
| 85 logcounts, | 90 logcounts, |
| 86 opt$out, | 91 opt$out, |
| 87 col.names = T, | 92 col.names = TRUE, |
| 88 row.names = F, | 93 row.names = FALSE, |
| 89 quote = F, | 94 quote = FALSE, |
| 90 sep = "\t" | 95 sep = "\t" |
| 91 ) | 96 ) |
