Skip to content

Commit

Permalink
initial not yet finished correlation on request
Browse files Browse the repository at this point in the history
  • Loading branch information
andreassteffen committed Jan 23, 2023
1 parent 4746e96 commit 02c6962
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions database_schema/6-query.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
DROP FUNCTION IF EXISTS get_correlated_genes(study_id int, omics_id int);
CREATE OR REPLACE FUNCTION get_correlated_genes(study_id int, omics_id int)
RETURNS table
(
omics_id INT,
pearson FLOAT
)
AS
$$
import pandas as pd
import scanpy as sc
from joblib import Parallel, delayed, cpu_count
import numpy as np
import scipy


def compute_correlation(df, genes, goi):
collect = []
for gene in genes:
tmp = df[[goi,gene]]
tmp_both = tmp.loc[~(tmp==0).all(axis=1)]
try:
r, p = scipy.stats.pearsonr(*tmp_both.to_dict(orient = 'list').values())
if abs(r)>=0.2:
collect.append({'gene': gene, 'r':r, 'p': p})
except:
pass
return pd.DataFrame(collect)
fn = '../scratch/blood_covid.h5ad'
adata = sc.read(fn)
df = adata.to_df()
chunks = np.array_split(df.columns.drop(goi),8)
result = Parallel(n_jobs=cpu_count())(delayed(compute_correlation)(df = df,genes=chunk, goi=goi) for chunk in chunks)
pd.concat(result).sort_values('r', ascending = False).reset_index(drop = True)
$$ LANGUAGE plpython3u
IMMUTABLE
SECURITY DEFINER
PARALLEL SAFE;

DROP FUNCTION IF EXISTS annotation_value_coocurrence(study_id int, annotation_group_id_1 int, annotation_value_id_2 int);
CREATE OR REPLACE FUNCTION annotation_value_coocurrence(study_id int, annotation_group_id_1 int, annotation_group_id_2 int)
RETURNS TABLE
Expand Down

0 comments on commit 02c6962

Please sign in to comment.