Skip to content

Commit

Permalink
Update imports
Browse files Browse the repository at this point in the history
  • Loading branch information
saketkc committed Jan 12, 2024
1 parent 8f2912e commit 5e762ac
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 24 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Package: covmuller
Type: Package
Title: A tool to model COVID19 variant prevalence using data from GISAID
Version: 0.1.2.0001
Date: 2023-09-19
Version: 0.1.2.0002
Date: 2024-01-12
Author: Saket Choudhary
Maintainer: Saket Choudhary <[email protected]>
Description: covmuller can be used to process data from GISAID and
perform modeling on variant prevalence with lots of supported visualisation.
License: MIT
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
URL: https://saketkc.github.io/covmuller
BugReports: https://github.com/saketkc/covmuller/issues
Depends:
Expand All @@ -30,6 +30,7 @@ Imports:
magrittr,
nnet,
patchwork,
readr,
reshape2,
RJSONIO,
scales,
Expand Down
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ importFrom(dplyr,mutate)
importFrom(dplyr,pull)
importFrom(dplyr,recode)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,select_if)
importFrom(dplyr,summarise)
importFrom(dplyr,summarise_all)
Expand All @@ -77,7 +78,9 @@ importFrom(ggplot2,ggtitle)
importFrom(ggplot2,guide_axis)
importFrom(ggplot2,guides)
importFrom(ggplot2,labs)
importFrom(ggplot2,position_dodge)
importFrom(ggplot2,scale_color_identity)
importFrom(ggplot2,scale_color_manual)
importFrom(ggplot2,scale_fill_brewer)
importFrom(ggplot2,scale_fill_gradient2)
importFrom(ggplot2,scale_x_date)
Expand All @@ -101,7 +104,11 @@ importFrom(scales,cut_short_scale)
importFrom(scales,label_number)
importFrom(scales,label_percent)
importFrom(splines,ns)
importFrom(stats,as.formula)
importFrom(stats,median)
importFrom(stats,predict)
importFrom(stringi,stri_split_fixed)
importFrom(stringr,str_count)
importFrom(stringr,str_split_fixed)
importFrom(stringr,str_squish)
importFrom(stringr,str_to_title)
Expand All @@ -114,4 +121,6 @@ importFrom(tsibble,scale_x_yearweek)
importFrom(tsibble,yearmonth)
importFrom(tsibble,yearweek)
importFrom(utils,URLencode)
importFrom(utils,read.csv)
importFrom(utils,untar)
importFrom(zoo,as.yearmon)
2 changes: 1 addition & 1 deletion R/fetch_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ GetIndiaHospitalization <- function(url = "") {
#' @returns A data frame containing monthly cases for each state in long form

#' @importFrom magrittr %>%
#' @importFrom dplyr arrange group_by summarise_all rename
#' @importFrom dplyr arrange group_by select summarise_all rename
#' @importFrom readr read_csv
#' @importFrom reshape2 melt
#' @export
Expand Down
4 changes: 3 additions & 1 deletion R/gisaid.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @importFrom stringr str_split_fixed str_trim
#' @importFrom tools file_ext
#' @importFrom data.table fread
#' @importFrom utils untar
#' @export
ReadGISAIDMetada <- function(path, showProgress = FALSE, ...) {
file.ext <- file_ext(path)
Expand Down Expand Up @@ -85,6 +86,7 @@ FormatGISAIDMetadata <- function(df, collection_col = "Collection date", submiss
#' @returns A dataframe with all the instrument related metadata
#' @importFrom stringi stri_split_fixed
#' @importFrom dplyr bind_rows distinct
#' @importFrom utils read.csv
#' @export
ReadAuspiceInstrument <- function(path) {
metadata <- list()
Expand All @@ -94,7 +96,7 @@ ReadAuspiceInstrument <- function(path) {
)) {
date_path <- unlist(stri_split_fixed(str = file, pattern = "/"))
date_path <- date_path[length(date_path) - 1]
metadata[[date_path]] <- read.csv(file, sep = "\t")
metadata[[date_path]] <- read.csv(file = file, sep = "\t")
}
seq_metadata <- bind_rows(metadata) %>% distinct()

Expand Down
2 changes: 2 additions & 0 deletions R/model.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#' @importFrom stats as.formula
#' @importFrom magrittr %>%
#' @importFrom tibble deframe
#' @importFrom nnet multinom
Expand All @@ -14,6 +15,7 @@ FitMultinom <- function(data,
return(fit)
}

#' @importFrom stats predict
#' @importFrom magrittr %>%
#' @importFrom tibble deframe
#' @importFrom nnet multinom
Expand Down
7 changes: 7 additions & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
#' @returns A vector with dates converted to MonthYear format (zoo::as.yearmon)
#' @importFrom dplyr pull
#' @importFrom zoo as.yearmon
#' @importFrom stringr str_count
#' @export
GetMonthYear <- function(datecol, datefmt = "%Y-%m-%d") {
number_dashes <- str_count(string = datecol, pattern = "-")
if (datefmt == "%Y-%m-%d"){
datecol[number_dashes == 1] <- paste0(datecol[number_dashes == 1], "-01")
} else if (datefmt == "%d-%m-%Y"){
datecol[number_dashes == 1] <- paste0("01-", datecol[number_dashes == 1])
}
Date <- as.Date(datecol, format = datefmt)
Month <- strftime(Date, "%m")
Year <- strftime(Date, "%Y")
Expand Down
9 changes: 5 additions & 4 deletions R/visualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ CovmullerTheme <- function() {
#' @importFrom hrbrthemes theme_ipsum
#' @importFrom ggtext element_markdown
#' @importFrom patchwork wrap_plots
#' @importFrom stats median
#' @export
PlotTotalHeatmap <- function(df, color_legend = "Total cases") {
df_india <- df %>% filter(State == "India")
Expand Down Expand Up @@ -111,7 +112,7 @@ PlotSequencedPropHeatmap <- function(df) {

#' @importFrom magrittr %>%
#' @importFrom tibble deframe
#' @importFrom ggplot2 aes_string coord_cartesian ggplot geom_bar geom_text labs scale_y_continuous scale_x_discrete xlab ylab guide_axis
#' @importFrom ggplot2 aes_string coord_cartesian ggplot geom_bar geom_text labs scale_y_continuous scale_x_discrete xlab ylab guide_axis position_dodge
#' @importFrom scales comma label_percent label_number cut_short_scale
#' @importFrom ggtext element_markdown
#' @importFrom patchwork wrap_plots
Expand Down Expand Up @@ -189,7 +190,7 @@ BarPlot <- function(df, xaxis = "MonthYear",
return(wrap_plots(p))
}

#' @importFrom ggplot2 ggplot geom_bar labs scale_fill_brewer scale_x_discrete xlab ylab guide_axis
#' @importFrom ggplot2 ggplot geom_bar labs scale_fill_brewer scale_x_discrete xlab ylab guide_axis scale_color_manual
#' @importFrom scales label_number cut_short_scale
#' @importFrom ggtext element_markdown
#' @importFrom patchwork wrap_plots
Expand Down Expand Up @@ -234,7 +235,7 @@ PlotMullerDailyPrevalence <- function(df, ncol = 4) {
wrap_plots(p)
}

#' @importFrom ggplot2 ggplot geom_line geom_label scale_fill_brewer scale_y_continuous xlab ylab labs ggtitle guide_axis theme element_text
#' @importFrom ggplot2 ggplot geom_line geom_label scale_fill_brewer scale_y_continuous xlab ylab labs ggtitle guide_axis theme element_text scale_color_manual
#' @importFrom scales label_number cut_short_scale
#' @importFrom gganimate transition_reveal view_follow animate gifski_renderer
#' @importFrom tsibble scale_x_yearweek
Expand All @@ -254,7 +255,7 @@ PlotVariantPrevalenceAnimated <- function(df, title = NULL, caption = "**Source:
) +
geom_line() +
scale_x_yearweek(date_breaks = date_breaks, date_labels = "%d %b %Y", guide = guide_axis(angle = 90)) +
scale_y_continuous(label = label_number(accuracy = 1, scale_cut = cut_short_scale())) + # , trans = trans_y) +
scale_y_continuous(labels = label_number(accuracy = 1, scale_cut = cut_short_scale())) + # , trans = trans_y) +
geom_label(hjust = 0, aes(label = variant), nudge_x = 10, show.legend = FALSE) +
geom_point() +
coord_cartesian(ylim = c(0, NA), clip = "off") +
Expand Down
Binary file modified docs/articles/USA_animated.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 75 additions & 13 deletions vignettes/VariantAnimation-USA.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,49 @@ gisaid_usa <- CollapseLineageToVOCs(
# Get weekly cases for USA

```{r, warning=FALSE, message=FALSE}
confirmed <- covid19(country = "USA", level = 1) %>%
select(date, confirmed) %>%
filter(!is.na(confirmed))
confirmed$daily_cases <- c(confirmed$confirmed[1], diff(confirmed$confirmed))
GetCases <- function(){
data <- read.csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/cases_deaths/new_cases.csv")
confirmed <- data %>% select(date, United.States)
colnames(confirmed)[2] <- c("cases")
confirmed$MonthYear <- GetMonthYear(confirmed$date)
confirmed$WeekYear <- tsibble::yearweek(confirmed$date)
return (confirmed)
}
confirmed_subset_dateweekwise_long <- confirmed %>%
group_by(WeekYear) %>%
summarise(n = ceiling(mean(daily_cases, na.rm = T))) %>%
arrange(WeekYear) %>%
rename(WeekYearCollected = WeekYear)
GetCasesLong <- function(){
data <- read.csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/cases_deaths/new_cases.csv")
confirmed <- data %>% select(date, United.States)
colnames(confirmed)[2] <- c("cases")
confirmed$MonthYear <- GetMonthYear(confirmed$date)
confirmed$WeekYear <- tsibble::yearweek(confirmed$date)
confirmed_subset_weekwise <- confirmed %>%
group_by(WeekYear) %>%
summarise(cases = mean(cases, na.rm=T)) %>%
arrange(WeekYear)
confirmed_subset_weekwise$cases <- ceiling(confirmed_subset_weekwise$cases)
confirmed_subset_dateweekwise_long_india <- confirmed_subset_weekwise %>%
rename(n = cases) %>%
rename(WeekYearCollected = WeekYear)
}
confirmed <- GetCases()
confirmed_subset_dateweekwise_long <- GetCasesLong()
#
# confirmed <- covid19(country = "USA", level = 1) %>%
# select(date, confirmed) %>%
# filter(!is.na(confirmed))
#
# confirmed$daily_cases <- c(confirmed$confirmed[1], diff(confirmed$confirmed))
# confirmed$WeekYear <- tsibble::yearweek(confirmed$date)
#
# confirmed_subset_dateweekwise_long <- confirmed %>%
# group_by(WeekYear) %>%
# summarise(n = ceiling(mean(daily_cases, na.rm = T))) %>%
# arrange(WeekYear) %>%
# rename(WeekYearCollected = WeekYear)
gisaid_usa_weekwise <- SummarizeVariantsWeekwise(gisaid_usa)
Expand Down Expand Up @@ -133,7 +164,7 @@ voc_to_keep <- gisaid_usa_weekwise %>%
gisaid_usa_weekwise <- gisaid_usa_weekwise %>% filter(lineage_collapsed %in% voc_to_keep)
usa_cases_pred_prob_sel_long <- FitMultinomWeekly(gisaid_usa_weekwise, confirmed_subset_dateweekwise_long)
the_anim <- PlotVariantPrevalenceAnimated(usa_cases_pred_prob_sel_long, title = "Estimated cases (weekly average) in the USA by variant", caption = "**Source: gisaid.org and covid19nytimes**<br>", date_breaks = "28 days")
the_anim <- PlotVariantPrevalenceAnimated(usa_cases_pred_prob_sel_long, title = "Estimated cases (weekly average) in the USA by variant", caption = "**Source: gisaid.org and covid19nytimes**<br>", date_breaks = "100 days")
gganimate::anim_save(filename = here::here("docs/articles/USA_animated.gif"), animation = the_anim)
```

Expand Down Expand Up @@ -163,12 +194,12 @@ voc_to_keep <- gisaid_usa_weekwise %>%
gisaid_usa_weekwise <- gisaid_usa_weekwise %>% filter(lineage_collapsed %in% voc_to_keep)
usa_cases_pred_prob_sel_long <- FitMultinomWeekly(gisaid_usa_weekwise, confirmed_subset_dateweekwise_long)
the_anim <- PlotVariantPrevalenceAnimated(usa_cases_pred_prob_sel_long, title = "Estimated cases (weekly average) in the USA by variant", caption = "**Source: gisaid.org and covid19nytimes**<br>")
the_anim <- PlotVariantPrevalenceAnimated(usa_cases_pred_prob_sel_long, title = "Estimated cases (weekly average) in the USA by variant", caption = "**Source: gisaid.org and covid19nytimes**<br>", date_breaks = "100 days")
gganimate::anim_save(filename = here::here("docs/articles/USA_animated_2021.gif"), animation = the_anim)
```

![](USA_animated_2021.gif)
Look at cases in the past few weeks
Look at cases from 2023

```{r}
confirmed$MonthYear <- GetMonthYear(confirmed$date)
Expand Down Expand Up @@ -197,3 +228,34 @@ gganimate::anim_save(filename = here::here("docs/articles/USA_animated_2023.gif"
```

![](USA_animated_2023.gif)



Look at cases in the past few weeks

```{r}
confirmed$MonthYear <- GetMonthYear(confirmed$date)
confirmed_subset_dateweekwise_long <- confirmed %>%
filter(MonthYear > "April 2023") %>%
group_by(WeekYear) %>%
summarise(n = ceiling(mean(daily_cases, na.rm = T))) %>%
arrange(WeekYear) %>%
rename(WeekYearCollected = WeekYear)
gisaid_usa_subset <- gisaid_usa %>% filter(MonthYearCollected > "December 2022")
gisaid_usa_weekwise <- SummarizeVariantsWeekwise(gisaid_usa_subset)
voc_to_keep <- gisaid_usa_weekwise %>%
group_by(lineage_collapsed) %>%
summarise(n_sum = sum(n)) %>%
filter(n_sum > 50) %>%
pull(lineage_collapsed) %>%
unique()
gisaid_usa_weekwise <- gisaid_usa_weekwise %>% filter(lineage_collapsed %in% voc_to_keep)
usa_cases_pred_prob_sel_long <- FitMultinomWeekly(gisaid_usa_weekwise, confirmed_subset_dateweekwise_long)
the_anim <- PlotVariantPrevalenceAnimated(usa_cases_pred_prob_sel_long, title = "Estimated cases (weekly average) in the USA by variant", caption = "**Source: gisaid.org and covid19nytimes**<br>")
gganimate::anim_save(filename = here::here("docs/articles/USA_animated_2024.gif"), animation = the_anim)
```
![](USA_animated_2024.gif)

0 comments on commit 5e762ac

Please sign in to comment.