学习1
import anndata as ad, pandas as pd, numpy as np
from scipy import sparse
a = ad.AnnData(
X=sparse.csr_matrix(np.array([[0, 1], [2, 3]])),
obs=pd.DataFrame({"group": ["a", "b"]}, index=["s1", "s2"]),
var=pd.DataFrame(index=["var1", "var2"]),
varm={
"ones": np.ones((2, 5)),
"rand": np.random.randn(2, 3),
"zeros": np.zeros((2, 5)),
},
uns={"a": 1, "b": 2, "c": {"c.a": 3, "c.b": 4}},
)
b = ad.AnnData(
X=sparse.csr_matrix(np.array([[4, 5, 6], [7, 8, 9]])),
obs=pd.DataFrame(
{"group": ["b", "c"], "measure": [1.2, 4.3]}, index=["s3", "s4"]
),
var=pd.DataFrame(index=["var1", "var2", "var3"]),
varm={"ones": np.ones((3, 5)), "rand": np.random.randn(3, 5)},
uns={"a": 1, "b": 3, "c": {"c.b": 4}},
)
c = ad.AnnData(
X=sparse.csr_matrix(np.array([[10, 11], [12, 13]])),
obs=pd.DataFrame({"group": ["a", "b"]}, index=["s1", "s2"]),
var=pd.DataFrame(index=["var3", "var4"]),
uns={"a": 1, "b": 4, "c": {"c.a": 3, "c.b": 4, "c.c": 5}},
)
可以看到,label=batch
只是添加的列名,而不是按照这列合并,而keys的值仅仅是一个标识值,没有特殊含义,我可以用整数区分,我也可以用唯一的字符串区分,如病人名
adata_concat = ad.concat(Batch_list, label="slice_name", keys=section_ids)
学习2
import anndata as ad, pandas as pd, numpy as np
from scipy import sparse
a = ad.AnnData(
X=sparse.csr_matrix(np.array([[0, 1], [2, 3]])),
obs=pd.DataFrame({"group": ["a", "b"]}, index=["s1", "s2"]),
var=pd.DataFrame(index=["var1", "var2"]),
varm={
"ones": np.ones((2, 5)),
"rand": np.random.randn(2, 3),
"zeros": np.zeros((2, 5)),
},
uns={"a": 1, "b": 2, "c": {"c.a": 3, "c.b": 4}},
)
b = ad.AnnData(
X=sparse.csr_matrix(np.array([[4, 5, 6], [7, 8, 9]])),
obs=pd.DataFrame(
{"group": ["b", "c"], "measure": [1.2, 4.3]}, index=["s3", "s4"]
),
var=pd.DataFrame(index=["var1", "var2", "var3"]),
varm={"ones": np.ones((3, 5)), "rand": np.random.randn(3, 5)},
uns={"a": 1, "b": 3, "c": {"c.b": 4}},
)
c = ad.AnnData(
X=sparse.csr_matrix(np.array([[10, 11], [12, 13]])),
obs=pd.DataFrame({"group": ["a", "b"]}, index=["s1", "s2"]),
var=pd.DataFrame(index=["var3", "var4"]),
uns={"a": 1, "b": 4, "c": {"c.a": 3, "c.b": 4, "c.c": 5}},
)
print(a)
print(b)
print(a.X.toarray())
print(b.X.toarray())
注意这里的ad.concat, 这里的拼接是默认对var取交集的,但是对adata是去并集,也就是说adata的行是增加的,但列是减少的,这个需要特别注意