<jupyter_start><jupyter_text>For each gene, pick the one TL-seq peak that best matches<jupyter_code># I first read in the file produced by CAGEr with gene names associated
# This is based off of the concensus clusters from all samples

df = pd.read_csv('181130clusters_diff_exp_loose_with_genes.csv', sep='\t')
df = df.dropna('rows')
df = df.reset_index().drop('index', axis=1)
df = df[df['tpm'] > 4]


# to return only a single TSS per gene
# find instances in which at least one end of the peak is upstream of the gene's ORF 
# and pick the closest peak to the gene's start

# start by converting to a bed file in order to use bedtools

tlbed = pd.DataFrame(columns=['chr', 'start', 'end', 'name', 'score', 'strand'])

tlbed['chr'] = df['coordinates'].str.split(':', expand=True)[0]
tlbed['start'] = (df['coordinates']
                      .str.split(':', expand=True)[1]
                      .str.split('-', expand=True)[0]) 
tlbed['start'] = tlbed['start'].astype(int)
tlbed['end'] = (df['coordinates']
                    .str.split(':', expand=True)[1]
                    .str.split('-', expand=True)[1])
tlbed['name'] = df['genes']
tlbed['score'] = 1
tlbed['strand'] = df['coordinates'].str.split(':', expand=True)[2]

# fill in the end value for single bp peaks and then add one to all

tlbed['end'].fillna(value=tlbed['start'], inplace=True)
tlbed['end'] = tlbed['end'].astype(int) + 1



# load in the bed file for the genome's ORFs

bed = pd.read_csv('SK1.genes_w_paralogs_no_dups.bed',
                 sep='\t', header=None)

bed[1] = bed[1].astype(int)
bed[2] = bed[2].astype(int)



# merge the two dataframes on gene name

tlmerge = tlbed.merge(bed, how='left', left_on='name', right_on=3).dropna()


# determine the distance between the TSS stop and the ORF start

tlmerge['dist'] = [ (tlmerge.loc[idx, 1] - tlmerge.loc[idx, 'start']) 
                   if x == '+' 
                   else (tlmerge.loc[idx, 'end'] - tlmerge.loc[idx, 2])  
                   for idx, x in tlmerge['strand'].iteritems()]


# remove any TSSs that are entirely in the ORF

tlmerge = tlmerge[tlmerge['dist'] > 0]


# select the peak that is closest to the promoter

df2 = pd.DataFrame()

for gene, group in tlmerge.groupby('name'):
    keep_index = group['dist'].idxmin()
    keep = tlmerge.loc[keep_index]
    df2 = df2.append(keep)

# only keep the best identified peak in the final dataframe
df2['start'] = df2['start'].astype(int).astype(str)
searchfor = df2['chr'] + ':' + df2['start']

df = df[df['coordinates'].str.contains('|'.join(searchfor))]

df.drop(["score", "consensus.cluster","tpm", "annotation"], axis=1, inplace=True)

df.to_csv('181130single_tss_per_gene.csv', index=False, )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16617754952311078, 'fraction_numerical': 0.014306676449009537, 'mean_word_length': 3.7926186291739894, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '213695', 'license': 'MIT', 'n_tokens_mistral': 990, 'n_tokens_neox': 942, 'n_words': 259, 'path': 'single_TSS_per_gene.ipynb', 'repo_name': 'atresen/LUTI_key_features', 'size': '2726'}