# Written by: Reilly Pidgeon
# This script was used to download each study from the curatedMetagenomicData ExperimentHub into separate RData files named after the study number(s) (Snapshot 2023-04-24)
# This code was run on a laptop with the following specs:
  # Processor	Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz   2.21 GHz
  # Installed RAM	16.0 GB (15.6 GB usable)
  # System type	64-bit operating system, x64-based processor
# The resulting RData files can then be uploaded to an HPC cluster and processed using: 2_cMD_FromRData.R

  # Info on curatedMetagenomicData can be found here: 
  # Bioconductor info: https://bioconductor.riken.jp/packages/3.14/data/experiment/manuals/curatedMetagenomicData/man/curatedMetagenomicData.pdf
  # All available studies: https://waldronlab.io/curatedMetagenomicData/articles/available-studies.html
  # General: https://waldronlab.io/curatedMetagenomicData/articles/curatedMetagenomicData.html

# Load libraries needed to run the script
library(ExperimentHub)
library(curatedMetagenomicData)
library(dplyr)

# Set working directory
setwd("C:/Users/Reilly/Desktop")
# Create an Experiment Hub object
eh <- ExperimentHub()
# Answer yes when prompted to create a directory

# This lists all available packages in Experiment Hub
unique(package(eh))

# This lists all resources (studies) containing gene_families entries for the curatedMetagenomicData package in Experiment Hub
listResources(eh, "curatedMetagenomicData", "gene_families")

# Get each study in cMG database (all data) - Snapshot date for all samples was 2023-04-24
# Each subset of studies was saved as RData and then cleared manually from the environment in RStudio

  Study1 <- curatedMetagenomicData("AsnicarF_2017", dryrun = FALSE)  
  Study2 <- curatedMetagenomicData("AsnicarF_2021", dryrun = FALSE)  
  Study3 <- curatedMetagenomicData("BackhedF_2015", dryrun = FALSE)  
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies1-3.RData")

  Study4 <- curatedMetagenomicData("BedarfJR_2017", dryrun = FALSE)  
  Study5 <- curatedMetagenomicData("Bengtsson-PalmeJ_2015", dryrun = FALSE)  
  Study6 <- curatedMetagenomicData("BritoIL_2016", dryrun = FALSE)  
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies4-6.RData")

  Study7 <- curatedMetagenomicData("BrooksB_2017", dryrun = FALSE)  
  Study8 <- curatedMetagenomicData("Castro-NallarE_2015", dryrun = FALSE)  
  Study9 <- curatedMetagenomicData("ChengpingW_2017", dryrun = FALSE)  
  Study10 <- curatedMetagenomicData("ChngKR_2016", dryrun = FALSE)  
  Study11 <- curatedMetagenomicData("ChuDM_2017", dryrun = FALSE)  
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies7-11.RData")

  Study12 <- curatedMetagenomicData("CosteaPI_2017", dryrun = FALSE)  
  Study13 <- curatedMetagenomicData("DavidLA_2015", dryrun = FALSE)  
  Study14 <- curatedMetagenomicData("DeFilippisF_2019", dryrun = FALSE)  
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies12-14.RData")

  Study15 <- curatedMetagenomicData("DhakanDB_2019", dryrun = FALSE)  
  Study16 <- curatedMetagenomicData("FengQ_2015", dryrun = FALSE)  
  Study17 <- curatedMetagenomicData("FerrettiP_2018", dryrun = FALSE)  
  Study18 <- curatedMetagenomicData("FrankelAE_2017", dryrun = FALSE)  
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies15-18.RData")

  Study19 <- curatedMetagenomicData("GhensiP_2019", dryrun = FALSE)  
  Study20 <- curatedMetagenomicData("GopalakrishnanV_2018", dryrun = FALSE)  
  Study21 <- curatedMetagenomicData("GuptaA_2019", dryrun = FALSE)  
  Study22 <- curatedMetagenomicData("HallAB_2017", dryrun = FALSE)  
  Study23 <- curatedMetagenomicData("HanniganGD_2017", dryrun = FALSE)  
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies19-23.RData")

  Study24 <- curatedMetagenomicData("HansenLBS_2018", dryrun = FALSE)  
  Study25 <- curatedMetagenomicData("Heitz-BuschartA_2016", dryrun = FALSE)  
  Study26 <- curatedMetagenomicData("HMP_2012", dryrun = FALSE)  
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies24-26.RData")

  Study27 <- curatedMetagenomicData("HMP_2019_ibdmdb", dryrun = FALSE)
  Study28 <- curatedMetagenomicData("HMP_2019_t2d", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies27-28.RData")

  Study29 <- curatedMetagenomicData("IaniroG_2022", dryrun = FALSE)
  Study30 <- curatedMetagenomicData("IjazUZ_2017", dryrun = FALSE)
  Study31 <- curatedMetagenomicData("JieZ_2017", dryrun = FALSE)
  Study32 <- curatedMetagenomicData("KarlssonFH_2013", dryrun = FALSE)
  Study33 <- curatedMetagenomicData("KaurK_2020", dryrun = FALSE)
  Study34 <- curatedMetagenomicData("KeohaneDM_2020", dryrun = FALSE)
  Study35 <- curatedMetagenomicData("KieserS_2018", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies29-35.RData")

  Study36 <- curatedMetagenomicData("KosticAD_2015", dryrun = FALSE)
  Study37 <- curatedMetagenomicData("LassalleF_2017", dryrun = FALSE)
  Study38 <- curatedMetagenomicData("LeChatelierE_2013", dryrun = FALSE)
  Study39 <- curatedMetagenomicData("LeeKA_2022", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies36-39.RData")

  Study40 <- curatedMetagenomicData("LifeLinesDeep_2016", dryrun = FALSE)
  Study41 <- curatedMetagenomicData("LiJ_2014", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies40-41.RData")

  Study42 <- curatedMetagenomicData("LiJ_2017", dryrun = FALSE)
  Study43 <- curatedMetagenomicData("LiSS_2016", dryrun = FALSE)
  Study44 <- curatedMetagenomicData("LiuW_2016", dryrun = FALSE)
  Study45 <- curatedMetagenomicData("LokmerA_2019", dryrun = FALSE)
  Study46 <- curatedMetagenomicData("LomanNJ_2013", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies42-46.RData")

  Study47 <- curatedMetagenomicData("LoombaR_2017", dryrun = FALSE)
  Study48 <- curatedMetagenomicData("LouisS_2016", dryrun = FALSE)
  Study49 <- curatedMetagenomicData("MatsonV_2018", dryrun = FALSE)
  Study50 <- curatedMetagenomicData("MehtaRS_2018", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies47-50.RData")

  Study51 <- curatedMetagenomicData("MetaCardis_2020_a", dryrun = FALSE)  
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies51.RData")

  Study52 <- curatedMetagenomicData("NagySzakalD_2017", dryrun = FALSE)
  Study53 <- curatedMetagenomicData("NielsenHB_2014", dryrun = FALSE)
  Study54 <- curatedMetagenomicData("Obregon-TitoAJ_2015", dryrun = FALSE)
  Study55 <- curatedMetagenomicData("OhJ_2014", dryrun = FALSE)
  Study56 <- curatedMetagenomicData("OlmMR_2017", dryrun = FALSE)
  Study57 <- curatedMetagenomicData("PasolliE_2019", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies52-57.RData")
  
  Study58 <- curatedMetagenomicData("PehrssonE_2016", dryrun = FALSE)
  Study59 <- curatedMetagenomicData("PetersBA_2019", dryrun = FALSE)
  Study60 <- curatedMetagenomicData("QinJ_2012", dryrun = FALSE)
  Study61 <- curatedMetagenomicData("QinN_2014", dryrun = FALSE)
  Study62 <- curatedMetagenomicData("RampelliS_2015", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies58-62.RData")

  Study63 <- curatedMetagenomicData("RaymondF_2016", dryrun = FALSE)
  Study64 <- curatedMetagenomicData("RosaBA_2018", dryrun = FALSE)
  Study65 <- curatedMetagenomicData("RubelMA_2020", dryrun = FALSE)
  Study66 <- curatedMetagenomicData("SankaranarayananK_2015", dryrun = FALSE)
  Study67 <- curatedMetagenomicData("SchirmerM_2016", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies63-67.RData")

  Study68 <- curatedMetagenomicData("ShaoY_2019", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies68.RData")

  Study69 <- curatedMetagenomicData("ShiB_2015", dryrun = FALSE)
  Study70 <- curatedMetagenomicData("SmitsSA_2017", dryrun = FALSE)
  Study71 <- curatedMetagenomicData("TettAJ_2016", dryrun = FALSE)
  Study72 <- curatedMetagenomicData("TettAJ_2019_a", dryrun = FALSE)
  Study73 <- curatedMetagenomicData("TettAJ_2019_b", dryrun = FALSE)
  Study74 <- curatedMetagenomicData("TettAJ_2019_c", dryrun = FALSE)
  Study75 <- curatedMetagenomicData("ThomasAM_2018a", dryrun = FALSE)
  Study76 <- curatedMetagenomicData("ThomasAM_2018b", dryrun = FALSE)
  Study77 <- curatedMetagenomicData("ThomasAM_2019_c", dryrun = FALSE)
  Study78 <- curatedMetagenomicData("VatanenT_2016", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies69-78.RData")
  
  Study79 <- curatedMetagenomicData("VilaAV_2018", dryrun = FALSE)
  Study80 <- curatedMetagenomicData("VincentC_2016", dryrun = FALSE)
  Study81 <- curatedMetagenomicData("VogtmannE_2016", dryrun = FALSE)
  Study82 <- curatedMetagenomicData("WampachL_2018", dryrun = FALSE)
  Study83 <- curatedMetagenomicData("WindTT_2020", dryrun = FALSE)
  Study84 <- curatedMetagenomicData("WirbelJ_2018", dryrun = FALSE)
  Study85 <- curatedMetagenomicData("XieH_2016", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies79-85.RData")
  
  Study86 <- curatedMetagenomicData("YachidaS_2019", dryrun = FALSE)
  Study87 <- curatedMetagenomicData("YassourM_2016", dryrun = FALSE)
  Study88 <- curatedMetagenomicData("YassourM_2018", dryrun = FALSE)
  Study89 <- curatedMetagenomicData("YeZ_2018", dryrun = FALSE)
  Study90 <- curatedMetagenomicData("YuJ_2015", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies88-90.RData")
  
  Study91 <- curatedMetagenomicData("ZeeviD_2015", dryrun = FALSE)
  Study92 <- curatedMetagenomicData("ZellerG_2014", dryrun = FALSE)
  Study93 <- curatedMetagenomicData("ZhuF_2020", dryrun = FALSE)
# Save studies as RData (manageable size)
save.image("C:/Users/Reilly/OneDrive - McGill University/PhD/Data/RP01-79 Enterocloster spp Metabo and Operon/RP01-79 20230606 cMG All Studies RData/Studies91-93.RData")


#All studies have now been saves as RData and can be imported in a separate R environment in an HPC cluster for processing/querying 