## Using Metaverse for Meta-Analyses ##
## by Erica Holdridge ##
# updated June 4, 2021 #

#install packages
install.packages(c("remotes","dplyr","ggplot2","ggraph","igraph","readr","devtools"))
remotes::install_github("rmetaverse/metaverse")

#synthesisr
#import, assemble and de-duplicate bibliography
library(synthesisr)

#litsearchr
#search strategy development

#load necessary packages
library(dplyr)
library(ggplot2)
library(ggraph)
library(igraph)
library(readr)
library(devtools)
remotes::install_github("elizagrames/litsearchr", ref="main")
library(litsearchr)

# Now you need to create a "naive search" that litsearchr can use to make
# improvements. You can use any database you like (Google Scholar, Web of
# Science, Scopus, etc) to retrieve a file that litsearchr can read. For this
# example, I'm using a .txt file that I downloaded from Web of Science using
# the search terms bacteriophage, phage, ecology, "environmental sample", 
# "nutrient cycle", metabolism, -medicine.

naive_results<-litsearchr::import_results(file="/Users/ericaholdridge/Downloads/savedrecs.txt")

# You can look at the imported file to get an idea of the structure
View(naive_results)
colnames(naive_results)

# We can use the extract_terms() function to gather keywords that might help
# us find more articles of interest. We can pull them from any column, so let's
# try author-provided keywords and titles. We can set minimum number 
# of words in these extracted terms (min_n), minimum number of results it has
# to appear in to be counted (min_freq), and the method for filtering out terms.
# We also want to try to filter out words that generally appear a lot in English
# but are uninformative to us.

#this is a nonexhaustive list of general ecology terms that I made
micro_eco_stopwords<-readLines("/Users/ericaholdridge/Desktop/microbial_ecology_stopwords.txt")
#this combines that list with commonly used words in English
all_stopwords <- c(get_stopwords("English"), micro_eco_stopwords)
key_terms<-extract_terms(keywords=naive_results[, "keywords"], method="tagged", min_n=1, min_freq=3,
                         language = "English", stopwords=all_stopwords)
title_terms<-extract_terms(text=naive_results[, "title"], method="fakerake", min_n=1, min_freq=3,
                           language = "English", stopwords=all_stopwords)
terms<-unique(c(key_terms,title_terms))

# We can now visualize how often these search terms are used together with
# network analysis. This will let us identify terms that are only mentioned in
# a few articles without many of the other search terms.

docs <- paste(naive_results[, "title"], naive_results[, "abstract"])
df <- create_dfm(elements=docs, features=terms)
g <- create_network(df, min_studies=3)

ggraph(g, layout="stress") +
  coord_fixed() +
  expand_limits(x=c(-3, 3)) +
  geom_edge_link(aes(alpha=weight)) +
  geom_node_point(shape="circle filled", fill="white") +
  geom_node_text(aes(label=name), hjust="outward", check_overlap=TRUE) +
  guides(edge_alpha=FALSE)

# In our case, notice outliers like "antibiotic resistance" and "coliophages".
# These won't be very useful for our purposes so we can eliminate them.

strengths <- strength(g)
data.frame(term=names(strengths), strength=strengths, row.names=NULL) %>%
  mutate(rank=rank(strength, ties.method="min")) %>%
  arrange(strength) ->
  term_strengths
# We can look at which terms are most weakly linked (listed from weakest to 
# strongest)
term_strengths

# We  can use the strengths to create a cutoff, below which we will remove
# terms with weak associations. We will let litsearchr find cutoff 3 points where
# there are big "jumps" in strength
cutoff<-find_cutoff(g, method="changepoint", knot_num=3)

ggplot(term_strengths, aes(x=rank, y=strength, label=term)) +
  geom_line() +
  geom_point() +
  geom_text(data=filter(term_strengths, rank>5), hjust="right", nudge_y=20, check_overlap=TRUE) +
  geom_hline(yintercept=cutoff, linetype="dashed")

g_redux <- reduce_graph(g, cutoff[1])
selected_terms <- get_keywords(g_redux)
selected_terms

# Now we manually group our new search terms into subtopics, which will
# help us create a new search query.

grouped_terms <- list(organismal=selected_terms[c(1,2,3,11,12,13,20,21)],
                      environment=selected_terms[c(6,9,14,15,16,19,22)],
                      technique=selected_terms[c(8,17,18)],
                      general=selected_terms[c(4,5,7,10)])

# And we can use these new grouping to create a new search that will 
# hopefully return more articles of interest. We can copy and paste the 
# new search (the contents of the .txt file) back into our database and
# see our new results.

write_search(
  grouped_terms,
  languages="English",
  exactphrase=TRUE,
  stemming=FALSE,
  closure="left",
  writesearch=TRUE
)

# I manually curated this a bit and ended up with:
# bacteriophage OR phage OR virus AND environment OR aquatic OR marine OR soil
# AND genomics OR isolated OR isolation AND ecology OR "microbial ecology" 
# NOT medicine.
# This increased results on WoS from 117 to 325.
# We can check to make sure all the results from our first search are included
# in the new one and add in any ones that aren't. You may want to manually 
# check these over to make sure they really are relevant.

new_results <- import_results(file="/Users/ericaholdridge/Downloads/savedrecs_new.txt")

naive_results %>%
  mutate(in_new_results=title %in% new_results[, "title"]) ->
  naive_results

not_included<- naive_results %>%
  filter(!in_new_results)
View(not_included)

#I'm doing a little work here to make sure the column names line up
cols_to_keep <- intersect(colnames(not_included),colnames(new_results))
not_included2 <- not_included[,cols_to_keep,drop=FALSE]
new_results2 <- new_results[,cols_to_keep,drop=FALSE]

all_results <- rbind(not_included2,new_results2)

#This set gets rid of any duplicates 
library(synthesisr)
dedup_results<- deduplicate(
  all_results,
  match_by = "title",
  method = "exact"
)

#Now we have a little over 400 papers that match our search criteria

#revtools
#article screening for evidence synthesis
library(revtools)

# We can double check to make sure we removed all the duplicates.
screen_duplicates(all_results)

# We can use the app to manually screen paper titles and exclude any that don't
# seem relevant. Upon saving and exiting the app, it will write these new 
# results back to 'new_all_results'. You can also screen by abstracts using
# the screen_abstracts() function.

new_all_results <- screen_titles(all_results)

# Now we can screen our results in the revtools app. This will generate a 
# model that clusters similar papers together and ranks the terms that produce
# these similarities. 

screen_topics(all_results)
# Once the screen launches, select the variables you want to use to build the
# model. I selected title, abstract, keywords, and research_areas to start. 
# From the drop down menu, select "Show one point per: title". You can adjust
# the settings under "Processing" and "Model" or leave them as the defaults.
# Increasing the number of iterations will improve the model quality but will
# take longer to run. When you're ready, hit "Calculate Model".

# You will see an ordination plot, which groups papers that are similar to
# one another. If you select "Show words" under the "Display" tab, you can see 
# which terms are grouping similar papers together. This can help you see which
# papers are grouped into which subtopics (e.g. in this example, Group 4 
# contains papers that use sequencing and metagenomics). If any groups don't
# match your topic of interest, you can eliminate the entire group my clicking
# on the bar for that group on the right and selecting "Exclude". This also 
# allows you to visualize "oulier" papers that aren't strongly similar to 
# the rest of the bunch. 

#metadigitise
#extraction of data from figures

#robvis
#risk of bias

