forked from zetumers/UCSF-De-Identified-Data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrelim FLOWSHEET Funcs.R
176 lines (135 loc) · 5.57 KB
/
Prelim FLOWSHEET Funcs.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
library(stringr)
library(xlsx)
library(gtools)
library(stringdist)
library(randomForest)
library(rpart)
library(ROCR)
library(pscl)
library(neuralnet)
library(lubridate)
library(dplyr)
library(mi)
detach("package:plyr", unload = TRUE)
get_rows <- function(file_name, chunk_size){
con = file(file_name, "r")
lines <- 0
while((new_lines <- length(readLines(con,chunk_size)))>0){
lines <- lines + new_lines
}
close(con)
#minus 1 for the header
return(lines - 1)
}
#for all future pulls, put the row number you find in row_num
#it allows the program to read the csv file in huge chunks.
###pulls the whole database
#not really useful
pull_db <- function(file_name, row_num, chunk_size){
header <- colnames(read.csv(file_name, header = TRUE, nrows = 2, stringsAsFactors = TRUE))
con = file(file_name, "r")
new_df <- read.csv(con, nrow = chunk_size, stringsAsFactors = TRUE, header = TRUE)
rows_left <- row_num - chunk_size
while(rows_left > 0){
bite <- min(chunk_size,rows_left)
df <- read.csv(con, nrow = bite, stringsAsFactors = TRUE, header = FALSE)
colnames(df) <- header
new_df <- rbind(new_df, df)
rows_left <- rows_left - bite
print(c(bite,rows_left, nrow(new_df)))
}
close(con)
return(new_df)
}
#combine this function with piper
piper_unique_pts <- function(output, df){
pts <- unique(c(output,unique(df$patient_ID)))
return(pts)
}
####piper pipes in whole database a chunk at a time and
####applies a function to that chunk.
####
piper <- function(file_name, func, row_num, chunk_size, ...){
header <- colnames(read.csv(file_name, header = TRUE, nrows = 2, stringsAsFactors = TRUE))
con = file(file_name, "r")
#read the first bit outside the loop
new_df <- read.csv(con, nrow = chunk_size, stringsAsFactors = TRUE, header = TRUE)
rows_left <- row_num - chunk_size
output <- NULL
output <- match.fun(func)(output, new_df, ...)
while(rows_left > 0){
#how much to look at next
bite <- min(chunk_size,rows_left)
#pulling that much into R
df <- read.csv(con, nrow = bite, stringsAsFactors = TRUE, header = FALSE)
colnames(df) <- header
#updating the output
output <- match.fun(func)(output, df, ...)
#how many rows left do we have?
rows_left <- rows_left - bite
}
close(con)
return(output)
}
#gets all the information in the table on pt_id's patients
#appends it to what we have so far
#doing this without rbind would be ideal.
piper_pt_extract <- function(output, df, pt_id){
output <- rbind(output, df[df$patient_ID %in% pt_id,])
return(output)
}
####creates a list for batches of 25 patients
pt_batch <- function(pt_list){
print("Remember it's index up to (index+1) - 1")
index <- seq(1, length(pt_list),25)
index <- c(index, length(pt_list))
return(index)
}
#note: only works on Sam's computer. Change the directories accordingly.
#goal_dir is the place you want to put all the patient files
pt_scrape <- function(file_name, pt_lt, row_num, chunk_size, goal_dir, ...){
#goal directory
directory <- getwd()
#create the batches of patients
index <- pt_batch(pt_lt)
#cycle through an order of an index of patients
for(i in 1:(length(index)-1)){
#get the directory back
setwd(directory)
#create a list of current pts.
current_pts <- pt_lt[index[i]:(index[i+1]-1)]
new_file_name <- paste("Pts ",index[i]," to ",index[i+1]-1, ".csv",sep ='')
df<- piper(file_name = file_name, func = piper_pt_extract,
row_num = row_num, chunk_size = chunk_size,
pt_id = current_pts)
#now put it in the right place!
setwd(goal_dir)
write.csv(df, new_file_name)
}
}
#Now the same functions with the labs.
#use piper but use lab scrape, etc.
lab_piper_pt_extract <- function(output, df, pt_id){
output <- rbind(output, df[df$Patient_ID %in% pt_id,])
return(output)
}
lab_pt_scrape <- function(file_name, pt_lt, row_num, chunk_size, goal_dir, ...){
#goal directory
directory <- getwd()
#create the batches of patients
index <- pt_batch(pt_lt)
#cycle through an order of an index of patients
for(i in 1:(length(index)-1)){
#get the directory back
setwd(directory)
#create a list of current pts.
current_pts <- pt_lt[index[i]:(index[i+1]-1)]
new_file_name <- paste("LABS for Pts ",index[i]," to ",index[i+1]-1, ".csv",sep ='')
df<- piper(file_name = file_name, func = lab_piper_pt_extract,
row_num = row_num, chunk_size = chunk_size,
pt_id = current_pts)
#now put it in the right place!
setwd(goal_dir)
write.csv(df, new_file_name)
}
}