@@ -191,86 +191,65 @@ def plot_cell_gene_corr(
191191 dataDF = dataDF .subtract (X .var ["means" ].values )
192192 dataDF [hue ] = X .obs [hue ].values
193193 dataDF ["Cell Type" ] = X .obs [cellType ].values
194- alpha = 0.3
195194
196- dataDF = dataDF .groupby ([hue , "Cell Type" ], observed = True ).mean ().reset_index ()
197- alpha = 1
195+ meanDF = dataDF .groupby ([hue , "Cell Type" ], observed = True ).mean ().reset_index ()
196+ pivoted = meanDF . pivot ( index = hue , columns = "Cell Type" , values = genes )
198197
199- corrDF = pd .DataFrame ()
200- for cond in dataDF [hue ].unique ():
201- cell_gene1 = dataDF .loc [
202- (dataDF [hue ] == cond ) & (dataDF ["Cell Type" ] == cells [0 ])
203- ][genes [0 ]].values
204- cell_gene2 = dataDF .loc [
205- (dataDF [hue ] == cond ) & (dataDF ["Cell Type" ] == cells [1 ])
206- ][genes [1 ]].values
207- corrDF = pd .concat (
208- [
209- corrDF ,
210- pd .DataFrame (
211- {
212- hue : cond ,
213- cells [0 ] + " " + genes [0 ]: cell_gene1 ,
214- cells [1 ] + " " + genes [1 ]: cell_gene2 ,
215- }
216- ),
217- ]
218- )
198+ col1 = (genes [0 ], cells [0 ])
199+ col2 = (genes [1 ], cells [1 ])
200+
201+ x_name = f"{ cells [0 ]} { genes [0 ]} "
202+ y_name = f"{ cells [1 ]} { genes [1 ]} "
203+
204+ if col1 in pivoted .columns and col2 in pivoted .columns :
205+ corrDF = pd .DataFrame (
206+ {
207+ hue : pivoted .index ,
208+ x_name : pivoted [col1 ].values ,
209+ y_name : pivoted [col2 ].values ,
210+ }
211+ ).dropna ()
212+ else :
213+ corrDF = pd .DataFrame (columns = [hue , x_name , y_name ])
219214
220215 if unique is not None :
221216 corrDF [hue ] = corrDF [hue ].astype (str )
222217 corrDF .loc [~ corrDF [hue ].isin (unique ), hue ] = "Other"
223218
224219 sns .scatterplot (
225220 data = corrDF ,
226- x = cells [ 0 ] + " " + genes [ 0 ] ,
227- y = cells [ 1 ] + " " + genes [ 1 ] ,
221+ x = x_name ,
222+ y = y_name ,
228223 hue = hue ,
229224 ax = ax ,
230- alpha = alpha ,
225+ alpha = 1.0 ,
231226 )
232227
233228
234229def cell_count_perc_df (X , celltype = "Cell Type" ):
235230 """Returns DF with cell counts and percentages for experiment"""
236-
237231 grouping = [celltype , "Condition" ]
238-
239232 df = X .obs [grouping ].reset_index (drop = True )
240233
241- dfCond = (
242- df .groupby (["Condition" ], observed = True ).size ().reset_index (name = "Cell Count" )
243- )
244234 dfCellType = (
245235 df .groupby (grouping , observed = True ).size ().reset_index (name = "Cell Count" )
246236 )
247237 dfCellType ["Cell Count" ] = dfCellType ["Cell Count" ].astype ("float" )
248238
249- dfCellType ["Cell Type Percentage" ] = 0.0
250- for cond in np .unique (df ["Condition" ]):
251- dfCellType .loc [dfCellType ["Condition" ] == cond , "Cell Type Percentage" ] = (
252- 100
253- * dfCellType .loc [dfCellType ["Condition" ] == cond , "Cell Count" ].to_numpy ()
254- / dfCond .loc [dfCond ["Condition" ] == cond ]["Cell Count" ].to_numpy ()
255- )
256-
257- dfCellType .rename (columns = {celltype : "Cell Type" }, inplace = True )
239+ dfCellType ["Cell Type Percentage" ] = (
240+ 100
241+ * dfCellType ["Cell Count" ]
242+ / dfCellType .groupby ("Condition" )["Cell Count" ].transform ("sum" )
243+ )
258244
245+ dfCellType = dfCellType .rename (columns = {celltype : "Cell Type" })
259246 return dfCellType
260247
261248
262249def cell_count_perc_lupus_df (X , celltype = "Cell Type" ):
263- """Returns DF with cell counts and percentages for experiment"""
264- grouping_all = [
265- celltype ,
266- "Condition" ,
267- "SLE_status" ,
268- "Processing_Cohort" ,
269- "condition_unique_idxs" ,
270- ]
271- grouping = [celltype , "Condition" ]
250+ """Returns DF with cell counts and percentages for experiment with Lupus metadata"""
251+ dfCellType = cell_count_perc_df (X , celltype = celltype )
272252
273- df = X .obs [grouping_all ].reset_index (drop = True )
274253 status_mapping = X .obs .groupby ("Condition" , observed = False )["SLE_status" ].first ()
275254 cohort_mapping = X .obs .groupby ("Condition" , observed = False )[
276255 "Processing_Cohort"
@@ -279,37 +258,18 @@ def cell_count_perc_lupus_df(X, celltype="Cell Type"):
279258 "condition_unique_idxs"
280259 ].first ()
281260
282- dfCond = (
283- df .groupby (["Condition" ], observed = True ).size ().reset_index (name = "Cell Count" )
284- )
285- dfCellType = (
286- df .groupby (grouping , observed = True ).size ().reset_index (name = "Cell Count" )
287- )
288- dfCellType ["Cell Count" ] = dfCellType ["Cell Count" ].astype ("float" )
289-
290- dfCellType ["Cell Type Percentage" ] = 0.0
291- for cond in np .unique (df ["Condition" ]):
292- dfCellType .loc [dfCellType ["Condition" ] == cond , "Cell Type Percentage" ] = (
293- 100
294- * dfCellType .loc [dfCellType ["Condition" ] == cond , "Cell Count" ].to_numpy ()
295- / dfCond .loc [dfCond ["Condition" ] == cond ]["Cell Count" ].to_numpy ()
296- )
297-
298261 dfCellType ["SLE_status" ] = dfCellType ["Condition" ].map (status_mapping )
299262 dfCellType ["Processing_Cohort" ] = dfCellType ["Condition" ].map (cohort_mapping )
300263 dfCellType ["condition_unique_idxs" ] = dfCellType ["Condition" ].map (idx_mapping )
301- dfCellType .rename (columns = {celltype : "Cell Type" }, inplace = True )
302264
303265 return dfCellType
304266
305267
306268def rotate_xaxis (ax , rotation = 90 ):
307- """Rotates text by 90 degrees for x-axis"""
308- ax .set_xticks (ax .get_xticks ())
309- ax .set_xticklabels (labels = ax .get_xticklabels (), rotation = rotation )
269+ """Rotates text for x-axis"""
270+ ax .tick_params (axis = "x" , rotation = rotation )
310271
311272
312273def rotate_yaxis (ax , rotation = 90 ):
313- """Rotates text by 90 degrees for y-axis"""
314- ax .set_yticks (ax .get_yticks ())
315- ax .set_yticklabels (labels = ax .get_yticklabels (), rotation = rotation )
274+ """Rotates text for y-axis"""
275+ ax .tick_params (axis = "y" , rotation = rotation )
0 commit comments