def compute_dists(index): ntcr = sample_tcrs.loc[index, ] #tcrr edists = [] for tindex, tcr in epigroup.iterrows(): if tcr['tcr_info'] == ntcr['tcr_info']: continue edists.append( ch_tcr_distances.compute_distance(tcr['tcr_info'], ntcr['tcr_info'], chains, distance_params, rep_dists=rep_dists)) edists.sort() for nbrdist_percentile in nbrdist_percentiles: nbrdist = ch_tcr_distances.sort_and_compute_nbrdist_from_distances( edists, nbrdist_percentile, dont_sort=True) wtd_nbrdist = ch_tcr_distances.sort_and_compute_weighted_nbrdist_from_distances( edists, nbrdist_percentile, dont_sort=True) ntcr['{}_{}_nbrdist{}'.format( epitope, chains, nbrdist_percentile)] = '{:.3f}'.format(nbrdist) ntcr['{}_{}_wtd_nbrdist{}'.format( epitope, chains, nbrdist_percentile)] = '{:.3f}'.format(wtd_nbrdist) return ntcr
def compute_dists(index): ntcr = all_tcrs.loc[index, ] edists = [] for tindex, tcr in epigroup.iterrows(): if tcr['tcr_info'] == ntcr['tcr_info'] or tindex == index: #if epitope=='LLWNGPMAV': # print(tcr['tcr_info'], ntcr['tcr_info'], 'continued') continue edists.append( ch_tcr_distances.compute_distance(tcr['tcr_info'], ntcr['tcr_info'], chains, distance_params, rep_dists=rep_dists)) if edists[-1] == 0: print(tcr['tcr_info'], ntcr['tcr_info'], 'added to dists') edists.sort() for nbrdist_percentile in nbrdist_percentiles: nbrdist = ch_tcr_distances.sort_and_compute_nbrdist_from_distances( edists, nbrdist_percentile, dont_sort=True) wtd_nbrdist = ch_tcr_distances.sort_and_compute_weighted_nbrdist_from_distances( edists, nbrdist_percentile, dont_sort=True) ntcr['{}_{}_nbrdist{}'.format( epitope, chains, nbrdist_percentile)] = '{:.3f}'.format(nbrdist) ntcr['{}_{}_wtd_nbrdist{}'.format( epitope, chains, nbrdist_percentile)] = '{:.3f}'.format(wtd_nbrdist) return ntcr
def compute_dists(rindex): random_tcr = random_tcrs.loc[rindex, ] edists = [] for tindex, tcr in epigroup.iterrows(): edists.append(ch_tcr_distances.compute_distance(tcr['tcr_info'], random_tcr['tcr_info'], chains, distance_params, rep_dists=rep_dists)) edists.sort() for nbrdist_percentile in nbrdist_percentiles: nbrdist = ch_tcr_distances.sort_and_compute_nbrdist_from_distances(edists, nbrdist_percentile, dont_sort=True) wtd_nbrdist = ch_tcr_distances.sort_and_compute_weighted_nbrdist_from_distances(edists, nbrdist_percentile, dont_sort=True) random_tcr['{}_{}_nbrdist{}'.format(epitope, chains, nbrdist_percentile)] = '{:.3f}'.format( nbrdist) random_tcr['{}_{}_wtd_nbrdist{}'.format(epitope, chains, nbrdist_percentile)] = '{:.3f}'.format( wtd_nbrdist) return random_tcr
rep_dists = ch_tcr_distances.compute_all_v_region_distances_shugay(vmatrix, organism, distance_params) else: rep_dists = ch_tcr_distances.compute_all_v_region_distances(organism, distance_params) tcr_col = ['cdr1.alpha', 'cdr2.alpha', 'cdr2.5.alpha', 'cdr3.alpha', 'cdr1.beta', 'cdr2.beta', 'cdr2.5.beta', 'cdr3.beta', 'v.alpha', 'v.beta', epitope_col, 'species'] all_tcrs = ch_read_files.read_tcr(clones_file, organism, chains, epitope_col, tcr_col) tcr_col_end = ['cdr3.1', 'v.1', '{}.1'.format(epitope_col), 'cdr3.2', 'v.2', '{}.2'.format(epitope_col), 'distance'] #sys.exit() with open(outfile, 'w') as out: out.write('{}\n'.format('\t'.join(tcr_col_end))) for iti, tcr in all_tcrs.iterrows(): print(iti) for _, ntcr in all_tcrs.iterrows(): distance = ch_tcr_distances.compute_distance(tcr['tcr_info'], ntcr['tcr_info'], chains, distance_params, rep_dists=rep_dists) out.write('{}\t{}\t{}\t{}\t{}\t{}\t{}\n'.format(tcr['cdr3.beta'], tcr['v.beta'], tcr[epitope_col], ntcr['cdr3.beta'], ntcr['v.beta'], ntcr[epitope_col], distance)) print('Computation of distances is completed')