Skip to content

Commit

Permalink
split similarity
Browse files Browse the repository at this point in the history
  • Loading branch information
bioqxu committed Apr 29, 2024
1 parent 8934f76 commit 197a30a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 41 deletions.
48 changes: 7 additions & 41 deletions sc2heoca/sc2heoca.py → sc2heoca/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@


def seed_everything(TORCH_SEED):
random.seed(TORCH_SEED)
os.environ['PYTHONHASHSEED'] = str(TORCH_SEED)
np.random.seed(TORCH_SEED)
torch.manual_seed(TORCH_SEED)
torch.cuda.manual_seed_all(TORCH_SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
random.seed(TORCH_SEED)
os.environ['PYTHONHASHSEED'] = str(TORCH_SEED)
np.random.seed(TORCH_SEED)
torch.manual_seed(TORCH_SEED)
torch.cuda.manual_seed_all(TORCH_SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

def load_colorpalette():
color_file = os.path.join(PACKAGE_DIR, "db", "gut_scpoli_color.txt")
Expand Down Expand Up @@ -275,37 +275,3 @@ def find_de_genes(self, tissue, adata_query, adata_ctrl=None, method='wilcoxauc'
de_res_final = pd.DataFrame.from_dict(uni_genes, orient='index').T

return de_res_final

class Similarity:
def __init__(self, r_path, model_file):
self.r_path = r_path

os.environ["R_HOME"] = self.r_path
from rpy2.robjects.packages import importr
base = importr('base')
miloR = importr('miloR')

self.milo_model = base.readRDS(f"{model_file}")

def run_milo(self, adata, sample_name):

os.environ["R_HOME"] = self.r_path
import rpy2.robjects as robjects
import anndata2ri
anndata2ri.activate()

adata_sce = anndata2ri.py2rpy(adata)

r_source = robjects.r['source']

milo_file = os.path.join(PACKAGE_DIR, "r", "run_milo.R")
r_source(milo_file)

r_getname = robjects.globalenv['run_milo']

res = r_getname(self.milo_model, adata_sce, 'cell_ontology_class')
res['sample']=sample_name

return res


58 changes: 58 additions & 0 deletions sc2heoca/similarity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
from copy import copy

import numpy as np
import pandas as pd

import anndata
import scanpy as sc

from . import PACKAGE_DIR

import torch
import random



def seed_everything(TORCH_SEED):
random.seed(TORCH_SEED)
os.environ['PYTHONHASHSEED'] = str(TORCH_SEED)
np.random.seed(TORCH_SEED)
torch.manual_seed(TORCH_SEED)
torch.cuda.manual_seed_all(TORCH_SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

class Similarity:
def __init__(self, r_path, model_file):
self.r_path = r_path

os.environ["R_HOME"] = self.r_path
from rpy2.robjects.packages import importr
base = importr('base')
miloR = importr('miloR')

self.milo_model = base.readRDS(f"{model_file}")

def run_milo(self, adata, sample_name):

os.environ["R_HOME"] = self.r_path
import rpy2.robjects as robjects
import anndata2ri
anndata2ri.activate()

adata_sce = anndata2ri.py2rpy(adata)

r_source = robjects.r['source']

milo_file = os.path.join(PACKAGE_DIR, "r", "run_milo.R")
r_source(milo_file)

r_getname = robjects.globalenv['run_milo']

res = r_getname(self.milo_model, adata_sce, 'cell_ontology_class')
res['sample']=sample_name

return res


0 comments on commit 197a30a

Please sign in to comment.