File I/O

[1]:
library(tidyverse)
Registered S3 methods overwritten by 'ggplot2':
  method         from
  [.quosures     rlang
  c.quosures     rlang
  print.quosures rlang
── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
 ggplot2 3.1.1      purrr   0.3.2
 tibble  2.1.2      dplyr   0.8.1
 tidyr   0.8.3      stringr 1.4.0
 readr   1.3.1      forcats 0.4.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
 dplyr::filter() masks stats::filter()
 dplyr::lag()    masks stats::lag()

Strucrure of count data from STAR

  • column 1: gene ID

  • column 2: counts for unstranded RNA-seq

  • column 3: counts for the 1st read strand aligned with RNA (htseq-count option -s yes)

  • column 4: counts for the 2nd read strand aligned with RNA (htseq-count option -s reverse)

Get list of files

[2]:
count_dir <- "/data/hts_2019_data/hts2019_pilot_counts"
[3]:
files <- list.files(path=count_dir, pattern="*.tab", full.names = TRUE)
[4]:
files[1]
'/data/hts_2019_data/hts2019_pilot_counts/1_2019_P_M1_S1_L001_ReadsPerGene.out.tab'

Read in first 2 files

[5]:
d1 <- read_tsv(files[1], col_names=FALSE, col_type=cols())
[6]:
d2 <- read_tsv(files[2], col_names=FALSE, col_type=cols())
[7]:
d1 %>% head(5)
A tibble: 5 × 4
X1X2X3X4
<chr><dbl><dbl><dbl>
N_unmapped 21162 2116221162
N_multimapping 95540 9554095540
N_noFeature 17075426022226923
N_ambiguous 418493 1416 606
CNAG_04548 0 0 0
[8]:
d2 %>% head(5)
A tibble: 5 × 4
X1X2X3X4
<chr><dbl><dbl><dbl>
N_unmapped 19644 1964419644
N_multimapping 94070 9407094070
N_noFeature 16407419589226053
N_ambiguous 412656 1410 638
CNAG_04548 0 0 0

Combining files

[9]:
bind_cols(d1, d2) %>% head(5)
A tibble: 5 × 8
X1X2X3X4X11X21X31X41
<chr><dbl><dbl><dbl><chr><dbl><dbl><dbl>
N_unmapped 21162 2116221162N_unmapped 19644 1964419644
N_multimapping 95540 9554095540N_multimapping 94070 9407094070
N_noFeature 17075426022226923N_noFeature 16407419589226053
N_ambiguous 418493 1416 606N_ambiguous 412656 1410 638
CNAG_04548 0 0 0CNAG_04548 0 0 0
[10]:
bind_rows(d1, d2) %>% head(5)
A tibble: 5 × 4
X1X2X3X4
<chr><dbl><dbl><dbl>
N_unmapped 21162 2116221162
N_multimapping 95540 9554095540
N_noFeature 17075426022226923
N_ambiguous 418493 1416 606
CNAG_04548 0 0 0
[11]:
inner_join(x=d1, y=d2, by='X1') %>% head(5)
A tibble: 5 × 7
X1X2.xX3.xX4.xX2.yX3.yX4.y
<chr><dbl><dbl><dbl><dbl><dbl><dbl>
N_unmapped 21162 2116221162 19644 1964419644
N_multimapping 95540 9554095540 94070 9407094070
N_noFeature 17075426022226923 16407419589226053
N_ambiguous 418493 1416 606412656 1410 638
CNAG_04548 0 0 0 0 0 0

Custom function to read files

[12]:
read_file <- function(file) {
    read_tsv(file, col_names = FALSE, skip=4, col_types = cols()) %>%
    mutate(source=basename(file)) %>%
    select(source=source, gene=X1, count=X4) %>%
    separate(source, sep='_', into=c("sample", "year", "p", "method", "s", "lane", "junk")) %>%
    select(sample, year, p, method, s, lane, gene, count)
}
[13]:
read_file(files[1]) %>% head(5)
A tibble: 5 × 8
sampleyearpmethodslanegenecount
<chr><chr><chr><chr><chr><chr><chr><dbl>
12019PM1S1L001CNAG_04548 0
12019PM1S1L001CNAG_07303 0
12019PM1S1L001CNAG_0730414
12019PM1S1L001CNAG_00001 0
12019PM1S1L001CNAG_07305 1
[ ]: