forked from scRNA-tools/scRNA-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_csv.R
169 lines (144 loc) · 5.47 KB
/
process_csv.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
#!/usr/bin/env Rscript
library(docopt)
"Usage: process_csv OUTPUT_DIR
-h --help show this
This utility script converts the 'single_cell_software.csv'
spreadsheet to a set of files including:
- data/software.json
- data/categories.json
" -> doc
opts <- docopt(doc)
print(opts)
library(readr)
library(jsonlite)
library(dplyr)
library(tidyr)
library(lubridate)
library(stringr)
library(rvest)
library(rcrossref)
#' Create tidy sheet from the google sheet
#' @export
get_swsheet <- function() {
message("Getting Bioconductor package list...")
bioc.pkgs <- BiocInstaller::all_group()
names(bioc.pkgs) <- str_to_lower(bioc.pkgs)
message("Getting PyPI package list...")
pypi.pkgs <- read_html("https://pypi.python.org/simple/") %>%
html_nodes("a") %>%
html_text()
names(pypi.pkgs) <- str_to_lower(pypi.pkgs)
message("Getting CRAN package list...")
cran.url <- "https://cran.r-project.org/web/packages/available_packages_by_name.html"
cran.pkgs <- read_html(cran.url) %>%
html_nodes("a") %>%
html_text() %>%
setdiff(LETTERS) # Remove letter links at top of page
names(cran.pkgs) <- str_to_lower(cran.pkgs)
message("Processing table...")
swsheet <- read_csv("single_cell_software.csv",
col_types = cols(
.default = col_logical(),
Name = col_character(),
Platform = col_character(),
DOI = col_character(),
PubDate = col_character(),
Code = col_character(),
Description = col_character(),
License = col_character(),
Added = col_date(format = ""),
Updated = col_date(format = "")
)) %>%
mutate(Preprint = (PubDate == "PREPRINT")) %>%
mutate(PubDate = ifelse(Preprint == FALSE, PubDate, NA)) %>%
mutate(PubDate = as_date(PubDate)) %>%
mutate(Preprint = ifelse(Preprint == TRUE, TRUE, NA)) %>%
mutate(DOI_url = ifelse(is.na(DOI), NA,
paste0('http://dx.doi.org/', DOI))) %>%
mutate(Github = ifelse(str_detect(Code, "github"),
str_replace(Code, "https://github.com/", ""),
NA)) %>%
mutate(Bioconductor = str_to_lower(Name) %in% names(bioc.pkgs)) %>%
mutate(Bioconductor = ifelse(Bioconductor,
bioc.pkgs[str_to_lower(Name)], NA)) %>%
mutate(CRAN = str_to_lower(Name) %in% names(cran.pkgs)) %>%
mutate(CRAN = ifelse(CRAN, cran.pkgs[str_to_lower(Name)], NA)) %>%
mutate(CRAN = ifelse(str_detect(Platform, "R"), CRAN, NA)) %>%
mutate(pypi = str_to_lower(Name) %in% names(pypi.pkgs)) %>%
mutate(pypi = ifelse(pypi, pypi.pkgs[str_to_lower(Name)], NA)) %>%
mutate(pypi = ifelse(str_detect(str_to_lower(Platform), "python"),
pypi, NA))
message("Getting citations...")
swsheet$citations <- get_citations(swsheet$DOI)
return(swsheet)
}
tidy_swsheet <- function(swsheet) {
message("Tidying data...")
gather(swsheet, key = 'category', value = 'val',
-Description, -Name, -Platform, -DOI, -PubDate, -Updated, -Added,
-Preprint, -Code, -Github, -DOI_url, -License, -Bioconductor, -pypi,
-CRAN, -citations) %>%
filter(val == TRUE) %>%
select(-val) %>%
arrange(Name)
}
get_citations <- function(dois) {
cites <- sapply(dois, function(doi) {
if (is.na(doi)) {
return(NA)
}
cit <- tryCatch({
cr_citation_count(doi)
}, error = function(e) {
NA
})
Sys.sleep(sample(seq(0,2,0.5), 1))
return(cit)
})
return(cites)
}
tidysw_to_list_df <- function(tidysw) {
catlist <- split(tidysw$category, f = tidysw$Name)
tidyswl <- tidysw %>%
select(-category) %>%
unique()
tidyswl[['categories']] <- catlist[tidyswl$Name]
tidyswl
}
tidysw_to_cat_df <- function(tidysw, swsheet) {
namelist <- split(tidysw$Name, f = tidysw$category)
namelist <- lapply(namelist, function(x) {
swsheet %>%
filter(Name %in% x) %>%
select(Name, Bioconductor, CRAN, pypi)
})
tidyswl <- tidysw %>%
select(category) %>%
arrange(category) %>%
unique()
tidyswl[['software']] <- namelist[tidyswl$category]
tidyswl
}
add_cats_column <- function(swsheet, tidysw) {
catlist <- split(tidysw$category, f = tidysw$Name)
catdf <- data.frame(Name = names(catlist), stringsAsFactors = FALSE)
catdf[['categories']] <- catlist
swsheet <- left_join(swsheet, catdf, by = "Name")
}
#' write out json and csv files
#'
#' @export
write_files <- function(destdir) {
dir.create(destdir, recursive = TRUE)
swsheet <- get_swsheet()
tidysw <- tidy_swsheet(swsheet)
#write_csv(swsheet,path=file.path(destdir,'single-cell-software_tidy.csv'))
swsheet <- add_cats_column(swsheet, tidysw)
writeLines(toJSON(swsheet, pretty = TRUE),
file.path(destdir, 'software-table.json'))
writeLines(toJSON(tidysw_to_list_df(tidysw), pretty = TRUE),
file.path(destdir, 'software.json'))
writeLines(toJSON(tidysw_to_cat_df(tidysw, swsheet), pretty = TRUE),
file.path(destdir, 'categories.json'))
}
write_files(opts$OUTPUT_DIR)