Skip to main content

The Transferase Python API

Project description

pyxfr

The pyxfr package is an API for transferase. Learn more about transferase here. This package allows the same queries to be done within Python as with the transferase command line app. Almost all other utilities for manipulating transferase data are available through pyxfr.

Although documentation is still sparse for pyxfr, each class and function in pyxfr has built-in documentation:

from pyxfr import pyxfr, pyxfr_utils
help(pyxfr)
help(pyxfr_utils)

Requirements

  • Linux: Python >= 3.12
  • macOS: Python >= 3.12 and macOS >= 13 (at least Ventura)

Usage examples

First we import the pyxfr module so we can set our preferred log level for the session or in your Python scripts. Setting it to "debug" let's us see everything. It will be a lot of information, most of it actually for debugging.

import pyxfr
pyxfr.set_log_level("debug")

Next we want to set up transferase for the user (i.e., your login account) on the host system (e.g., your laptop). The following will do a default setup, and might take up to a minute:

from pyxfr import MConfig
config = MConfig()
config.install(["hg38"])

This will create files in ~/.config/transferase which are safe to delete anytime because you can just run the same command again. The reason this is done in two steps is because you might want to change something in the config before doing the installation. By typing the name of the variable config you will see a dump of its values. For now, leave them as their default values -- they only need to be changed if you are using local data.

You can select other genomes in the install step (e.g., mm39, rn7, bosTau9, etc.). If the genomes don't exist or are not on the server, you should see a RuntimeError exception in Python, indicating a problem downloading. The server can't tell the difference between an invalid genome assembly name, one that is misspelled, and a real one that simply isn't on the server. You can find the list of available genomes by checking out MethBase2 through the UCSC Genome Browser, or using a command I will show below.

With the setup has completed, we can get a client object:

from pyxfr import MClient
client = MClient()

The client object is what makes the queries. Our query will be based on a set of genomic intervals, which you would get from a BED format file. However, before working with the genomic intervals we need to first load a genome index, which guarantees that we are working with the exact reference genome that the transferase server expects.

from pyxfr import GenomeIndex
genome_index = GenomeIndex.read(client.get_index_dir(), "hg38")

We will now read genomic intervals. If you have a BED format file for hg38, for example around 100k intervals, you can use it. Otherwise you can find the intervals.bed.gz in the docs directory of the repo (likely alongside this file), gunzip it and put it in your working directory.

from pyxfr import GenomicInterval
intervals = GenomicInterval.read(genome_index, "intervals.bed")

At this point we can do a query:

genome_name = "hg38"
methylome_names = ["ERX9474770","ERX9474769"]
levels = client.get_levels(genome_name, methylome_names, intervals)

The levels is an object of class MLevels, which is a matrix where rows correspond to intervals and columns correspond to methylomes in the query.

The following loop will allow us to see the first 10 methylation levels that were retrieved for the second (ERX9474769) of the two methylomes in our query:

print("\n".join([str(levels.at(i, 1)) for i in range(10)]))

It should look like this:

(4, 1)
(1, 471)
(0, 0)
(45, 29)
(0, 0)
(334, 346)
(62, 1581)
(51, 1755)
(74, 664)
(199, 1753)

If you are doing multiple queries that involve the same set of genomic intervals, it's more efficient to make a MQuery object out of them. This is done internally by the query above, but the work to do it can be skipped if they you already have them. Here is an example:

genome_name = "hg38"
methylome_names = ["ERX9474770","ERX9474769"]
query = genome_index.make_query(intervals)
levels = client.get_levels_covered(genome_name, methylome_names, query)

If you want to see the methylation levels alongside the original genomic intervals, including printing the chromosome names for each genomic interval, you can do it as follows:

for col in range(levels.n_methylomes):
    for row in range(10):
        print(intervals[row].to_string(genome_index), levels.at(row, col))
    print()

The at function for class MLevels will return a tuple of (n_meth,n_unmeth) and if the _covered version of the query was used, it will return a tuple of (n_meth,n_unmeth,n_covered). These create the tuple objects that they return Python. If you want the corresponding values without creating the tuple, you can get them with the get_n_meth(i,j), get_n_unmeth(i,j) and get_n_covered(i,j) functions, where i is the row/interval, and j is the column/methylome. Here is an example:

for i in range(10):  # 10 for brevity
    for j in range(levels.n_methylomes):
        print(levels.get_n_covered(i, j), end=' ')
    print()

The get_n_covered(i,j) will raise an exception if the information about covered sites was not requested in the query. There is also a get_wmean(i,j) function that can be applied similarly, which gives the weighted mean methylation level for the corresponding interval. This is likely the most useful summary statistic in all of methylome analysis.

You can convert an MLevels object into a numpy array, which is already familiar to many Python users. The following commands assume that the levels object was obtained from a get_levels_covered query, hence the 3 as the final dimension in a.shape:

a = levels.view_nparray()
methylome_names = ["ERX9474770","ERX9474769"]
assert a.shape == (len(methylome_names), len(intervals), 3)

The intervals in that command came from an earlier command above. The full set of weighted mean methylation levels can be obtained as a numpy array (matrix) using the all_wmeans(min_reads) function, applied to the entire levels object. The min_reads parameter indicates a value below which the fraction is not interpretable. This is needed, because the information about whether there are even any reads at all for a given interval would be lost. Entries without enough reads are assigned a value of -1.0. Here is an example (the output is a numpy array):

min_reads = 2
means = levels.all_wmeans(min_reads)
print(means[0:10])

As we have seen, the queries named with _covered return, along with methylation levels, the number of CpG sites covered by reads in each query interval. It's also helpful to know how many total CpG sites are in the interval. This is a property of the reference genome, not a particular methylome. If you have a set of intervals and a GenomeIndex, you can get the total number of CpG sites in each interval like this:

n_cpgs_intervals = genome_index.get_n_cpgs(intervals)
print("\n".join([str(i) for i in n_cpgs_intervals[0:10]]))

You can get the metadata for MethBase2 methylomes available on the public transferase server. Here is the command and along with what you might expect to see:

methbase_metadata = pyxfr_utils.load_methbase_metadata("hg38")
sum(methbase_metadata.sample_name == "Sperm")
# 174
list(methbase_metadata.columns.values)
# ['study', 'experiment', 'bsrate', 'meth', 'depth', 'mapping', 'uniq',...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyxfr-0.6.3-cp313-none-manylinux_2_17_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

pyxfr-0.6.3-cp313-none-macosx_13_0_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

pyxfr-0.6.3-cp313-none-macosx_13_0_arm64.whl (2.3 MB view details)

Uploaded CPython 3.13macOS 13.0+ ARM64

pyxfr-0.6.3-cp312-none-manylinux_2_17_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

pyxfr-0.6.3-cp312-none-macosx_13_0_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.12macOS 13.0+ x86-64

pyxfr-0.6.3-cp312-none-macosx_13_0_arm64.whl (2.3 MB view details)

Uploaded CPython 3.12macOS 13.0+ ARM64

File details

Details for the file pyxfr-0.6.3-cp313-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for pyxfr-0.6.3-cp313-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 684820c0f74ffa194ecb2b957b5ebeefe40cab623d8e5c8d616fc242622d9f0a
MD5 26aea6620975dd9e7768f51778e31f15
BLAKE2b-256 137f925689915dd8052f82e25edfa0cf0fc95c9a0b2808487df24f496bd2efe2

See more details on using hashes here.

File details

Details for the file pyxfr-0.6.3-cp313-none-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for pyxfr-0.6.3-cp313-none-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 a6089c5a6b96f29530a34fef2b9073d882ecba2f64b3a066445d089615d9fc3e
MD5 3d00ef40b0ae2a3fc57600d8ba03dfcc
BLAKE2b-256 350c7394cdcf1053882199ccb4d33c530b960170e79fac717a6aaf6385dea949

See more details on using hashes here.

File details

Details for the file pyxfr-0.6.3-cp313-none-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for pyxfr-0.6.3-cp313-none-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 593425b68ec175019e485e8a0cdf957280e624dae904927f9ab8ed3f7e4784a0
MD5 7e51f12debe075a66fece86ce052d511
BLAKE2b-256 9a7976473e84e562e3504bbc644d6f1dbc4162b81a0a5e26bfaed3a9763cc81b

See more details on using hashes here.

File details

Details for the file pyxfr-0.6.3-cp312-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for pyxfr-0.6.3-cp312-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 605def6f913db47324e765d472c2c031e5e467f189e4766337fda4c2b6e26153
MD5 c363c7cb27465fda32c87883e6c8d230
BLAKE2b-256 77f131bf1cb7adffceb08ee116912e9529d9d6f5f418c4cdbb1b71b78651f805

See more details on using hashes here.

File details

Details for the file pyxfr-0.6.3-cp312-none-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for pyxfr-0.6.3-cp312-none-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 2a4046b668d724202ea22095c874578b7dab1ddb94b809de6d9855b100e314ac
MD5 27489e9859850a2cce2b758f8f88c25e
BLAKE2b-256 496daa8768f2320437f543ca99af0a7067a48901dfd668aa2f96187322a3c271

See more details on using hashes here.

File details

Details for the file pyxfr-0.6.3-cp312-none-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for pyxfr-0.6.3-cp312-none-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 ecd2539e0a4c51f79a6e1690a060dd8dc0788605f9bc3d7947677bf0afeba87e
MD5 b656e71132fedb2c1a010039483ad594
BLAKE2b-256 3545cf0b8163edb421352ba9d69ff0f6f7403271228e2588b536d9759739222f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page