---
title: 'Fastq-dump: download and extract single 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)
```

# BDSS configuration file

First, we create a bdss configuration file `bdss.cfg` in the current directory.

```{r}
system('echo "[metadata_repository]" > bdss.cfg')
system('echo url=http://bdss.bioinfo.wsu.edu/ >> bdss.cfg')
```

# 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) {
  # build URL from SRA accession
  url = paste0('ftp://ftp.ncbi.nlm.nih.gov/sra/sra-instant/reads/ByRun/sra/SRR/',
               substr(id, 1, 6), '/', id, '/', id, '.sra')
  # download sra file with bdss
  bdss_command = paste0('/tool_deps/_conda/bin/bdss transfer -u ', url)
  system(bdss_command, intern = TRUE)
  # convert .sra to .fastq/.fasta
  if('FORMAT' == 'fasta') {
    command = paste0('fastq-dump --fasta ', '-O read_files_directory ', id)
  } else {
    command = paste0('fastq-dump ', '-O read_files_directory ', id)
  }
}

# remove all .sra file
system('rm *.sra', intern = TRUE)
```

* `fastq-dump` command
```{r}
print(command)
```

* `command line stdout`

```{r}
system(command = command, intern = TRUE)
```

