Last active
June 13, 2019 17:09
-
-
Save davebridges/24ff2b5b57ac09f05d87ac932b59c081 to your computer and use it in GitHub Desktop.
Code for extracting data from mousedb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#To use this you must supply: | |
#a cohort_id number | |
#an output datafile ('something.csv') | |
#the api_key to access the database | |
#for example: | |
#raw_data_file <- "Raw MRI Data.csv" | |
#cohort_id <- 35 | |
#api_key <- 'SEE SLACK FOR THE API KEY' | |
#update.data(cohort_id=cohort_id, datafile=raw_data_file,api_key=api_key) | |
update.data <- function(cohort_id, datafile, api_key){ | |
library(RCurl) | |
library(jsonlite) | |
#get data from this cohort | |
cohort.url <- paste('http://bridgeslab.sph.umich.edu/mousedb/api/v1/data/?format=json&',api_key,'&limit=1000&animal__cohort=',cohort_id, sep="") | |
cohort.json <- fromJSON(getURL(cohort.url)) | |
data_values <- cohort.json$meta$total_count | |
if(data_values <1000) { | |
data <- fromJSON(getURL(cohort.url), flatten=T)$objects | |
} else{ | |
offsets <- seq(1000,ceiling(data_values / 1000) * 1000,by=1000) #get integers from 2000 to x000 rounding up from data_values | |
data <- fromJSON(getURL(cohort.url), flatten=T)$objects | |
for (offset in offsets){ | |
bigger.cohort.url <- paste('http://bridgeslab.sph.umich.edu/mousedb/api/v1/data/?format=json&',api_key,'&limit=1000&&offset=',offset,'&animal__cohort=',cohort_id, sep="") | |
data <- bind_rows(data, fromJSON(getURL(bigger.cohort.url), flatten=T)$objects) | |
} | |
} | |
data <- | |
data %>% | |
rename(MouseID = animal.MouseID, | |
assay = assay.assay, | |
Sex = animal.Gender, | |
Genotype = animal.Genotype) %>% | |
select(age,animal.id,MouseID,Sex,Genotype,values, assay, experiment.date) | |
write_csv(x=data, path=datafile) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment