Giter Site home page Giter Site logo

archs4py's Introduction

archs4py

archs4py - Official Python package to load and query ARCHS4 data

Official ARCHS4 companion package. This package is a wrapper for basic H5 commands performed on the ARCHS4 data files. Some of the data access is optimized for specific query strategies and should make this implementation faster than manually querying the data. The package supports automated file download, mutithreading, and some convenience functions such as data normalization.

ARCHS4py also supports the ARCHS4 alignment pipeline. When aligning FASTQ files using ARCHS4py gene and transcript counts will be compatible with the preprocessed ARCHS4 samples.

Installation | Download H5 Files | List H5 Contents | Extract Counts | Extract Meta Data | Normalize Samples | Filter Genes | Aggregate Duplicate Genes | FASTQ Alignment | Versions

ARCHS4 data

ARCHS4 data is regularly updated to include publically available gene expression samples from RNA-seq. ARCHS4 processes the major platforms for human and mouse. As of 6/2023 ARCHS4 encompasses more than 1.5 million RNA-seq samples. All samples in ARCHS4 are homogeniously processed. ARCHS4 does currently not decern whether samples are bulk or single-cell and purely crawls GEO. Since samples are not always correctly annotated as single cell ARCHS4 uses a machine learning approach to predict single-cell samples and associated a singlecellprobability to each sample. Samples with a value larger than 0.5 can be removed from the queries if needed.

Installation

The python package can be directly installed from this GitHub repository using the following command (pip or pip3 depending on system setup)

pip3 install archs4py

Usage

Download data file

The data is stored in large HDF5 files which first need to be downloaded. HDF5 stores matrix information in a compressed datastructure that allows efficient data access to slices of the data. There are separate files for human and mouse data. The supported files are gene counts and transcript counts. As of 6/2023 the files are larger than 30GB and depending on the network speed will take some time to download.

import archs4py as a4

file_path = a4.download.counts("human", path="", version="latest")

List data fields in H5

The H5 files contain data and meta data information. To list the contents of ARCHS4 H5 files use the built in ls function.

import archs4py as a4

file = "human_gene_v2.4.h5"
a4.ls(file)

Data access

archs4py supports several ways to load gene expression data. When querying ARCHS4 be aware that when loading too many samples the system might run out of memory. (e.g. the meta data search term is very broad). In most cases loading several thousand samples at the same time should be no problem. To find relevant samples there are 5 main functions in the archs4py.data module. A function to extract N random samples archs4py.data.rand(), a function to extract samples by index archs4py.data.index(), a function to extract samples based on meta data search archs4py.data.meta(), a function to extract samples based on a list of geo accessions archs4py.data.samples() and lastly a function to extract all samples belonging to a series archs4.data.series().

Extract a random set of samples

To extract a random gene expression matrix use the archs4py.data.rand() function. The function will return a pandas dataframe with samples as columns and genes as rows.

import archs4py as a4

#path to file
file = "human_gene_v2.4.h5"

# extract 100 random samples and remove sinle cell data
rand_counts = a4.data.rand(file, 100, remove_sc=True)

Extract samples at specified index positions

Extract samples based on their index positions in the H5 file.

import archs4py as a4

#path to file
file = "human_gene_v2.4.h5"

# get counts for samples at position [0,1,2,3,4]
pos_counts = a4.data.index(file, [0,1,2,3,4])

Extract samples matching search term in meta data

The ARCHS4 H5 file contains all meta data of samples. Using meta data search all matching samples can be extracted with the use of search terms. There is also an archs4py.meta module that will only return meta data. Meta data fields to be returned can be specified meta_fields=["geo_accession", "series_id", "characteristics_ch1", "extract_protocol_ch1", "source_name_ch1", "title"]

import archs4py as a4

#path to file
file = "human_gene_v2.4.h5"

# search and extract samples matching regex (ignores whitespaces)
meta_counts = a4.data.meta(file, "myoblast", remove_sc=True)

Extract samples in a list of GEO accession IDs

Samples can directly be downloaded by providing a list of GSM IDs. Samples not contained in ARCHS4 will be ignored.

import archs4py as a4

#path to file
file = "human_gene_v2.4.h5"

#get sample counts
sample_counts = a4.data.samples(file, ["GSM1158284","GSM1482938","GSM1562817"])

Extract samples belonging to a GEO series

To download all samples of a GEO series for example GSE64016 use the series function.

import archs4py as a4

#path to file
file = "human_gene_v2.4.h5"

#get sample counts
series_counts = a4.data.series(file, "GSE64016")

Meta data

Additinally to the data module archs4py also supports the extraction of meta data. It supports similar endpoints to the archs4.data module. Meta data fields can be specified with: meta_fields=["geo_accession", "series_id", "characteristics_ch1", "extract_protocol_ch1", "source_name_ch1", "title"]

import archs4py as a4

#path to file
file = "human_gene_v2.4.h5"

# get sample meta data based on search term
meta_meta = a4.meta.meta(file, "myoblast", meta_fields=["characteristics_ch1", "source_name_ch1"])

# get sample meta data
sample_meta = a4.meta.samples(file, ["GSM1158284","GSM1482938","GSM1562817"])

# get series meta data
series_meta = a4.meta.series(file, "GSE64016")

# get all entries of a meta data field for all samples. In this example get all sample ids and gene symbols in H5 file
all_samples = a4.meta.field(file, "geo_accession")
all_symbols = a4.meta.field(file, "symbol")

Normalizing data

The package also supports simple normalization. Currently supported are quantile normalization, log2 + quantile normalization, and cpm. In the example below we load 100 random samples and apply log quantile.

import archs4py as a4

file = "human_gene_v2.4.h5"
rand_counts = a4.data.rand(file, 100)

#normalize using log quantile (method options for now = ["log_quantile", "quantile", "cpm", "tmm"])
norm_exp = a4.normalize(rand_counts, method="log_quantile")

Filter genes with low expression

To filter genes with low expression use the utils.filter() function. It uses two parameters to determine which genes to filter. readThreshold and sampleThreshold. In the example below genes are removed that don't have at least 50 reads in 2% of samples. aggregate will also deal with duplicate gene symbols in the ARCHS4 data and aggregate the counts.

import archs4py as a4

file = "human_gene_v2.4.h5"
rand_counts = a4.data.rand(file, 100)

# aggregate duplicate genes
filtered_exp = a4.utils.filter_genes(exp, readThreshold=50, sampleThreshold: float=0.02, deterministic: bool=True, aggregate=True)

Aggregate duplicate genes

Some gene symbols are duplicated, which is an artifact from the Ensembl gene annotation. The transcript sequences are often identical and reads are split between the different entries. The utils.aggregate_duplicate_genes() function will sum all counts of duplicate gene symbols and eliminate duplicate entries.

import archs4py as a4

file = "human_gene_v2.4.h5"
rand_counts = a4.data.rand(file, 100)

# filter genes with low expression
agg_exp = a4.utils.aggregate_duplicate_genes(rand_counts)

Sequence alignment

The align module contains a replication of the ARCHS4 alignment pipeline. When used on FASTQ files the resulting gene or transcript counts are compatible with the previously aligned samples in ARCHS4. The package is highly automated and only required a path to a FASTQ file or a folder containing multiple FASTQ files. All file dependencies will downloaded automatically and index will be built when needed.

Align FASTQ file

Pass either a single or paired FASTQ file. This function can return transcript count, gene counts, or transcript level TPM data.

import archs4py as a4

a4.align.load(["SRR14457464"], "data/example_1")

result = a4.align.fastq("human", "data/example_1/SRR14457464.fastq", return_type="gene", identifier="symbol")

The next example is a SRR file that extracts into a pair of paired end FASTQ files. They can be passed to ARCHS4py like this:

import archs4py as a4

# the sample is paired-end and will result in two files (SRR15972519_1.fastq, SRR15972519_2.fastq)
a4.align.load(["SRR15972519"], "data/example_2")

result = a4.align.fastq("mouse", ["data/example_2/SRR15972519_1.fastq", "data/example_2/SRR15972519_2.fastq"], return_type="transcript")

Align FASTQ files from folder

Align all FASTQ files in folder using the function a4.align.folder(). ARCHS4py will automatically matching samples if data is paired end.

import archs4py as a4

a4.align.load(["SRR15972519", "SRR15972520", "SRR15972521"], "data/example_3")

result = a4.align.folder("mouse", "data/example_3", return_type="gene", identifier="symbol")

List versions

ARCHS4 has different versions to download from. Recommended is the default setting, which will download the latest data release.

import archs4 as a4

print(a4.versions())

Citation

When using ARCHS4 please cite the following reference:

Lachmann, Alexander, Denis Torre, Alexandra B. Keenan, Kathleen M. Jagodnik, Hoyjin J. Lee, Lily Wang, Moshe C. Silverstein, and Avi Ma’ayan. "Massive mining of publicly available RNA-seq data from human and mouse." Nature communications 9, no. 1 (2018): 1366. https://www.nature.com/articles/s41467-018-03751-6

archs4py's People

Contributors

lachmann12 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

archs4py's Issues

archs4py does not install in python 3.12

I tried to use the archs4py for easier handling of the archs4 dataset. However, when I tried installing it in my conda envrionment via pip3 install archs4py I got the following error during numpy 1.24.3 building:

      Traceback (most recent call last):
        File "/users/daniel.malzl/.conda/envs/scllm/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/users/daniel.malzl/.conda/envs/scllm/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/users/daniel.malzl/.conda/envs/scllm/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 112, in get_requires_for_build_wheel
          backend = _build_backend()
                    ^^^^^^^^^^^^^^^^
        File "/users/daniel.malzl/.conda/envs/scllm/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend
          obj = import_module(mod_path)
                ^^^^^^^^^^^^^^^^^^^^^^^
        File "/users/daniel.malzl/.conda/envs/scllm/lib/python3.12/importlib/__init__.py", line 90, in import_module
          return _bootstrap._gcd_import(name[level:], package, level)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "<frozen importlib._bootstrap>", line 1381, in _gcd_import
        File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
        File "<frozen importlib._bootstrap>", line 1304, in _find_and_load_unlocked
        File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
        File "<frozen importlib._bootstrap>", line 1381, in _gcd_import
        File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
        File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked
        File "<frozen importlib._bootstrap>", line 929, in _load_unlocked
        File "<frozen importlib._bootstrap_external>", line 994, in exec_module
        File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
        File "/tmp/pip-build-env-1dnxzgn8/overlay/lib/python3.12/site-packages/setuptools/__init__.py", line 16, in <module>
          import setuptools.version
        File "/tmp/pip-build-env-1dnxzgn8/overlay/lib/python3.12/site-packages/setuptools/version.py", line 1, in <module>
          import pkg_resources
        File "/tmp/pip-build-env-1dnxzgn8/overlay/lib/python3.12/site-packages/pkg_resources/__init__.py", line 2172, in <module>
          register_finder(pkgutil.ImpImporter, find_on_path)
                          ^^^^^^^^^^^^^^^^^^^
      AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?

A quick google search implied that this might be an issue with the newest Python version 3.12 which I am also running. Newer numpy versions installed fine (I currently have 1.26.2 installed which is the latest). I guess this can be fixed by simply relaxing the requirement from numpy==1.24.3 to numpy>=1.24.3 or something.

Greek characters are not decoded correctly

To proceed with my work I quickly just copied the relevant code from your package and adapted it where needed for my problem. In the course of this I noticed that there seems to be a problem with the character encoding in the ARCHS5 file. I basically decode the metadata similar to you by using

[x.decode('utf-8') for x in table[key][:]]

However, some characters are not decoded correctly, specifically this seems to affect greek letters as the examples I encountered until now are the letter mu and the letter beta (but might not be limited to them). I tried several other encodings but was not able to decode it properly. Could you have a look or at least tell me the right encoding?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.