connect for downloading files from the SRA db (optional)
(http://downloads.asperasoft.com/connect2/)
- GNU parallel (optional)
http://www.gnu.org/software/parallel/


## Setup environment

We first prepare the directory structure, these directory are created only if they don't exist, as follows,


```bash
mkdir -p RSEM_test/{GEO,SRA,FASTQ,RSEM,Reference_Genome}
```



```r
# load libraries
library(data.table)
library(reshape2)
library(GEOquery)
library(SRAdb)
```


```r
gd <- getGEO("GSE45735", destdir = "RSEM_test/GEO/")
```

```
## ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE45nnn/GSE45735/matrix/
## Found 1 file(s)
## GSE45735_series_matrix.txt.gz
## Using locally cached version: RSEM_test/GEO//GSE45735_series_matrix.txt.gz
## Using locally cached version of GPL10999 found here:
## RSEM_test/GEO//GPL10999.soft
```

```r
pd <- pData(gd[[1]])
```


```r
if(!file.exists('RSEM_test/SRAmetadb.sqlite')) {
  sqlfile <- getSRAdbFile(destdir = "RSEM_test/")
  }
sra_con <- dbConnect(SQLite(),'RSEM_test/SRAmetadb.sqlite')
sra_tables <- dbListTables(sra_con)
```

## Download SRA files

We now check whether SRA files have been downloaded already, and download the files if they haven't been dowloaded yet


SRA files have been downloaded: TRUE


```r
# Let's just use the first 2 files as an exercise
pd_small <- pd[1:2,]
SRX_number <- rep(NA, nrow(pd_small))
```


```r
# Find ascp utility and SSH key for Aspera Connect
ascp <- '/usr/local/bin/ascp'
sshkey <- '/etc/aspera/asperaweb_id_dsa.openssh'
if(! (file.exists(ascp) & (file.exists(sshkey)))) {
    switch(Sys.info()[['sysname']],
    Linux = {
             asperahome <- '~/.aspera/connect/'
             ascp <- paste0(asperahome, 'bin/ascp')
             sshkey <- paste0(asperahome, 'etc/asperaweb_id_dsa.openssh')
         },
    Darwin = {
             asperahome <- '~/Applications/Aspera\\ Connect.app/Contents/Resources/'
             ascp <- paste0(asperahome, 'ascp')
             sshkey <- paste0(asperahome, 'asperaweb_id_dsa.openssh')
         }
    )
}

for(i in 1:nrow(