{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ArchR I/O" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[ArchR](https://www.archrproject.com/) is an R package for scATAC-seq data analysis [(Granja et al., 2021)](https://www.nature.com/articles/s41588-021-00790-6)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`chame` comes with a reader for ArchR's [Arrow files](https://www.archrproject.com/bookdown/what-is-an-arrow-file-archrproject.html):" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from chame.io import read_arrow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ArchR: Creating Arrow Files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For resproducibility, we will follow ArchR's own [tutorial](https://www.archrproject.com/bookdown/creating-arrow-files.html) on creating Arrow files.\n", "\n", "Here, we use `rpy2` for R calls, and it is assumed that the corresponding R environemnt is configured and has ArchR installed.\n", "\n", "In practice, one would most likely use existing Arrow files. So feel free to skip this section!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.chdir(\"../../\")\n", "if not os.path.exists(\"archr\"):\n", " os.mkdir(\"archr\")\n", "os.chdir(\"archr/\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "R[write to console]: \n", " / |\n", " / \\\n", " . / |.\n", " \\\\\\ / |.\n", " \\\\\\ / `|.\n", " \\\\\\ / |.\n", " \\ / |\\\n", " \\\\#####\\ / ||\n", " ==###########> / ||\n", " \\\\##==......\\ / ||\n", " ______ = =|__ /__ || \\\\\\\n", " ,--' ,----`-,__ ___/' --,-`-===================##========>\n", " \\ ' ##_______ _____ ,--,__,=##,__ ///\n", " , __== ___,-,__,--'#' ===' `-' | ##,-/\n", " -,____,---' \\\\####\\\\________________,--\\\\_##,/\n", " ___ .______ ______ __ __ .______ \n", " / \\ | _ \\ / || | | | | _ \\ \n", " / ^ \\ | |_) | | ,----'| |__| | | |_) | \n", " / /_\\ \\ | / | | | __ | | / \n", " / _____ \\ | |\\ \\\\___ | `----.| | | | | |\\ \\\\___.\n", " /__/ \\__\\ | _| `._____| \\______||__| |__| | _| `._____|\n", " \n", "\n", "R[write to console]: Setting default number of Parallel threads to 4.\n", "\n", "R[write to console]: Setting addArchRVerbose = FALSE\n", "\n" ] } ], "source": [ "from rpy2.robjects.packages import importr\n", "\n", "archr = importr(\"ArchR\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "R[write to console]: Setting default genome to Hg19.\n", "\n", "R[write to console]: Setting default number of Parallel threads to 6.\n", "\n" ] } ], "source": [ "archr.addArchRGenome(\"hg19\");\n", "archr.addArchRThreads(threads = 6);" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "inputFiles = archr.getTutorialData(\"Hematopoiesis\")\n", "\n", "arrowFiles = archr.createArrowFiles(\n", " inputFiles=inputFiles,\n", " sampleNames=inputFiles.names,\n", " minTSS=4,\n", " filterFrags=1000, \n", " addTileMat=True,\n", " addGeneScoreMat=True,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading matrices from Arrow files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Tiles" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With a matrix specified, an AnnData object will be returned. For *tiles*, it would have dimensions *n_cells* by *n_tiles*:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AnnData object with n_obs × n_vars = 2453 × 6072620\n", " obs: 'BlacklistRatio', 'CellNames', 'Completed', 'Date', 'NucleosomeRatio', 'PassQC', 'PromoterRatio', 'ReadsInBlacklist', 'ReadsInPromoter', 'ReadsInTSS', 'Sample', 'TSSEnrichment', 'nDiFrags', 'nFrags', 'nMonoFrags', 'nMultiFrags'\n", " var: 'Chromosome', 'idx', 'Start', 'End'\n", " uns: 'params'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tiles = read_arrow(\"scATAC_PBMC_R1.arrow\", matrix=\"tiles\")\n", "tiles" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As displayed, this AnnData object has cell metadata included together with the information about features (tiles):" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
BlacklistRatioCellNamesCompletedDateNucleosomeRatioPassQCPromoterRatioReadsInBlacklistReadsInPromoterReadsInTSSSampleTSSEnrichmentnDiFragsnFragsnMonoFragsnMultiFrags
CCGTGAGAGACCATAA-10.010204CCGTGAGAGACCATAA-1Finished2022-06-103.5784721.00.0644011861.011746.02926.0scATAC_PBMC_R16.65247936.091194.019918.023340.0
TGCACCTTCGGCTATA-10.008097TGCACCTTCGGCTATA-1Finished2022-06-104.8093451.00.0427071351.07126.01571.0scATAC_PBMC_R14.69946671.083428.014361.022396.0
\n", "
" ], "text/plain": [ " BlacklistRatio CellNames Completed Date \\\n", "CCGTGAGAGACCATAA-1 0.010204 CCGTGAGAGACCATAA-1 Finished 2022-06-10 \n", "TGCACCTTCGGCTATA-1 0.008097 TGCACCTTCGGCTATA-1 Finished 2022-06-10 \n", "\n", " NucleosomeRatio PassQC PromoterRatio ReadsInBlacklist \\\n", "CCGTGAGAGACCATAA-1 3.578472 1.0 0.064401 1861.0 \n", "TGCACCTTCGGCTATA-1 4.809345 1.0 0.042707 1351.0 \n", "\n", " ReadsInPromoter ReadsInTSS Sample \\\n", "CCGTGAGAGACCATAA-1 11746.0 2926.0 scATAC_PBMC_R1 \n", "TGCACCTTCGGCTATA-1 7126.0 1571.0 scATAC_PBMC_R1 \n", "\n", " TSSEnrichment nDiFrags nFrags nMonoFrags nMultiFrags \n", "CCGTGAGAGACCATAA-1 6.652 47936.0 91194.0 19918.0 23340.0 \n", "TGCACCTTCGGCTATA-1 4.699 46671.0 83428.0 14361.0 22396.0 " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tiles.obs.head(2)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ChromosomeidxStartEnd
chr1:0-500chr110500
chr1:500-1000chr125001000
chr1:1000-1500chr1310001500
chr1:1500-2000chr1415002000
chr1:2000-2500chr1520002500
\n", "
" ], "text/plain": [ " Chromosome idx Start End\n", "chr1:0-500 chr1 1 0 500\n", "chr1:500-1000 chr1 2 500 1000\n", "chr1:1000-1500 chr1 3 1000 1500\n", "chr1:1500-2000 chr1 4 1500 2000\n", "chr1:2000-2500 chr1 5 2000 2500" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tiles.var.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "↑ As AnnData currently requires string indices for both cells and features, tiles here are automatically named by `chame` according to their coordinates." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notably, `chame` strives to return data frames that can be readily used to create PyRanges objects:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "+--------------+-----------+-----------+-----------+\n", "| Chromosome | idx | Start | End |\n", "| (category) | (int32) | (int32) | (int32) |\n", "|--------------+-----------+-----------+-----------|\n", "| chr1 | 1 | 0 | 500 |\n", "| chr1 | 2 | 500 | 1000 |\n", "| chr1 | 3 | 1000 | 1500 |\n", "| chr1 | 4 | 1500 | 2000 |\n", "| ... | ... | ... | ... |\n", "| chr1 | 6 | 2500 | 3000 |\n", "| chr1 | 7 | 3000 | 3500 |\n", "| chr1 | 8 | 3500 | 4000 |\n", "| chr1 | 9 | 4000 | 4500 |\n", "+--------------+-----------+-----------+-----------+\n", "Unstranded PyRanges object has 9 rows and 4 columns from 1 chromosomes.\n", "For printing, the PyRanges was sorted on Chromosome." ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyranges import PyRanges\n", "pr = PyRanges(tiles.var)\n", "\n", "pr[\"chr1\", 0:4200]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Params for the *TileMatrix* from the Arrow file are carefully preserved and put into `.uns`:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'seqnames': 'chr1', 'length': 249250621, 'tileSize': 500.0},\n", " {'seqnames': 'chr2', 'length': 243199373, 'tileSize': 500.0},\n", " {'seqnames': 'chr3', 'length': 198022430, 'tileSize': 500.0}]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tiles.uns[\"params\"][:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Gene scores" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For *gene scores*, variables will be respectively genes:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AnnData object with n_obs × n_vars = 2453 × 23127\n", " obs: 'BlacklistRatio', 'CellNames', 'Completed', 'Date', 'NucleosomeRatio', 'PassQC', 'PromoterRatio', 'ReadsInBlacklist', 'ReadsInPromoter', 'ReadsInTSS', 'Sample', 'TSSEnrichment', 'nDiFrags', 'nFrags', 'nMonoFrags', 'nMultiFrags'\n", " var: 'seqnames', 'start', 'end', 'strand', 'name', 'idx'\n", " uns: 'params'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gene_scores = read_arrow(\"scATAC_PBMC_R1.arrow\", matrix=\"gene_scores\")\n", "gene_scores" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Params for the *GeneScoresMatrix* from the Arrow file are also put into `.uns`:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'extendUpstream': 1000.0,\n", " 'extendDownstream': 1000.0,\n", " 'geneUpstream': 1000.0,\n", " 'geneDownstream': 1000.0,\n", " 'scaleTo': 10000.0,\n", " 'tileSize': 500.0,\n", " 'ceiling': 4.0,\n", " 'geneModel': b'exp(-abs(x)/5000) + exp(-1)'},\n", " {'extendUpstream': 100000.0,\n", " 'extendDownstream': 100000.0,\n", " 'geneUpstream': 100000.0,\n", " 'geneDownstream': 100000.0,\n", " 'scaleTo': 10000.0,\n", " 'tileSize': 500.0,\n", " 'ceiling': 4.0,\n", " 'geneModel': b'exp(-abs(x)/5000) + exp(-1)'}]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gene_scores.uns[\"params\"][:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### All matrices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If no matrix is specified, all available matrices are read into a MuData object by default:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "lines_to_next_cell": 0 }, "outputs": [ { "data": { "text/html": [ "
MuData object with n_obs × n_vars = 2453 × 6095747\n",
       "  obs:\t'BlacklistRatio', 'CellNames', 'Completed', 'Date', 'NucleosomeRatio', 'PassQC', 'PromoterRatio', 'ReadsInBlacklist', 'ReadsInPromoter', 'ReadsInTSS', 'Sample', 'TSSEnrichment', 'nDiFrags', 'nFrags', 'nMonoFrags', 'nMultiFrags'\n",
       "  var:\t'idx'\n",
       "  2 modalities\n",
       "    tiles:\t2453 x 6072620\n",
       "      var:\t'Chromosome', 'idx', 'Start', 'End'\n",
       "      uns:\t'params'\n",
       "    gene_scores:\t2453 x 23127\n",
       "      var:\t'seqnames', 'start', 'end', 'strand', 'name', 'idx'\n",
       "      uns:\t'params'
" ], "text/plain": [ "MuData object with n_obs × n_vars = 2453 × 6095747\n", " obs:\t'BlacklistRatio', 'CellNames', 'Completed', 'Date', 'NucleosomeRatio', 'PassQC', 'PromoterRatio', 'ReadsInBlacklist', 'ReadsInPromoter', 'ReadsInTSS', 'Sample', 'TSSEnrichment', 'nDiFrags', 'nFrags', 'nMonoFrags', 'nMultiFrags'\n", " var:\t'idx'\n", " 2 modalities\n", " tiles:\t2453 x 6072620\n", " var:\t'Chromosome', 'idx', 'Start', 'End'\n", " uns:\t'params'\n", " gene_scores:\t2453 x 23127\n", " var:\t'seqnames', 'start', 'end', 'strand', 'name', 'idx'\n", " uns:\t'params'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mdata = read_arrow(\"scATAC_PBMC_R1.arrow\")\n", "mdata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.13" } }, "nbformat": 4, "nbformat_minor": 4 }