DownloadProtocol_v3.R 3.29 KB
#!/usr/bin/env Rscript
library("optparse")
library("GEOquery")
source("/home/egaytan/automatic-extraction-growth-conditions/extraction-geo/bin/download_functions_v3.R")
#-------------------------------------------------------------------------------
# Objective
# download GSEnnn.soft.gz files for a list of GSE ids
#
# Input parameters
# --d     download dir
# --i     GSE information file
#
# Paramenters example
# -d ../download/srr_galagan/
# -o ../outputs/srr_galagan
# -i ../input/normalized_srr-gsm_des_v4.tsv
# -b ../input/listMetaCampo.txt
# -r ../reports/srr_galagan/extract_report.txt
#
# Examples
# nohup Rscript DownloadProtocol_v2.R -d ../download/srr_galagan/ -i ../input/normalized_srr-gsm_des_v4.tsv -r ../reports/srr_galagan/download_report.txt > download_nohup.out 
# nohup Rscript DownloadProtocol_v2.R -d ../download/all_srr/ -i ../input/all_srr_geo_rnaseq.txt -r ../reports/all_srr/download_report.txt > download_nohup.out 
# nohup Rscript DownloadProtocol_v3.R -d ../download/srr_htregulondb/ -i ../input/srr_htregulondb/SRR_GEO_RNASeq_Expressed.txt -r ../reports/srr_htregulondb/download_report.txt > download_nohup.out &
#######################################################################################
#-----------------------------------------ARGS-----------------------------------------
#######################################################################################

option_list = list( make_option(c("-d", "--downloadPath"), 
								type="character", 
								default=NULL, 
								help="download directory", 
								metavar="character"),
					make_option(c("-i", "--infoFile"),  
								type="character", 
								default=NULL, 
								help="GSE id information file", 
								metavar="character"),
					make_option(c("-r", "--report"),  
								type="character", 
								default=NULL, 
								help="download report file", 
								metavar="character")
					);

opt_parser = OptionParser(option_list=option_list);
opt = parse_args(opt_parser);

if (!length(opt)){
  print_help(opt_parser)
  stop("At least one argument must be supplied (input file).n", call.=FALSE)
}

#######################################################################################
#-----------------------------------------MAIN-----------------------------------------
#######################################################################################

## Input files and output directories
infoFile <- opt$infoFile


## Load main variables

# GSE-GSM
gseInfo  <- read.table( infoFile,header = T, sep = "\t" )
gseInfo  <- gseInfo[grep("GSE", gseInfo$gse, value=F), ]
gseInfo  <- gseInfo[grep("GSM", gseInfo$gsm, value=F), ]
gseInfo  <- gseInfo[complete.cases(gseInfo),]

ngse <- length(unique(gseInfo$gse))
ngsm <- length(unique(gseInfo$gsm))

message("Required GSE: ", ngse)
message("Required GSM: ", ngsm)
#gseInfo

## Download GSE-list
sink(opt$report, append = FALSE, split = FALSE)
cat("total gse id: ", (length(unique(gseInfo$gse))), "\n")
ngse_down=0
for (geoid in unique(gseInfo$gse)) {
  print(geoid)
  report <- tryCatch( 
   DownloadGEO( geoid, opt$downloadPath ), 
   error = function( e ) return( "download failed" ) )
  print(report)
  if(report == "successful download"){
	  ngse_down = ngse_down + 1
  }
}
cat("download id: ", length(list.dirs(opt$downloadPath, recursive = FALSE)))

message("Required GSE: ", ngse_down)