Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored cndx and repi code #134

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 4 additions & 39 deletions R/mc1.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,11 @@ mc1 <- function(ac, wr = FALSE) {
## Set conc to three significant figures
dat[ , conc := signif(conc, 3)]

## Define replicate id
# Order by the following columns
setkeyv(dat, c('acid', 'srcf', 'apid', 'coli', 'rowi', 'spid', 'conc'))
# Define rpid column for test compound wells
nconc <- dat[wllt == "t" ,
list(n = lu(conc)),
by = list(acid, apid, spid)][ , list(nconc = min(n)), by = acid]
dat[wllt == "t" & acid %in% nconc[nconc > 1, acid],
rpid := paste(acid, spid, wllt, srcf, apid, "rep1", conc, sep = "_")]
dat[wllt == "t" & acid %in% nconc[nconc == 1, acid],
rpid := paste(acid, spid, wllt, srcf, "rep1", conc, sep = "_")]
# Define rpid column for non-test compound wells
dat[wllt != "t",
rpid := paste(acid, spid, wllt, srcf, apid, "rep1", conc, sep = "_")]

# set repid based on rowid
dat[, dat_rpid := rowid(rpid)]
dat[, rpid := sub("_rep[0-9]+.*", "",rpid, useBytes = TRUE)]
dat[, rpid := paste0(rpid,"_rep",dat_rpid)]

## Define concentration index
indexfunc <- function(x) as.integer(rank(unique(x))[match(x, unique(x))])
dat[ , cndx := indexfunc(conc), by = list(rpid)]
# set concentration index
dat[, cndx := frank(conc, ties.method = "dense"), by = .(acid, spid, wllt, srcf, apid)]

## Define replicate index
# Create temporary table containing the unique replicate ids

trdt <- unique(dat[wllt %in% c("t", "c") , list(acid, spid, wllt, rpid)])
trdt_rpid <- trdt[ , rpid]
trdt[ , rpid := NULL]
trdt[ , repi := rowidv(trdt)]
trdt[ , rpid := trdt_rpid]
rm(trdt_rpid)
# Map replicate index back to dat
setkey(dat, rpid)
setkey(trdt, rpid)
dat[ , repi := trdt[dat, repi]]

## Remove rpid column
dat[, rpid := NULL]
# set replicate index
dat[, repi:=rowid(conc),by= .(acid, spid, wllt, srcf, apid)]

ttime <- round(difftime(Sys.time(), stime, units = "sec"), 2)
ttime <- paste(unclass(ttime), units(ttime))
Expand Down