import requests
from pprint import pprint
import pandas as pd
import numpy as np
# use the url with port number of single cell explorer application you want to access
appUrl="http://54.159.6.229:8002/"
# download all normalized gene expression data from NK cells from "E-MTAB-6678 fetal-maternal interface" data
url=appUrl+"api/getAllNormalizedGeneExpr";
data={
"mapid":"5bfe36bdbef42b0d23956527",
"clstrType":"cellType",
"clstrName":"NK",
}
#if clstrType and clstrName is None. will return all gene expressions
res = requests.post(url, data=data)
res=res.json();
df = pd.DataFrame(np.array(res["data"]), columns=res["head"],index=res["index"])
df.head()
# query a set of genes from all cell types from "E-MTAB-6678 fetal-maternal interface" data
url=appUrl+"api/getNormalizedGeneExpr";
data={
"mapid":"5bfe36bdbef42b0d23956527",
"clstrType":None,
"clstrName":None,
"genes":",".join(["CD3E","CD14","CD1C","CIITA","TPSB2",])
}
res = requests.post(url, data=data)
res=res.json();
df = pd.DataFrame(np.array(res["data"]), columns=res["head"],index=res["index"])
df
# full API path
url=appUrl+"api/getClstrsByMapidAndClstrType";
# use mapid and cell type to call barcode and celltype information
data={
"mapid":"5bfe36bdbef42b0d23956527",
"clstrType":"cellType"
}
res = requests.post(url, data=data)
res=res.json();
df = pd.DataFrame( list(res.values()), columns= [data["clstrType"]],index=list(res.keys()) )
df.head()
url=appUrl+"api/getMarkGenesByMapidAndClstrType";
data={
"mapid":"5bfe36bdbef42b0d23956527",
"clstrType":"cellType"
}
res = requests.post(url, data=data)
# API return result in a json object.
pprint(res.json())
url=appUrl+"api/getMaps";
#query can set any search condition
query={
'disease': 'Healthy',
}
res = requests.post(url, data=query)
res=res.json();
pprint(res)
url=appUrl+"api/getNormalizedGeneExprByTwoClstrs";
data={
"mapid":"5c8bf53ea05a37a5c7d707ce",
"clstrType1":"cellType",
"clstrName1":"Tcells",
"clstrType2":"cellType",
"clstrName2":"Bcells",
#"zscoreFilter": 0.15,#if set "" , skip
#"log2fc":4,#if set "" , skip
}
res = requests.post(url, data=data)
res=res.json();
pprint(len(res.keys()))
# follow is an example for running t-test
import diffxpy.api as de
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats
import numpy as np
import time
import scanpy.api as sc
#make adata
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
test_wilcox = de.test.wilcoxon(
data=adata,
grouping="condition",
);
## B cells specific genes
test_wilcox.summary().sort_values(by=["qval","log2fc"],ascending=True).head()