<jupyter_start><jupyter_text><NAME>, Eliza, and <NAME>. 2018. “Regulation of the X Chromosome in the Germline and Soma of Drosophila Melanogaster Males.” Genes 9 (5). https://doi.org/10.3390/genes9050242.I just want to re-create some tables that they had in this paper.<jupyter_code>import os
import sys
from pathlib import Path

from IPython.display import display, HTML, Markdown
import numpy as np
import pandas as pd

import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns

# Project level imports
from larval_gonad.notebook import Nb
# Setup notebook
nbconfig = Nb.setup_notebook()
tpm = pd.read_parquet('../output/scrnaseq-wf/tpm.parquet')
tpm.head()
chroms = nbconfig.fbgn2chrom.copy()
chroms.replace({'chr2L': 'A', 'chr2R': 'A', 'chr3L': 'A', 'chr3R': 'A'}, inplace=True)
chroms = chroms.query('chrom != "chr4" & chrom != "chrM" & chrom != "chrY"').copy()
xgenes = chroms.query('chrom == "chrX"').index.tolist()
agenes = chroms.query('chrom == "A"').index.tolist()
tpmChrom = tpm.join(chroms, how='left')
tpmOn = (tpm > 0).join(chroms, how='left')
numGenesOn = tpmOn.groupby('chrom').sum().T
numGenesOn.columns = ['# A genes', '# X genes']
numGenesOn
medGenes = tpmChrom.groupby('chrom').median().T
medGenes.columns = ['A median', 'X median']
medGenes['X/A'] = medGenes['X median'] / medGenes['A median']
medGenes
from scipy.stats import mannwhitneyu
tpm.head()
def mann(dat):
    _x = dat.reindex(xgenes).dropna()
    _a = dat.reindex(agenes).dropna()
    _, pval = mannwhitneyu(_x, _a, alternative='less')
    return pd.Series([np.round(pval, 4)], name=dat.name, index=['p-value'])

p_value = tpm.apply(mann).T
parsch1 = numGenesOn.join(medGenes).join(p_value)
parsch1 = parsch1.loc[nbconfig.CLUSTER_ORDER, ['# X genes', 'X median', '# A genes', 'A median', 'X/A', 'p-value']]
display(parsch1)
display(Markdown('p-value: One-sided Mann-Whiteney U'))
def prop_in_bin(dat):
    ct = pd.cut(dat, [2, 4, 8, 16, 32, 64, 128, 256, 99999999999], duplicates='drop')
    ct.name = f'bins'

    cnts = chroms.join(ct, how='right').groupby([f'bins', 'chrom']).size()
    cnts.name = 'cnts'
    cnts = cnts.to_frame().reset_index()

    _x = cnts.query('chrom == "chrX"').copy()
    expectation = _x.cnts.sum() / _x.shape[0]
    _x[f'{dat.name}'] = _x['cnts'] / expectation
    return _x[['bins', f'{dat.name}']].set_index('bins', drop=True)
dfs = []
for i, col in tpm.iteritems():
    dfs.append(prop_in_bin(col))
binned = pd.concat(dfs, axis=1).reset_index().melt(id_vars='bins')
def myplot(data, name, ax):
    sns.barplot('bins', 'value', data=data, ax=ax)
    ax.axhline(1, color='k', ls=':')
    ax.text(0.5, .99, name, ha='center', va='top', fontdict={'size': 10}, transform=ax.transAxes)
    ax.set_xlabel('')
    ax.set_ylabel('')
    plt.setp(ax.get_xticklabels(), rotation=45, ha='right')
from itertools import zip_longest
fig, axes = plt.subplots(3, 4, sharex=True, sharey=True, figsize=(10, 10), gridspec_kw={'wspace': 0.01, 'hspace': 0.01})
for ax, name in zip_longest(axes.flatten(), nbconfig.CLUSTER_ORDER):
    if name is None:
        ax.set_visible(False)
        continue
    dd = binned.query(f'variable == "{name}"')
    myplot(dd, name, ax)
    
fig.text(0.05, 0.5, 'X-linked Genes (Observed/Expected)', rotation=90, transform=fig.transFigure, fontsize=12, ha='left', va='center')
fig.text(0.5, 0.01, 'Bins', transform=fig.transFigure, fontsize=12, ha='center', va='bottom')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1873006668599594, 'fraction_numerical': 0.02783415482748623, 'mean_word_length': 4.86734693877551, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '486597', 'license': 'MIT', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1347, 'n_words': 297, 'path': 'notebook/2018-07-26_parsch.ipynb', 'repo_name': 'jfear/larval_gonad', 'size': '3464'}