For detailed instructions on how to use LongReadTools, please refer to the documentation.
Example Usage of LongReadTools
This section provides a practical example of how to apply LongReadTools in a bioinformatics workflow. We will demonstrate the process of converting isomatrix text files into Anndata objects, which are suitable for high-throughput single-cell genomics analysis. The example will cover the necessary steps from data retrieval and processing to the final conversion using LongReadTools’ specialized functions.
In this section, we will retrieve a list of isomatrix files for conversion into Anndata objects. The isomatrix_tools module within the LongReadTools library provides a function multiple_isomatrix_conversion, which allows for batch conversion of isomatrix text files into Anndata objects, a binary format for representing large datasets in the context of single-cell genomics.
import osimport redirectory ='/data/analysis/data_mcandrew/000-sclr-discovair/'pattern = re.compile('.*(_BIOP_INT|BIOP_NAS)$')matching_files = [os.path.join(directory, f) for f in os.listdir(directory) if pattern.match(f)]print(matching_files)individual_runs = [f +'_isomatrix.txt'for f in matching_files]isomatrix_paths = [os.path.join(f, os.path.basename(f) +'_isomatrix.txt') for f in matching_files]
In this section, we will leverage the isomatool module from the LongReadTools library to convert the isomatrix files, which we have previously identified and listed in isomatrix_paths, into Anndata objects. Anndata objects are a binary format designed for large-scale single-cell genomics data, which facilitates efficient data handling and manipulation, making them ideal for high-throughput computational analysis. The multiple_isomatrix_conversion function from isomatool will be used to perform this batch conversion.
from longreadtools.isomatool import*import scanpy as sc
File /data/analysis/data_mcandrew/000-sclr-discovair/D498_BIOP_INT/D498_BIOP_INT_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D500_BIOP_NAS/D500_BIOP_NAS_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D500_BIOP_INT/D500_BIOP_INT_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D493_BIOP_NAS/D493_BIOP_NAS_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D494_BIOP_NAS/D494_BIOP_NAS_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D493_BIOP_INT/D493_BIOP_INT_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D499_BIOP_INT/D499_BIOP_INT_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D494_BIOP_INT/D494_BIOP_INT_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D492_BIOP_INT/D492_BIOP_INT_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D495_BIOP_INT/D495_BIOP_INT_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D490_BIOP_INT/D490_BIOP_INT_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D496_BIOP_INT/D496_BIOP_INT_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D534_BIOP_INT/D534_BIOP_INT_isomatrix.h5ad was successfully written to disk.
File /data/analysis/data_mcandrew/000-sclr-discovair/D492_BIOP_NAS/D492_BIOP_NAS_isomatrix.h5ad was successfully written to disk.
Reading .h5ad files...
Applying feature set standardization...
Concatenating AnnData objects and adding batch keys with scanpy...
Setting .var attribute...
Final Check...
Concatenation complete.
Standardizing anndata features via union: 100%|██████████| 14/14 [01:05<00:00, 4.68s/it]
Now that we have concatenated the Anndata objects, let’s examine the resulting object to ensure it’s structured correctly and ready for downstream analysis. We will display the shape of the matrix, the metadata associated with observations (cells), and the variables (genes) to get an overview of the dataset.
# Display the shape of the concatenated Anndata objectprint(f"The Anndata object has {andata_concat.n_obs} observations (cells) and {andata_concat.n_vars} variables (genes).")# Display the first few entries of the observation metadata to inspect batch information and other annotationsprint("Observation metadata (first 5 entries):")print(andata_concat.obs.head())# Display the first few entries of the variable metadata to inspect gene and transcript informationprint("Variable metadata (first 5 entries):")print(andata_concat.var.head())# Check for unique observation names and make them unique if necessaryifnot andata_concat.obs_names.is_unique: andata_concat.obs_names_make_unique()print("Observation names were not unique; they have been made unique.")
The Anndata object has 122872 observations (cells) and 89177 variables (genes).
Observation metadata (first 5 entries):
batch
AGGAAATGTACAAGCG D498_BIOP_INT
GCCATTCGTCGGAACA D498_BIOP_INT
TCGACCTCAGTGTGCC D498_BIOP_INT
CGTAGTATCAGTGTGT D498_BIOP_INT
GCCAGGTGTCTAACTG D498_BIOP_INT
Variable metadata (first 5 entries):
geneId transcriptId nbExons
transcriptId
ENST00000548501 CYP4F12 ENST00000548501 4
ENST00000324229 CALCB ENST00000324229 5
ENST00000371489 MYOF ENST00000371489 15
ENST00000368659 SLC27A3 ENST00000368659 2
ENST00000669353 TMEM161B-AS1 ENST00000669353 4
Observation names were not unique; they have been made unique.
Access the count matrix from the concatenated Anndata object to analyze the transcript count data.
Utilizing Scanpy, this function call will serialize the andata_concat object to an HDF5 file, a format widely adopted for storing extensive scientific data. The chosen filename ‘discovair_long_read_transcript_matrix.h5ad’ clearly reflects the file’s contents, representing the transcript matrix obtained from long-read sequencing data.
Here we employ the sc.read_h5ad function to import Anndata objects encapsulating transcriptomic data derived from long-read and short-read sequencing approaches. Long-read sequencing data, renowned for capturing full-length transcripts that unveil isoform diversity, is encapsulated within the Anndata object from the file ‘discovair_long_read_transcript_matrix.h5ad’. Conversely, short-read sequencing data, with its larger cell number and potentially more accurate gene-level quantification, is contained within the Anndata object from the file ‘integrated_V10.h5ad’.
The short-read gene quantification dataset contains a significantly higher number of cells compared to the long-read dataset. Notably, the short-read dataset is annotated, whereas the long-read dataset lacks annotations. Given that both datasets originate from the same library, which was subsequently divided and sequenced on different platforms, there is an expected overlap in cell identities. This commonality provides an opportunity to transfer annotations from the short-read to the long-read dataset by matching corresponding cells.
gene_anndata_from_short_reads.obs
manip
donor
method
position
n_genes_by_counts
total_counts
total_counts_mt
pct_counts_mt
total_counts_ribo
pct_counts_ribo
...
celltype_lv2_V5
celltype_lv0_V5
celltype_lv1_V5
leiden_scANVI
disease_score
smoker_phenotype
leiden_scANVI_hvg_10000
leiden_scANVI_nl_50
leiden_scANVI_hvg_10000_nl_50
celltype_lv3_V5
D460_BIOP_PRO1GGCTTGGAGCGCCTCA-1
D460_BIOP_PRO1
D460
BIOP
PRO
2150
5919.0
283.0
4.782021
1510.0
25.515377
...
Veinous
Endothelial
Endothelial
11
GAP Stage 1
non-smoker_IPF
9
9
8
Veinous
D463_BIOP_NAS1TCACTCGCATTGGGAG-1
D463_BIOP_NAS1
D463
BIOP
NAS
1927
4979.0
474.0
9.519984
1357.0
27.254469
...
Veinous
Endothelial
Endothelial
11
GAP Stage 1
non-smoker_IPF
9
9
8
Veinous
D534_BIOP_PROAATCGACAGCAAGTCG-1
D534_BIOP_PRO
D534
BIOP
PRO
1264
3013.0
311.0
10.321939
779.0
25.854630
...
Capillary
Endothelial
Endothelial
11
Healthy
non-smoker_CTRL
9
9
8
Capillary
D463_BIOP_NAS1TCGCTTGTCACTTGGA-1
D463_BIOP_NAS1
D463
BIOP
NAS
3691
11794.0
1314.0
11.141258
2867.0
24.308971
...
Veinous
Endothelial
Endothelial
11
GAP Stage 1
non-smoker_IPF
9
9
8
Veinous
D489_BIOP_PROAGGGAGTTCGGTCTGG-1
D489_BIOP_PRO
D489
BIOP
PRO
738
1096.0
57.0
5.200730
127.0
11.587591
...
Capillary
Endothelial
Endothelial
11
GOLD 1
non-smoker_BPCO
9
9
8
Capillary
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
D460_BRUS_NAS1TCTATACCAATGGGTG-1
D460_BRUS_NAS1
D460
BRUS
NAS
1500
4263.0
447.0
10.485574
1342.0
31.480179
...
Suprabasal
Epithelial
Suprabasal
0
GAP Stage 1
non-smoker_IPF
2
1
1
Suprabasal
D460_BRUS_NAS1GTTATGGCAATGGCAG-1
D460_BRUS_NAS1
D460
BRUS
NAS
2422
6089.0
774.0
12.711448
740.0
12.153063
...
Ionocyte
Epithelial
Ionocyte
24
GAP Stage 1
non-smoker_IPF
29
27
27
Ionocyte
D460_BRUS_NAS1ATGAGTCAGCCGTTGC-1
D460_BRUS_NAS1
D460
BRUS
NAS
2784
11638.0
1460.0
12.545111
2642.0
22.701494
...
Goblet
Epithelial
Goblet
5
GAP Stage 1
non-smoker_IPF
13
5
4
Goblet
D460_BRUS_NAS1TCATACTAGCAGTAAT-1
D460_BRUS_NAS1
D460
BRUS
NAS
2563
8025.0
919.0
11.451714
1619.0
20.174454
...
Goblet
Epithelial
Goblet
5
GAP Stage 1
non-smoker_IPF
13
5
4
Goblet
D460_BRUS_NAS1TTGTTGTCAAGATGTA-1
D460_BRUS_NAS1
D460
BRUS
NAS
1380
3443.0
255.0
7.406332
724.0
21.028173
...
Goblet
Epithelial
Goblet
5
GAP Stage 1
non-smoker_IPF
13
5
4
Goblet
414609 rows × 47 columns
To ensure a coherent and integrated analysis of the transcriptomic data derived from both long-read and short-read sequencing technologies, it is imperative to harmonize the indexes of the corresponding Anndata objects. This step is crucial as it aligns the observations (cells) across the datasets, enabling a direct comparison and subsequent operations such as data integration, differential expression analysis, and visualization.
After the standardization of the Anndata objects’ indexes, we can confirm that the indexes are now aligned and ready for comparative analysis. This alignment is crucial for the integration of the long-read and short-read transcriptomic data, as it ensures that the same cells are represented in both datasets can be identified.
In this section, we are going to utilize the subset_common_cells function from the longreadtools library to harmonize our datasets. This function is crucial for ensuring that we are comparing the same cells across the two Anndata objects - one derived from long-read sequencing and the other from short-read sequencing. By importing and applying this function, we can identify the intersection of cells present in both datasets, allowing for a consistent and integrated analysis.
from longreadtools.Standardization import*isoform_matrix = subset_common_cells(isoform_anndata_from_long_reads, gene_anndata_from_short_reads)
In the previous steps, we have successfully standardized the indexes of our Anndata objects and utilized the subset_common_cells function to refine the isoform Anndata object derived from long-read sequencing data. The next logical step is to apply the same subsetting process to the gene Anndata object from short-read sequencing data. This ensures that both datasets are synchronized and contain only the cells common to both, which is a prerequisite for accurate annotation transfer.
The next step in our analysis pipeline is to transfer the observation annotations from the gene_matrix Anndata object, which contains the short-read sequencing data, to the isoform_matrix Anndata object, which contains the long-read sequencing data. The transfer_obs function from the longreadtools library is instrumental in this process. It meticulously maps the .obs attributes from one Anndata object to another based on the shared cell identifiers, thus preserving the integrity of the data and enabling a seamless integration.
In this step, we delve into the annotated isoform matrix, which is a product of the meticulous standardization and subsetting processes we have applied to our Anndata objects. The annotated_isoform_matrix is a rich dataset that combines the detailed isoform data obtained from long-read sequencing with the comprehensive annotations transferred from the gene matrix derived from short-read sequencing.
By examining this matrix, we gain insights into the transcriptomic landscape at an isoform resolution, which is crucial for understanding the complexity of gene expression patterns. The annotations included in this matrix, such as cell type, donor information, and technical attributes, are pivotal for subsequent analyses that aim to unravel the biological and clinical significance of the data within the context of the longreadtools framework. Lets save it to disk for later use!