R/featurePrior.R

featurePrior <- function(DT_featureLibrary, DT_methodMassInfo){

    ## Combine MRM method with feature data table
    DT <- cbind(DT_methodMassInfo, DT_featureLibrary)

    ## Melt data table such that variable=sample run and value=feature
    meltDT_featureLibrary <- meltDT(DT = DT)

    ## Remove NA feature values if the transition was not monitored
    ## |seq_along| means the code will 'skip' this for loop if there is only one
    ## method like in the GPC datasets so far
    for(i in seq_along(DT_methodMassInfo)){
        e <- colnames(DT_methodMassInfo)[i]
        e <- parse(text=e)
        remove <- meltDT_featureLibrary[eval(e) == 0 & method == colnames(DT_methodMassInfo)[i]]
        meltDT <- meltDT_featureLibrary[
            !sort(meltDT_featureLibrary[
                remove, on=c(colnames(meltDT_featureLibrary)), which=TRUE, nomatch=0])]
    }
    rm(DT, i)

    ## Estimate priors
    meltDT_featureLibrary[, c("prior_num") := sum(!is.na(value)), by = c("matrix", "Barcode")]
    meltDT_featureLibrary[, c("prior_den") := length(value), by = c("matrix", "Barcode")]
    meltDT_featureLibrary[, c("prior") := prior_num / prior_den]

    return(meltDT_featureLibrary)
}
jchitpin/blistR documentation built on July 8, 2019, 6:29 p.m.