@sjcrh/proteinpaint-server 2.48.1 → 2.49.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.
package/src/run_R.js ADDED
@@ -0,0 +1,66 @@
1
+ /*
2
+ Module for running R
3
+
4
+ Arguments:
5
+ - <path>: [string] path to R script.
6
+ - <data>: [string] input data for R script.
7
+ - <args>: [array] arguments for R script.
8
+
9
+ Input data is streamed into the standard input of the R script.
10
+ Standard output of the R script is returned.
11
+ */
12
+
13
+ import fs from 'fs'
14
+ import serverconfig from './serverconfig'
15
+ import { spawn } from 'child_process'
16
+ import { Readable } from 'stream'
17
+
18
+ export default async function run_R(path, data, args) {
19
+ try {
20
+ await fs.promises.stat(path)
21
+ } catch (e) {
22
+ throw `${path} does not exist`
23
+ }
24
+ return new Promise((resolve, reject) => {
25
+ const _stdout = []
26
+ const _stderr = []
27
+ // spawn R child process
28
+ const sp = spawn(serverconfig.Rscript, args ? [path, ...args] : [path])
29
+ if (data) {
30
+ // stream input data into R
31
+ try {
32
+ const input = data.endsWith('\n') ? data : data + '\n' // R expects a final end-of-line marker
33
+ Readable.from(input).pipe(sp.stdin)
34
+ } catch (e) {
35
+ sp.kill()
36
+ let errmsg = e
37
+ const stderr = _stderr.join('').trim()
38
+ if (stderr) errmsg += `\nR stderr: ${stderr}`
39
+ reject(errmsg)
40
+ }
41
+ }
42
+ // store stdout and stderr from R
43
+ sp.stdout.on('data', data => _stdout.push(data))
44
+ sp.stderr.on('data', data => _stderr.push(data))
45
+ sp.on('error', err => reject(err))
46
+ // return stdout and stderr when R process closes
47
+ sp.on('close', code => {
48
+ const stdout = _stdout.join('').trim()
49
+ const stderr = _stderr.join('').trim()
50
+ if (code !== 0) {
51
+ // handle non-zero exit status
52
+ let errmsg = `R process exited with non-zero status code=${code}`
53
+ if (stdout) errmsg += `\nR stdout: ${stdout}`
54
+ if (stderr) errmsg += `\nR stderr: ${stderr}`
55
+ reject(errmsg)
56
+ }
57
+ if (stderr) {
58
+ // handle R stderr
59
+ const errmsg = `R process emitted standard error\nR stderr: ${stderr}`
60
+ reject(errmsg)
61
+ }
62
+ // return standard out from R
63
+ resolve(stdout)
64
+ })
65
+ })
66
+ }
package/utils/burden.R CHANGED
@@ -10,13 +10,17 @@ library(parallel)
10
10
 
11
11
  options(warn=-1)
12
12
 
13
- # Input from lines2R
13
+ # stream in json input data
14
+ con <- file("stdin", "r")
15
+ json <- readLines(con)
16
+ close(con)
17
+ input <- fromJSON(json)
18
+ # handle input arguments
14
19
  args <- commandArgs(trailingOnly = T)
15
- if (length(args) != 4) stop("Usage: Rscript burden.R in.json fitsData survData sampleData > results")
16
- infile <- args[1]
17
- fitsData <- args[2]
18
- survData <- args[3]
19
- sampleData <- args[4]
20
+ if (length(args) != 3) stop("Usage: echo <in_json> | Rscript burden.R fitsData survData sampleData > <out_json>")
21
+ fitsData <- args[1]
22
+ survData <- args[2]
23
+ sampleData <- args[3]
20
24
 
21
25
  chc_nums <- c(1:32)[-c(2,5,14,20,23,26)] # CHCs. 6 out of 32 CHCs not used.
22
26
  availCores <- detectCores()
@@ -82,7 +86,6 @@ newdata_chc_sampled$t.endage=seq(6,71,1)
82
86
  ### originally data fit to 60 only. using cphfits can get est up to 60 only. ==> later I further cut at 50 or so to fit lines, becuase original data had 95th percentile around age 50 or so.
83
87
  newdata_chc_sampled=newdata_chc_sampled[newdata_chc_sampled$t.endage<=60,]
84
88
 
85
- input <- fromJSON(infile)
86
89
  # paste(names(input), input, sep = ":", collapse = ",")
87
90
  pr=input$diaggrp
88
91
  sexval=input$sex
package/utils/cuminc.R CHANGED
@@ -1,15 +1,15 @@
1
1
  #################################
2
- # Cumulative incidence analysis #
2
+ # CUMULATIVE INCIDENCE ANALYSIS #
3
3
  #################################
4
4
 
5
5
  #########
6
- # Usage #
6
+ # USAGE
7
7
  #########
8
8
 
9
- # Usage: Rscript cuminc.R in.json > results
9
+ # Usage: echo <in_json> | Rscript cuminc.R > <out_json>
10
10
 
11
- # Input data is in JSON format and is read in from <in.json> file.
12
- # Cuminc results are written in JSON format to stdout.
11
+ # in_json: [string] input data in JSON format. Streamed through stdin.
12
+ # out_json: [string] cumulative incidence results in JSON format. Streamed to stdout.
13
13
 
14
14
  # Input JSON:
15
15
  # {
@@ -53,13 +53,18 @@
53
53
 
54
54
 
55
55
  ########
56
- # Code #
56
+ # CODE
57
57
  ########
58
58
 
59
59
  library(parallel)
60
60
  library(jsonlite)
61
61
  suppressPackageStartupMessages(library(cmprsk))
62
62
 
63
+
64
+ #############
65
+ # FUNCTIONS #
66
+ #############
67
+
63
68
  # function to run cumulative incidence analysis on data for a chart
64
69
  run_cuminc <- function(chart, startTime) {
65
70
  chart$event <- as.factor(chart$event)
@@ -242,11 +247,15 @@ compute_counts <- function(res, chart) {
242
247
  }
243
248
 
244
249
 
245
- # read in data
246
- args <- commandArgs(trailingOnly = T)
247
- if (length(args) != 1) stop("Usage: Rscript cuminc.R in.json > results")
248
- infile <- args[1]
249
- input <- fromJSON(infile)
250
+ ################
251
+ # PREPARE DATA #
252
+ ################
253
+
254
+ # stream in json input
255
+ con <- file("stdin", "r")
256
+ json <- readLines(con)
257
+ close(con)
258
+ input <- fromJSON(json)
250
259
 
251
260
  dat <- input$data
252
261
  startTime <- ifelse("startTime" %in% names(input), input$startTime, NA)
@@ -254,6 +263,11 @@ startTime <- ifelse("startTime" %in% names(input), input$startTime, NA)
254
263
  #save.image("~/test.RData")
255
264
  #stop("stop here")
256
265
 
266
+
267
+ ################
268
+ # RUN ANALYSIS #
269
+ ################
270
+
257
271
  # perform cumulative incidence analysis
258
272
  # parallelize the analysis across charts/variants
259
273
  availCores <- detectCores()
package/utils/hclust.R CHANGED
@@ -1,5 +1,7 @@
1
- # Usage:
2
- # time Rscript hclust.R in.json
1
+ # Usage: echo <in_json> | Rscript hclust.R > <out_json>
2
+
3
+ # in_json: [string] input data in JSON format. Streamed through stdin.
4
+ # out_json: [string] clustering results in JSON format. Streamed to stdout.
3
5
 
4
6
  # Image is in Rplots.pdf
5
7
 
@@ -21,10 +23,10 @@ suppressWarnings({
21
23
  #library(ggplot2) # Uncomment this line to plot heatmap in R
22
24
 
23
25
  # Distance matrix
24
- args <- commandArgs(trailingOnly = T)
25
- if (length(args) != 1) stop("Usage: Rscript test.R in.json > results")
26
- infile <- args[1]
27
- input <- fromJSON(infile)
26
+ con <- file("stdin", "r")
27
+ json <- readLines(con)
28
+ close(con)
29
+ input <- fromJSON(json)
28
30
 
29
31
  #if (length(input$valueIsTransformed) == 0 || input$valueIsTransformed == FALSE) {
30
32
  # normalized_matrix <- t(scale(t(input$matrix))) # Applying z-score normalization
package/utils/lowess.R CHANGED
@@ -1,9 +1,9 @@
1
1
  library(jsonlite)
2
2
 
3
3
  # read in data
4
- args <- commandArgs(trailingOnly = T)
5
- if (length(args) != 1) stop("input coordinates needed")
6
- infile <- args[1]
7
- data <- fromJSON(infile)
4
+ con <- file("stdin", "r")
5
+ json <- readLines(con)
6
+ close(con)
7
+ data <- fromJSON(json)
8
8
  result = lowess(data$X, data$Y)
9
9
  toJSON(result)
@@ -6,10 +6,10 @@
6
6
  # USAGE
7
7
  ###########
8
8
 
9
- # Usage: Rscript regression.R in.json > results
9
+ # Usage: echo <in_json> | Rscript regression.R > <out_json>
10
10
 
11
- # Input data is in JSON format and is read in from <in.json> file.
12
- # Regression results are written in JSON format to stdout.
11
+ # in_json: [string] input data in JSON format. Streamed through stdin.
12
+ # out_json: [string] regression results in JSON format. Streamed to stdout.
13
13
 
14
14
  # Input JSON specifications:
15
15
  # {
@@ -77,20 +77,18 @@ suppressPackageStartupMessages({
77
77
  library(lmtest)
78
78
  })
79
79
 
80
- args <- commandArgs(trailingOnly = T)
81
- if (length(args) != 1) stop("Usage: Rscript regression.R in.json > results")
82
- infile <- args[1]
83
-
84
80
  benchmark <- list()
85
81
 
86
-
87
82
  ################
88
83
  # PREPARE DATA #
89
84
  ################
90
85
 
91
- # read in json input
86
+ # stream in json input
92
87
  stime <- Sys.time()
93
- input <- fromJSON(infile)
88
+ con <- file("stdin", "r")
89
+ json <- readLines(con)
90
+ close(con)
91
+ input <- fromJSON(json)
94
92
  etime <- Sys.time()
95
93
  dtime <- etime - stime
96
94
  benchmark[["read_json_input"]] <- unbox(paste(round(as.numeric(dtime), 4), attr(dtime, "units")))
package/utils/survival.R CHANGED
@@ -6,10 +6,10 @@
6
6
  # Usage #
7
7
  #########
8
8
 
9
- # Usage: Rscript survival.R in.json > results
9
+ # Usage: echo <in_json> | Rscript survival.R > <out_json>
10
10
 
11
- # Input data is in JSON format and is read in from <in.json> file.
12
- # Survival results are written in JSON format to stdout.
11
+ # in_json: [string] input data in JSON format. Streamed through stdin.
12
+ # out_json: [string] survival results in JSON format. Streamed to stdout.
13
13
 
14
14
  # Input JSON:
15
15
  # [
@@ -34,11 +34,11 @@
34
34
  library(jsonlite)
35
35
  library(survival)
36
36
 
37
- # read in data
38
- args <- commandArgs(trailingOnly = T)
39
- if (length(args) != 1) stop("one argument needed")
40
- infile <- args[1]
41
- dat <- fromJSON(infile)
37
+ # stream in json input
38
+ con <- file("stdin", "r")
39
+ json <- readLines(con)
40
+ close(con)
41
+ dat <- fromJSON(json)
42
42
 
43
43
  # perform survival analysis
44
44
  dat$series <- as.factor(dat$series)
package/src/lines2R.js DELETED
@@ -1,62 +0,0 @@
1
- /*
2
- Stream JavaScript data into and out of R.
3
-
4
- Arguments:
5
- - <Rscript>: [string] path to R script.
6
- - <lines>: [array] data lines.
7
- - <args>: [array] R script arguments.
8
-
9
- Given an R script and a JavaScript array of input data lines, the data lines are streamed into the standard input of the R script. The standard output of the R script is then returned as a JavaScript array of output data lines.
10
- */
11
-
12
- import fs from 'fs'
13
- import path from 'path'
14
- import serverconfig from './serverconfig'
15
- import { spawn } from 'child_process'
16
- import { Readable } from 'stream'
17
-
18
- export default async function lines2R(Rscript, lines, args = []) {
19
- try {
20
- await fs.promises.stat(Rscript)
21
- } catch (e) {
22
- throw `${Rscript} does not exist`
23
- }
24
- const stdout = []
25
- const stderr = []
26
- return new Promise((resolve, reject) => {
27
- const sp = spawn(serverconfig.Rscript, [Rscript, ...args])
28
- if (lines && lines.length > 0) {
29
- // if data lines are present, then data will be streamed into R
30
- // otherwise, data will be read into R using an argument
31
- try {
32
- const table = lines.join('\n') + '\n'
33
- Readable.from(table).pipe(sp.stdin)
34
- } catch (error) {
35
- sp.kill()
36
- let errmsg = error
37
- if (stderr.length > 0) errmsg += `\nR stderr: ${stderr.join('').trim()}`
38
- reject(errmsg)
39
- }
40
- }
41
- sp.stdout.on('data', data => stdout.push(data))
42
- sp.stderr.on('data', data => stderr.push(data))
43
- sp.on('error', err => reject(err))
44
- sp.on('close', code => {
45
- if (code !== 0) {
46
- // handle non-zero exit status
47
- let errmsg = `R process exited with non-zero status code=${code}`
48
- if (stdout.length > 0) errmsg += `\nR stdout: ${stdout.join('').trim()}`
49
- if (stderr.length > 0) errmsg += `\nR stderr: ${stderr.join('').trim()}`
50
- reject(errmsg)
51
- }
52
- if (stderr.length > 0) {
53
- // handle R stderr
54
- const err = stderr.join('').trim()
55
- const errmsg = `R process emitted standard error\nR stderr: ${err}`
56
- reject(errmsg)
57
- }
58
- const out = stdout.join('').trim().split('\n')
59
- resolve(out)
60
- })
61
- })
62
- }