Skip to content

Commit

Permalink
Merge pull request #7 from lzj1769/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
lzj1769 authored Feb 4, 2023
2 parents 25aad67 + abeeb19 commit 8515d8a
Show file tree
Hide file tree
Showing 9 changed files with 571 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install scikit-learn
python -m pip install --upgrade pip
python -m pip install flake8 pytest
python -m pip install ./
Expand Down

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions docs/source/notebooks/compare_with_chromVAR.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "33fe3950-e22b-4a28-a6dc-693fc6592dc4",
"metadata": {},
"source": [
"# Comparison between pychromVAR and chromVAR: Part II"
"# Compare with chromVAR: Part II"
]
},
{
Expand Down Expand Up @@ -50,7 +50,7 @@
{
"data": {
"text/plain": [
"'0.0.2'"
"'0.0.3'"
]
},
"execution_count": 2,
Expand Down Expand Up @@ -92,6 +92,16 @@
"adata"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0c5b5f7b-79d1-4e8d-bf09-b255c0d088dd",
"metadata": {},
"outputs": [],
"source": [
"adata.write_h5ad(\"example_data.h5ad\")"
]
},
{
"cell_type": "markdown",
"id": "2bd28801-44b0-4a30-8bb3-230d1d33feff",
Expand Down Expand Up @@ -1049,7 +1059,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down
126 changes: 124 additions & 2 deletions docs/source/notebooks/run_chromVAR.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pychromvar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0.3"
__version__ = "0.0.4"
__version_info__ = tuple([int(num) for num in __version__.split('.')]) # noqa: F401

from .preprocessing import get_bg_peaks, add_gc_bias, add_peak_seq
Expand Down
9 changes: 5 additions & 4 deletions pychromvar/compute_deviations.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def compute_deviations(data: Union[AnnData, MuData], n_jobs=-1) -> AnnData:
logging.info('computing observed motif deviations...')
motif_match = adata.varm['motif_match'].transpose()

obs_dev = _compute_dev(
obs_dev = _compute_deviations(
(motif_match, adata.X.transpose(), expectation.transpose())).transpose()

# compute background deviations for bias-correction
Expand All @@ -67,7 +67,7 @@ def compute_deviations(data: Union[AnnData, MuData], n_jobs=-1) -> AnnData:
bg_peak_idx = adata.varm['bg_peaks'][:, i]
bg_motif_match = adata.varm['motif_match'][bg_peak_idx, :].transpose(
)
bg_dev[i, :, :] = _compute_dev((bg_motif_match, adata.X.transpose(),
bg_dev[i, :, :] = _compute_deviations((bg_motif_match, adata.X.transpose(),
expectation.transpose())).transpose()

elif n_jobs > 1:
Expand All @@ -83,7 +83,7 @@ def compute_deviations(data: Union[AnnData, MuData], n_jobs=-1) -> AnnData:

# run the function with multiple cpus
with Pool(processes=n_jobs) as pool:
all_results = pool.map(_compute_dev, arguments_list)
all_results = pool.map(_compute_deviations, arguments_list)

# parse the results
for i in range(n_bg_peaks):
Expand All @@ -102,7 +102,7 @@ def compute_deviations(data: Union[AnnData, MuData], n_jobs=-1) -> AnnData:
return dev


def _compute_dev(arguments):
def _compute_deviations(arguments):
motif_match, count, expectation = arguments

observed = np.dot(motif_match, count)
Expand Down Expand Up @@ -134,3 +134,4 @@ def compute_expectation(count: np.array) -> np.array:
exp = np.dot(b, a)

return exp

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"

[project]
name = "pychromvar"
version = "0.0.3"
version = "0.0.4"
description = "A python package for chromVAR"
authors = [
{name = "Zhijian Li", email = "[email protected]"}
Expand All @@ -18,14 +18,14 @@ classifiers = [
"Topic :: Scientific/Engineering :: Bio-Informatics",
"Intended Audience :: Science/Research"
]
requires-python = ">= 3.6"
requires-python = ">= 3.8"
dependencies = [
"scikit-learn",
"anndata",
"numpy",
"scipy",
"mudata",
"scanpy",
"scikit-learn",
"muon",
"biopython",
"MOODS-python",
Expand Down
17 changes: 15 additions & 2 deletions tests/test_compute_deviations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np

from pychromvar.compute_deviations import compute_expectation
from pychromvar.compute_deviations import compute_expectation, _compute_deviations

def test_compute_expectation():
count = np.array([[1, 0, 1, ], [0, 1, 1]], dtype=np.float32)
Expand All @@ -11,3 +10,17 @@ def test_compute_expectation():

# check the output
assert np.array_equal(exp, np.array([[0.5, 0.5, 1], [0.5, 0.5, 1]]))

def test_compute_deviations():
count = np.array([[1, 0, 1, ], [0, 1, 1]], dtype=np.float32)
exp = compute_expectation(count)

motif_match = np.array([[1, 1], [0, 1], [1, 0]], dtype=np.int8)

dev = _compute_deviations((motif_match, count, exp)).transpose()

# make sure output has same dimensionas
assert dev.shape == (count.shape[1], motif_match.shape[0])

# check the output
assert np.array_equal(dev, np.array([[0.0, -1, 1], [0, 1, -1], [0, 0, 0]]))
11 changes: 11 additions & 0 deletions tests/test_preprocessing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import numpy as np
from anndata import AnnData

from pychromvar.preprocessing import add_gc_bias

def test_gc_bias():
data = AnnData(np.array([[1, 2,], [3, 4]]), dtype=np.float32)
data.uns["peak_seq"] = ["AAAAAAAA", "CCCCGGGG"]
add_gc_bias(data=data)

assert np.array_equal(data.var['gc_bias'].values, np.array([0.0, 1.0]))

0 comments on commit 8515d8a

Please sign in to comment.