---
title: 'Fastq-dump: download and extract paired end reads into FASTQ/FASTA file'
output:
    html_document:
      number_sections: true
      toc: true
      theme: cosmo
      highlight: tango
---

```{r setup, include=FALSE, warning=FALSE, message=FALSE}
knitr::opts_chunk$set(
  echo = ECHO
)
```

# Command line arguments

```{r 'command line arguments'}
str(opt)
```

# Download and extract reads

```{r 'download and extract reads'}
# create a directory to store read files
dir.create('read_files_directory')
# download and extract reads
sra_accessions = strsplit(gsub(',', ' ', 'SRA_ACCESSION'), ' ')[[1]]
sra_accessions = sra_accessions[sra_accessions != '']
# loop through SRA accessions to download and extract reads.
for(id in sra_accessions) {
  if('FORMAT' == 'fasta') {
    command = paste0('fastq-dump --fasta --split_files ', '-O read_files_directory ')
  } else {
    command = paste0('fastq-dump --fastq --split_files ', '-O read_files_directory ')
  }
  
  print(command)
  system(command = command)
}
```

# Rename files

```{r}
old_files = paste0('./read_files_directory/', list.files('./read_files_directory'))
new_files = gsub('_1', '_forward', old_files)
new_files = gsub('_2', '_reverse', new_files)
file.copy('./read_files_directory/')
```

