<jupyter_start><jupyter_text>Gene tracks<jupyter_code>import os
import re

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib import patches, gridspec
import seaborn as sns
from IPython.display import display_markdown, Markdown

from genetrack_utils import plot_gene_track

%matplotlib inline
sns.set(font='Arial')
plt.rcParams['svg.fonttype'] = 'none'
style = sns.axes_style('white')
style.update(sns.axes_style('ticks'))
style['xtick.major.size'] = 1
style['ytick.major.size'] = 1
sns.set(font_scale=1.2, style=style)
pal = sns.color_palette(['#0072b2', '#d55e00', '#009e73', '#f0e442', '#cc79a7', '#56b4e9', '#e69f00'])
cmap = ListedColormap(pal.as_hex())
sns.set_palette(pal)
sns.palplot(pal)
plt.show()
OUTPUT_PATH = snakemake.output.gene_tracks
if not os.path.exists(OUTPUT_PATH):
    os.makedirs(OUTPUT_PATH)
GENETRACK_TEMP = 20
FWD_BWS = sorted([fn for fn in snakemake.input.bws if re.search(f'{GENETRACK_TEMP}c.fwd.bw', fn)])
REV_BWS = sorted([fn for fn in snakemake.input.bws if re.search(f'{GENETRACK_TEMP}c.rev.bw', fn)])
LABELS = ['Col-0', 'fio1-3']
def convert_coords(coords):
    if not coords:
        return None
    else:
        return np.fromstring(coords.strip('[] '), sep=' ', dtype=int)

psi = pd.read_csv(
    snakemake.input.psi,
    header=[0, 1],
    skiprows=[2,],
    index_col=0,
    converters={2: str,
                3: convert_coords,
                4: convert_coords}
)

gene_psi = psi[psi.metadata.gene_id.str.contains(snakemake.wildcards.gene_id)].psi
gene_psi_melt = pd.melt(
    gene_psi.reset_index(),
    id_vars='index',
    value_vars=gene_psi.columns,
    var_name='sample_id',
    value_name='psi'
)
gene_psi_melt[['geno', 'temp']] = gene_psi_melt.sample_id.str.split('_', expand=True)[[0, 1]]
gene_psi_melt['temp'] = gene_psi_melt.temp.str.extract('(\d+)c', expand=True).astype(int)

psi_fit = pd.read_csv(
    snakemake.input.psi_fit,
    converters={'chrom': str,
                'alt1': convert_coords,
                'alt2': convert_coords},
    index_col=0
)
gene_psi_fit = psi_fit[psi_fit.gene_id.str.contains(snakemake.wildcards.gene_id)]
gene_psi_fit_sig = gene_psi_fit.query('(geno_fdr < 0.05 | gxt_fdr < 0.05) & abs(dpsi) > 0.05')
gene_psi_fit_sig
display_markdown(Markdown(f'## {snakemake.wildcards.gene_id} gene tracks and boxplots'))
for event_id, record in gene_psi_fit_sig.iterrows():
    try:
        plot_gene_track(
            record, gene_psi_melt.query(f'index == "{event_id}"'),
            FWD_BWS if record.strand == '+' else REV_BWS,
            LABELS,
            snakemake.input.fasta,
            title=f'{snakemake.wildcards.gene_id} {event_id}'
        )
        plt.savefig(os.path.join(OUTPUT_PATH, f'{snakemake.wildcards.gene_id}_{event_id}_gene_track.svg'))
        plt.show()
    except NotImplementedError:
        continue<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.144570058239123, 'fraction_numerical': 0.020212401507365536, 'mean_word_length': 4.149911816578483, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, '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': '33104', 'license': 'MIT', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1151, 'n_words': 188, 'path': 'fiona_temp_rnaseq/pipeline/notebook_processed/AT1G01060_gene_tracks_denovo.py.ipynb', 'repo_name': 'bartongroup/Simpson_Davies_Barton_U6_methylation', 'size': '2919'}