-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.py
More file actions
147 lines (133 loc) · 4.8 KB
/
Copy pathrun.py
File metadata and controls
147 lines (133 loc) · 4.8 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
import argparse
import os
from mftools import config
from mftools import fileio
from mftools import segmentation
from mftools import util
from mftools import stats
from mftools import barcodes
from mftools import cellgene
from mftools import plotting
def create_barcode_table(merlin_result, masks, cell_links):
codebook = merlin_result.load_codebook()
bcs = barcodes.make_table(merlin_result, codebook)
per_bit_error = barcodes.set_barcode_stats(merlin_result, bcs, config.get("barcode_colors"))
plotting.exact_vs_corrected()
per_gene_error = barcodes.per_gene_error(bcs)
plotting.confidence_ratios(per_gene_error)
if per_bit_error is not None:
plotting.per_bit_error_bar(per_bit_error, config.get("barcode_colors"))
plotting.per_bit_error_line(per_bit_error, config.get("barcode_colors"))
plotting.per_hyb_error(per_bit_error)
plotting.per_color_error(per_bit_error, config.get("barcode_colors"))
# per_fov_error = barcodes.per_fov_error(bcs)
# plotting.fov_error_bar(per_fov_error)
# plotting.fov_error_spatial(per_fov_error, positions)
# plotting.spatial_transcripts_per_fov(bcs, positions)
barcodes.mark_barcodes_in_overlaps(bcs, masks.positions.find_fov_overlaps(get_trim=True))
barcodes.assign_to_cells(bcs, masks)
barcodes.calculate_global_coordinates(
bcs, masks.positions.positions
) # Replace with util.fov_to_global_coordinates
barcodes.link_cell_ids(bcs, cell_links)
for dataset in config.get("reference_counts"):
plotting.rnaseq_correlation(bcs, dataset)
return bcs
def analyze_experiment():
stats.savefile = config.path("stats.json")
merlin_result = fileio.MerlinOutput(config.get("merlin_folder"))
if config.has("image_folder"):
imagedata = fileio.ImageDataset(
config.get("image_folder"),
data_organization=merlin_result.load_data_organization(),
segdict={"hyb": 0, "frame": 53},
)
output = fileio.MerfishAnalysis(config.get("output_folder"))
masks = segmentation.CellSegmentation(
config.get("segmentation_folder"),
output=output,
positions=merlin_result.load_fov_positions(),
imagedata=imagedata,
)
positions = merlin_result.load_fov_positions()
n_fovs = len(positions)
stats.set("FOVs", n_fovs)
# f os.path.exists(config.path("cell_metadata.csv")):
# celldata = fileio.load_cell_metadata(config.path("cell_metadata.csv"))
# cell_links = fileio.load_cell_links(config.path("cell_links.txt"))
# else:
celldata = masks.metadata
cell_links = masks.linked_cells
stats.set("Segmented cells", len(celldata))
try:
bcs = output.load_barcode_table()
except FileNotFoundError:
bcs = create_barcode_table(merlin_result, masks, cell_links)
output.save_barcode_table(bcs)
try:
counts = output.load_cell_by_gene_table()
except FileNotFoundError:
counts = barcodes.create_cell_by_gene_table(bcs)
output.save_cell_by_gene_table(counts)
plotting.counts_per_cell_histogram(counts)
plotting.genes_detected_per_cell_histogram(counts)
codebook = merlin_result.load_codebook()
adata = cellgene.create_scanpy_object(counts, celldata, positions, codebook)
adata.write(config.path("scanpy_object.h5ad"))
cellgene.normalize(adata)
n_pcs = cellgene.optimize_number_PCs(adata)
cellgene.cluster_cells(adata, n_pcs)
adata.write(config.path("scanpy_object.h5ad"))
plotting.umap_clusters(adata)
plotting.spatial_cell_clusters(adata)
def main():
parser = argparse.ArgumentParser(description="Run the MERFISH analysis pipeline.")
parser.add_argument(
"-e",
"--experiment",
help="The name of the experiment",
dest="experiment_name",
type=str,
required=True,
)
parser.add_argument(
"-a",
"--analysis_root",
help="Location of MERlin analysis directories",
dest="analysis_root",
type=str,
)
parser.add_argument(
"-d",
"--data_root",
help="Location of MERlin raw data folders",
dest="data_root",
type=str,
)
parser.add_argument(
"-r",
"--rerun",
help="Force rerun all steps, overwriting existing files",
dest="rerun",
action="store_true",
)
parser.add_argument(
"-c",
"--config_file",
help="Path to the configuration file in JSON format",
dest="config_file",
type=str,
)
parser.add_argument(
"-o",
"--output",
help="Sub-folder to save all files produced by the pipeline",
dest="result_folder",
type=str,
default="",
)
args = parser.parse_args()
config.load(args)
analyze_experiment()
if __name__ == "__main__":
main()