-
Notifications
You must be signed in to change notification settings - Fork 2
/
dispVariance.R
149 lines (136 loc) · 5.58 KB
/
dispVariance.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
## dispVariance-methods
##' @name dispVariance
##' @aliases dispVariance
##' @title dispVariance
##' @rdname dispVariance-methods
##' @docType methods
##'
##' @description calculate square displacements for all tracks in a trackll
##' datatype, and return the variances for the dispacements of each
##' trajectories.
##'
##' @usage
##' dispVariance(trackll, min=7, plot=FALSE, limits=c(), log=FALSE,
##' output=FALSE)
##' @param trackll a list of track lists.
##' @param min minimum points on trajectory, should be at least 3 to work.
##' @param plot default: False, if true, show density plot for variances.
##' @param limits vector of size2, variance cut-off range that one wants to
##' plot.
##' This will not affect the returned result.
##' @param log default: False, if true, apply log10 to variance value for new
##' spread. like limits, this will only affect the plot, not the returned
##' value.
##' @param output if True, generate a csv output for each tracklist files that
##' are in trackll.
##' @return \itemize{
##' \item{Variances} calculated variacne for all trakcs in trackll}
##'
##' @examples
##' folder=system.file('extdata','SWR1',package='sojourner')
##' trackll=createTrackll(folder=folder, input=3)
##'
##' # run dispVariance with default minimum tracklength (min=7)
##' dispVars = dispVariance(trackll)
##'
##' # run dispVariance by setting the minimum tracklength to 3
##' dispVars = dispVariance(trackll, min=3)
##'
##' # display plot only within certain range
##' dispVariance(trackll, plot=TRUE, limits = c(0,0.002))
##'
##' # display plot with log-scale applied
##' dispVars = dispVariance(trackll, min=3, plot=TRUE, log=TRUE)
##'
##' # display plot. Could get csv files if output = TRUE
##' dispVars = dispVariance(trackll, min=3, plot=TRUE, output=FALSE)
##'
##' @details
##' dispVariance applies the squareDisp function to each dataframe
##' containing trajectories. the tracks somehow had to be converted
##' into dataframes although they were expected to be in dataframes
##' in the first place.
##'
##' Since the tracks of shorter length are filtered out in the process,
##' there is no guqrante that the length of tracklists equal that of
##' the input.
##'
##' The tracks should have length of at least 3, in order to have a
##' valid displacement variance. If min argument is less than 3, the
##' function will not be executed.
##'
##' Generally, when plotting, you would want to use only one of limits
##' or log. Although you may use both, using only one of the two would
##' do the job.
##'
##' @export dispVariance
###############################################################################
##-----------------------------------------------------------------------------
## dispVariance_track
## calculate variance for displacements single track
##' @export dispVariance_track
dispVariance_track = function(track) {
withDisp = squareDisp(track) # get displacement
sqDisp = withDisp[[1]]$square.disp #[[1]] is necessary b/c
# squaredisplacement give a list in result
clean = sqrt(sqDisp[!is.na(sqDisp)]) #get rid of NA and do the sqrt since
# it is currently dx^2 + dy^2
var(clean) # return variance value
}
##-----------------------------------------------------------------------------
## dispVariance.trackl
## calculate variance for displacements single tracklist
dispVariance.trackl = function(trackl) {
lapply(trackl, dispVariance_track)
# track.df = lapply(trackl, data.frame) track.withdisp =
# sapply(track.df, squareDisp) sqdisp =
# lapply(track.withdisp,function(x) {x[7]})#column with square-disp
# sqdisp.clean = lapply(sqdisp, function(x) {x[!is.na(x)]}) disp.var =
# lapply(sqdisp.clean, var) disp.var
}
##-----------------------------------------------------------------------------
## dispVariance
## calculate displacement variance for all tracks in given trackll also
## filter based on tracklength(must be greater than 2) plotting feature
## can be enabled when plot=TRUE is set. limits on the variance range
## can be set by providing a vector of length 2.
##' @export dispVariance
dispVariance = function(trackll, min = 7, plot = FALSE, limits = c(),
log = FALSE, output = FALSE) {
if (min < 3) {
stop("min value should be at least 3")
}
# filter by length
filtered = filterTrack(trackll, filter = c(min = min, max = Inf))
result = lapply(filtered, dispVariance.trackl)
if (plot == TRUE) {
melted = reshape2::melt(result) #convert to dataframe
# rename columns to more reasonable ones
names(melted) = c("variance", "track.name", "trackList")
if (log) {
melted$variance = log10(melted$variance)
}
# different tracklists will show up as different colors with some
# transparency
plt = ggplot2::ggplot(melted, ggplot2::aes_string(x = "variance",
color = "trackList")) + ggplot2::geom_line(alpha = 0.5,
position = "identity", stat = "density")
if (length(limits) != 2) {
plot(plt)
} else {
plot(plt + ggplot2::xlim(limits)) #apply range limits
}
}
if (output == TRUE) {
for (i in seq_along(result)) {
track.df = reshape2::melt(result[[i]])
names(track.df) = c("dispVariance", "track.name")
track.df = track.df[, c(2, 1)]
fileName = paste("DispVariance Individual-",
.timeStamp(names(result)[i]), ".csv", sep = "")
cat("\nOutput dispVariance for individual trajectories.\n")
write.csv(file = fileName, track.df)
}
}
return(result)
}