22import os
33import imageio .v3 as imageio
44from glob import glob
5+ from pathlib import Path
56
67import matplotlib
78import matplotlib .pyplot as plt
89import numpy as np
910import pandas as pd
1011from matplotlib import cm , colors
1112
12- from util import sliding_runlength_sum , frequency_mapping , SYNAPSE_DIR_ROOT
13+ from util import sliding_runlength_sum , frequency_mapping , SYNAPSE_DIR_ROOT , to_alias
1314
1415INPUT_ROOT = "/home/pape/Work/my_projects/flamingo-tools/scripts/M_LR_000227_R/scale3"
1516
@@ -103,12 +104,12 @@ def fig_03c_rl(save_path, plot=False):
103104def fig_03c_octave (save_path , plot = False ):
104105 ihc_version = "ihc_counts_v4c"
105106 tables = glob (os .path .join (SYNAPSE_DIR_ROOT , ihc_version , "ihc_count_M_LR*.tsv" ))
106- assert len (tables ) == 4
107+ assert len (tables ) == 4 , len ( tables )
107108
108109 result = {"cochlea" : [], "octave_band" : [], "value" : []}
109110 for tab_path in tables :
110- # TODO map to alias
111- alias = os . path . basename ( tab_path )[ 10 : - 4 ]. replace ( "_" , "" ). replace ( "0" , "" )
111+ cochlea = Path ( tab_path ). stem . lstrip ( "ihc_count" )
112+ alias = to_alias ( cochlea )
112113 tab = pd .read_csv (tab_path , sep = "\t " )
113114 freq = tab ["frequency" ].values
114115 syn_count = tab ["synapse_count" ].values
@@ -143,33 +144,67 @@ def fig_03c_octave(save_path, plot=False):
143144 plt .close ()
144145
145146
146- def fig_03d (save_path , plot , print_stats = True ):
147+ def fig_03d_fraction (save_path , plot ):
147148 result_folder = "../measurements/subtype_analysis"
148149 files = glob (os .path .join (result_folder , "*.tsv" ))
149150
151+ # FIXME
152+ analysis = {
153+ "M_AMD_N62_L" : ["CR" , "Calb1" ],
154+ "M_LR_000214_L" : ["CR" ],
155+ }
156+
157+ results = {"type" : [], "fraction" : [], "cochlea" : []}
150158 for ff in files :
151159 fname = os .path .basename (ff )
152160 cochlea = fname [:- len ("_subtype_analysis.tsv" )]
161+
162+ if cochlea not in analysis :
163+ continue
164+
153165 table = pd .read_csv (ff , sep = "\t " )
154166
155167 subtype_table = table [[col for col in table .columns if col .startswith ("is_" )]]
156168 assert subtype_table .shape [1 ] == 2
157169 n_sgns = len (subtype_table )
158170
159- if print_stats :
160- print (cochlea )
161- for col in subtype_table .columns :
162- vals = table [col ].values
163- subtype = col [3 :]
164- n_subtype = vals .sum ()
165- channel = TYPE_TO_CHANNEL [subtype ]
166- print (
167- f"{ subtype } ({ channel } ):" , n_subtype , "/" , n_sgns ,
168- f"({ np .round (float (n_subtype ) / n_sgns * 100 , 2 )} %)"
169- )
171+ print (cochlea )
172+ for col in subtype_table .columns :
173+ vals = table [col ].values
174+ subtype = col [3 :]
175+ channel = TYPE_TO_CHANNEL [subtype ]
176+ if channel not in analysis [cochlea ]:
177+ continue
178+ n_subtype = vals .sum ()
179+ subtype_fraction = np .round (float (n_subtype ) / n_sgns * 100 , 2 )
180+ name = f"{ subtype } ({ channel } )"
181+ print ("{name}:" , n_subtype , "/" , n_sgns , f"({ subtype_fraction } %)" )
182+
183+ results ["type" ].append (name )
184+ results ["fraction" ].append (subtype_fraction )
185+ results ["cochlea" ].append (cochlea )
186+
187+ # coexpr = np.logical_and(subtype_table.iloc[:, 0].values, subtype_table.iloc[:, 1].values)
188+ # print("Co-expression:", coexpr.sum())
189+
190+ results = pd .DataFrame (results )
191+ fig , ax = plt .subplots ()
192+ for cochlea , group in results .groupby ("cochlea" ):
193+ ax .scatter (group ["type" ], group ["fraction" ], label = cochlea )
194+ ax .set_ylabel ("Fraction" )
195+ ax .set_xlabel ("Type" )
196+ ax .legend (title = "Cochlea ID" )
197+
198+ plt .savefig (save_path , bbox_inches = "tight" , pad_inches = 0.1 , dpi = png_dpi )
199+ if plot :
200+ plt .show ()
201+ else :
202+ plt .close ()
203+
170204
171- coexpr = np .logical_and (subtype_table .iloc [:, 0 ].values , subtype_table .iloc [:, 1 ].values )
172- print ("Co-expression:" , coexpr .sum ())
205+ # TODO
206+ def fig_03d_octave (save_path , plot ):
207+ pass
173208
174209
175210def main ():
@@ -181,15 +216,16 @@ def main():
181216 os .makedirs (args .figure_dir , exist_ok = True )
182217
183218 # Panel A: Tonotopic mapping of SGNs and IHCs (rendering in napari + heatmap)
184- # fig_03a(save_path=os.path.join(args.figure_dir, "fig_03a_cmap.png"), plot=args.plot, plot_napari=True)
219+ fig_03a (save_path = os .path .join (args .figure_dir , "fig_03a_cmap.png" ), plot = args .plot , plot_napari = True )
185220
186221 # Panel C: Spatial distribution of synapses across the cochlea.
187222 # We have two options: running sum over the runlength or per octave band
188- # fig_03c_rl(save_path=os.path.join(args.figure_dir, "fig_03c_runlength.png"), plot=args.plot)
223+ fig_03c_rl (save_path = os .path .join (args .figure_dir , "fig_03c_runlength.png" ), plot = args .plot )
189224 fig_03c_octave (save_path = os .path .join (args .figure_dir , "fig_03c_octave.png" ), plot = args .plot )
190225
191226 # Panel D: Spatial distribution of SGN sub-types.
192- fig_03d (save_path = os .path .join (args .figure_dir , "fig_03d.png" ), plot = args .plot )
227+ fig_03d_fraction (save_path = os .path .join (args .figure_dir , "fig_03d_fraction.png" ), plot = args .plot )
228+ fig_03d_octave (save_path = os .path .join (args .figure_dir , "fig_03d_octave.png" ), plot = args .plot )
193229
194230
195231if __name__ == "__main__" :
0 commit comments