@@ -95,7 +95,9 @@ def count(
9595 return adata
9696
9797
98- def get_edges (adata : AnnData , L_bool : np .array , R_bool : np .array , sig_bool : np .array ):
98+ def get_edges (
99+ adata : AnnData , L_bool : np .ndarray , R_bool : np .ndarray , sig_bool : np .ndarray
100+ ):
99101 """Gets a list edges representing significant interactions.
100102
101103 Parameters
@@ -116,14 +118,14 @@ def get_edges(adata: AnnData, L_bool: np.array, R_bool: np.array, sig_bool: np.a
116118 interactions between spots.
117119 """
118120 # Getting the neighbourhoods #
119- neighbours , neighbourhood_bcs , neighbourhood_indices = get_neighbourhoods (adata )
121+ neighbourhood_bcs , neighbourhood_indices = get_neighbourhoods (adata )
120122
121123 # Getting the edges to draw in-between #
122124 L_spot_indices = np .where (np .logical_and (L_bool , sig_bool ))[0 ]
123125 R_spot_indices = np .where (np .logical_and (R_bool , sig_bool ))[0 ]
124126
125127 # To keep the get_between_spot_edge_array function happy #
126- cell_data = np .ones ((1 , len (sig_bool )))[0 , :].astype (np .float_ )
128+ cell_data = np .ones ((1 , len (sig_bool )))[0 , :].astype (np .float64 )
127129
128130 # Retrieving the edges #
129131 gene_bools = [R_bool , L_bool ]
@@ -162,12 +164,8 @@ def count_interactions(
162164):
163165 """Counts the interactions."""
164166 # Getting minimal information necessary for the counting #
165- (
166- spot_bcs ,
167- cell_data ,
168- neighbourhood_bcs ,
169- neighbourhood_indices ,
170- ) = get_data_for_counting (adata , use_label , mix_mode , all_set )
167+ cell_data = get_data_for_counting (adata , use_label , mix_mode , all_set )
168+ neighbourhood_bcs , neighbourhood_indices = get_neighbourhoods (adata )
171169
172170 # if trans_dir, rows are transmitter cell, cols receiver, otherwise reverse.
173171 int_matrix = np .zeros ((len (all_set ), len (all_set )), dtype = int )
@@ -186,7 +184,7 @@ def count_interactions(
186184 A_gene1_sig_indices = np .where (A_gene1_sig_bool )[0 ]
187185
188186 for j , cell_B in enumerate (all_set ): # receiver if trans_dir else transmitter
189- cellA_cellB_counts = len (
187+ cell_a_cell_b_counts = len (
190188 edge_core (
191189 cell_data ,
192190 j ,
@@ -197,7 +195,7 @@ def count_interactions(
197195 cutoff = cell_prop_cutoff ,
198196 )
199197 )
200- int_matrix [i , j ] = cellA_cellB_counts
198+ int_matrix [i , j ] = cell_a_cell_b_counts
201199
202200 return int_matrix if trans_dir else int_matrix .transpose ()
203201
@@ -207,7 +205,6 @@ def get_interaction_pvals(
207205 int_matrix ,
208206 n_perms ,
209207 cell_data ,
210- neighbourhood_bcs ,
211208 neighbourhood_indices ,
212209 all_set ,
213210 sig_bool ,
@@ -217,14 +214,20 @@ def get_interaction_pvals(
217214):
218215 """Gets the p-values for the interaction counts."""
219216
217+ # Counting how many times permutation of spots cell data creates interaction
218+ # counts greater than that observed, in order to calculate p-values.
220219 shape_ = (n_perms , int_matrix .shape [0 ], int_matrix .shape [1 ])
220+ # Storing the instances where the count is greater randomly for each perm.
221+ # Allows for embarassing parallelisation.
221222 greater_counts = np .zeros (shape_ , dtype = np .int64 )
222223 indices = np .zeros ((cell_data .shape [0 ]), dtype = np .int64 )
223224 for i in range (cell_data .shape [0 ]):
224225 indices [i ] = i
225226
227+ # If dealing with discrete data, no need to randomise columns indendently #
226228 discrete = np .all (np .logical_or (cell_data == 0 , cell_data == 1 ))
227229 for i in prange (n_perms ):
230+ # Permuting the cell data by swapping between spots for each column #
228231 if not discrete :
229232 perm_data = cell_data .copy ()
230233 for j in range (cell_data .shape [1 ]):
@@ -234,6 +237,7 @@ def get_interaction_pvals(
234237 rand_indices = np .random .choice (indices , cell_data .shape [0 ], False )
235238 perm_data = cell_data [rand_indices , :]
236239
240+ # Calculating interactions for permuted labels #
237241 perm_matrix = get_interaction_matrix (
238242 perm_data ,
239243 neighbourhood_indices ,
@@ -311,57 +315,6 @@ def get_interaction_matrix(
311315 return int_matrix
312316
313317
314- @njit
315- def get_interactions (
316- cell_data ,
317- neighbourhood_bcs ,
318- neighbourhood_indices ,
319- all_set ,
320- sig_bool ,
321- gene1_bool ,
322- gene2_bool ,
323- cell_prop_cutoff = None ,
324- ):
325- """ Gets spot edges between cell types where the first cell type fits \
326- criteria of gene1_bool, & second second cell type of gene2_bool.
327- """
328-
329- # Creating list of lists to store edges for respective cell types #
330- interaction_edges = List ()
331-
332- # Now retrieving the interaction edges #
333- for i in range (all_set .shape [0 ]):
334- # Determining which spots have cell type A #
335- A_bool_2 = cell_data [:, i ] > cell_prop_cutoff
336- A_gene1_bool = np .logical_and (A_bool_2 , gene1_bool )
337-
338- A_gene1_sig_bool = np .logical_and (A_gene1_bool , sig_bool )
339- n_true = A_gene1_sig_bool .sum ()
340- A_gene1_sig_indices = np .zeros ((1 , n_true ), dtype = np .int32 )[
341- 0 , :
342- ] # np.where(A_gene1_sig_bool)[0]
343- index = 0
344- for k in range (A_gene1_sig_bool .shape [0 ]):
345- if A_gene1_sig_bool [k ]:
346- A_gene1_sig_indices [index ] = k
347- index += 1
348-
349- for j in range (all_set .shape [0 ]):
350- edge_list = edge_core (
351- cell_data ,
352- j ,
353- neighbourhood_bcs ,
354- neighbourhood_indices ,
355- spot_indices = A_gene1_sig_indices ,
356- neigh_bool = gene2_bool ,
357- cutoff = cell_prop_cutoff ,
358- )
359-
360- interaction_edges .append (edge_list )
361-
362- return interaction_edges
363-
364-
365318def create_grids (adata : AnnData , num_row : int , num_col : int , radius : int = 1 ):
366319 """Generate screening grids across the tissue sample
367320 Parameters
@@ -472,20 +425,20 @@ def count_grid(
472425@jit (parallel = True , forceobj = True )
473426def grid_parallel (
474427 grid_coords : np .ndarray ,
475- xedges : np .array ,
476- yedges : np .array ,
428+ xedges : np .ndarray ,
429+ yedges : np .ndarray ,
477430 n_row : int ,
478431 n_col : int ,
479- xs : np .array ,
480- ys : np .array ,
481- cell_bcs : np .array ,
482- grid_cell_counts : np .array ,
432+ xs : np .ndarray ,
433+ ys : np .ndarray ,
434+ cell_bcs : np .ndarray ,
435+ grid_cell_counts : np .ndarray ,
483436 grid_expr : np .ndarray ,
484437 cell_expr : np .ndarray ,
485438 use_label_bool : bool ,
486- cell_labels : np .array ,
439+ cell_labels : np .ndarray ,
487440 cell_info : np .ndarray ,
488- cell_set : np .array ,
441+ cell_set : np .ndarray ,
489442):
490443 """Grids the gene expression information."""
491444 # generate grids from top to bottom and left to right
0 commit comments