diff --git a/R/data-model.R b/R/data-model.R index 973b9223caf1fb5268978e0f5c71d664391b5b0d..04ce025cae08488e1718359b63254bc2e3e8916b 100644 --- a/R/data-model.R +++ b/R/data-model.R @@ -118,7 +118,7 @@ empty_cgram_ms1 <- function(n=0L) { empty_cgram_ms2 <- function(n=0L) { r = data.table(precid=integer(n), ce=numeric(n), - scan=integer(n), + scan=character(n), idx=integer(n), rt=numeric(n), intensity=numeric(n)) @@ -128,11 +128,11 @@ empty_cgram_ms2 <- function(n=0L) { } -empty_spectra_table <- function() { - r = data.table(precid=integer(0), - scan=character(0), - mz=numeric(0), - intensity=numeric(0)) +empty_spectra_table <- function(n=0L) { + r = data.table(precid=integer(n), + scan=character(n), + mz=numeric(n), + intensity=numeric(n)) setkey(r,precid,scan) r } diff --git a/inst/testdata/plotting-test-data.rds b/inst/testdata/plotting-test-data.rds deleted file mode 100644 index 6a7745a4655b5663e348687366499a25a1ad0147..0000000000000000000000000000000000000000 Binary files a/inst/testdata/plotting-test-data.rds and /dev/null differ diff --git a/tests/testthat/helper-plotting.R b/tests/testthat/helper-plotting.R index fe15a08dd74147a4050a8eed68a2f6f8cc308115..87665e1e0da2ac9e4c3b1d86315d0c15ef8bb1d7 100644 --- a/tests/testthat/helper-plotting.R +++ b/tests/testthat/helper-plotting.R @@ -1,2 +1,2 @@ -PLOTTING_DATA <- readRDS(system.file("testdata","plotting-test-data.rds",package="shinyscreen")) +## PLOTTING_DATA <- readRDS(system.file("testdata","plotting-test-data.rds",package="shinyscreen")) diff --git a/tests/testthat/setup-plotting.R b/tests/testthat/setup-plotting.R index 6debe7807fa960185fcb09e8b16558800e7c7202..db9b6ea87138d888e679f7ab56c9777ad85ec8ad 100644 --- a/tests/testthat/setup-plotting.R +++ b/tests/testthat/setup-plotting.R @@ -1 +1,125 @@ -PLOTTINGDATA <- system.file("testdata","plotting-test-data.rds") + + + +PLOTTING_STATE_DB = readRDS(system.file(package="shinyscreen","testdata","plotting-state-db.rds")) + + +synthetise_cgm_ms1 <- function(n,fac,shift) { + dt = data.table(rt=numeric(n),intensity=numeric(n)) + dt[1L:n,rt:=seq(length.out=n)] + rt0 = dt[.N/2L + shift,rt] + dt[,intensity:=fac*exp(-((rt - rt0)/2.)**2)] +} + +synthetise_cgm_ms2 <- function(cgm1,shift) { + dt = copy(cgm1) + m = dt[,max(intensity)] + n0 = dt[,.N%/%2L] + shift + dt[,intensity:=NA_real_] + dt[n0-1L,intensity:=0.05*m] + dt[n0,intensity:=0.1*m] + dt[n0+1L,intensity:=0.07*m] + dt +} + + +synthetise_spectra <- function(n_mz,precursors,cgm2) { + + do_int <- function(n_mz,i) { + things = runif(n_mz - 1L,0.5*i,0.9*i) + c(things,i) + } + do_mass <- function(n_mz,mz) { + c(runif(n_mz-1L,0.1*mz,0.7*mz),mz) + } + cgm2[precursors,on=.(precid), + .(mz=do_mass(n_mz,i.mz), + intensity=do_int(n_mz,intensity[[1]]), + scan=scan[[1]]), + by=.EACHI] + +} + +synthetise_pseudo_state_db <- function(db) { + + + ## Fix input parameters. + set.seed(22) # Random generator seed for reproducible results. + n_time = 11L + n_mz = 3L + n_precid = length(db$precursors[,unique(precid)]) + n_cgm = n_time * n_precid + n_spectra = n_time * n_precid * n_mz + + shifts = sample(c(-1L,0L,1L),size=n_precid,replace=T) + facs = runif(n_precid,min=0.5)*1.e8 + inp = data.table(precid=db$precursors[,unique(precid)], + fac=facs, + shift=shifts) + setkey(inp,precid) + + + + + extr = list() + extr$cgm$ms1 = empty_cgram_ms1(n_cgm) + extr$cgm$ms2 = empty_cgram_ms2(n_cgm) + extr$spectra = empty_spectra_table(n_spectra) + uprec = db$precursors[,unique(.SD),.SDcols=c("file","tag","precid")] + uprec[,{ + f=file + for (i in seq(length.out=length(precid))) { + b=(.GRP-1L)*length(precid)*n_time + (i-1L)*n_time + 1L + e=b+n_time - 1L + pp = precid[[i]] + cx=(.GRP-1L)*length(precid) + i + + faci = inp[precid==pp,fac] + shifti = inp[precid==pp,shift] + ri = synthetise_cgm_ms1(n=n_time, + fac=faci, + shift=shifti) + + ri2 = synthetise_cgm_ms2(ri, + shift=shifti) + + extr$cgm$ms1[b:e, + `:=`(precid=pp, + cgmidx=cx, + file=f, + rt=ri[,rt], + intensity=ri[,intensity])] + + scans = paste0("F1.S",formatC(b:e,width=6,flag="0")) + extr$cgm$ms2[b:e, + `:=`(precid=pp, + ce=50, + scan=scans, + idx=b:e, + rt=ri2[,rt], + intensity=ri2[,intensity])] + + + + + } + + }, + by=c("file","tag")] + extr$cgm$ms2 = extr$cgm$ms2[!is.na(intensity)] + db$extr = extr + db$extr$spectra = synthetise_spectra(n_mz,db$precursors,db$extr$cgm$ms2) + + set.seed(NULL) + + browser() + 1+1 + db + +} + +## synthetise_eic_ms1 <- function(precursors) { +## ## N = +## dt = empty_cgram_ms1( +## } + diff --git a/tests/testthat/test-integration.R b/tests/testthat/test-integration.R index 7c0a667602ba4052725bec275f4af2dc84e1b0a6..3b46dc0ffb0c950440c359dd06e5b26959a0160c 100644 --- a/tests/testthat/test-integration.R +++ b/tests/testthat/test-integration.R @@ -13,6 +13,5 @@ test_that("Extraction returns what is needed.",{ m = run(envopts=eo,m=m,phase="extract") m = run(envopts=eo,m=m,phase="prescreen") - browser() expect_true(1==1) }) diff --git a/tests/testthat/test-plotting.R b/tests/testthat/test-plotting.R index 1f33bce0ee3ca62ede34c3a04563a3f8cb5ba9f4..bfcf5a3c1af798d510422306f79cd1cee02a7cde 100644 --- a/tests/testthat/test-plotting.R +++ b/tests/testthat/test-plotting.R @@ -1,3 +1,10 @@ +test_that("get_data_from_key",{ + + s = synthetise_pseudo_state_db(PLOTTING_STATE_DB) + expect_identical(1L,1L) +}) + + ## test_that("make_eic_ms1_plot",{ ## ms1 <- PLOTTING_DATA$ms1