-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZINB_Transform.py
More file actions
414 lines (322 loc) · 13.2 KB
/
ZINB_Transform.py
File metadata and controls
414 lines (322 loc) · 13.2 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""
ZINB-based scStyleBulk Generator
This script transforms bulk RNA-seq data into single-cell-style expression profiles
by fitting Zero-Inflated Negative Binomial (ZINB) distributions to single-cell data
and mapping bulk expression values to the learned single-cell distribution.
Author: [Your Name]
Date: 2025
"""
import os
import random
import argparse
from functools import partial
from concurrent.futures import ProcessPoolExecutor
import numpy as np
import pandas as pd
import scanpy as sc
from tqdm import tqdm
from scipy.stats import nbinom
from scipy.optimize import minimize
from scipy.stats import zscore, norm
import matplotlib.pyplot as plt
import umap
from sklearn.preprocessing import StandardScaler
np.random.seed(42)
# =============================================================================
# Utility Functions
# =============================================================================
def wrap_with_tqdm(executor, fn, args_list, desc="Processing"):
"""
Wrap parallel execution with a tqdm progress bar.
Args:
executor: ProcessPoolExecutor instance
fn: Function to apply to each argument
args_list: List of arguments to process
desc: Description for progress bar
Returns:
Iterator with tqdm wrapper
"""
return tqdm(executor.map(fn, args_list), total=len(args_list), desc=desc)
def neg_log_likelihood(params, data):
"""
Calculate negative log-likelihood for ZINB distribution.
Args:
params: Tuple of (r, p, pi) parameters
data: Observed expression data
Returns:
Negative log-likelihood value
"""
r, p, pi = params
likelihoods = (1 - pi) * nbinom.pmf(data, r, p) + (pi * (data == 0))
likelihoods = np.maximum(likelihoods, 1e-10)
return -np.sum(np.log(likelihoods))
def zinb_mean_variance(r, p, pi):
"""
Compute mean and variance of ZINB distribution.
Args:
r: Size parameter
p: Probability parameter
pi: Zero-inflation parameter
Returns:
Tuple of (mean, variance)
"""
mean = (1 - pi) * (r * (1 - p) / p)
variance = mean * (1 + ((1 - p) / p) * r + pi * (r * (1 - p) / p))
return mean, variance
def init_random_zinb():
"""
Initialize ZINB parameters with random values.
Returns:
Tuple of (r, p, pi) initial parameters
"""
return (
np.random.uniform(0.5, 5),
np.random.uniform(0.1, 0.3),
np.random.uniform(0.7, 0.9)
)
def process_gene(args):
"""
Optimize ZINB parameters and generate samples for a single gene.
Args:
args: Tuple of (gene_name, expression_data, num_samples)
Returns:
Dictionary containing optimized parameters and generated samples
"""
gene, data, num_samples = args
r_init, p_init, pi_init = init_random_zinb()
initial_guess = [r_init, p_init, pi_init]
bounds = [(1e-5, None), (1e-5, 1 - 1e-5), (1e-5, 1 - 1e-5)]
result = minimize(
neg_log_likelihood,
initial_guess,
args=(data,),
bounds=bounds,
method="L-BFGS-B"
)
r_opt, p_opt, pi_opt = result.x
mean, variance = zinb_mean_variance(r_opt, p_opt, pi_opt)
samples = generate_zinb_sample(r_opt, p_opt, pi_opt, num_samples)
return {
"Gene": gene,
"Optimal r": r_opt,
"Optimal p": p_opt,
"Optimal pi": pi_opt,
"Optimal mean": mean,
"Optimal variance": variance,
"samples": samples
}
def generate_zinb_sample(r, p, pi, n_samples=1):
"""
Generate samples from ZINB distribution using loop-based approach.
Args:
r: Size parameter
p: Probability parameter
pi: Zero-inflation parameter
n_samples: Number of samples to generate
Returns:
Array of generated samples
"""
data = np.zeros(n_samples)
for i in range(n_samples):
if np.random.random() < pi:
data[i] = 0
else:
data[i] = nbinom.rvs(r, p)
return data
def generate_zinb_sample_vec(r, p, pi, n_samples):
"""
Generate samples from ZINB distribution using vectorized approach.
Args:
r: Size parameter
p: Probability parameter
pi: Zero-inflation parameter
n_samples: Number of samples to generate
Returns:
Array of generated samples
"""
mask = np.random.random(n_samples) >= pi
data = np.zeros(n_samples)
data[mask] = nbinom.rvs(r, p, size=np.sum(mask))
return data
def create_sc_rate_df(ratios, counts):
"""
Create cumulative distribution DataFrame for single-cell expression bins.
Args:
ratios: Probability ratios for each expression value
counts: Value counts for expression levels
Returns:
DataFrame with start_rate, end_rate, and value_rate columns
"""
cumulative = np.cumsum(ratios)
start_rate = np.insert(cumulative[:-1], 0, 0)
end_rate = cumulative
return pd.DataFrame(
{
"start_rate": start_rate,
"end_rate": end_rate,
"value_rate": counts.index,
}
)
def plot_combined_umap(bulk_data, singlecell_data, scStylebulk_data,
drug="Erlotinib.png", figsize=(12, 10)):
"""
Generate a single UMAP plot combining bulk, single-cell, and scStyleBulk data.
Args:
bulk_data: DataFrame with bulk RNA-seq expression data (samples x genes)
singlecell_data: DataFrame with single-cell RNA-seq data (cells x genes)
scStylebulk_data: DataFrame with scStyleBulk data (samples x genes)
drug: Path to save the output figure
figsize: Figure size as tuple (width, height)
Returns:
umap_embedding: Combined UMAP coordinates for all data types
"""
# Align genes across all datasets
common_genes = list(set(bulk_data.columns) &
set(singlecell_data.columns) &
set(scStylebulk_data.columns))
bulk_aligned = bulk_data[common_genes]
sc_aligned = singlecell_data[common_genes]
scStyle_aligned = scStylebulk_data[common_genes]
print(f"Number of common genes: {len(common_genes)}")
print(f"Bulk samples: {bulk_aligned.shape[0]}")
print(f"Single-cell samples: {sc_aligned.shape[0]}")
print(f"scStyleBulk samples: {scStyle_aligned.shape[0]}")
# Create labels for each data type
bulk_labels = ['Bulk'] * bulk_aligned.shape[0]
sc_labels = ['Single-cell'] * sc_aligned.shape[0]
scStyle_labels = ['scStyleBulk'] * scStyle_aligned.shape[0]
# Combine all data
combined_data = pd.concat([bulk_aligned, sc_aligned, scStyle_aligned],
axis=0, ignore_index=True)
combined_labels = bulk_labels + sc_labels + scStyle_labels
# Standardize features
scaler = StandardScaler()
combined_scaled = scaler.fit_transform(combined_data)
# Apply UMAP
print("Computing UMAP embedding...")
reducer = umap.UMAP(n_neighbors=15, min_dist=0.1, n_components=2,
random_state=42, metric='euclidean')
umap_embedding = reducer.fit_transform(combined_scaled)
# Create plot
plt.figure(figsize=figsize)
# Define colors and markers for each data type
colors = {'Bulk': '#8da9cf', 'Single-cell': '#e7c583', 'scStyleBulk': '#c7dda6'}
markers = {'Bulk': 's', 'Single-cell': 'o', 'scStyleBulk': '^'}
sizes = {'Bulk': 80, 'Single-cell': 30, 'scStyleBulk': 80}
# Plot each data type
for data_type in ['Single-cell', 'Bulk', 'scStyleBulk']:
mask = [label == data_type for label in combined_labels]
plt.scatter(
umap_embedding[mask, 0],
umap_embedding[mask, 1],
c=colors[data_type],
marker=markers[data_type],
s=sizes[data_type],
alpha=0.5,
label=data_type,
edgecolors='black',
linewidths=0.5
)
plt.xlabel('UMAP 1', fontsize=14)
plt.ylabel('UMAP 2', fontsize=14)
plt.title('Combined UMAP: Bulk vs Single-cell vs scStyleBulk', fontsize=16, fontweight='bold')
plt.legend(loc='best', fontsize=12, frameon=True, shadow=True)
plt.grid(False)
plt.tight_layout()
# Save figure
plt.savefig(drug, dpi=300, bbox_inches='tight')
print(f"UMAP plot saved to: {drug}")
# Return embedding and labels for further analysis
return umap_embedding, combined_labels
# =============================================================================
# Main Pipeline
# =============================================================================
def main(args):
"""
Main execution pipeline:
1. Load bulk and single-cell RNA-seq data
2. Fit ZINB distributions to single-cell data
3. Generate simulated single-cell data
4. Transform bulk data into scStyleBulk format
Args:
args: Command-line arguments containing file paths and parameters
"""
# -------------------------------------------------------------------------
# Step 1: Load and preprocess data
# -------------------------------------------------------------------------
Bulk = pd.read_csv(f"{args.bulk_data}", index_col=0)
SingleCell = pd.read_csv(f"{args.sc_data}", index_col=0)
print("Load Bulk and Single-cell data")
print('Bulk: ', Bulk.shape)
print('Single-cell: ', SingleCell.shape)
num_cores = max(1, os.cpu_count() - 2)
print(f"Using {num_cores} cores for parallel processing")
com_genes = list(set(Bulk.columns) & set(SingleCell.columns))
Bulk_comgenes = Bulk[com_genes]
SingleCell_comgenes = SingleCell[com_genes].round()
num_samples = SingleCell_comgenes.shape[0]
Bulk_comgenes.to_csv(f"{args.save_path}/Bulk_{args.drug}_comgenes.csv")
SingleCell_comgenes.to_csv(f"{args.save_path}/SingleCell_{args.drug}_comgenes.csv")
# -------------------------------------------------------------------------
# Step 2: ZINB parameter estimation and sample generation
# -------------------------------------------------------------------------
print("Starting ZINB parameter estimation")
gene_args = [(gene, SingleCell_comgenes[gene].values, num_samples) for gene in com_genes]
all_results, all_samples = [], {}
with ProcessPoolExecutor(max_workers=num_cores) as executor:
for result in wrap_with_tqdm(executor, process_gene, gene_args, desc="Processing"):
if result is not None:
all_results.append({k: v for k, v in result.items() if k != "samples"})
all_samples[result["Gene"]] = result["samples"]
simulated_sc_data = pd.DataFrame(all_samples)
simulated_sc_data.to_csv(f"{args.save_path}/Simulated_singlecell_{args.drug}.csv")
print("Completed ZINB parameter estimation")
print(simulated_sc_data)
print("Bulk shape:", Bulk.shape)
print("Single-cell shape:", SingleCell.shape)
print("Simulated Single-cell shape:", simulated_sc_data.shape)
# -------------------------------------------------------------------------
# Step 3: Transform bulk RNA-seq into scStyleBulk
# -------------------------------------------------------------------------
print("Starting transformation of bulk RNA-seq into scStyleBulk")
z_Bulk = Bulk.apply(zscore, axis=0)
cdf_Bulk = z_Bulk.apply(norm.cdf)
all_genes = list(simulated_sc_data.keys())
all_values = {gene: pd.Series(value) for gene, value in simulated_sc_data.items()}
value_counts = {gene: values.value_counts().sort_index() for gene, values in all_values.items()}
value_ratios = {gene: counts / counts.sum() for gene, counts in value_counts.items()}
sc_rate_dfs = {gene: create_sc_rate_df(value_ratios[gene].values, value_counts[gene]) for gene in all_genes}
bins = {gene: df["end_rate"].values for gene, df in sc_rate_dfs.items()}
labels = {gene: df["value_rate"][1:].values for gene, df in sc_rate_dfs.items()}
new_tmp_df = pd.DataFrame(
{
gene: pd.cut(
cdf_Bulk[gene],
bins=bins[gene],
labels=labels[gene],
include_lowest=True,
)
for gene in all_genes
}
)
scStyleBulk = new_tmp_df.astype(float).fillna(0)
output_path = f"{args.save_path}/scStyleBulk_{args.drug}.csv"
scStyleBulk.to_csv(output_path)
print("Completed Transformation")
print("Saved scStyleBulk to:", output_path)
print(scStyleBulk)
plot_combined_umap(Bulk, SingleCell, scStyleBulk, drug=f"fig/{args.drug}.png")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Transform bulk RNA-seq data into single-cell-style expression profiles using ZINB modeling"
)
parser.add_argument("--drug", type=str, required=True,
help="Drug name")
parser.add_argument("--bulk_data", type=str, default="",
help="Path to bulk RNA-seq data CSV file")
parser.add_argument("--sc_data", type=str, default="",
help="Path to single-cell RNA-seq data CSV file")
parser.add_argument("--save_path", type=str, required=True,
help="Directory path to save output files")
args = parser.parse_args()
main(args)