R/filterTrack.R

## filterTrack-methods
##
##
###############################################################################
##' @name filterTrack
##' @aliases filterTrack
##' @title filterTrack
##' @rdname filterTrack-methods
##' @docType methods
##'
##' @description methods for filter and trim tracks based on track length.

##' @usage
##' filterTrack(trackll,filter=c(min=7,max=Inf)))
##' trimTrack(trackll,trimmer=c(min=1,max=32)))

##' @param trackll Track list output from readDiatrack().
##' @param filter length of track used for filtration. Only tracks pass through filter will be selected.
##' @param trimmer length of track used for trimming. All tracks will be trimmed by the trimmer.


##' @return
##' \itemize{
##' \item{trackll} filtered or trimmed tracks.
##' }

##' @details filter is used to filter out tracks that has length within a
##'   specified range (default 5~Inf); On the other hand, despite the lengths of
##'   tracks, trimmer is used to trim /cutoff all tracks to a specified range
##'   (default 1~32).
##'
##'
##' @examples
##' folder=system.file("extdata","SWR1",package="smt")
##' trackll=readDiatrack(folder)
##'
##' trackll.filter=filterTrack(trackll,filter=c(7,Inf))
##' trackll.trim=trimTrack(trackll,trimmer=c(1,20))
##'
##' # see the min and max length of the trackll
##' # trackLength() is a helper function output track length of trackll
##' lapply(trackLength(trackll),min)
##' lapply(trackLength(trackll.filter),min)
##'
##' lapply(trackLength(trackll),max)
##' lapply(trackLength(trackll.trim),max)
##'



##------------------------------------------------------------------------------
## filterTrack

## a function to filter trackll based on specified fitler value (filterTrack on track length), default 6 frames/steps to Inf

##' @export filterTrack
filterTrack=function(trackll,filter=c(min=7,max=Inf)){

    #filter=match.arg(filter)

    # reinforce name
    names(filter)=c("min","max")

    cat("applying filter, min",filter["min"],"  max",filter["max"],"\n")


    track.len=list()
    for (i in 1:length(trackll)){
        track.len[[i]]=sapply(trackll[[i]],function(track){dim(track)[1]})
        trackll[[i]]=trackll[[i]][ track.len[[i]]>=filter["min"] & track.len[[i]]<filter["max"] ]
    }

    return(trackll)
}


# no need for the focus swtich, as one can simply filter on a number that is bigger than the dt he wanted to draw on

##------------------------------------------------------------------------------
## trim long tracks into shorter ones

.trimTrack=function(track,min=1,max=32){
    t=track[min:max,]
    cc.t=complete.cases(t)
    t=t[cc.t,]
    return(t)
}

##' @export trimTrack
trimTrack=function(trackll,trimmer=c(min=1,max=32)){

    # reinforce name
    names(trimmer)=c("min","max")

    cat("applying trimmer, min",trimmer["min"],"  max",trimmer["max"],"\n")


    trackll.trim=list()
    length(trackll.trim)=length(trackll)
    names(trackll.trim)=names(trackll)
    for (i in 1:length(trackll)){
        trackll.trim[[i]]=lapply(trackll[[i]],.trimTrack,
                                 min=trimmer["min"],max=trimmer["max"])
    }

    return(trackll.trim)

}

# max(sapply(x[[1]],dim))

##' @export trackLength
trackLength=function(trackll){
    len=list()

    length(len)=length(trackll)
    names(len)=names(trackll)

    for (i in 1:length(trackll)){
        len[[i]]=sapply(trackll[[i]],function(track){
            dim(track)[1]}
        )
    }
    return(len)
}
snjy9182/smt-0.3.9-BETA documentation built on May 6, 2019, 7:32 a.m.