forked from cytomining/pycytominer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotate_custom.py
127 lines (106 loc) · 3.9 KB
/
annotate_custom.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
import numpy as np
def annotate_cmap(
annotated, annotate_join_on, cell_id="unknown", perturbation_mode="none"
):
"""Annotates data frame with custom options according to CMAP specifications
Parameters
----------
annotated : pandas.core.frame.DataFrame
DataFrame of profiles.
annotate_join_on : str
Typically the well metadata, but how to join external data
cell_id : str, default "unknown"
provide a string to annotate cell id column
perturbation_mode : str, default "none"
How to annotate CMAP specific data (options = ["chemical" , "genetic"])
Returns
-------
annotated
CMAP annotated data
"""
pert_opts = ["none", "chemical", "genetic"]
assert ( # noqa: S101
perturbation_mode in pert_opts
), f"perturbation mode must be one of {pert_opts}"
assert ( # noqa: S101
"Metadata_broad_sample" in annotated.columns
), "Are you sure this is a CMAP file? 'Metadata_broad_sample column not found.'"
annotated = annotated.assign(
Metadata_pert_id=annotated.Metadata_broad_sample.str.extract(
r"(BRD[-N][A-Z0-9]+)"
),
Metadata_pert_mfc_id=annotated.Metadata_broad_sample,
Metadata_pert_well=annotated.loc[:, annotate_join_on],
Metadata_pert_id_vendor="",
)
if "Metadata_pert_iname" in annotated.columns:
annotated = annotated.assign(
Metadata_pert_mfc_desc=annotated.Metadata_pert_iname,
Metadata_pert_name=annotated.Metadata_pert_iname,
)
if "Metadata_cell_id" not in annotated.columns:
annotated = annotated.assign(Metadata_cell_id=cell_id)
if perturbation_mode == "chemical":
annotated = annotated.assign(
Metadata_broad_sample_type=[
"control" if x in ["DMSO", np.nan] else "trt"
for x in annotated.Metadata_broad_sample
]
)
# Generate Metadata_broad_sample column
annotated.loc[
annotated.Metadata_broad_sample_type == "control",
"Metadata_broad_sample",
] = "DMSO"
annotated.loc[
annotated.Metadata_broad_sample == "empty", "Metadata_broad_sample_type"
] = "empty"
if "Metadata_mmoles_per_liter" in annotated.columns:
annotated.loc[
annotated.Metadata_broad_sample_type == "control",
"Metadata_mmoles_per_liter",
] = 0
if "Metadata_solvent" in annotated.columns:
annotated = annotated.assign(
Metadata_pert_vehicle=annotated.Metadata_solvent
)
if "Metadata_mg_per_ml" in annotated.columns:
annotated.loc[
annotated.Metadata_broad_sample_type == "control",
"Metadata_mg_per_ml",
] = 0
if perturbation_mode == "genetic" and "Metadata_pert_name" in annotated.columns:
annotated = annotated.assign(
Metadata_broad_sample_type=[
"control" if x == "EMPTY" else "trt"
for x in annotated.Metadata_pert_name
]
)
if "Metadata_broad_sample_type" in annotated.columns:
annotated = annotated.assign(
Metadata_pert_type=annotated.Metadata_broad_sample_type
)
else:
annotated = annotated.assign(
Metadata_pert_type="", Metadata_broad_sample_type=""
)
return annotated
def cp_clean(profiles):
"""Specifically clean certain column names derived from different CellProfiler versions
Parameters
----------
profiles : pandas.core.frame.DataFrame
DataFrame of profiles.
Returns
-------
profiles
Renamed to standard metadata
"""
profiles = profiles.rename(
{
"Image_Metadata_Plate": "Metadata_Plate",
"Image_Metadata_Well": "Metadata_Well",
},
axis="columns",
)
return profiles