######

  ### Run these two lines first time to install the package FlowRepositoryR:
  # source("http://bioconductor.org/biocLite.R")
  # biocLite('FlowRepositoryR')

  ### The AML data will be downloaded to the directory specified in
  ### the variable datadir.

######
library(FlowRepositoryR)

datadir <- '~' 
  # Parent directory for data from Flow Repository
flowID <- "FR-FCM-ZZYA" # ID for AML data set
downloaddir <- file.path(datadir, flowID)
dir.create(downloaddir)

smalldownload <- TRUE#FALSE 
  # Should a small part of the data set (5 normal + 5 AML from tube 6) be downloaded 
  # or a larger part (100 first samples from tube 6)?

### Explore data set
ds <- flowRep.get(flowID)
summary(ds)
ds@purpose
attachFiles <- attachments(ds)

### Download meta data file AML.csv
if (!file.exists(file.path(downloaddir,attachFiles[[1]]@name)))
  attachDownload <- download(attachFiles[[1]], dirpath = downloaddir)

### Select samples to download
meta.data <- read.csv(file.path(downloaddir, 'AML.csv'),stringsAsFactor = FALSE)
head(meta.data)
if (smalldownload) {
  ### Find 5 normal and 5 AML samples from tube 6
  rows.normal <- (meta.data$Tube.number == 6) & (meta.data$Condition == 'normal')
  file.names.normal <- meta.data$FCS.file[rows.normal][1:5]
  
  rows.AML <- (meta.data$Tube.number == 6) & (meta.data$Condition == 'aml')
  file.names.AML <- meta.data$FCS.file[rows.AML][1:5]
  
  file.names <- c(file.names.normal,file.names.AML)
  
} else {
  ### Select 100 first samples from tube 6
  rows.tube6 <- (meta.data$Tube.number == 6)
  file.names <- meta.data$FCS.file[rows.tube6][1:100]
  
}

### Download data
for (file in fcs.files(ds)) {
  if (file@name %in% file.names) {
    if (!file.exists(file.path(downloaddir,file@name))) {
      cat('Downloading', file@name, '\n')
      download(file, dirpath = downloaddir)
    } else {
      cat(file@name, 'already downloaded\n')
    }
  }
}
print("Download complete")
