1414from package .core .tysserand .draw_per_sample import draw_per_sample
1515from package .utils .find_sample import find_sample
1616from package .core .tysserand .generate_cmap import generate_cmap
17+ from package .utils .find_sample_from_file import find_sample_from_file
1718
1819from mosna import mosna
1920
@@ -129,15 +130,16 @@ def main():
129130 save_dir .mkdir (exist_ok = True , parents = True )
130131
131132 args_list = [(
132- patient_sample ,
133+ node_file ,
133134 X ,
134135 Y ,
135136 'niches' ,
136137 c_map ,
137138 'delaunay' ,3 ,
138139 save_dir ,'None' ,
139140 kwargs ['id_level_1' ],kwargs ['id_level_2' ],
140- 'parquet' )for patient_sample in files ]
141+ 'parquet' , '/' .join (node_file .rsplit ('/' , 1 )[:- 1 ] + [node_file .rsplit ('/' , 1 )[- 1 ].replace ('nodes_' , 'edges_' , 1 )])
142+ ) for node_file in files ]
141143
142144 process_map (
143145 worker_draw_wrapper ,
@@ -148,50 +150,89 @@ def main():
148150 )
149151
150152 elif per_sample :
151-
153+ from package . core . NAS . niches_per_sample import niches_per_sample
152154 save_dir = save_dir = working_dir / "Niche_Analysis/Per_sample" / config ['Saving directory' ]
153155 save_dir .mkdir (exist_ok = True , parents = True )
154- kwargs = {
155- "method" : config .get ("method" , "NAS" ),
156- "net_dir" : net_dir ,
157- "save_dir" : save_dir ,
158- "temp_dir" : net_dir ,
159- "pheno_col" : config ["Phenotype column" ],
160- "uniq_phenotype" : uniq_phenotype ,
161- "stat_funcs" : config .get ("stat_funcs" , "default" ),
162- "stat_names" : config .get ("stat_names" , "default" ),
163- "id_level_1" : config .get ("Patient column name" , "patient" ),
164- "id_level_2" : config .get ("Sample column name" , "sample" ),
165156
166- ############# Clustering / réduction
167- "reducer_type" : config ["Per sample" ].get ("reducer_type" , "umap" ),
168- "clusterer_type" : config ["Per sample" ].get ("clusterer_type" , "leiden" ),
169- "n_neighbors" : int (config ["Per sample" ].get ("n_neighbors" , 15 )),
170- "metric" : config ["Per sample" ].get ("metric" , "euclidean" ),
171- "n_clusters" : int (config ["Per sample" ].get ("n_clusters" , 15 )),
172- "resolution" : float (config ["Per sample" ].get ("resolution" , 0.005 )),
173- "min_dist" : float (config ["Per sample" ].get ("min_dist" , 0.0 )),
174- "dim_clust" : int (config ["Per sample" ].get ("dim_clust" , 2 )),
175- "min_cluster_size" : float (config ["Per sample" ].get ("min_cluster_size" , 0.001 )),
176- "k_cluster" : int (config ["Per sample" ].get ("k_cluster" , 8 )),
177-
178- ########## Normalisation composition niches
179- "normalize" : config ["Per sample" ].get ("normalize" , "total" ),
180- }
181- from package .core .NAS .niches_per_sample import niches_per_sample
182- niches_per_sample (** kwargs )
183-
184- emit_qt_info ('[INFO] Niches found for each samples' )
185-
186- for path in save_dir .glob ("reducer-umap*" ):
187- if path .is_dir ():
188- shutil .rmtree (path )
189- save_config (save_dir , config )
190-
191- X , Y = config ['X coordinates column for niches' ], config ['Y coordinates column for niches' ]
192-
193- if X is not None and Y is not None :
194- pass
157+ files = find_sample (net_dir , 'parquet' , config .get ("Patient column name" , "patient" ), config .get ("Sample column name" , "sample" ))
158+ data_info = []
159+ for file in files :
160+ if config .get ("Sample column name" , "sample" ) is None :
161+ patient = find_sample_from_file (file , config .get ("Patient column name" , "patient" ), config .get ("Sample column name" , "sample" ))
162+ data_info .append ([patient ])
163+ else :
164+ patient , sample = find_sample_from_file (file , config .get ("Patient column name" , "patient" ), config .get ("Sample column name" , "sample" ))
165+ data_info .append ([patient , sample ])
166+
167+ for sample in data_info :
168+ if config .get ("Sample column name" , "sample" ) is None :
169+ patient_sample = f'{ config .get ("Patient column name" , "patient" )} -{ sample [0 ]} '
170+ else :
171+ patient_sample = f'{ config .get ("Patient column name" , "patient" )} -{ sample [0 ]} _{ config .get ("Sample column name" , "sample" )} -{ sample [1 ]} '
172+ save_dir_sample = save_dir / f'{ patient_sample } '
173+
174+ kwargs = {
175+ "method" : config .get ("method" , "NAS" ),
176+ "net_dir" : net_dir ,
177+ "save_dir" : save_dir_sample ,
178+ "data_info" : sample ,
179+ "pheno_col" : config ["Phenotype column" ],
180+ "uniq_phenotype" : uniq_phenotype ,
181+ "stat_funcs" : config .get ("stat_funcs" , "default" ),
182+ "stat_names" : config .get ("stat_names" , "default" ),
183+ "id_level_1" : config .get ("Patient column name" , "patient" ),
184+ "id_level_2" : config .get ("Sample column name" , "sample" ),
185+
186+ ############# Clustering / réduction
187+ "reducer_type" : config ["Per sample" ].get ("reducer_type" , "umap" ),
188+ "clusterer_type" : config ["Per sample" ].get ("clusterer_type" , "leiden" ),
189+ "n_neighbors" : int (config ["Per sample" ].get ("n_neighbors" , 15 )),
190+ "metric" : config ["Per sample" ].get ("metric" , "euclidean" ),
191+ "n_clusters" : int (config ["Per sample" ].get ("n_clusters" , 15 )),
192+ "resolution" : float (config ["Per sample" ].get ("resolution" , 0.005 )),
193+ "min_dist" : float (config ["Per sample" ].get ("min_dist" , 0.0 )),
194+ "dim_clust" : int (config ["Per sample" ].get ("dim_clust" , 2 )),
195+ "min_cluster_size" : float (config ["Per sample" ].get ("min_cluster_size" , 0.001 )),
196+ "k_cluster" : int (config ["Per sample" ].get ("k_cluster" , 8 )),
197+
198+ ########## Normalisation composition niches
199+ "normalize" : config ["Per sample" ].get ("normalize" , "total" ),
200+ }
201+
202+ niches_per_sample (** kwargs )
203+
204+ emit_qt_info ('[INFO] Niches found for each samples' )
205+
206+ for path in save_dir_sample .glob ("reducer-umap*" ):
207+ if path .is_dir ():
208+ shutil .rmtree (path )
209+ save_config (save_dir_sample , config )
210+
211+ X , Y = config ['X coordinates column for niches' ], config ['Y coordinates column for niches' ]
212+
213+ if X is not None and Y is not None :
214+ c_map = generate_cmap (net_dir , 'niche' , 'parquet' , kwargs ['id_level_1' ], kwargs ['id_level_2' ])
215+ files = find_sample (net_dir , 'parquet' , kwargs ['id_level_1' ], kwargs ['id_level_2' ])
216+ cpu_max = verif_cpu (config ['CPU' ], len (files ))
217+
218+ save_dir_sample_tysserand = save_dir_sample / 'Tysserand_Network_Niches'
219+ save_dir_sample_tysserand .mkdir (exist_ok = True , parents = True )
220+
221+ node_file = net_dir / f'nodes_{ patient_sample } .parquet'
222+ edge_file = net_dir / f'edges_{ patient_sample } .parquet'
223+
224+ args_list = [
225+ node_file ,
226+ X ,
227+ Y ,
228+ 'niches' ,
229+ c_map ,
230+ 'delaunay' ,3 ,
231+ save_dir_sample_tysserand ,'None' ,
232+ kwargs ['id_level_1' ],kwargs ['id_level_2' ],
233+ 'parquet' ,
234+ edge_file ]
235+ draw_per_sample (args_list )
195236
196237if __name__ == '__main__' :
197238 main ()
0 commit comments