# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import scipy as sp
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt

# %matplotlib inline

mpl.rcParams['figure.figsize'] = (8, 4)

df = pd.read_table('consistency.tsv')
df.head()

df.shape[0]

# #### Simple sum of taxa

df.iloc[:, 3:].sum()


# #### Sum taxa above threshold

def sum_above(df, th):
    dfx = df.query('count >= %d' % th)
    print('%d taxa >= %d' % (dfx.shape[0], th))
    print(dfx.iloc[:, 3:].sum())


sum_above(df, 2)

sum_above(df, 5)

sum_above(df, 10)

sum_above(df, 20)

sum_above(df, 50)

sum_above(df, 100)

# #### Weight by taxon size

dfs = df.query('count > 1')[['rank', 'taxon']].copy()
for tree in df.columns[3:]:
    dfs[tree] = df[tree] * df['count']
dfs.head()

dfs.iloc[:, 2:].sum()

# #### Sum by rank

ranks = ['kingdom', 'phylum', 'class', 'order', 'family', 'genus', 'species']

for rank in ranks:
    print(rank)
    print(dfs.query('rank == "%s"' % rank).iloc[:, 2:].sum())
    print()

# #### Summarize

dfr = pd.DataFrame()
for rank in ranks[1:]:
    df_ = dfs.query('rank == "%s"' % rank).iloc[:, 2:].sum()
    df_.name = rank
    dfr = pd.concat([dfr, df_], axis=1, sort=True)
dfr

dfr.astype(int)

# #### Plot

th = 10

dfc = df.query('count >= %d' % th).drop(['count'], axis=1).melt(
    ['rank', 'taxon'], var_name='method', value_name='consistency')
dfc.head()

dfc = dfc.query('rank != "kingdom"')
dfc = dfc[dfc['method'].isin(['astral', 'concat.cons', 'concat.rand'])]
dfc.shape

dfc['rank'].value_counts()

ax = sns.barplot(x='method', y='consistency', hue='rank', data=dfc)
ax.set_yscale('log')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0);

fig = ax.get_figure()
fig.tight_layout()
fig.savefig('output.pdf', bbox_to_inches='tight')



StarcoderdataJupyterScriptsDedupFiltered