Home Install User Guide Python Notebook API FAQ

DEG analysis

DEGexample

Compute Differentiallt Expressed Genes

In [1]:
import requests
from pprint import pprint
import pandas as pd
import numpy as np
In [ ]:
import diffxpy.api as de
In [3]:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats
import time
import scanpy.api as sc

Your app URL with port

In [4]:
appUrl="http://54.159.6.229:8002/"

Retrieve expression matrix using API

In [5]:
## HTTP api url 
url=appUrl+"api/getNormalizedGeneExprByTwoClstrs";
## collect normalized counts data from two cell types: Tcells and Bcells
data={
    "mapid":"5c8bf53ea05a37a5c7d707ce",
    "clstrType1":"cellType",
    "clstrName1":"Tcells",
    "clstrType2":"cellType",
    "clstrName2":"Bcells",
    "zscoreFilter":"" ,   # if set "" , skip the z score filter
    "log2fc":""           # if set "" , skip the fold change filter
}
## use API
res = requests.post(url, data=data)
res=res.json();
pprint(len(res.keys()))
16477
In [ ]:
 
In [8]:
# create adata object
data=[];
genes=[];
key=list(res.keys())[0]
g1len= len(res[key][0]);
g2len= len(res[key][1]);
condition = [0]*g1len+[1]*g2len;
for i in res:
    genes.append(i);
    data.append(res[i][0]+res[i][1]) 
data=np.array(data,dtype="float32");
adata=sc.AnnData(X=data);
adata.obs.index=genes;
adata=adata.T;
adata.obs["condition"]=condition

run DEG test using Diffxpy from Theis Lab

one p-value is computed per gene

In [10]:
test_w = de.test.wilcoxon(
    data=adata,
    grouping="condition",    
);
/usr/local/lib/python3.6/dist-packages/xarray/core/computation.py:565: RuntimeWarning: divide by zero encountered in log
  result_data = func(*input_data)
In [13]:
test_w.summary().sort_values(by=["qval","log2fc"],ascending=True).head()
Out[13]:
gene pval qval log2fc mean
15391 CD79A 5.249853e-213 8.650182e-209 8.654743 0.235708
5301 HLA-DRA 8.563910e-128 7.055377e-124 3.976070 0.670985
8650 MS4A1 6.066316e-117 3.331823e-113 5.346273 0.196800
13704 CD79B 1.361846e-114 5.609786e-111 4.084745 0.312431
11817 LINC00926 4.393352e-107 1.447785e-103 7.681458 0.100990

above table contain B cells specific genes compared to T cells

In [ ]: