{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# File I/O" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Registered S3 methods overwritten by 'ggplot2':\n", " method from \n", " [.quosures rlang\n", " c.quosures rlang\n", " print.quosures rlang\n", "── \u001b[1mAttaching packages\u001b[22m ─────────────────────────────────────── tidyverse 1.2.1 ──\n", "\u001b[32m✔\u001b[39m \u001b[34mggplot2\u001b[39m 3.1.1 \u001b[32m✔\u001b[39m \u001b[34mpurrr \u001b[39m 0.3.2\n", "\u001b[32m✔\u001b[39m \u001b[34mtibble \u001b[39m 2.1.2 \u001b[32m✔\u001b[39m \u001b[34mdplyr \u001b[39m 0.8.1\n", "\u001b[32m✔\u001b[39m \u001b[34mtidyr \u001b[39m 0.8.3 \u001b[32m✔\u001b[39m \u001b[34mstringr\u001b[39m 1.4.0\n", "\u001b[32m✔\u001b[39m \u001b[34mreadr \u001b[39m 1.3.1 \u001b[32m✔\u001b[39m \u001b[34mforcats\u001b[39m 0.4.0\n", "── \u001b[1mConflicts\u001b[22m ────────────────────────────────────────── tidyverse_conflicts() ──\n", "\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mfilter()\u001b[39m masks \u001b[34mstats\u001b[39m::filter()\n", "\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mlag()\u001b[39m masks \u001b[34mstats\u001b[39m::lag()\n" ] } ], "source": [ "library(tidyverse)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strucrure of count data from STAR" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- column 1: gene ID\n", "- column 2: counts for unstranded RNA-seq\n", "- column 3: counts for the 1st read strand aligned with RNA (htseq-count option -s yes)\n", "- column 4: counts for the 2nd read strand aligned with RNA (htseq-count option -s reverse)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get list of files" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "count_dir <- \"/data/hts_2019_data/hts2019_pilot_counts\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "files <- list.files(path=count_dir, pattern=\"*.tab\", full.names = TRUE)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "'/data/hts_2019_data/hts2019_pilot_counts/1_2019_P_M1_S1_L001_ReadsPerGene.out.tab'" ], "text/latex": [ "'/data/hts\\_2019\\_data/hts2019\\_pilot\\_counts/1\\_2019\\_P\\_M1\\_S1\\_L001\\_ReadsPerGene.out.tab'" ], "text/markdown": [ "'/data/hts_2019_data/hts2019_pilot_counts/1_2019_P_M1_S1_L001_ReadsPerGene.out.tab'" ], "text/plain": [ "[1] \"/data/hts_2019_data/hts2019_pilot_counts/1_2019_P_M1_S1_L001_ReadsPerGene.out.tab\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "files[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read in first 2 files" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "d1 <- read_tsv(files[1], col_names=FALSE, col_type=cols())" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "d2 <- read_tsv(files[2], col_names=FALSE, col_type=cols())" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
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
\n" ], "text/latex": [ "A tibble: 5 × 4\n", "\\begin{tabular}{r|llll}\n", " X1 & X2 & X3 & X4\\\\\n", " & & & \\\\\n", "\\hline\n", "\t N\\_unmapped & 21162 & 21162 & 21162\\\\\n", "\t N\\_multimapping & 95540 & 95540 & 95540\\\\\n", "\t N\\_noFeature & 17075 & 4260222 & 26923\\\\\n", "\t N\\_ambiguous & 418493 & 1416 & 606\\\\\n", "\t CNAG\\_04548 & 0 & 0 & 0\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 5 × 4\n", "\n", "| X1 <chr> | X2 <dbl> | X3 <dbl> | X4 <dbl> |\n", "|---|---|---|---|\n", "| N_unmapped | 21162 | 21162 | 21162 |\n", "| N_multimapping | 95540 | 95540 | 95540 |\n", "| N_noFeature | 17075 | 4260222 | 26923 |\n", "| N_ambiguous | 418493 | 1416 | 606 |\n", "| CNAG_04548 | 0 | 0 | 0 |\n", "\n" ], "text/plain": [ " X1 X2 X3 X4 \n", "1 N_unmapped 21162 21162 21162\n", "2 N_multimapping 95540 95540 95540\n", "3 N_noFeature 17075 4260222 26923\n", "4 N_ambiguous 418493 1416 606\n", "5 CNAG_04548 0 0 0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "d1 %>% head(5)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
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
\n" ], "text/latex": [ "A tibble: 5 × 4\n", "\\begin{tabular}{r|llll}\n", " X1 & X2 & X3 & X4\\\\\n", " & & & \\\\\n", "\\hline\n", "\t N\\_unmapped & 19644 & 19644 & 19644\\\\\n", "\t N\\_multimapping & 94070 & 94070 & 94070\\\\\n", "\t N\\_noFeature & 16407 & 4195892 & 26053\\\\\n", "\t N\\_ambiguous & 412656 & 1410 & 638\\\\\n", "\t CNAG\\_04548 & 0 & 0 & 0\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 5 × 4\n", "\n", "| X1 <chr> | X2 <dbl> | X3 <dbl> | X4 <dbl> |\n", "|---|---|---|---|\n", "| N_unmapped | 19644 | 19644 | 19644 |\n", "| N_multimapping | 94070 | 94070 | 94070 |\n", "| N_noFeature | 16407 | 4195892 | 26053 |\n", "| N_ambiguous | 412656 | 1410 | 638 |\n", "| CNAG_04548 | 0 | 0 | 0 |\n", "\n" ], "text/plain": [ " X1 X2 X3 X4 \n", "1 N_unmapped 19644 19644 19644\n", "2 N_multimapping 94070 94070 94070\n", "3 N_noFeature 16407 4195892 26053\n", "4 N_ambiguous 412656 1410 638\n", "5 CNAG_04548 0 0 0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "d2 %>% head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Combining files" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
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
\n" ], "text/latex": [ "A tibble: 5 × 8\n", "\\begin{tabular}{r|llllllll}\n", " X1 & X2 & X3 & X4 & X11 & X21 & X31 & X41\\\\\n", " & & & & & & & \\\\\n", "\\hline\n", "\t N\\_unmapped & 21162 & 21162 & 21162 & N\\_unmapped & 19644 & 19644 & 19644\\\\\n", "\t N\\_multimapping & 95540 & 95540 & 95540 & N\\_multimapping & 94070 & 94070 & 94070\\\\\n", "\t N\\_noFeature & 17075 & 4260222 & 26923 & N\\_noFeature & 16407 & 4195892 & 26053\\\\\n", "\t N\\_ambiguous & 418493 & 1416 & 606 & N\\_ambiguous & 412656 & 1410 & 638\\\\\n", "\t CNAG\\_04548 & 0 & 0 & 0 & CNAG\\_04548 & 0 & 0 & 0\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 5 × 8\n", "\n", "| X1 <chr> | X2 <dbl> | X3 <dbl> | X4 <dbl> | X11 <chr> | X21 <dbl> | X31 <dbl> | X41 <dbl> |\n", "|---|---|---|---|---|---|---|---|\n", "| N_unmapped | 21162 | 21162 | 21162 | N_unmapped | 19644 | 19644 | 19644 |\n", "| N_multimapping | 95540 | 95540 | 95540 | N_multimapping | 94070 | 94070 | 94070 |\n", "| N_noFeature | 17075 | 4260222 | 26923 | N_noFeature | 16407 | 4195892 | 26053 |\n", "| N_ambiguous | 418493 | 1416 | 606 | N_ambiguous | 412656 | 1410 | 638 |\n", "| CNAG_04548 | 0 | 0 | 0 | CNAG_04548 | 0 | 0 | 0 |\n", "\n" ], "text/plain": [ " X1 X2 X3 X4 X11 X21 X31 X41 \n", "1 N_unmapped 21162 21162 21162 N_unmapped 19644 19644 19644\n", "2 N_multimapping 95540 95540 95540 N_multimapping 94070 94070 94070\n", "3 N_noFeature 17075 4260222 26923 N_noFeature 16407 4195892 26053\n", "4 N_ambiguous 418493 1416 606 N_ambiguous 412656 1410 638\n", "5 CNAG_04548 0 0 0 CNAG_04548 0 0 0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bind_cols(d1, d2) %>% head(5)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
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
\n" ], "text/latex": [ "A tibble: 5 × 4\n", "\\begin{tabular}{r|llll}\n", " X1 & X2 & X3 & X4\\\\\n", " & & & \\\\\n", "\\hline\n", "\t N\\_unmapped & 21162 & 21162 & 21162\\\\\n", "\t N\\_multimapping & 95540 & 95540 & 95540\\\\\n", "\t N\\_noFeature & 17075 & 4260222 & 26923\\\\\n", "\t N\\_ambiguous & 418493 & 1416 & 606\\\\\n", "\t CNAG\\_04548 & 0 & 0 & 0\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 5 × 4\n", "\n", "| X1 <chr> | X2 <dbl> | X3 <dbl> | X4 <dbl> |\n", "|---|---|---|---|\n", "| N_unmapped | 21162 | 21162 | 21162 |\n", "| N_multimapping | 95540 | 95540 | 95540 |\n", "| N_noFeature | 17075 | 4260222 | 26923 |\n", "| N_ambiguous | 418493 | 1416 | 606 |\n", "| CNAG_04548 | 0 | 0 | 0 |\n", "\n" ], "text/plain": [ " X1 X2 X3 X4 \n", "1 N_unmapped 21162 21162 21162\n", "2 N_multimapping 95540 95540 95540\n", "3 N_noFeature 17075 4260222 26923\n", "4 N_ambiguous 418493 1416 606\n", "5 CNAG_04548 0 0 0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "bind_rows(d1, d2) %>% head(5)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
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
\n" ], "text/latex": [ "A tibble: 5 × 7\n", "\\begin{tabular}{r|lllllll}\n", " X1 & X2.x & X3.x & X4.x & X2.y & X3.y & X4.y\\\\\n", " & & & & & & \\\\\n", "\\hline\n", "\t N\\_unmapped & 21162 & 21162 & 21162 & 19644 & 19644 & 19644\\\\\n", "\t N\\_multimapping & 95540 & 95540 & 95540 & 94070 & 94070 & 94070\\\\\n", "\t N\\_noFeature & 17075 & 4260222 & 26923 & 16407 & 4195892 & 26053\\\\\n", "\t N\\_ambiguous & 418493 & 1416 & 606 & 412656 & 1410 & 638\\\\\n", "\t CNAG\\_04548 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 5 × 7\n", "\n", "| X1 <chr> | X2.x <dbl> | X3.x <dbl> | X4.x <dbl> | X2.y <dbl> | X3.y <dbl> | X4.y <dbl> |\n", "|---|---|---|---|---|---|---|\n", "| N_unmapped | 21162 | 21162 | 21162 | 19644 | 19644 | 19644 |\n", "| N_multimapping | 95540 | 95540 | 95540 | 94070 | 94070 | 94070 |\n", "| N_noFeature | 17075 | 4260222 | 26923 | 16407 | 4195892 | 26053 |\n", "| N_ambiguous | 418493 | 1416 | 606 | 412656 | 1410 | 638 |\n", "| CNAG_04548 | 0 | 0 | 0 | 0 | 0 | 0 |\n", "\n" ], "text/plain": [ " X1 X2.x X3.x X4.x X2.y X3.y X4.y \n", "1 N_unmapped 21162 21162 21162 19644 19644 19644\n", "2 N_multimapping 95540 95540 95540 94070 94070 94070\n", "3 N_noFeature 17075 4260222 26923 16407 4195892 26053\n", "4 N_ambiguous 418493 1416 606 412656 1410 638\n", "5 CNAG_04548 0 0 0 0 0 0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "inner_join(x=d1, y=d2, by='X1') %>% head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom function to read files" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "read_file <- function(file) {\n", " read_tsv(file, col_names = FALSE, skip=4, col_types = cols()) %>%\n", " mutate(source=basename(file)) %>%\n", " select(source=source, gene=X1, count=X4) %>%\n", " separate(source, sep='_', into=c(\"sample\", \"year\", \"p\", \"method\", \"s\", \"lane\", \"junk\")) %>%\n", " select(sample, year, p, method, s, lane, gene, count)\n", "}" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
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
\n" ], "text/latex": [ "A tibble: 5 × 8\n", "\\begin{tabular}{r|llllllll}\n", " sample & year & p & method & s & lane & gene & count\\\\\n", " & & & & & & & \\\\\n", "\\hline\n", "\t 1 & 2019 & P & M1 & S1 & L001 & CNAG\\_04548 & 0\\\\\n", "\t 1 & 2019 & P & M1 & S1 & L001 & CNAG\\_07303 & 0\\\\\n", "\t 1 & 2019 & P & M1 & S1 & L001 & CNAG\\_07304 & 14\\\\\n", "\t 1 & 2019 & P & M1 & S1 & L001 & CNAG\\_00001 & 0\\\\\n", "\t 1 & 2019 & P & M1 & S1 & L001 & CNAG\\_07305 & 1\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A tibble: 5 × 8\n", "\n", "| sample <chr> | year <chr> | p <chr> | method <chr> | s <chr> | lane <chr> | gene <chr> | count <dbl> |\n", "|---|---|---|---|---|---|---|---|\n", "| 1 | 2019 | P | M1 | S1 | L001 | CNAG_04548 | 0 |\n", "| 1 | 2019 | P | M1 | S1 | L001 | CNAG_07303 | 0 |\n", "| 1 | 2019 | P | M1 | S1 | L001 | CNAG_07304 | 14 |\n", "| 1 | 2019 | P | M1 | S1 | L001 | CNAG_00001 | 0 |\n", "| 1 | 2019 | P | M1 | S1 | L001 | CNAG_07305 | 1 |\n", "\n" ], "text/plain": [ " sample year p method s lane gene count\n", "1 1 2019 P M1 S1 L001 CNAG_04548 0 \n", "2 1 2019 P M1 S1 L001 CNAG_07303 0 \n", "3 1 2019 P M1 S1 L001 CNAG_07304 14 \n", "4 1 2019 P M1 S1 L001 CNAG_00001 0 \n", "5 1 2019 P M1 S1 L001 CNAG_07305 1 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "read_file(files[1]) %>% head(5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }