Improved T-cell Receptor Antigen Pairing

public public 1yr ago 0 bookmarks

This repository contains the code to identify ITRAP filters for single-cell immune profiling data.

License

ITRAP is developed by Morten Nielsen's group at the Technical University of Denmark (DTU). ITRAP code and data can be used freely by academic groups for non-commercial purposes. If you plan to use ITRAP or any data provided with the script in any for-profit application, you are required to obtain a separate license (contact Morten Nielsen, [email protected]).

For scientific questions, please contact Morten Nielsen ([email protected]).

Run ITRAP

Designed with snakemake workflow (v5.7.4)

snakemake --config exp=exp13 run=run1 --use-conda

Run individual steps

Each script may also be run by command line. For help run scripts/ -h The Snakefile links the required environments for each script. The environment files are found in envs/.

Requirements

Anaconda or other Python source (Python 3.7.3) Specific requirements for each script are logged in envs/

Data

The pipeline expects a TSV file indexed by 10x barcodes, i.e. GEMs, containing features of TCR, pMHC, & cell hashing. These features may be generated using Cellranger multi .

The pipeline expects database of TCR-pMHC annotated sequences, which is stored in tools/tcr_dbs.csv.gz.

Citation

Code Snippets

  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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import itertools
from ast import literal_eval
import re
from scipy import stats
from random import sample
import seaborn as sns
import matplotlib.gridspec as gridspec
import argparse

import os
import sys

sns.set_style('ticks', {'axes.edgecolor': '0',  
                        'xtick.color': '0',
                        'ytick.color': '0'})
sns.set_context("paper",font_scale=2)

def lst_converter(x):
    return x.split("|")

def HLA_lst_converter(x):
    return [hla_lst.split(';') for hla_lst in x.split('|')]

def HLA_cd8_converter(x):
    #define format of datetime
    return x.replace("[","").replace("]","").replace(",", "").replace("'","").split(" ")

def cdr3_lst_converter(x):
    #define format of datetime
    return x.replace("[","").replace("]","").replace("'","").split(" ")

def epitope_converter(x):
    #define format of datetime
    return [y for y in x.replace("[","").replace("]","").replace("\n","").split("'") if (y != '') & (y != ' ')]

def peptide_hla_converter(x):
    return re.findall("\w+\s{1}\w{1}\d+", x.replace("[","").replace("]","").replace("\n","").replace("'",""))

def literal_converter(val):
    # replace NaN with '' and perform literal eval on the rest
    try:
        return [] if val == '' else [v for v in literal_eval(val)]
    except:
        return [] if val == '' else [float(v) for v in lst_converter(val)]


converters = {'peptide_HLA_lst': lst_converter, #peptide_hla_converter,
              'umi_count_lst_mhc': literal_converter, #literal_eval,
              'umi_count_lst_cd8': literal_converter,
              'umi_count_lst_TRA': literal_converter,'umi_count_lst_TRB': literal_converter,
              'cdr3_lst_TRA': lst_converter, #cdr3_lst_converter,
              'cdr3_lst_TRB': lst_converter, #cdr3_lst_converter,
              'HLA_lst_mhc': lst_converter, #cdr3_lst_converter,
              'HLA_pool_cd8': lst_converter, #cdr3_lst_converter,
              'HLA_cd8': lst_converter, #HLA_cd8_converter,
              'HLA_lst_cd8': HLA_lst_converter,'sample_id_lst':literal_converter}

def calc_binding_concordance(df, clonotype_fmt):
    gems_per_specificity        = df.groupby([clonotype_fmt,'peptide_HLA']).gem.count().to_dict()
    df['gems_per_specificity']  = df.set_index([clonotype_fmt,'peptide_HLA']).index.map(gems_per_specificity)
    gems_per_spec_hla_match     = df[df.HLA_match == True].groupby([clonotype_fmt, 'peptide_HLA']).gem.count().to_dict()
    df['gems_per_spec_hla_match'] = df.set_index([clonotype_fmt,'peptide_HLA']).index.map(gems_per_spec_hla_match)
    gems_per_clonotype          = df.groupby([clonotype_fmt]).gem.count().to_dict()
    df['gems_per_clonotype']    = df[clonotype_fmt].map(gems_per_clonotype)
    df['binding_concordance']   = df.gems_per_specificity / df.gems_per_clonotype
    df['hla_concordance']       = df.gems_per_spec_hla_match / df.gems_per_specificity
    df['hla_concordance']       = df.hla_concordance.fillna(0)
    return df

def get_argparser():
    """Return an argparse argument parser."""
    parser = argparse.ArgumentParser(prog = 'Evaluate clonotypes',
                                     description = 'Evaluates which clonotypes can be used for grid search to identify optimal UMI thresholds')
    add_arguments(parser)
    return parser

def add_arguments(parser):
    parser.add_argument('--input', required=True, help='Filepath for data')
    parser.add_argument('--output', required=True, help='Filepath for output data')
    parser.add_argument('--plots', required=True, help='Filepath for output plots (must contain two placeholders, ie plt_dir/%s/%d.pdf)')

#########################################################################################################
#                                                 Class                                                 #
#########################################################################################################
class Evaluate_Clonotype():
    """
    An instance is a clonotype subset of the data.
    """
    value_bin = set() # clonotypes Evaluate_Clonotype.value_bin
    trash_bin = set() # peptide-HLAs
    ct_checks = dict()

    def __init__(self, df, ct, selected_clonotypes, use_relative_umi=False, variable='peptide_HLA'):
        self.df = df[df.ct == ct]
        self.ct = int(ct)
        self.idx = self.df.index
        self.rel_umi = use_relative_umi
        self.fig_flg = 'None'
        self.variable = variable

        if self.variable == 'peptide_HLA':
            self.var_lst = 'peptide_HLA_lst'
            self.umi_lst = 'umi_count_lst_mhc'
        elif self.variable == 'sample_id':
            self.var_lst = 'sample_id_lst'
            self.umi_lst = 'umi_count_lst_cd8'
        elif self.variable == 'HLA_cd8':
            self.var_lst = 'HLA_pool_cd8'
            self.umi_lst = 'umi_count_lst_cd8'

        # Initialize list of clonotypes
        self.sel_cts = selected_clonotypes
        self.sel_cts.index = self.sel_cts.index.astype(int)

        # Initialize matrix of query count per GEM 
        self.queries = df[self.var_lst].explode().drop_duplicates()
        self.mat = pd.DataFrame(index=self.queries, columns=df.gem.unique())

        # Count no. of GEMs within grp that are annotated with a specific pMHC
        self.gems_per_query = self.df.explode(self.variable).groupby(self.variable).size()
        self.gems_per_all_q = pd.concat([self.gems_per_query,
                                         pd.Series(0, index=self.queries[~self.queries.isin(self.gems_per_query.index)])])

    def sum_umi(self):
        # Sum UMIs for each peptide across GEMs (multiple peptides per GEM)
        for idx, row in self.df.iterrows():
            if self.variable == 'HLA_cd8':
                var = 'HLA_lst_cd8'
                var_lst = [item for sublist in row[var] for item in sublist if item != '']
                umi_lst = [row[self.umi_lst][i] for i, sublist in enumerate(row[var]) for item in sublist if item != '']
            else:
                var = self.var_lst
                var_lst = row[self.var_lst]
                umi_lst = row[self.umi_lst]

            if len(row[self.umi_lst]) == len(row[var]):
                self.mat.loc[var_lst, row.gem] = umi_lst
            else:
                self.mat.loc[var_lst, row.gem] = [0] * len(var_lst)
        self.mat.fillna(0, inplace=True)

    def calc_summary(self):
        self.summary_df = self.mat.sum(axis=1).sort_values(ascending=False).to_frame().rename(columns={0:'s'})
        self.summary_df['avg'] = self.mat.mean(axis=1)
        self.summary_df['col'] = 'grey'
        self.summary_df['r'] = self.summary_df.s / self.summary_df.s.max() # Unnecessary

    def calc_relative_umi(self):
        if self.variable == 'peptide_HLA':
            umi = 'umi_count_mhc'
        else:
            umi = 'umi_count_cd8'
        if self.rel_umi:
            self.mat = self.mat / self.df[umi].quantile(0.9, interpolation='lower')
        return self.df[umi] / self.df[umi].quantile(0.9, interpolation='lower')

    def select_queries(self, n=11):
        self.selected_queries = self.summary_df.head(n).index
        self.selected_mat = self.mat.loc[self.mat.index.isin(self.selected_queries), self.df.gem]

    def transform_data_for_plotting(self):
        """
        For each row have unique combination of pMHC and GEM.
        """
        self.plt_df = self.selected_mat.melt(ignore_index=False, var_name='gem', value_name='umi').fillna(0)
        self.plt_df.umi = self.plt_df.umi.astype(int)

    def add_gem_count(self):
        """
        Make a variable for iterating over GEMs (fx in a GIF)
        """
        dct = (self.plt_df.groupby('gem', sort=False).size()
               .to_frame().reset_index().reset_index().set_index('gem')
               .rename(columns={'index':'gem_count',0:'query_count'}))
        dct['gem_count'] = dct.gem_count + 1

        # Make a var for iterating over GEMs
        self.plt_df['gem_count'] = self.plt_df.gem.map(dct.gem_count)

    def sort_data(self):
        self.plt_df.reset_index(inplace=True)
        if self.variable != 'sample_id':
            self.plt_df[self.var_lst] = self.plt_df[self.var_lst].astype("category") #why as category?
            self.plt_df[self.var_lst] = self.plt_df[self.var_lst].cat.set_categories(self.selected_queries)
        else:
            self.plt_df[self.var_lst] = self.plt_df[self.var_lst].fillna('').astype(str) #why do I have nans?
        self.plt_df.sort_values(by=self.var_lst, inplace=True)

    def transform_to_concordance(self):
        self.conc_df = (self.plt_df.sort_values(by=['gem','umi'])
                        .drop_duplicates(subset='gem', keep='last')
                        .groupby(self.var_lst).gem.count()
                        .to_frame())
        self.conc_df['clonotype'] = f'clonotype {self.ct}'
        self.conc_df.reset_index(inplace=True)
        self.conc_df['concordance'] = self.conc_df.gem / self.conc_df.gem.sum()
        self.conc_df.replace(0, np.nan, inplace=True)

    def plot_advanced_figure(self, figname):
        def get_legend_n_handle(l,h,key='gem'):
            leg = list()
            hdl = list()
            if key is None:
                keep = True
            else:
                keep = False
            for i,e in enumerate(l):
                if keep:
                    if int(float(e)) > 0:
                        leg.append(int(float(e)))
                        hdl.append(h[i])
                if e == key:
                    keep = True
            return hdl, leg

        fig = plt.figure(figsize=(20,7))
        fig.suptitle(f"Clonotype {self.ct}")

        gs = gridspec.GridSpec(1, 3, width_ratios=[2, 7, 2], wspace=0.2) #, left=0.05
        ax1 = fig.add_subplot(gs[0])
        ax2 = fig.add_subplot(gs[1])
        ax3 = fig.add_subplot(gs[2])

        ########################
        # Add multipletplot
        ###############################
        tx1 = ax1.twinx()
        sns.scatterplot(data=self.plt_df, x="gem", y='peptide_HLA_lst', size='umi', color="gray", ax=ax1, legend='brief')

        h,l = ax1.get_legend_handles_labels()
        h,l = get_legend_n_handle(l, h, key=None)
        ax1.legend(h, l, bbox_to_anchor=(-1, 0.5), loc=10, frameon=False, title='UMI')

        sns.scatterplot(data=self.plt_df, x="gem", y='peptide_HLA_lst', size='umi', color="gray", ax=tx1, legend=False)

        ########################
        # Add boxplot
        ###############################
        PROPS = {'boxprops':{'alpha':0.3},
                 'medianprops':{'alpha':0.3},
                 'whiskerprops':{'alpha':0.3},
                 'capprops':{'alpha':0.3}}
        EMPTY = {'boxprops':{'alpha':0},
                 'medianprops':{'alpha':0},
                 'whiskerprops':{'alpha':0},
                 'capprops':{'alpha':0}}
        ARGS = {'x':"umi", 'y':"peptide_HLA_lst", 'data':self.plt_df, 'showfliers':False}
        order = self.plt_df.peptide_HLA_lst.unique()#[::-1]

        tx2 = ax2.twinx() # hack to get matching ticks on the right
        sns.boxplot(**ARGS, **PROPS, order=order, ax=ax2)
        sns.stripplot(data=self.plt_df, x="umi", y='peptide_HLA_lst', ax=ax2, order=order, jitter=0.2, edgecolor='white',linewidth=0.5, size=6)
        sns.boxplot(**ARGS, **EMPTY, order=order, ax=tx2)

        # Add significance bar
        if self.test_dist():
            l = len(self.plt_df.peptide_HLA_lst.unique()) - 1
            y = [0,0,1,1] #[l, l, l-1, l-1] #
            x0 = self.plt_df.umi.max()
            x1 = x0 * 1.02
            x2 = x0 * 1.025
            x3 = x0 * 1.035

            ax2.plot([x1, x2, x2, x1], y, lw=0.7, c='0') #lw=1.5, 
            ax2.plot(x3, np.mean(y), marker="*", c='0')

        ######################################
        # Add concordance plot
        #########################################
        # Hack to get colorbar
        plot = ax3.scatter([np.nan]*len(order), order, c=[np.nan]*len(order), cmap='viridis_r', vmin=0, vmax=1)
        fig.colorbar(plot, ax=ax3)
        sns.scatterplot(data=self.conc_df, x='clonotype', y='peptide_HLA_lst',
                        size='gem', hue='concordance', hue_norm=(0,1), palette='viridis_r', ax=ax3)

        # Remove automatic sns legend for hue, keep only legend for size.
        h,l = ax3.get_legend_handles_labels()
        h,l = get_legend_n_handle(l, h, key='gem')
        ax3.legend(h, l, bbox_to_anchor=(1.5, 0.5), loc=6, frameon=False, title='GEM')

        ######################################
        # Prettify
        #########################################

        ax1.set_title('Peptide MHC multiplets')
        ax2.set_title('UMI distribution per peptide MHC')
        ax3.set_title('Concordance')

        xmax = round(self.plt_df.umi.max(), -1)
        ax2.set_xticks(np.arange(0, xmax+10, 10))
        ax3.set_yticks([])

        ax1.set_xticklabels([])
        tx1.set_yticklabels([f'n= {n}' for n in self.plt_df.groupby('peptide_HLA_lst').size()])
        ax2.set_yticklabels([])
        ax3.set_yticklabels([])

        ax1.set_xlabel('GEM')
        ax1.set_ylabel('Peptide HLA', labelpad=20)
        tx1.set_ylabel('')
        ax2.set_xlabel('Peptide UMI')
        ax2.set_ylabel('')
        tx2.set_ylabel('')
        ax3.set_ylabel('')
        ax3.set_xlabel('')

        tx1.tick_params(axis='y', pad=20)

        ax2.spines['bottom'].set_bounds(0, xmax) # Hack to fix x-axis

        sns.despine(trim=True, right=True, ax=ax1)
        sns.despine(trim=True, right=False, ax=tx1)
        sns.despine(trim=True, right=False, ax=ax2)
        sns.despine(trim=True, right=False, ax=tx2)
        sns.despine(trim=True, left=True, ax=ax3)

        plt.savefig(figname %(self.fig_flg, self.ct), bbox_inches='tight')
        plt.show()


    def get_plotting_stats(self):
        self.summed_umis = self.summary_df.s.head(10)
        self.summed_gems = (self.mat > 0).sum(axis=1)

    def test_dist(self):
        assert not self.selected_mat.isna().any().any()
        # Select the peptides to test: the most abundant and the second most abundant (UMI wise)
        p1 = self.summary_df.index[0]
        p2 = self.summary_df.index[1]
        # Extract the UMI distribution for the two selected peptides.
        s1 = self.selected_mat.T[p1]
        s2 = self.selected_mat.T[p2]

        if sum(s1.fillna(0)-s2.fillna(0)) == 0:
            return False

        w, p = stats.wilcoxon(s1.fillna(0)-s2.fillna(0), alternative='greater')

        if p <= 0.05:
            return True
        return False

    def get_imputed_query(self):
        return self.summary_df.index[0]

    def get_remaining_queries(self):
        return self.summary_df.index[1:10]

    def update_bins(self):
        Evaluate_Clonotype.value_bin.update([self.ct])
        Evaluate_Clonotype.trash_bin.update(self.summary_df.index[1:10])

    def update_variable_analysis(self):
        Evaluate_Clonotype.ct_checks[self.variable] = self.plt_df

    def update_flag(self, flag):
        self.fig_flg = flag

#########################################################################################################
#                                                 Input                                                 #
#########################################################################################################
try:
    INPUT = snakemake.input.data
    OUTPUT = snakemake.output.data
    PLOT = snakemake.params.plots
except:
    parser = get_argparser()
    args = parser.parse_args()

    INPUT = args.input
    OUTPUT = args.output
    PLOT = args.plots

########################################################################################################
#                                                 Load                                                 #
########################################################################################################
df = pd.read_csv(INPUT, converters=converters)

########################################################################################################
#                                               Prepare                                                #
########################################################################################################

variables = ['peptide_HLA']
imp_vars = ['ct_pep']
no_hashing = True

selected_clonotypes = df.groupby('ct').size()
selected_clonotypes = selected_clonotypes[selected_clonotypes >= 10] # OBS!

for ct, size in selected_clonotypes.items():
    for variable, imp_var in zip(variables, imp_vars):
        inst = Evaluate_Clonotype(df, ct, selected_clonotypes, variable=variable)
        inst.sum_umi()
        inst.calc_summary()
        inst.select_queries()
        inst.transform_data_for_plotting()
        inst.add_gem_count()
        inst.sort_data()
        inst.transform_to_concordance() # Only relevant for pMHC...
        inst.update_variable_analysis()

        if inst.test_dist():
            df.loc[inst.idx, imp_var] = inst.get_imputed_query()

            if variable == 'peptide_HLA':
                inst.update_bins()

    if ct in Evaluate_Clonotype.value_bin:
        tmp = calc_binding_concordance(df[df.ct == ct].copy(), 'ct')
        conc_pep = tmp[tmp.binding_concordance == tmp.binding_concordance.max()].peptide_HLA.unique()[0]
        if conc_pep == tmp.ct_pep.unique()[0]:
            inst.update_flag('significant_match')
        else:
            inst.update_flag('significant_mismatch')
    else:
        inst.update_flag('insignificant')

    ########################################################################################################
    #                                                 Plot                                                 #
    ########################################################################################################
    if no_hashing:
        fig, ax1 = plt.subplots(1,1) #, figsize=(7,7)
        # pMHC
        order = Evaluate_Clonotype.ct_checks[variable].peptide_HLA_lst.unique() #inst.plt_df.peptide_HLA_lst.unique()#[::-1]
        ARGS = {'x':"umi", 'y':"peptide_HLA_lst", 'data':Evaluate_Clonotype.ct_checks['peptide_HLA'], 'order':order, 'ax':ax1}
        sns.boxplot(**ARGS, showfliers=False)
        sns.stripplot(**ARGS, jitter=0.2, edgecolor='white',linewidth=0.5, size=6)
        ax1.set_title("Peptide HLA")
        sns.despine(trim=True, ax=ax1)

        fig.suptitle(f'Clonotype {inst.ct} ({size} GEMs)')
        fig.savefig(PLOT %(inst.fig_flg, inst.ct), bbox_inches='tight')
        plt.close()
        plt.clf()
    else:
        ### PLOTTING ###
        fig, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(20,7))
        # pMHC
        order = Evaluate_Clonotype.ct_checks[variable].peptide_HLA_lst.unique() #inst.plt_df.peptide_HLA_lst.unique()#[::-1]
        ARGS = {'x':"umi", 'y':"peptide_HLA_lst", 'data':Evaluate_Clonotype.ct_checks['peptide_HLA'], 'order':order, 'ax':ax1}
        sns.boxplot(**ARGS, showfliers=False)
        sns.stripplot(**ARGS, jitter=0.2, edgecolor='white',linewidth=0.5, size=6)
        ax1.set_title("Peptide HLA")
        sns.despine(trim=True, ax=ax1)

        # Sample
        ARGS = {'x':"umi", 'y':"sample_id_lst", 'data':Evaluate_Clonotype.ct_checks['sample_id'], 'ax':ax3}
        sns.boxplot(**ARGS, showfliers=False)
        sns.stripplot(**ARGS, jitter=0.2, edgecolor='white',linewidth=0.5, size=6)
        ax3.set_title('Sample ID')
        sns.despine(trim=True, ax=ax3)

        # Hashing
        ARGS = {'x':"umi", 'y':"HLA_pool_cd8", 'data':Evaluate_Clonotype.ct_checks['HLA_cd8'], 'ax':ax2}
        sns.boxplot(**ARGS, showfliers=False)
        sns.stripplot(**ARGS, jitter=0.2, edgecolor='white',linewidth=0.5, size=6)
        ax2.set_title('Sample HLA')
        sns.despine(trim=True, ax=ax2)

        fig.suptitle(f'Clonotype {inst.ct} ({size} GEMs)')
        fig.savefig(PLOT %(inst.fig_flg, inst.ct), bbox_inches='tight')
        plt.close()
        plt.clf()

# Possible fix:
# https://github.com/mwaskom/seaborn/commit/1a537c100dd58c4a22187b8f2a02ab53a88030a2
# Check the sns version on computerome.


########################################################################################################
#                                                 Eval                                                 #
########################################################################################################
def notnan(var):
    return var == var

def determine_pep_match(row):
    if notnan(row.peptide_HLA) & notnan(row.ct_pep):
        return row.peptide_HLA == row.ct_pep
    else:
        return np.nan


df['HLA_mhc'] = df.peptide_HLA.str.split(' ', expand=True)[1]
df['pep_match'] = df.apply(lambda row: determine_pep_match(row), axis=1)
df['valid_ct'] = df.ct.isin(Evaluate_Clonotype.value_bin)

df.to_csv(OUTPUT, index=False)
  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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import re
import numpy as np
import pandas as pd
from ast import literal_eval
from itertools import chain, combinations
import os
import sys
import argparse

def HLA_cd8_converter(x):
    # "['A0201', 'A2501', 'B0702', 'B3501', 'C0401', 'C0702']"
    return x.replace("[","").replace("]","").replace(",", "").replace("'","").split(" ")

def cdr3_lst_converter(x):
    # "['A0201' 'A0101' 'B0801']"
    return x.replace("[","").replace("]","").replace("'","").split(" ")

def epitope_converter(x):
    # "['06_1_1' '45_1_49' 'V15_A1  CMV  pp50  VTE' '45_1_3' '47_1_78'\n 'V17_B8 EBV BZLF1 (C9)']"
    return [y for y in x.replace("[","").replace("]","").replace("\n","").split("'") if (y != '') & (y != ' ')]

def peptide_hla_converter(x):
    # "['ALPGVPPV A0201' 'RQAYLTNQY A0101' 'VTEHDTLLY A0101' 'EERQAYLTNQY A0101'\n 'AMLIRDRL B0801' 'RAKFKQLL B0801' 'p1.a1 *A0101']"
    return re.findall("\w+\s{1}\w{1}\d+|p1.a1 p\*\w\d{4}", x.replace("[","").replace("]","").replace("\n","").replace("'",""))

def literal_converter(val):
    # replace NaN with '' and perform literal eval on the rest
    return [] if val == '' else literal_eval(val)

converters = {'peptide_HLA_lst': peptide_hla_converter,
              'umi_count_lst_mhc': literal_eval,
              'umi_count_lst_TRA': literal_converter,'umi_count_lst_TRB': literal_converter,
              'cdr3_lst_TRA': cdr3_lst_converter,
              'cdr3_lst_TRB': cdr3_lst_converter,
              'HLA_lst_mhc': cdr3_lst_converter,'HLA_cd8': HLA_cd8_converter}

def calc_binding_concordance(df, clonotype_fmt):
    gems_per_specificity        = df.groupby([clonotype_fmt,'peptide_HLA']).gem.count().to_dict()
    df['gems_per_specificity']  = df.set_index([clonotype_fmt,'peptide_HLA']).index.map(gems_per_specificity)
    gems_per_spec_hla_match     = df[df.HLA_match == True].groupby([clonotype_fmt, 'peptide_HLA']).gem.count().to_dict()
    df['gems_per_spec_hla_match'] = df.set_index([clonotype_fmt,'peptide_HLA']).index.map(gems_per_spec_hla_match)
    gems_per_clonotype          = df.groupby([clonotype_fmt]).gem.count().to_dict()
    df['gems_per_clonotype']    = df[clonotype_fmt].map(gems_per_clonotype)
    df['binding_concordance']   = df.gems_per_specificity / df.gems_per_clonotype
    df['hla_concordance']       = df.gems_per_spec_hla_match / df.gems_per_specificity
    df['hla_concordance']       = df.hla_concordance.fillna(0)
    return df

def get_argparser():
    """Return an argparse argument parser."""
    parser = argparse.ArgumentParser(prog = 'Grid Search',
                                     description = 'Searching UMI thresholds to clean data based on clonotypes with significant pMHC profile.')
    add_arguments(parser)
    return parser

def add_arguments(parser):
    parser.add_argument('--input', required=True, help='Filepath for data')
    parser.add_argument('--ext_thr', required=False, default=0, help='External threshold for delta_umi_mhc')
    parser.add_argument('--output', required=True, help='Filepath for output data')

try:
    INPUT = snakemake.input.valid_df
    OUTPUT = snakemake.output.grid
    D_UMI_MHC = float(snakemake.params.ext_thr)
except:
    parser = get_argparser()
    args = parser.parse_args()

    INPUT = args.input
    OUTPUT = args.output
    D_UMI_MHC = float(args.ext_thr)

# Main
df = pd.read_csv(INPUT, converters=converters).fillna(value={"umi_count_mhc": 0, "delta_umi_mhc": 0,
                                                             "umi_count_mhc_rel":0,
                                                             "umi_count_cd8": 0, "delta_umi_cd8": 0,
                                                             "umi_count_TRA": 0, "delta_umi_TRA": 0,
                                                             "umi_count_TRB": 0, "delta_umi_TRB": 0})

value_bin = df[~df.ct_pep.isna()].ct.unique() # sign. peptides

# Set range of thresholds
umi_count_TRA_l = np.arange(0, df.umi_count_TRA.quantile(0.4, interpolation='higher'))
delta_umi_TRA_l = np.arange(0, 4) #2**np.linspace(-0.4,1.5,10)
umi_count_TRB_l = np.arange(0, df.umi_count_TRB.quantile(0.4, interpolation='higher'))
delta_umi_TRB_l = np.arange(0, 4) #2**np.linspace(-0.4,1.5,10)
umi_count_mhc_l = np.arange(1, df.umi_count_mhc.quantile(0.5, interpolation='higher'))
delta_umi_mhc_l = [D_UMI_MHC]
umi_relat_mhc_l = [0] #[D_UMI_MHC/10000]

observations = (len(umi_count_TRA_l) *
                len(delta_umi_TRA_l) *
                len(umi_count_TRB_l) *
                len(delta_umi_TRB_l) *
                len(umi_count_mhc_l) *
                len(delta_umi_mhc_l) *
                len(umi_relat_mhc_l))

features = ['accuracy','n_matches','n_mismatches',
            'trash_gems','ratio_retained_gems','trash_cts','ratio_retained_cts','avg_conc', 
            #'ct','gems_per_ct','ct_pep',
            'umi_count_mhc','umi_relat_mhc_l','delta_umi_mhc',
            'umi_count_TRA','delta_umi_TRA',
            'umi_count_TRB','delta_umi_TRB',
            'trash_gems_total','ratio_retained_gems_total','trash_cts_total','ratio_retained_cts_total']
table = pd.DataFrame(columns=features) 

N_total_gems = len(df)
N_total_tcrs = len(df.ct.unique())
n_total_gems = len(df[df.ct.isin(value_bin)])
n_total_tcrs = len(value_bin)

i = -1
for uca in umi_count_TRA_l:
    for dua in delta_umi_TRA_l:
        for ucb in umi_count_TRB_l:
            for dub in delta_umi_TRB_l:
                for ucm in umi_count_mhc_l:
                    for urm in umi_relat_mhc_l:
                        for dum in delta_umi_mhc_l:
                            i += 1
                            filter_bool = ((df.umi_count_TRA >= uca) &
                                           (df.delta_umi_TRA >= dua) &
                                           (df.umi_count_TRB >= ucb) &
                                           (df.delta_umi_TRB >= dub) &
                                           (df.umi_count_mhc >= ucm) &
                                           (df.delta_umi_mhc >= dum) &
                                           (df.umi_count_mhc_rel >= urm))

                            flt = df[filter_bool & df.ct.isin(value_bin)].copy()
                            flt = calc_binding_concordance(flt, 'ct')

                            n_gems = len(flt)
                            n_tcrs = len(flt.ct.unique())

                            conc = flt.binding_concordance.mean()

                            assert not flt.pep_match.isna().any(), 'Make sure flt only contains value cts'
                            assert n_gems == len(flt.pep_match.dropna())
                            n_mat = flt.pep_match.sum()
                            n_mis = n_gems - n_mat

                            g_trash = n_total_gems - n_gems
                            t_trash = n_total_tcrs - len(flt.ct.unique())
                            G_trash = N_total_gems - n_gems
                            T_trash = N_total_tcrs - n_tcrs

                            g_ratio = round(n_gems / n_total_gems, 3)
                            t_ratio = round(n_tcrs / n_total_tcrs, 3)
                            G_ratio = round(n_gems / N_total_gems, 3)
                            T_ratio = round(n_tcrs / N_total_tcrs, 3)

                            acc = round(n_mat/n_gems, 3)

                            table.loc[i] = (acc, n_mat, n_mis,
                                            g_trash, g_ratio, t_trash, t_ratio,
                                            conc, ucm, urm, dum, uca, dua, ucb, dub,
                                            G_trash, G_ratio, T_trash, T_ratio)

                            if i % 1000 == 0:
                                print(f'{round(i/observations * 100, 2)}% done')

table.to_csv(OUTPUT, index=False)
  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
148
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
import itertools
from ast import literal_eval
import re
from scipy import stats
from random import sample
import random
import re
import statistics
import argparse

plt.style.use('ggplot')

def HLA_cd8_converter(x):
    return x.replace("[","").replace("]","").replace(",", "").replace("'","").split(" ")

def cdr3_lst_converter(x):
    return x.replace("[","").replace("]","").replace("'","").split(" ")

def epitope_converter(x):
    return [y for y in x.replace("[","").replace("]","").replace("\n","").split("'") if (y != '') & (y != ' ')]

def peptide_hla_converter(x):
    return re.findall("\w+\s{1}\w{1}\d+", x.replace("[","").replace("]","").replace("\n","").replace("'",""))

def literal_converter(val):
    # replace NaN with '' and perform literal eval on the rest
    return [] if val == '' else literal_eval(val)


converters = {'peptide_HLA_lst': peptide_hla_converter,
              'umi_count_lst_mhc': literal_eval,
              'umi_count_lst_cd8': literal_converter,
              'umi_count_lst_TRA': literal_converter,'umi_count_lst_TRB': literal_converter,
              'cdr3_lst_TRA': cdr3_lst_converter,
              'cdr3_lst_TRB': cdr3_lst_converter,
              'HLA_lst_mhc': cdr3_lst_converter,
              'HLA_pool_cd8':cdr3_lst_converter,
              'HLA_cd8': HLA_cd8_converter,
              'HLA_lst_cd8':literal_converter,'sample_id_lst':literal_converter} #

def plot_grid(grid, opt_thr_idx, output):
    x_min = grid.index.max()*0.01
    x_max = grid.index.max()

    fig = plt.figure(figsize=(16,9))
    ax = plt.gca()
    trans = transforms.blended_transform_factory(ax.transAxes, ax.transData)

    x = grid.index
    y = grid.accuracy
    plt.scatter(x,y, label='Accuracy', marker='.')

    x = grid.index
    y = grid.ratio_retained_gems
    plt.scatter(x,y, label='Retained GEMs', marker='.')

    x = grid.index
    y = grid.mix_mean
    plt.scatter(x,y, label='Mix mean', marker='.')

    acc, rat = grid.loc[opt_thr_idx, ['accuracy','ratio_retained_gems']]

    plt.hlines(y=[acc, rat], xmin=-x_min, xmax=opt_thr_idx, colors='grey', linestyles='--')
    plt.vlines(x=opt_thr_idx, ymin=0.05, ymax=acc, colors='grey', linestyles='--')

    t = ', '.join(grid.loc[opt_thr_idx, ['umi_count_mhc','delta_umi_mhc',
                                         'umi_count_TRA','delta_umi_TRA',
                                         'umi_count_TRB','delta_umi_TRB']].astype(int).astype(str).to_list())
    plt.text(opt_thr_idx, 0.04, t, ha='center', va='top')
    plt.text(-0.01, acc, str(acc), ha='right', va='center', transform=trans)
    plt.text(-0.01, rat, str(rat), ha='right', va='center', transform=trans)

    plt.xlim(-x_min, x_max + x_min)
    plt.ylim(-0.01, 1.01)
    plt.legend(bbox_to_anchor=(0.99, 0.5), loc='center right', frameon=False)
    plt.xlabel('Grid index')

    for f in output:
        plt.savefig(f, bbox_inches='tight')

def get_argparser():
    """Return an argparse argument parser."""
    parser = argparse.ArgumentParser(prog = 'Extract Optimal Threshold',
                                     description = 'Evaluates the grid search, plots the grid, and extracts the optimal threshold.')
    add_arguments(parser)
    return parser

def add_arguments(parser):
    parser.add_argument('--data', required=True, help='Filepath for data')
    parser.add_argument('--grids', required=True, nargs='+', help='List of filepaths for grids')
    parser.add_argument('--output', required=True, help='Filepath for output data')
    parser.add_argument('--plot', required=True, nargs='+', help='Filepath for output plot')

try:
    VALID = snakemake.input.valid
    GRIDS = snakemake.input.grids
    PLOT = snakemake.output.plots
    THRESHOLD = snakemake.output.opt_thr
except:
    parser = get_argparser()
    args = parser.parse_args()

    VALID = args.data
    GRIDS = args.grids
    PLOT = args.plot
    THRESHOLD = args.output

# # Load
valid_df = pd.read_csv(VALID, converters=converters).fillna('')

tmp = list()
for filename in GRIDS:
    tmp.append(pd.read_csv(filename))
grid = pd.concat(tmp)

# # Main
n = 2
grid['mix_mean'] = (grid.accuracy * n + grid.ratio_retained_gems)/(n+1)

# Set index according to sorting so that plot will look nicer
grid.sort_values(by=['accuracy','ratio_retained_gems'], inplace=True)
grid.reset_index(drop=True, inplace=True)

optimal_thresholds = (grid
                      .sort_values(by=['mix_mean', 'accuracy','ratio_retained_gems', #'umi_count_mhc_rel',
                                       'umi_count_mhc', 'delta_umi_mhc','umi_count_TRB','delta_umi_TRB'],
                                   ascending=[True, True, True, False, False, False, False])
                      .tail(20))

# Get index of optimal threshold
opt_thr_idx = optimal_thresholds.tail(1).index[0]

# Get threshold values
opt_thr = optimal_thresholds.loc[opt_thr_idx, ['umi_count_mhc','delta_umi_mhc', #'umi_count_mhc_rel',
                                               #'umi_count_cd8','delta_umi_cd8',
                                               'umi_count_TRA','delta_umi_TRA',
                                               'umi_count_TRB','delta_umi_TRB']]


opt_thr.to_csv(THRESHOLD, header=None)
plot_grid(grid, opt_thr_idx, PLOT)
  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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import pandas as pd
import numpy as np
from ast import literal_eval
import re
import yaml
import argparse

def get_argparser():
    """Return an argparse argument parser."""
    parser = argparse.ArgumentParser(prog = 'Set filters',
                                     description = 'Generates boolean arrays by which to filter data on.')
    add_arguments(parser)
    return parser

def add_arguments(parser):
    parser.add_argument('--data', required=True, help='Filepath for data')
    parser.add_argument('--opt-thr', required=True, help='Filepaths for optimal threshold')
    parser.add_argument('--setting', required=True, choices=['indv','comb'], help='Defined whether filters are applied individually or combined additively.')
    parser.add_argument('--labels', required=True, help='Filepath for output filter labels')
    parser.add_argument('--filters', required=True, help='Filepath for output filters')

def HLA_cd8_converter(x):
    return x.replace("[","").replace("]","").replace(",", "").replace("'","").split(" ")

def cdr3_lst_converter(x):
    return x.replace("[","").replace("]","").replace("'","").split(" ")

def epitope_converter(x):
    return [y for y in x.replace("[","").replace("]","").replace("\n","").split("'") if (y != '') & (y != ' ')]

def peptide_hla_converter(x):
    return re.findall("\w+\s{1}\w{1}\d+", x.replace("[","").replace("]","").replace("\n","").replace("'",""))

def literal_converter(val):
    # replace NaN with '' and perform literal eval on the rest
    return [] if val == '' else literal_eval(val)

converters = {'peptide_HLA_lst': peptide_hla_converter,
              'umi_count_lst_mhc': literal_eval,
              'umi_count_lst_TRA': literal_converter,'umi_count_lst_TRB': literal_converter,
              'cdr3_lst_TRA': cdr3_lst_converter,
              'cdr3_lst_TRB': cdr3_lst_converter,
              'HLA_lst_mhc': cdr3_lst_converter,'HLA_cd8': HLA_cd8_converter} #

def notnan(x):
    return x == x

def get_multiplets(df):
    dct = df.groupby(['ct','peptide_HLA']).gem.count() > 1
    idx = df.set_index(['ct','peptide_HLA']).index.map(dct)
    return pd.Series(idx.fillna(False))

##########################################################
#                          Load                          #
##########################################################

try:
    VALID = snakemake.input.valid
    THRESHOLD = snakemake.input.opt_thr
    filter_set = snakemake.params.flt
    YAML = snakemake.output.lbl
    DATA = snakemake.output.flt
except:
    parser = get_argparser()
    args = parser.parse_args()

    VALID = args.data
    THRESHOLD = args.opt_thr
    filter_set = args.setting
    YAML = args.labels
    DATA = args.filters


opt_thr = pd.read_csv(THRESHOLD, index_col=0, header=None, names=['thr']).thr.dropna()
df = pd.read_csv(VALID, converters=converters, low_memory=False)


##########################################################
#                          Prep                          #
##########################################################
# when filtering for optimal threshold it is important to have values in UMI and delta
df.fillna({'umi_count_mhc':0, 'delta_umi_mhc':0, "umi_count_mhc_rel":0,
           'umi_count_TRA':0, 'delta_umi_TRA':0,
           'umi_count_TRB':0, 'delta_umi_TRB':0}, inplace=True)

# Add extra features
df.single_barcode_mhc = np.where(df.peptide_HLA_lst.apply(len) > 1, 'pMHC singlet','pMHC multiplet')
df['clonotype_multiplet'] = df.ct.map(df.groupby('ct').size() > 1)
df['HLA_match_per_gem'] = df.apply(lambda row: row.HLA_mhc in row.HLA_cd8 if row.HLA_cd8 == row.HLA_cd8 else False, axis=1)
df['complete_tcrs'] = df.cdr3_TRA.notna() & df.cdr3_TRB.notna()


##########################################################
#                         Filters                        #
##########################################################
# idx0: raw
# idx1: UMI thresholds
# idx2: Hashing singlets
# idx3: Matching HLA
# idx4: Complete TCRs
# idx5: Specificity multiplets
# idx6: Is cell (Cellranger)
# idx7: Viable cells (GEX)
idx0 = ~df.gem.isna()
idx1 = eval(' & '.join([f'(df.{k} >= {abs(v)})' for k,v in opt_thr.items()]))
idx2 = df.hto_global_class == 'Singlet'
idx3 = df.apply(lambda row: row.peptide_HLA.split()[-1] in row.HLA_cd8 if (notnan(row.peptide_HLA) & notnan(row.HLA_cd8)) else False, axis=1)
idx4 = df['complete_tcrs'] #exclude_single-chain_TCRs
idx5 = get_multiplets(df)
try:
    idx6 = df.cell_flag # is_cell
except AttributeError:
    idx6 = df.gem.isna() # All false
idx7 = df.gex

if filter_set == 'indv':
    # Showing individual effects of filtering
    filterings = [idx0,
              idx1,
              idx3,
              idx2,
              idx4,
              idx5,
              idx6,
              idx7]
    labels = ['total','optimal threshold',
          'matching HLA',
          'hashing singlets',
          'complete TCRs',
          'specificity multiplets',
          'is cell%s' %(' (GEX)' if any(idx6) else ''),
          'is viable cell']
    palette = ['grey','yellow','#ffffcc','#c7e9b4','#7fcdbb','#41b6c4','#2c7fb8','black']

    flt_to_remove = list()
    for i, flt in enumerate(filterings):
        if sum(flt) == 0:
            flt_to_remove.append(i)

    for i in flt_to_remove[::-1]:
        del filterings[i]
        del labels[i]
        del palette[i]

elif filter_set == 'comb':
    # Showing combined effects in the same order
    labels = ['total','optimal threshold',
              'matching HLA',
              'hashing singlets',
              'complete TCRs',
              'specificity multiplets',
              'is cell%s' %(' (GEX)' if any(idx6) else ''),
              'is viable cell']
    palette = ['grey','yellow','#ffffcc','#c7e9b4','#7fcdbb','#41b6c4','#2c7fb8','black'] #'#253494'

    flt_to_remove = list()
    filterings = [idx0]
    for i, flt in enumerate([idx1, idx3, idx2, idx4, idx5, idx6, idx7], start=1):
        remaining_gems = sum(filterings[-1] & flt)
        if remaining_gems > 0:
            filterings.append((filterings[-1] & flt))
        else:
            flt_to_remove.append(i)

    for i in flt_to_remove[::-1]:
        del labels[i]
        del palette[i]

else:
    print('filterset name unknown')

##########################################################
#                       Output prep                      #
##########################################################
dct = dict(labels = labels,
           palette = palette)

tmp = pd.concat(filterings, axis=1)
tmp.columns = labels

##########################################################
#                  Write output to file                  #
##########################################################
with open(YAML, 'w') as outfile:
    yaml.dump(dct, outfile)

tmp.to_csv(DATA, index=False)
  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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import re
from ast import literal_eval
import seaborn as sns
import os
import yaml
import argparse

sns.set_style('ticks', {'axes.edgecolor': '0',  
                        'xtick.color': '0',
                        'ytick.color': '0'})


def get_argparser():
    """Return an argparse argument parser."""
    parser = argparse.ArgumentParser(prog = 'Plot staircase',
                                     description = 'Generates staircase plots from all filters.')
    add_arguments(parser)
    return parser

def add_arguments(parser):
    parser.add_argument('--data', required=True, help='Filepath for data')
    parser.add_argument('--labels', required=True, help='Filepath for output filter labels')
    parser.add_argument('--filters', required=True, help='Filepath for filters')
    parser.add_argument('--out-dir', required=True, help='Directory to place output plots')

def HLA_cd8_converter(x):
    return x.replace("[","").replace("]","").replace(",", "").replace("'","").split(" ")

def cdr3_lst_converter(x):
    return x.replace("[","").replace("]","").replace("'","").split(" ")

def epitope_converter(x):
    return [y for y in x.replace("[","").replace("]","").replace("\n","").split("'") if (y != '') & (y != ' ')]

def peptide_hla_converter(x):
    return re.findall("\w+\s{1}\w{1}\d+", x.replace("[","").replace("]","").replace("\n","").replace("'",""))

def literal_converter(val):
    # replace NaN with '' and perform literal eval on the rest
    return [] if val == '' else literal_eval(val)

converters = {'peptide_HLA_lst': peptide_hla_converter,
              'umi_count_lst_mhc': literal_eval,
              'umi_count_lst_TRA': literal_converter,'umi_count_lst_TRB': literal_converter,
              'cdr3_lst_TRA': cdr3_lst_converter,
              'cdr3_lst_TRB': cdr3_lst_converter,
              'HLA_lst_mhc': cdr3_lst_converter,'HLA_cd8': HLA_cd8_converter} #

def notnan(x):
    return x == x

def get_multiplets(df):
    dct = df.groupby(['ct','peptide_HLA']).gem.count() > 1
    idx = df.set_index(['ct','peptide_HLA']).index.map(dct)
    return idx.fillna(False)

def calc_binding_concordance(df, clonotype_fmt):
    gems_per_specificity        = df.groupby([clonotype_fmt,'peptide_HLA']).gem.count().to_dict()
    df['gems_per_specificity']  = df.set_index([clonotype_fmt,'peptide_HLA']).index.map(gems_per_specificity)
    gems_per_spec_hla_match     = df[df.HLA_match == True].groupby([clonotype_fmt, 'peptide_HLA']).gem.count().to_dict()
    df['gems_per_spec_hla_match'] = df.set_index([clonotype_fmt,'peptide_HLA']).index.map(gems_per_spec_hla_match)
    gems_per_clonotype          = df.groupby([clonotype_fmt]).gem.count().to_dict()
    df['gems_per_clonotype']    = df[clonotype_fmt].map(gems_per_clonotype)
    df['binding_concordance']   = df.gems_per_specificity / df.gems_per_clonotype
    df['hla_concordance']       = df.gems_per_spec_hla_match / df.gems_per_specificity
    df['hla_concordance']       = df.hla_concordance.fillna(0)
    return df


def plot_specificity(title, df, max_gems, save=True):
    # Sort
    df.ct = df.ct.astype(int).astype(str)

    try:
        df.sort_values(by=['epitope_rank','gems_per_specificity','binding_concordance'],
                           ascending=[True, False, False], inplace=True)
    except KeyError:
        df.sort_values(by=['gems_per_specificity','binding_concordance'],
                           ascending=[True, False, False], inplace=True)

    # devide GEMs by max concordance and outliers
    dct = df.groupby('ct').binding_concordance.max()
    df['max_conc'] = df.ct.map(dct)
    idx = df.binding_concordance == df.max_conc

    def modify_legend(h,l):
        flag = False
        labels = []
        handles = []
        for e, le in enumerate(l):
            if flag:
                labels.append(le)
                handles.append(h[e])
            if le == 'gems_per_specificity':
                flag = True

        idxs = np.linspace(0,len(labels)-1,5)
        l = []
        h = []
        for i in idxs:
            l.append(labels[int(i)])
            h.append(handles[int(i)])
        return h,l

    # Style
    # https://seaborn.pydata.org/generated/seaborn.axes_style.html
    sns.set_style('ticks', {'axes.edgecolor': '0', #'axes.facecolor':'lightgrey',
                            'xtick.color': '0',
                            'ytick.color': '0'})
    sns.set_context("paper",font_scale=2)

    fig_height = int(df.peptide_HLA.nunique()/2) # 6
    fig = plt.figure(figsize=(20,fig_height)) # 6
    sns.scatterplot(data=df[idx], x='ct', y='peptide_HLA',
                    size='gems_per_specificity', sizes=(10,1000), size_norm=(1,max_gems),
                    hue='binding_concordance', palette='viridis_r', hue_norm=(0,1),
                    legend='full', linewidth=0)
    sns.scatterplot(data=df[~idx], x='ct', y='peptide_HLA',
                    size='gems_per_specificity', sizes=(10,1000), size_norm=(1,max_gems),
                    hue='binding_concordance', palette='viridis_r', hue_norm=(0,1),
                    legend=False, linewidth=0)
    ax = plt.gca()
    sm = plt.cm.ScalarMappable(norm=matplotlib.colors.Normalize(vmin=0,vmax=1), cmap='viridis_r')
    sm.set_array([]) # hack for cbar
    fig.colorbar(sm, ax=ax, orientation='horizontal', label='Binding Concordance', fraction=0.06*6/fig_height, pad=0.15*6/fig_height)

    h,l = ax.get_legend_handles_labels()
    h,l = modify_legend(h,l)
    ax.legend(h, l, bbox_to_anchor=(0.5, -0.5*6/fig_height), loc=9, frameon=False, title='GEMs', ncol=len(l))

    plt.xlabel('%d clonotypes (across %d GEMs)' %(df.ct.nunique(), df.gem.nunique()))
    plt.ylabel('')

    sns.despine(bottom=False, trim=True, offset={'left':-30})
    ax.set_xticks([])
    ax.set_xticklabels([])
    if save:
        plt.savefig(title, bbox_inches='tight', dpi=100)
    plt.show()


##########################################################
#                          Main                          #
##########################################################
try:
    VALID = snakemake.input.df
    FLT = snakemake.input.lbl
    IDX = snakemake.input.flt
    OUT_DIR = os.path.dirname(snakemake.output[0])
except:
    parser = get_argparser()
    args = parser.parse_args()

    VALID = args.data
    FLT = args.labels
    IDX = args.filters
    OUT_DIR = args.out_dir


df = pd.read_csv(VALID, converters=converters)

df.fillna({'umi_count_mhc':0, 'delta_umi_mhc':0, 'umi_count_mhc_rel':0,
           'umi_count_cd8':0, 'delta_umi_cd8':0,
           'umi_count_TRA':0, 'delta_umi_TRA':0,
           'umi_count_TRB':0, 'delta_umi_TRB':0,
           'cdr3_TRA':'','cdr3_TRB':''}, inplace=True)
df = calc_binding_concordance(df.copy(), 'ct')

idx_df = pd.read_csv(IDX)


##########################################################
#                         Filters                        #
##########################################################

with open(FLT, 'r') as f:
    flt = yaml.load(f, Loader=yaml.FullLoader)
globals().update(flt)


##########################################################
#                    Compute statistics                  #
##########################################################
for label in labels:
    idx = idx_df[label]
    plt_df = calc_binding_concordance(df[idx].copy(), 'ct')

    filename = os.path.join(OUT_DIR, '%s.png' %( '_'.join(label.split()) ) )
    max_gems = df.gems_per_specificity.max() if df.gems_per_specificity.max() < 1000 else 1000

    plot_specificity(filename, plt_df, max_gems, save=True)
    plt.cla()
    plt.clf()
    plt.close()
43
44
45
46
47
shell:
    "python scripts/F_comp_cred_specificities.py \
        --input {input.data} \
        --output {output.data} \
        --plots {params.plots}"
58
59
60
61
62
shell:
    "python scripts/G1_grid_search.py \
        --input {input.valid_df} \
        --ext_thr {wildcards.ext_thr} \
        --output {output.grid}"
75
76
77
78
79
80
shell:
    "python scripts/G2_extract_optimal_threshold.py \
        --data {input.valid} \
        --grids {input.grids} \
        --output {output.opt_thr} \
        --plot {output.plots}"
 95
 96
 97
 98
 99
100
101
shell:
    "python scripts/H_set_filters.py \
        --data {input.valid} \
        --opt-thr {input.opt_thr} \
        --setting {params.flt} \
        --labels {output.lbl} \
        --filters {output.flt}"
114
115
116
117
118
119
shell:
    "python scripts/I_filter_impact.staircase.py \
        --data {input.df} \
        --labels {input.lbl} \
        --filters {input.flt} \
        --out-dir {params}"
SnakeMake From line 114 of master/Snakefile
ShowHide 7 more snippets with no or duplicated tags.

Login to post a comment if you would like to share your experience with this workflow.

Do you know this workflow well? If so, you can request seller status , and start supporting this workflow.

Free

Created: 1yr ago
Updated: 1yr ago
Maitainers: public
URL: https://github.com/mnielLab/itrap
Name: itrap
Version: 1
Badge:
workflow icon

Insert copied code into your website to add a link to this workflow.

Downloaded: 0
Copyright: Public Domain
License: None
  • Future updates

Related Workflows

cellranger-snakemake-gke
snakemake workflow to run cellranger on a given bucket using gke.
A Snakemake workflow for running cellranger on a given bucket using Google Kubernetes Engine. The usage of this workflow ...