-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakefile
executable file
·90 lines (70 loc) · 2.48 KB
/
Snakefile
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
import tifffile as tif
from scipy.io import loadmat
import os
import numpy as np
import pandas as pd
def parse_one_cell(image, mask, cell, channel, sample):
is_cell = mask == cell
x = np.where(is_cell)[0].mean()
y = np.where(is_cell)[1].mean()
size = is_cell.sum()
expression = image[is_cell].mean()
df = pd.DataFrame({
'sample': [sample],
'cell': [sample + "_" + str(cell)],
'x': [x],
'y': [y],
'size': [size],
'channel': [channel],
'expression': [expression]
})
df = df.set_index("cell")
return df
def parse_sample_single_channel(mask, image, channel, sample):
cell_ids = np.unique(np.reshape(mask, -1))
cell_ids = cell_ids[cell_ids != 0]
pds = [parse_one_cell(image, mask, cell, channel, sample) for cell in cell_ids]
return pd.concat(pds, axis=0)
def read_sample(mask, tiff_file, sample):
channel = tiff_file.split("/")[-1].replace(".tiff", "")
image = tif.imread(tiff_file)
return parse_sample_single_channel(mask, image, channel, sample)
samples = pd.read_csv("samples.txt", header=None)
samples = list(samples[samples.columns[0]])
channels = pd.read_csv("channels.txt", header=None)
channels = list(channels[channels.columns[0]])
rule all:
input:
expand("single-cell-data/{s}.csv", s=samples),
expand("single-cell-data/alt-masks/{alt_s}_{user}.csv",
alt_s = ['Cy1x5_32', 'Cy1x6_33', 'Cy1x8_35'], user = ['Catena', 'Jackson', 'Schulz'])
rule to_csv:
input:
expand("raw-data/{{s}}/{{s}}/{channel}.tiff",
channel=channels)
output:
"single-cell-data/{s}.csv"
run:
sample = wildcards.s
sample_short = sample.split("_")[0]
mask_file = f"raw-data/{sample}/CellProfiler/{sample_short}_Cells.mat"
mask = loadmat(mask_file)
mask = mask['Image']
files = input
df_list = [read_sample(mask, f, sample) for f in files]
df = pd.concat(df_list, axis=0)
df.to_csv(output[0])
rule to_csv_alt_masks:
input:
expand("raw-data/{{alt_s}}/{{alt_s}}/{channel}.tiff", channel=channels)
output:
"single-cell-data/alt-masks/{alt_s}_{user}.csv"
run:
sample = wildcards.alt_s
user = wildcards.user
mask_file = f"raw-data/alt_masks/{user}/{sample}_{user}_mask.tiff"
mask = tif.imread(mask_file)
files = input
df_list = [read_sample(mask, f, sample) for f in files]
df = pd.concat(df_list, axis=0)
df.to_csv(output[0])