@sjcrh/proteinpaint-server 2.110.0 → 2.111.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ library(jsonlite)
2
+ # This script reads in a json string from stdin, calculates the densities of each plot and returns the densities as a json string
3
+ # The input json string is a dictionary where each field maps to an array of numbers
4
+ # The output json string is a dictionary with the density for each plot. The density is represented like {x: [x density values], y: [y density values]}
5
+ # In order to test it you can run this from the command line replacing the arrays with your own:
6
+ # echo '{"plotA": [1.2, 2, 3], "plotB": [4.5, 5, 6]}' | Rscript ./density.R
7
+
8
+ con <- file("stdin", "r")
9
+ json <- readLines(con)
10
+ close(con)
11
+ data <- fromJSON(json)
12
+ densities <- list()
13
+ for(plot in names(data)){
14
+ values = data[[plot]]
15
+ # If the plot has less than 5 values or all the values are the same, we will return a flat line
16
+ if(length(values) <= 5 | length(unique(values)) == 1){
17
+ y = rep(0, length(values))
18
+ densities[[plot]] <- list(x=values, y=y)
19
+ next
20
+ }
21
+ den = density(x = values, from=min(values), to=max(values))
22
+ x = den$x
23
+ y = den$y
24
+ result = list(x=x, y=y) #This is an object with two keys x and y that are number arrays
25
+ densities[[plot]] <- result
26
+ }
27
+ toJSON(densities, digits = NA, na = "string") # will return a json like { plotA: {x:[...], y: [...]}}
28
+
29
+
package/utils/edge.R CHANGED
@@ -10,24 +10,11 @@ suppressWarnings({
10
10
  suppressPackageStartupMessages(library(dplyr))
11
11
  })
12
12
 
13
- filter_genes_by_global_variance <- function(read_counts, gene_id_symbols, num_variable_genes) {
14
- # Calculate the standard deviation of each row
15
- row_sd <- apply(read_counts, 1, sd)
16
- # Add the standard deviation as a new column to the dataframe
17
- read_counts$Row_SD <- row_sd
18
- # Add the gene_id_symbols as a new column to the dataframe
19
- read_counts$gene_id_symbols <- gene_id_symbols
20
- # Sort the dataframe based on the standard deviation column
21
- read_counts <- read_counts[order(read_counts$Row_SD, decreasing = TRUE), ]
22
- # Select top 3000 rows
23
- read_counts <- head(read_counts,num_variable_genes) # Currently hardcoded 3000 genes
24
- # Get gene id symbols corresponding to the reordered read count matrix
25
- gene_id_symbols <- read_counts$gene_id_symbols
26
- # Remove column Row_SD from read_counts dataframe
27
- read_counts <- read_counts[, !names(read_counts) %in% "Row_SD"]
28
- # Remove column gene_id_symbols from read_counts dataframe
29
- read_counts <- read_counts[, !names(read_counts) %in% "gene_id_symbols"]
30
- return(list(read_counts = read_counts, gene_id_symbols = gene_id_symbols))
13
+ # Filter based on CPM
14
+ filter_using_cpm <- function(y, gene_cpm_cutoff, sample_cpm_cutoff, count_cpm_cutoff) {
15
+ selr <- rowSums(cpm(y$counts)>gene_cpm_cutoff)>=sample_cpm_cutoff
16
+ selc <- colSums(cpm(y$counts))>=count_cpm_cutoff
17
+ y <- y[selr, selc]
31
18
  }
32
19
 
33
20
  # Read JSON input from stdin
@@ -86,21 +73,6 @@ read_counts_time <- system.time({
86
73
  conditions <- c(rep("Diseased", length(cases)), rep("Control", length(controls)))
87
74
  gene_id_symbols <- paste0(geneIDs, "\t", geneSymbols)
88
75
 
89
- filter_genes_time <- system.time({
90
- if (length(input$VarGenes) != 0) { # Filter out variable genes for DE analysis
91
- filtered_read_counts <- filter_genes_by_global_variance(read_counts, gene_id_symbols, input$VarGenes)
92
- read_counts <- filtered_read_counts$read_counts
93
- gene_id_symbols <- filtered_read_counts$gene_id_symbols
94
-
95
- #### Will implement filtering by per group variance later
96
- #filtered_read_counts <- filter_genes_by_group_variance(read_counts, gene_id_symbols, num_variable_genes, cases, controls)
97
- #read_counts <- filtered_read_counts$read_counts
98
- #gene_id_symbols <- filtered_read_counts$gene_id_symbols
99
- }
100
- })
101
-
102
- #cat("Time to filter genes: ", filter_genes_time[3], " seconds\n")
103
-
104
76
  # Create DGEList object
105
77
  dge_list_time <- system.time({
106
78
  y <- DGEList(counts = read_counts, group = conditions, genes = gene_id_symbols)
@@ -119,14 +91,31 @@ normalization_time <- system.time({
119
91
  })
120
92
  #cat("Normalization time: ", normalization_time[3], " seconds\n")
121
93
 
94
+ # Cutoffs for cpm, will add them as UI options later
95
+ if (length(samples_indices) > 100) {
96
+ gene_cpm_cutoff <- 15
97
+ sample_cpm_cutoff <- 30
98
+ count_cpm_cutoff <- 100000
99
+ } else {
100
+ gene_cpm_cutoff <- 5
101
+ sample_cpm_cutoff <- 15
102
+ count_cpm_cutoff <- 100000
103
+ }
104
+
105
+ filter_using_cpm_time <- system.time({
106
+ y <- filter_using_cpm(y, gene_cpm_cutoff, sample_cpm_cutoff, count_cpm_cutoff) # Filtering counts matrix based on gene_cpm_cutoff, sample_cpm_cutoff and count_cpm_cutoff
107
+ })
108
+ #cat("Filter using cpm time: ", filter_using_cpm_time[3], " seconds\n")
109
+
122
110
  # Saving MDS plot image
123
111
  set.seed(as.integer(Sys.time())) # Set the seed according to current time
124
112
  cachedir <- input$cachedir # Importing serverconfig.cachedir
125
113
  random_number <- runif(1, min = 0, max = 1) # Generating random number
126
114
  mds_image_name <- paste0("edgeR_mds_temp_",random_number,".png") # Generating random image name so that simultaneous server side requests do NOT generate the same edgeR file name
127
115
  png(filename = paste0(cachedir,"/",mds_image_name), width = 1000, height = 1000, res = 200) # Opening a png device
128
- par(oma = c(1, 1, 1, 1)) # Creating a margin
129
- plotMDS(y) # Plot the edgeR MDS plot
116
+ par(oma = c(0, 0, 0, 0)) # Creating a margin
117
+ mds_conditions <- c(rep("T", length(cases)), rep("C", length(controls))) # Case samples are labelled "T" and control samples are labelled "C". Single-letter labelling added because otherwise labels get overwritten on each other.
118
+ plotMDS(y, labels = mds_conditions) # Plot the edgeR MDS plot
130
119
  # dev.off() # Gives a null device message which breaks JSON. Commenting it out for now, will investigate it later
131
120
 
132
121
 
@@ -202,7 +191,7 @@ cachedir <- input$cachedir # Importing serverconfig.cachedir
202
191
  random_number <- runif(1, min = 0, max = 1) # Generating random number
203
192
  ql_image_name <- paste0("edgeR_ql_temp_",random_number,".png") # Generating random image name so that simultaneous server side requests do NOT generate the same edgeR file name
204
193
  png(filename = paste0(cachedir,"/",ql_image_name), width = 1000, height = 1000, res = 200) # Opening a png device
205
- par(oma = c(1, 1, 1, 1)) # Creating a margin
194
+ par(oma = c(0, 0, 0, 0)) # Creating a margin
206
195
  plotQLDisp(fit) # Plot the edgeR fit
207
196
  # dev.off() # Gives a null device message which breaks JSON. Commenting it out for now, will investigate it later
208
197