-
Notifications
You must be signed in to change notification settings - Fork 1
/
snakemake3_run_scvi.py
306 lines (245 loc) ยท 13.3 KB
/
snakemake3_run_scvi.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import numpy as np
import sklearn
import pandas as pd
from glob import glob
from tqdm import tqdm
import anndata
import torch
from scvi.dataset import GeneExpressionDataset
from scvi.models import VAE
from scvi.inference import UnsupervisedTrainer
from scvi.inference.posterior import Posterior
from openTSNE import TSNE
from openTSNE.callbacks import ErrorLogger
import os
import sys
from natsort import natsorted
import io
import requests
# how to call snakemake just to see if it works
# snakemake -j 1 -s snakemake3_run_scvi.py --keep-going --rerun-incomplete -pn
# how to call snakemake with a maximum of 100 cores used in parallel
# --mem-per-cpu=60200 requests 60GB RAM per CPU/core so that we avoid out of memory problems by having too many jobs in the same node
# snakemake -j 100 -s snakemake3_run_scvi.py --keep-going --rerun-incomplete --latency-wait 50 --cluster "sbatch -A lpachter -t 500 --mem-per-cpu=60200 --output=./logs/scvi_%j.scvi"
url="https://docs.google.com/spreadsheets/d/"+ \
"1-2bLIns8r8VRoDenHVk-cQE9feNDnXJXnGZNm70ROrA"+\
"/export?gid="+\
"0" + \
"&format=csv"
metadatas=pd.read_csv(io.StringIO(requests.get(url).content.decode('utf-8')))
#how many times to run scvi
NUMBER_OF_SCVI_REPLICATES=30
final_summary_files = []
for dataset_sample_id in metadatas[metadatas['scvi']==1]['dataset_sample_id']:
dataset_project_id = metadatas[metadatas['dataset_sample_id']==dataset_sample_id]['dataset_project_id']
final_summary_files.append('final_summaries/' + dataset_project_id + '-'+ dataset_sample_id + '-final_summary.csv')
# print( '๐๐๐๐๐๐๐๐๐๐ FINAL H5AD FILES ๐๐๐๐๐๐๐๐๐๐๐๐')
# print(final_stacked_h5ad_files)
# print( '๐บ๐บ๐บ๐บ๐บ๐บ๐บ๐บ๐บ๐บ MAKING ',len(final_summary_files), ' H5AD FILES ๐บ๐บ๐บ๐บ๐บ๐บ๐บ๐บ๐บ๐บ๐บ๐บ')
def make_partial_results_filenames(wildcards):
# print('๐๐๐๐')
STACKED_H5AD=f'stacked_h5ads/{wildcards.dataset_project_id}-{wildcards.dataset_sample_id}-stacked.h5ad'
adata=anndata.read(STACKED_H5AD)
total_cells = adata.n_obs
n_retained_cells = int(0.85*total_cells)
print('Total cells:', total_cells)
print('Retained cells:', n_retained_cells)
cells_sizes = []
#initial number of sampled cells
sampling_size = 500
print('๐๐๐๐')
print('total cells:', total_cells)
print(sampling_size)
print(n_retained_cells)
print( '๐บ๐บ๐บ')
while sampling_size < n_retained_cells:
cells_sizes.append(sampling_size)
sampling_size = int(sampling_size*np.sqrt(2))
ss_depths = [depth for depth in adata.layers.keys()]
print('๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ')
print(dataset_project_id)
print(dataset_sample_id)
print(cells_sizes)
print(ss_depths)
print('๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ๐ฅ')
return expand(
'scvi_output/partial_csvs/{{dataset_project_id}}-{{dataset_sample_id}}-c{ss_cells}-d{ss_depth}-SUCCESS.csv',
ss_cells=cells_sizes, ss_depth=ss_depths
)
rule all:
input:
final_summary_files
rule run_scvi:
input:
STACKED_H5AD='stacked_h5ads/{dataset_project_id}-{dataset_sample_id}-stacked.h5ad',
# STACKED_H5AD='stacked_h5ads/'+ dataset_project_id + '-' + dataset_sample_id + '-stacked.h5ad',
output:
SCVI_PARTIAL_SUMMARY='scvi_output/partial_csvs/{dataset_project_id}-{dataset_sample_id}-c{ss_cells}-d{ss_depth}.csv',
SCVI_PARTIAL_SUMMARY_SUCCESS='scvi_output/partial_csvs/{dataset_project_id}-{dataset_sample_id}-c{ss_cells}-d{ss_depth}-SUCCESS.csv',
run:
# try loading partial results of a previous run that might have failed, otherwise make a new dataframe
try:
df_full=pd.read_csv(output.SCVI_PARTIAL_SUMMARY, index=False)
except:
df_full=pd.DataFrame()
current_numer_of_replicates=len(df_full)
while current_numer_of_replicates < NUMBER_OF_SCVI_REPLICATES:
ds = wildcards.dataset_sample_id
ss_depth = int(wildcards.ss_depth)
ss_cells = int(wildcards.ss_cells)
print('============== PROCESSING DATASET: ', ds, 'subsampling depth: ', ss_depth, 'cells: ', ss_cells, '===============')
adata = anndata.read(str(input.STACKED_H5AD))
total_cells = adata.n_obs
n_retained_cells = int(0.85*total_cells)
print('Total cells:', total_cells)
print('Retained cells:', n_retained_cells)
cells_sizes = []
#initial number of sampled cells
sampling_size = 500
while sampling_size < n_retained_cells:
cells_sizes.append(sampling_size)
sampling_size = int(sampling_size*np.sqrt(2))
# cells_sizes = np.logspace(np.log2(500), np.log2(n_retained_cells), num=9, base=2).astype(int)
print('Number of sampled cells for ', ds, cells_sizes)
cells_dataset = GeneExpressionDataset()
X_ = adata.layers['0']
cells_dataset.populate_from_data(X_, gene_names=adata.var.index.values)
#we subsambple to 1000 genes for speed and to prevent overfitting
cells_dataset.filter_genes_by_count(per_batch=True)
cells_dataset.subsample_genes(1000)
sel_genes = cells_dataset.gene_names
n_validation = adata.shape[0] - n_retained_cells
print(ds, ' n_validation:', n_validation)
validation_cells = np.random.choice(adata.obs.index, size=n_validation, replace=False)
learning_cells = adata.obs.index.difference(validation_cells)
val_adata = adata[validation_cells]
lea_adata = adata[learning_cells]
ne_cells = X_.sum(axis=1) > 0
to_keep = np.where(ne_cells)[0]
log_counts = np.log(X_[to_keep].sum(axis=1))
local_means = (np.mean(log_counts) * np.ones((X_.shape[0], 1))).astype(np.float32)
local_vars = (np.var(log_counts) * np.ones((X_.shape[0], 1))).astype(np.float32)
batch_indices = np.ones((X_.shape[0], 1))
labels = np.zeros_like(batch_indices)
validation_cells_dataset = GeneExpressionDataset()
X_ = val_adata[:, sel_genes].layers['0']
validation_cells_dataset.populate_from_data(X_)
# this will store all dataframes we also save as csv
results_list = []
print(' ๐ ๐ฅฏ ๐ง ๐ฐ ๐ฅฎ ๐ฅ ๐ฅง ๐ ๐ฏ ๐ฅช ๐ฅ ๐ฅ ๐ฅ ')
print('======== ', ds, ' ======== SUBSAMPLED CELLS:', ss_cells, ' ================== ')
idx = np.random.choice(learning_cells, size=ss_cells, replace=False)
tmp_adata = lea_adata[idx]
n_epochs = int(27 * adata.shape[0] / ss_cells)
print('Running ', n_epochs, 'epochs')
print(ds, 'now running depth:', ss_depth)
result_dict = {'ss_depth': ss_depth, 'ss_cells': ss_cells}
print('๐ง ๐ฐ ๐ง ๐ฐ ๐ง ๐ฐ ๐ง ๐ฐ')
print(str(input.STACKED_H5AD))
print(tmp_adata[:, sel_genes].layers)
print(tmp_adata[:, sel_genes].layers.keys)
try:
X_ = tmp_adata[:, sel_genes].layers[str(ss_depth)]
except:
pass
try:
X_ = tmp_adata[:, sel_genes].layers[ss_depth]
except:
pass
try:
X_ = tmp_adata[:, sel_genes].layers[wildcards.ss_depth]
except:
X_ = tmp_adata[:, sel_genes].layers[wildcards.ss_depth]
ne_cells = X_.sum(axis=1) > 0
to_keep = np.where(ne_cells)[0]
log_counts = np.log(X_[to_keep].sum(axis=1))
local_means = (np.mean(log_counts) * np.ones((X_.shape[0], 1))).astype(np.float32)
local_vars = (np.var(log_counts) * np.ones((X_.shape[0], 1))).astype(np.float32)
batch_indices = np.ones((X_.shape[0], 1))
labels = np.zeros_like(batch_indices)
cells_dataset = GeneExpressionDataset()
cells_dataset.populate_from_data(X_, gene_names=sel_genes)
vae = VAE(cells_dataset.nb_genes,
dispersion='gene',
n_layers=2,
n_hidden=128,
reconstruction_loss='nb')
trainer = UnsupervisedTrainer(vae, cells_dataset)
# very rarely the trainer fails with nans for the loss,
# simplest solution is to just try a few times before giving up
# very unlikely it will fail 7 times in a row, if it does there's a real problem...
try:
trainer.train(n_epochs=n_epochs)
except:
try:
trainer.train(n_epochs=n_epochs)
except:
try:
trainer.train(n_epochs=n_epochs)
except:
try:
trainer.train(n_epochs=n_epochs)
except:
try:
trainer.train(n_epochs=n_epochs)
except:
try:
trainer.train(n_epochs=n_epochs)
except:
trainer.train(n_epochs=n_epochs)
validation_posterior = Posterior(vae, validation_cells_dataset, use_cuda=False)
print(X_.shape)
result_dict['validation_error'] = validation_posterior.reconstruction_error()
df=pd.DataFrame()
df['ss_depth'] = [result_dict['ss_depth']]
df['ss_cells'] = [result_dict['ss_cells']]
df['validation_error'] = [result_dict['validation_error']]
df_full=df_full.append(df)
df_full.to_csv(output.SCVI_PARTIAL_SUMMARY, index=False)
current_numer_of_replicates=len(df_full)
# once the dataframe has the desired number of replicates we exit the while loop and save the final version
df_full.to_csv(output.SCVI_PARTIAL_SUMMARY_SUCCESS, index=False)
rule make_final_summaries:
input:
SCVI_PARTIAL_SUMMARY_SUCCESS=make_partial_results_filenames,
params:
STACKED_H5AD='stacked_h5ads/{dataset_project_id}-{dataset_sample_id}-stacked.h5ad',
output:
FINAL_SUMMARY = 'final_summaries/{dataset_project_id}-{dataset_sample_id}-final_summary.csv'
run:
print('๐ฅง ๐ฅง ๐ฅง ๐ฅง FINISHED DATASET: ๐ฅง ๐ฅง ๐ฅง ๐ฅง')
print(input)
print('๐ฐ ๐ฐ ๐ฐ ๐ฐ ๐ฐ ๐ฐ ๐ฐ ๐ฐ ๐ฐ ๐ฐ')
dfs = []
ds = wildcards.dataset_sample_id
summary_results={}
for fname in tqdm(input):
df = pd.read_csv(fname)
dfs.append(df)
summary_results[ds] = pd.concat(dfs).reset_index(drop=True)
#loads anndata object used for calculating UMIs seen
adata = anndata.read(params.STACKED_H5AD)
total_cells = adata.n_obs
# this uses the anndata object to calculate the total UMIs for each subsampling depth
for subsampled_depth in summary_results[ds]['ss_depth'].unique():
total_UMIs = int(adata.layers[str(subsampled_depth)].sum())
summary_results[ds].loc[summary_results[ds].ss_depth == subsampled_depth, 'total_UMIs'] = total_UMIs
summary_results[ds]['total_UMIs'] = summary_results[ds]['total_UMIs'].astype('int')
# rfull data depth from `0` to the total reads in the dataset,
summary_results[ds]['ss_depth'] = summary_results[ds]['ss_depth'].map(lambda d: {0: int(metadatas[metadatas['dataset_sample_id']==dataset_sample_id]['total_reads'].values)}.get(d, d))
# we rename ss_depth to total_seqtk_reads to avoid abiguity
summary_results[ds].rename(columns={'ss_depth':'total_seqtk_reads'}, inplace=True)
summary_results[ds].rename(columns={'ss_cells':'sampled_cells'}, inplace=True)
summary_results[ds]['validation_error'] = summary_results[ds]['validation_error'].round(1)
summary_results[ds]['estimated_reads'] = (summary_results[ds]['sampled_cells'] / total_cells* summary_results[ds]['total_seqtk_reads']).astype('int')
summary_results[ds]['estimated_UMIs'] = (summary_results[ds]['sampled_cells'] / total_cells * summary_results[ds]['total_UMIs']).astype('int')
summary_results[ds]['reads_per_cell'] = (summary_results[ds]['total_seqtk_reads'] / total_cells).astype('int')
summary_results[ds]['UMIs_per_cell'] = (summary_results[ds]['total_UMIs'] / total_cells).astype('int')
summary_results[ds]['duplication_rate'] = (summary_results[ds]['total_seqtk_reads'] / summary_results[ds]['total_UMIs'] ).round(2)
# summary_results[ds].drop(['index'], axis=1, inplace=True)
summary_results[ds] = summary_results[ds].sort_values('total_seqtk_reads')
summary_results[ds].index.rename(ds, inplace=True)
summary_results[ds]['dataset'] = ds
print(ds, 'summary results shape:' , summary_results[ds].shape)
print('Number of points with missing validation errors: ', sum(summary_results[ds]['validation_error'].isna()) )
summary_results[ds].to_csv(output.FINAL_SUMMARY, index=False)