Step-class {pipeFrame} | R Documentation

## Methods for Step objects

### Description

Users can call Step object operation methods below to obtain information in objects.

### Usage

    ## S4 method for signature 'Step'
    init(.Object, prevSteps = list(), ...)

    ## S4 method for signature 'Step'
    getStepName(.Object, ...)

    ## S4 method for signature 'Step'
    getDefName(.Object, ...)

    ## S4 method for signature 'Step'
    input(.Object)

    ## S4 replacement method for signature 'Step'
    input(.Object) <- value

    ## S4 method for signature 'Step'
    output(.Object)

    ## S4 replacement method for signature 'Step'
    output(.Object) <- value

    ## S4 method for signature 'Step'
    param(.Object)

    ## S4 replacement method for signature 'Step'
    param(.Object) <- value

    ## S4 method for signature 'Step'
    property(.Object)

    ## S4 replacement method for signature 'Step'
    property(.Object) <- value

    ## S4 method for signature 'Step'
    report(.Object)

    ## S4 replacement method for signature 'Step'
    report(.Object) <- value

    ## S4 method for signature 'Step'
    argv(.Object)

    ## S4 replacement method for signature 'Step'
    argv(.Object) <- value

    ## S4 method for signature 'Step'
    x$name

    ## S4 method for signature 'Step'
    getParam(.Object, item, type = c("input", "output",
      "other"), ...)

    ## S4 method for signature 'Step'
    getParamItems(.Object, type = c("input", "output",
      "other"), ...)

    ## S4 method for signature 'Step'
    isReady(.Object, ...)

    ## S4 method for signature 'Step'
    clearStepCache(.Object, ...)

    ## S4 method for signature 'Step'
    getReportVal(.Object, item, ...)

    ## S4 method for signature 'Step'
    getReportItems(.Object, ...)

    ## S4 method for signature 'Step'
    getAutoPath(.Object, originPath, regexSuffixName, suffix,
      ...)

    ## S4 method for signature 'Step'
    checkRequireParam(.Object, ...)

    ## S4 method for signature 'Step'
    checkAllPath(.Object, ...)

    ## S4 method for signature 'Step'
    getParamMD5Path(.Object, ...)

    ## S4 method for signature 'Step'
    getStepWorkDir(.Object, filename = NULL, ...)

    ## S4 method for signature 'Step'
    getStepId(.Object, ...)

    ## S4 method for signature 'Step'
    writeLog(.Object, msg, ..., isWarnning = FALSE,
      appendLog = TRUE, showMsg = TRUE)

    processing(.Object, ...)

    ## S4 method for signature 'Step'
    getReportValImp(.Object, item, ...)

    ## S4 method for signature 'Step'
    getReportItemsImp(.Object, item, ...)

### Arguments

`.Object` | `Step` object scalar. Step object is returned by functions in each step.
`prevSteps` | `List` scalar of Step object
`...` | Additional arguments, currently unused.
`value` | any type scalar. The value to be set for corresponding item in a list.
`x` | `Step` object scalar. Step object is returned by functions in each step.
`name` | `Character` scalar. Name can be one of inputList, outputList, paramList, allList, propList or the item names of inputList, outputList or paramList
`item` | `Character` scalar. The items in parameter list (input, output and other) or report list.
`type` | `Character` scalar. Valid types of parameters including "input", "output" and "other"
`originPath` | `Character` scalar. The file name for output file is based on this original path name.
`regexSuffixName` | `Character` scalar. The suffix for replacement.
`suffix` | `Character` scalar. The new suffix for the file.
`filename` | `Character` scaler. The name of file under step working directiory
`msg` | `Character` scalar. The message to write into log file.
`isWarnning` | `Logical` scalar. Set this message as warning message. Default: FALSE
`appendLog` | `Logical` scalar. Append to the log file. Default: TRUE
`showMsg` | `Logical` scalar. Show the message on screen. Default: TRUE

### Details

`Step` is a S4 class for generating Step S4 objects. All Step objects generated by child classes inherit from Step. To generate new Step objects, a function wrapper with fixed arguments needs to be implemented. Users use this function to generate new Step functions rather than Step S4 class to generate objects.

### Value

the function and result of functions:

`init` | (For package developer only) A Step child class object with initialized input, output and other parameters
`getStepName` | get Step object Character name
`getDefName` | get Step object Character name
`input` | get input list
`input<-` | set input list
`output` | get output list
`output<-` | set output list
`param` | get other parameters list
`param<-` | set other parameters list
`property` | get property list
`property<-` | set property list
`report` | get report list
`report<-` | set report list
`argv` | get arguments list
`argv<-` | set argumnets list
`$` | get inputList, outputList, paramList, allList, propList or any item value in inputList, outputList or paramList
`getParam` | Get parameter value set by process function. See `getParamItems` to obtain valid items for query.
`getParamItems` | Get parameter name list
`isReady` | Is the process ready for downstream process
`clearStepCache` | Clear cache of Step object
`getReportVal` | Get report value of item. See `getReportItems` to obtain valid items for query.
`getReportItems` | Get all items that can be reported
`getAutoPath` | (For package developer) Developer can use this method to generate new file name based on exist input file name
`checkRequireParam` | (For package developer) Check required inputs or parameters are filled.
`checkRequireParam` | (For package developer) Check required inputs are filled.
`getParamMD5Path` | The Step object storage directory
`getStepWorkDir` | Get the step work directory of this object
`getStepId` | Get the step ID
`writeLog` | (For package developer) write log.
`processing` | (For package developer) Run pipeline step
`getReportValImp` | (For package developer) get Report Value
`getReportItemsImp` | (For package developer) getReportItemsImp

### Author(s)

Zheng Wei

### See Also

`setGenome` `setThreads`

### Examples

    library(BSgenome)
    library(rtracklayer)
    library(magrittr)

    # generate new Step : RandomRegionOnGenome
    setClass(Class = "RandomRegionOnGenome",
             contains = "Step"
    )

    setMethod(
        f = "init",
        signature = "RandomRegionOnGenome",
        definition = function(.Object,prevSteps = list(),...){
            # All arguments in function randomRegionOnGenome
            # will be passed from "..."
            # so get the arguments from "..." first.
            allparam <- list(...)
            sampleNumb <- allparam[["sampleNumb"]]
            regionLen <- allparam[["regionLen"]]
            genome <- allparam[["genome"]]
            outputBed <- allparam[["outputBed"]]
            # no previous steps for this step so ingnore the "prevSteps"
            # begin to set input parameters
            # no input for this step
            # begin to set output parameters
            if(is.null(outputBed)){
                output(.Object)$outputBed <-
                    getStepWorkDir(.Object,"random.bed")
            }else{
                output(.Object)$outputBed <- outputBed
            }
            # begin to set other parameters
            param(.Object)$regionLen <-  regionLen
            param(.Object)$sampleNumb <- sampleNumb
            if(is.null(genome)){
                param(.Object)$bsgenome <-  getBSgenome(getGenome())
            }else{
                param(.Object)$bsgenome <-  getBSgenome(genome)
            }
            # don't forget to return .Object
            .Object
        }
    )

    setMethod(
        f = "processing",
        signature = "RandomRegionOnGenome",
        definition = function(.Object,...){
            # All arguments are set in .Object
            # so we can get them from .Object
            allparam <- list(...)
            sampleNumb <- getParam(.Object,"sampleNumb")
            regionLen <- getParam(.Object,"regionLen")
            bsgenome <- getParam(.Object,"bsgenome")
            outputBed <- getParam(.Object,"outputBed")
            # begin the calculation
            chrlens <-seqlengths(bsgenome)
            selchr <- grep("_|M",names(chrlens),invert=TRUE)
            chrlens <- chrlens[selchr]
            startchrlens <- chrlens - regionLen
            spchrs <- sample(x = names(startchrlens),
            size =  sampleNumb, replace = TRUE,
            prob = startchrlens / sum(startchrlens))
            gr <- GRanges()
            for(chr in names(startchrlens)){
                startpt <- sample(x = 1:startchrlens[chr],
                size = sum(spchrs == chr),replace = FALSE)
                gr <- c(gr,GRanges(seqnames = chr,
                ranges = IRanges(start = startpt, width = 1000)))
            }
            result <- sort(gr,ignore.strand=TRUE)
            rtracklayer::export.bed(object = result, con =  outputBed)
            # don't forget to return .Object
            .Object
        }
    )

    # This function is exported in NAMESPACE for user to use
    randomRegionOnGenome <- function(sampleNumb, regionLen = 1000,
                                     genome = NULL, outputBed = NULL, ...){
        allpara <- c(list(Class = "RandomRegionOnGenome", prevSteps = list()),
                     as.list(environment()),list(...))
        step <- do.call(new,allpara)
        invisible(step)
    }

    # generate another new Step : OverlappedRandomRegion
    setClass(Class = "OverlappedRandomRegion",
             contains = "Step"
    )

    setMethod(
        f = "init",
        signature = "OverlappedRandomRegion",
        definition = function(.Object,prevSteps = list(),...){
            # All arguments in function overlappedRandomRegion and
            # runOerlappedRandomRegion will be passed from "..."
            # so get the arguments from "..." first.
            allparam <- list(...)
            inputBed <- allparam[["inputBed"]]
            randomBed <- allparam[["randomBed"]]
            outputBed <- allparam[["outputBed"]]
            # inputBed can obtain from previous step object when running
            # runOerlappedRandomRegion
            if(length(prevSteps)>0){
                prevStep <- prevSteps[[1]]
                input(.Object)$randomBed <-  getParam(prevStep,"outputBed")
            }
            # begin to set input parameters
            if(!is.null(inputBed)){
                input(.Object)$inputBed <- inputBed
            }
            if(!is.null(randomBed)){
                input(.Object)$randomBed <- randomBed
            }
            # begin to set output parameters
            # the output is recemended to set under the step work directory
            if(!is.null(outputBed)){
                output(.Object)$outputBed <-  outputBed
            }else{
                output(.Object)$outputBed <-
                    getAutoPath(.Object, getParam(.Object, "inputBed"),
                                "bed", suffix = "bed")
                # the path can also be generate in this way
                # ib <- getParam(.Object,"inputBed")
                # output(.Object)$outputBed <-
                #    file.path(getStepWorkDir(.Object),
                #    paste0(substring(ib,1,nchar(ib)-3), "bed"))
            }
            # begin to set other parameters
            # no other parameters
            # don't forget to return .Object

            .Object
        }
    )
    setMethod(
        f = "processing",
        signature = "OverlappedRandomRegion",
        definition = function(.Object,...){
            # All arguments are set in .Object
            # so we can get them from .Object
            allparam <- list(...)
            inputBed <- getParam(.Object,"inputBed")
            randomBed <- getParam(.Object,"randomBed")
            outputBed <- getParam(.Object,"outputBed")

            # begin the calculation
            gr1 <- import.bed(con = inputBed)
            gr2 <- import.bed(con = randomBed)
            gr <- second(findOverlapPairs(gr1,gr2))
            export.bed(gr,con = outputBed)
            # don't forget to return .Object
            .Object
        }
    )

    # This function is exported in NAMESPACE for user to use
    overlappedRandomRegion <- function(inputBed, randomBed,
                                       outputBed = NULL, ...){
        allpara <- c(list(Class = "OverlappedRandomRegion",
            prevSteps = list()),as.list(environment()),list(...))
        step <- do.call(new,allpara)
        invisible(step)
    }

    setGeneric("runOverlappedRandomRegion",
               function(prevStep,
                        inputBed,
                        randomBed = NULL,
                        outputBed = NULL,
                        ...) standardGeneric("runOverlappedRandomRegion"))

    setMethod(
        f = "runOverlappedRandomRegion",
        signature = "Step",
        definition = function(prevStep,
                              inputBed,
                              randomBed = NULL,
                              outputBed = NULL,
                              ...){
            allpara <- c(list(Class = "OverlappedRandomRegion",
                prevSteps = list(prevStep)),as.list(environment()),list(...))
            step <- do.call(new,allpara)
            invisible(step)
        }
    )

    # add to graph
    addEdges(edges = c("RandomRegionOnGenome","OverlappedRandomRegion"),
             argOrder = 1)
    # begin to test pipeline
    setGenome("hg19")
    # generate test BED file
    test_bed <- file.path(tempdir(),"test.bed")
    library(rtracklayer)
    export.bed(GRanges("chr7:1-127473000"),test_bed)

    rd <- randomRegionOnGenome(10000)
    overlap <- runOverlappedRandomRegion(rd, inputBed = test_bed)

    randombed <- getParam(rd,"outputBed")

    randombed

    overlap1 <-
        overlappedRandomRegion(inputBed = test_bed, randomBed = randombed)

    clearStepCache(overlap1)
    overlap1 <-
        overlappedRandomRegion(inputBed = test_bed, randomBed = randombed)
    clearStepCache(rd)
    clearStepCache(overlap1)
    rd <- randomRegionOnGenome(10000) %>%
    runOverlappedRandomRegion(inputBed = test_bed)

    getStepName(rd)
    getStepId(rd)
    getDefName(rd)

    isReady(rd)

[Package pipeFrame version 1.0.0 Index]