forked from broadinstitute/lincs-cell-painting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_cells.py
174 lines (152 loc) · 5.01 KB
/
profile_cells.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
"""
Perform the image-based profiling pipeline to process data
"""
import os
import sys
import pathlib
import pandas as pd
from pycytominer import aggregate, annotate, normalize, feature_select, cyto_utils
from pycytominer.cyto_utils.cells import SingleCells
from profile_utils import get_args
sys.path.append("../utils")
from dose import recode_dose
# Load Command Line Arguments
args = get_args()
sql_file = args.sql_file
batch = args.batch
plate_name = args.plate_name
platemap_file = args.platemap_file
barcode_platemap_file = args.barcode_platemap_file
moa_file = args.moa_file
cell_count_dir = args.cell_count_dir
cell_id = args.cell_id
output_dir = args.output_dir
plate_col = args.plate_col # Default is "Image_Metadata_Plate"
well_col = args.well_col # Default is "Image_Metadata_Well"
# Initialize profile processing
os.makedirs(output_dir, exist_ok=True)
os.makedirs(cell_count_dir, exist_ok=True)
aggregate_method = "median"
norm_method = "mad_robustize"
compression = {"method": "gzip", "mtime": 1}
float_format = "%.5g"
strata = [plate_col, well_col]
feature_select_ops = [
"drop_na_columns",
"variance_threshold",
"correlation_threshold",
"blocklist",
]
primary_dose_mapping = [0.04, 0.12, 0.37, 1.11, 3.33, 10, 20]
# Define external metadata to add to annotation
moa_df = pd.read_csv(moa_file, sep="\t")
barcode_platemap_df = pd.read_csv(barcode_platemap_file).query(
"Assay_Plate_Barcode == @plate_name"
)
# Aggregate profiles
out_file = pathlib.PurePath(output_dir, f"{plate_name}.csv.gz")
sc = SingleCells(
file_or_conn=sql_file, strata=strata, aggregation_operation=aggregate_method
)
sc.aggregate_profiles(
output_file=out_file, float_format=float_format, compression_options=compression
)
# Count cells
count_file = pathlib.PurePath(cell_count_dir, f"{plate_name}_cell_count.csv")
cell_count_df = sc.count_cells()
cell_count_df.to_csv(count_file, sep=",", index=False)
del sc
# Annotate profiles - Level 3 Data
anno_file = pathlib.PurePath(output_dir, f"{plate_name}_augmented.csv.gz")
anno_df = annotate(
profiles=out_file,
platemap=platemap_file,
join_on=["Metadata_well_position", well_col],
format_broad_cmap=True,
external_metadata=moa_df,
external_join_left=["Metadata_broad_sample"],
external_join_right=["Metadata_broad_sample"],
cmap_args={"cell_id": cell_id, "perturbation_mode": "chemical"},
)
# Rename columns
anno_df = anno_df.rename(
{"Image_Metadata_Plate": "Metadata_Plate", "Image_Metadata_Well": "Metadata_Well"},
axis="columns",
)
# Add barcode platemap info
try:
anno_df = anno_df.assign(
Metadata_Assay_Plate_Barcode=barcode_platemap_df.Assay_Plate_Barcode.values[0],
Metadata_Plate_Map_Name=barcode_platemap_df.Plate_Map_Name.values[0],
Metadata_Batch_Number=barcode_platemap_df.Batch_Number.values[0],
Metadata_Batch_Date=barcode_platemap_df.Batch_Date.values[0],
)
except AttributeError:
anno_df = anno_df.assign(
Metadata_Assay_Plate_Barcode=barcode_platemap_df.Assay_Plate_Barcode.values[0],
Metadata_Plate_Map_Name=barcode_platemap_df.Plate_Map_Name.values[0],
)
# Add dose recoding information
anno_df = anno_df.assign(
Metadata_dose_recode=(
anno_df.Metadata_mmoles_per_liter.apply(
lambda x: recode_dose(x, primary_dose_mapping, return_level=True)
)
)
)
# Reoroder columns
metadata_cols = cyto_utils.infer_cp_features(anno_df, metadata=True)
cp_cols = cyto_utils.infer_cp_features(anno_df)
reindex_cols = metadata_cols + cp_cols
anno_df = anno_df.reindex(reindex_cols, axis="columns")
# Output annotated file
cyto_utils.output(
df=anno_df,
output_filename=anno_file,
float_format=float_format,
compression_options=compression,
)
# Normalize Profiles (DMSO Control) - Level 4A Data
norm_dmso_file = pathlib.PurePath(output_dir, f"{plate_name}_normalized_dmso.csv.gz")
normalize(
profiles=anno_df,
samples="Metadata_broad_sample == 'DMSO'",
method=norm_method,
output_file=norm_dmso_file,
float_format=float_format,
compression_options=compression,
)
# Normalize Profiles (Whole Plate) - Level 4A Data
norm_file = pathlib.PurePath(output_dir, f"{plate_name}_normalized.csv.gz")
normalize(
profiles=anno_df,
samples="all",
method=norm_method,
output_file=norm_file,
float_format=float_format,
compression_options=compression,
)
# Feature Selection (DMSO Control) - Level 4B Data
feat_dmso_file = pathlib.PurePath(
output_dir, f"{plate_name}_normalized_feature_select_dmso.csv.gz"
)
feature_select(
profiles=norm_dmso_file,
features="infer",
operation=feature_select_ops,
output_file=feat_dmso_file,
float_format=float_format,
compression_options=compression,
)
# Feature Selection (Whole Plate) - Level 4B Data
feat_file = pathlib.PurePath(
output_dir, f"{plate_name}_normalized_feature_select.csv.gz"
)
feature_select(
profiles=norm_file,
features="infer",
operation=feature_select_ops,
output_file=feat_file,
float_format=float_format,
compression_options=compression,
)