-
Notifications
You must be signed in to change notification settings - Fork 19
/
dataset.py
executable file
·265 lines (226 loc) · 8.06 KB
/
dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python
"""
# Author: Xiong Lei
# Created Time : Wed 26 Dec 2018 03:46:19 PM CST
# File Name: batch.py
# Description:
"""
import os
import numpy as np
import pandas as pd
import scipy
from scipy.sparse import issparse
import torch
from torch.utils.data import Dataset
from torch.utils.data.sampler import Sampler
from torch.utils.data import DataLoader
from anndata import AnnData
import scanpy as sc
# import episcanpy as epi
from sklearn.preprocessing import maxabs_scale, MaxAbsScaler
from glob import glob
np.warnings.filterwarnings('ignore')
DATA_PATH = os.path.expanduser("~")+'/.scale/'
CHUNK_SIZE = 20000
def read_mtx(path):
"""
Read mtx format data folder including:
matrix file: e.g. count.mtx or matrix.mtx
barcode file: e.g. barcode.txt
feature file: e.g. feature.txt
"""
for filename in glob(path+'/*'):
if ('count' in filename or 'matrix' in filename or 'data' in filename) and ('mtx' in filename):
adata = sc.read_mtx(filename).T
for filename in glob(path+'/*'):
if 'barcode' in filename:
barcode = pd.read_csv(filename, sep='\t', header=None).iloc[:, -1].values
print(len(barcode), adata.shape[0])
if len(barcode) != adata.shape[0]:
adata = adata.transpose()
adata.obs = pd.DataFrame(index=barcode)
if 'gene' in filename or 'peaks' in filename or 'feature' in filename:
gene = pd.read_csv(filename, sep='\t', header=None).iloc[:, -1].values
if len(gene) != adata.shape[1]:
adata = adata.transpose()
adata.var = pd.DataFrame(index=gene)
return adata
def load_file(path):
"""
Load single cell dataset from file
"""
if os.path.exists(DATA_PATH+path+'.h5ad'):
adata = sc.read_h5ad(DATA_PATH+path+'.h5ad')
elif os.path.isdir(path): # mtx format
adata = read_mtx(path)
elif os.path.isfile(path):
if path.endswith(('.csv', '.csv.gz')):
adata = sc.read_csv(path).T
elif path.endswith(('.txt', '.txt.gz', '.tsv', '.tsv.gz')):
df = pd.read_csv(path, sep='\t', index_col=0).T
adata = AnnData(df.values, dict(obs_names=df.index.values), dict(var_names=df.columns.values))
elif path.endswith('.h5ad'):
adata = sc.read_h5ad(path)
elif path.endswith(tuple(['.h5mu/rna', '.h5mu/atac'])):
import muon as mu
adata = mu.read(path)
else:
raise ValueError("File {} not exists".format(path))
if not issparse(adata.X):
adata.X = scipy.sparse.csr_matrix(adata.X)
adata.var_names_make_unique()
return adata
def load_files(root):
"""
Load single cell dataset from files
"""
if root.split('/')[-1] == '*':
adata = []
for root in sorted(glob(root)):
adata.append(load_file(root))
return AnnData.concatenate(*adata, batch_key='sub_batch', index_unique=None)
else:
return load_file(root)
def concat_data(
data_list,
batch_categories=None,
join='inner',
batch_key='batch',
index_unique=None,
save=None
):
"""
Concat multiple datasets
"""
if len(data_list) == 1:
return load_files(data_list[0])
elif isinstance(data_list, str):
return load_files(data_list)
elif isinstance(data_list, AnnData):
return data_list
adata_list = []
for root in data_list:
adata = load_files(root)
adata_list.append(adata)
if batch_categories is None:
batch_categories = list(map(str, range(len(adata_list))))
else:
assert len(adata_list) == len(batch_categories)
[print(b, adata.shape) for adata,b in zip(adata_list, batch_categories)]
concat = AnnData.concatenate(*adata_list, join=join, batch_key=batch_key,
batch_categories=batch_categories, index_unique=index_unique)
if save:
concat.write(save, compression='gzip')
return concat
def preprocessing_atac(
adata,
min_genes=200,
min_cells=3, # 0.01,
n_top_genes=30000,
target_sum=None,
chunk_size=CHUNK_SIZE,
log=None
):
"""
preprocessing
"""
print('Raw dataset shape: {}'.format(adata.shape))
if log: log.info('Preprocessing')
if not issparse(adata.X):
adata.X = scipy.sparse.csr_matrix(adata.X)
adata.X[adata.X>1] = 1
if log: log.info('Filtering cells')
sc.pp.filter_cells(adata, min_genes=min_genes)
if log: log.info('Filtering genes')
if min_cells < 1:
min_cells = min_cells * adata.shape[0]
sc.pp.filter_genes(adata, min_cells=min_cells)
if n_top_genes != -1:
if log: log.info('Finding variable features')
sc.pp.highly_variable_genes(adata, n_top_genes=n_top_genes, inplace=False, subset=True)
# adata = epi.pp.select_var_feature(adata, nb_features=n_top_genes, show=False, copy=True)
# if log: log.infor('Normalizing total per cell')
# sc.pp.normalize_total(adata, target_sum=target_sum)
if log: log.info('Batch specific maxabs scaling')
# adata.X = maxabs_scale(adata.X)
# adata = batch_scale(adata, chunk_size=chunk_size)
print('Processed dataset shape: {}'.format(adata.shape))
return adata
def batch_scale(adata, chunk_size=CHUNK_SIZE):
"""
Batch-specific scale data
"""
for b in adata.obs['batch'].unique():
idx = np.where(adata.obs['batch']==b)[0]
scaler = MaxAbsScaler(copy=False).fit(adata.X[idx])
for i in range(len(idx)//chunk_size+1):
adata.X[idx[i*chunk_size:(i+1)*chunk_size]] = scaler.transform(adata.X[idx[i*chunk_size:(i+1)*chunk_size]])
return adata
def reindex(adata, genes):
"""
Reindex AnnData with gene list
"""
idx = [i for i, g in enumerate(genes) if g in adata.var_names]
print('There are {} gene in selected genes'.format(len(idx)))
new_X = scipy.sparse.csr_matrix((adata.shape[0], len(genes)))
new_X[:, idx] = adata[:, genes[idx]].X
adata = AnnData(new_X, obs=adata.obs, var={'var_names':genes})
return adata
class SingleCellDataset(Dataset):
"""
Dataset for dataloader
"""
def __init__(self, adata):
self.adata = adata
self.shape = adata.shape
def __len__(self):
return self.adata.X.shape[0]
def __getitem__(self, idx):
x = self.adata.X[idx].toarray().squeeze()
domain_id = self.adata.obs['batch'].cat.codes[idx]
# return x, domain_id, idx
return x
def load_dataset(
data_list,
batch_categories=None,
join='inner',
batch_key='batch',
batch_name='batch',
min_genes=200,
min_cells=3, # 0.01 previous default
n_top_genes=30000,
batch_size=64,
chunk_size=CHUNK_SIZE,
log=None,
transpose=False,
):
"""
Load dataset with preprocessing
"""
adata = concat_data(data_list, batch_categories, join=join, batch_key=batch_key)
if log: log.info('Raw dataset shape: {}'.format(adata.shape))
if batch_name!='batch':
adata.obs['batch'] = adata.obs[batch_name]
if 'batch' not in adata.obs:
adata.obs['batch'] = 'batch'
adata.obs['batch'] = adata.obs['batch'].astype('category')
adata = preprocessing_atac(
adata,
min_genes=min_genes,
min_cells=min_cells,
n_top_genes=n_top_genes,
chunk_size=chunk_size,
log=log,
)
scdata = SingleCellDataset(adata) # Wrap AnnData into Pytorch Dataset
trainloader = DataLoader(
scdata,
batch_size=batch_size,
drop_last=True,
shuffle=True,
num_workers=4
)
# batch_sampler = BatchSampler(batch_size, adata.obs['batch'], drop_last=False)
testloader = DataLoader(scdata, batch_size=batch_size, drop_last=False, shuffle=False)
# testloader = DataLoader(scdata, batch_sampler=batch_sampler)
return adata, trainloader, testloader