<jupyter_start><jupyter_text>It's important to load a non-rarefied table for ANCOM.<jupyter_code>table = q2.Artifact.load('data/feature-table.noblanks.40k.qza').view(pd.DataFrame)
dm = q2.Artifact.load('braycurtis.qza').view(skbio.DistanceMatrix)
feature_metadata = load_table('data/boland_biom_original.biom').metadata_to_dataframe('observation')
annotations = pd.read_csv('data/Biobank_Bile_Acids - Sheet1.tsv', sep='\t')<jupyter_output><empty_output><jupyter_text>We need to combine the mass over charge and the retention times, and additionally truncate any trailing zeroes to make them match the feature table.<jupyter_code>def combine(row):
    a = '%.4f' % float(row['row m/z'])
    b = '%.4f' % float(row['row retention time'])
    
    if a.endswith('0'):
        a = a[:-1]
    if b.endswith('0'):
        b = b[:-1]
    return '%s;%s' % (a, b)
    
annotations['id'] = annotations.apply(combine, axis=1)

annotations.set_index('id', inplace=True)
def renamer(row):
    if row['Compound_Name'] == '':
        return 'Unidentified %s bile acid' % row['Type of Bile Acid'].lower()
    else:
        return row['Compound_Name']
annotations['Compound_Name'] = annotations.apply(renamer, axis=1)
lookup = {
    'Cholic acid': 'Cholic acid',
    'Spectral Match to Cholic acid from NIST14': 'Cholic acid',
    '.beta.-Hyodeoxycholic acid': '',
    'glycochenodeoxycholic acid' : 'Glycochenodeoxycholic acid',
    'Spectral Match to Deoxycholic acid from NIST14': 'Deoxycholic acid',
    'Spectral Match to Taurocholic acid from NIST14': 'Taurocholic acid',
    'chenodeoxycholic acid': 'Chenodeoxycholic acid',
    'Phenylalanocholic acid': 'Phenylalanocholic acid',
    'Glycodeoxycholic acid': 'Glycodeoxycholic acid',
    'Spectral Match to Glycocholic acid from NIST14': 'Glycocholic acid',
    'Spectral Match to Ursodeoxycholic acid from NIST14': 'Ursodeoxycholic acid'}

annotations.Compound_Name.replace(lookup, inplace=True)<jupyter_output><empty_output><jupyter_text>We need to remove any conjugated types of bile acids because they are not very prevalent.<jupyter_code>annotations = annotations[annotations['Type of Bile Acid'].str.find('Conjugated') == -1]<jupyter_output><empty_output><jupyter_text>Combine the feature table and the annotations to convert them into a long table.<jupyter_code>annotations.head()
features = table.T.copy()
features['name'] = annotations['Type of Bile Acid']
features = features.dropna(axis=0, subset=['name'], how='any')
features = features.groupby('name').agg('sum').T
mf['category_2'].value_counts()
features['category_1'] = mf['category_1']
features['category_2'] = mf['category_2']
features['category_3'] = mf['category_3']
features['has_surgery'] = (mf['category_2'] != 'Control').astype(str)
features['endoscopic_activity'] = mf['endoscopic_activity']
features.dtypes
features.columns
alt = ['.alpha.-Hyodeoxycholic acid methyl ester', 'Chenodeoxycholic acid',
       'Cholic acid', 'Deoxycholic acid', 'Lithocholic acid',
       'Unidentified secondary bile acid', 'Ursodeoxycholic acid']
long_features = features.melt(value_name='intensity',
                value_vars=['Primary', 'Secondary'],
                id_vars=['category_1', 'category_2', 'category_3', 'has_surgery', 'endoscopic_activity']
               ).copy()
long_features.head()
long_features.head()
mf.endoscopic_activity.value_counts()
def swarmbox(x, y, **kwargs):
    ax = plt.gca()
    order = ['Control', 'Ileocolonic resection',
             'Ileorectal anastomosis', 'End ileostomy']
    
    sns.boxplot(x=x, y=y, ax=ax, order=order, **kwargs)
    sns.swarmplot(x=x, y=y, ax=ax, order=order, edgecolor="gray", linewidth=1, **kwargs)
    
    ax.ticklabel_format(axis='x', style='scientific', scilimits=(0, 1))
    ax.set_yticklabels(['CD no surgery', 'Ileocolonic resection',
                   'Ileorectal anastomosis', 'End ileostomy'])

g = sns.FacetGrid(long_features.query('category_1 == "cd"'), col='name', col_wrap=3, aspect=2,
                  sharey=False, sharex=False)
g = g.map(swarmbox, 'intensity', 'category_2')

g.savefig('cd-bile-acids.pdf')
def swarmbox(x, y, **kwargs):
    ax = plt.gca()
    order = ['Control', 'Pouchitis',
             'Subsequent dev of CD', 'Subsequent normal pouch']
    
    sns.boxplot(x=x, y=y, ax=ax, order=order, **kwargs)
    sns.swarmplot(x=x, y=y, ax=ax, order=order, edgecolor="gray", linewidth=1, **kwargs)
    
    ax.ticklabel_format(axis='x', style='scientific', scilimits=(0, 1))
    ax.set_yticklabels(['UC no surgery', 'Pouchitis',
                        'Subsequent dev of CD',
                        'Subsequent normal pouch'])

g = sns.FacetGrid(long_features.query('category_1 == "uc"'), col='name', col_wrap=3, aspect=2,
                  sharey=False, sharex=False)
g = g.map(swarmbox, 'intensity', 'category_3')
g.savefig('uc-bile-acids.pdf')
long_features.endoscopic_activity.value_counts()<jupyter_output><empty_output><jupyter_text>Primary and secondary bile acids (surgery vs no surgery)<jupyter_code>long_features.has_surgery.value_counts()
def swarmbox(x, y, **kwargs):
    ax = plt.gca()
    order = ['False', 'True']
    palette = {
        'False': '#66c2a5',
        'True': '#34495e',
    }
    
    sns.boxplot(x=x, y=y, ax=ax, order=order, palette=palette, **kwargs)
    sns.swarmplot(x=x, y=y, ax=ax, order=order, edgecolor="gray", dodge=True, linewidth=1,
                  palette=palette, **kwargs)
    
    ax.ticklabel_format(axis='x', style='scientific', scilimits=(0, 1))
    ax.set_yticklabels(['CD no surgery', 'Surgery'])

g = sns.FacetGrid(long_features.query('category_1 == "cd"'), col='name', col_wrap=1, aspect=2,
                  sharey=False, sharex=False)
g = g.map(swarmbox, 'intensity', 'has_surgery').add_legend()

g.savefig('cd-bile-acids-type.surgery-vs-no-surgery.pdf')
def swarmbox(x, y, **kwargs):
    ax = plt.gca()
    order = ['False', 'True']
    palette = {
        'False': '#66c2a5',
        'True': '#34495e',
    }
    
    sns.boxplot(x=x, y=y, ax=ax, order=order, palette=palette, **kwargs)
    sns.swarmplot(x=x, y=y, ax=ax, order=order, edgecolor="gray", dodge=True, linewidth=1,
                  palette=palette, **kwargs)
    
    ax.ticklabel_format(axis='x', style='scientific', scilimits=(0, 1))
    ax.set_yticklabels([' no surgery', 'Surgery'])

g = sns.FacetGrid(long_features.query('category_1 == "uc"'), col='name', col_wrap=1, aspect=2,
                  sharey=False, sharex=False)
g = g.map(swarmbox, 'intensity', 'has_surgery').add_legend()

g.savefig('uc-bile-acids-type.surgery-vs-no-surgery.pdf')<jupyter_output><empty_output><jupyter_text>Primary and secondary bile acids<jupyter_code>def swarmbox(x, y, **kwargs):
    ax = plt.gca()
    order = ['Control', 'Ileocolonic resection',
             'Ileorectal anastomosis', 'End ileostomy']
    
    sns.boxplot(x=x, y=y, ax=ax, order=order, **kwargs)
    sns.swarmplot(x=x, y=y, ax=ax, order=order, edgecolor="gray", dodge=True, linewidth=1, **kwargs)
    
    ax.ticklabel_format(axis='x', style='scientific', scilimits=(0, 1))
    ax.set_yticklabels(['CD no surgery', 'Ileocolonic resection',
                        'Ileorectal anastomosis', 'End ileostomy'])

g = sns.FacetGrid(long_features.query('category_1 == "cd"'), col='name', col_wrap=1, aspect=2,
                  sharey=False, sharex=False)
g = g.map(swarmbox, 'intensity', 'category_2').add_legend()

# g.savefig('cd-bile-acids-type.pdf')
def swarmbox(x, y, **kwargs):
    ax = plt.gca()
    order = ['Control', 'Pouchitis',
             'Subsequent dev of CD', 'Subsequent normal pouch']
    
    sns.boxplot(x=x, y=y, ax=ax, order=order, **kwargs)
    sns.swarmplot(x=x, y=y, ax=ax, order=order, dodge=True, edgecolor="gray", linewidth=1, **kwargs)
    
    ax.ticklabel_format(axis='x', style='scientific', scilimits=(0, 1))
    ax.set_yticklabels(['UC no surgery', 'Pouchitis',
                        'Subsequent dev of CD',
                        'Subsequent normal pouch'])

g = sns.FacetGrid(long_features.query('category_1 == "uc"'), col='name', col_wrap=1, aspect=2, legend_out=True,
                  sharey=False, sharex=False)
g = g.map(swarmbox, 'intensity', 'category_3').add_legend()
g.savefig('uc-bile-acids-type.pdf')<jupyter_output><empty_output><jupyter_text>Primary and secondary bile acids (with endoscopic activity)<jupyter_code>def swarmbox(x, y, hue, **kwargs):
    ax = plt.gca()
    order = ['Control', 'Ileocolonic resection',
             'Ileorectal anastomosis', 'End ileostomy']
    
    sns.boxplot(x=x, y=y, hue=hue, ax=ax, order=order, **kwargs)
    sns.swarmplot(x=x, y=y, hue=hue, ax=ax, order=order, edgecolor="gray", dodge=True, linewidth=1, **kwargs)
    
    ax.ticklabel_format(axis='x', style='scientific', scilimits=(0, 1))
    ax.set_yticklabels(['CD no surgery', 'Ileocolonic resection',
                   'Ileorectal anastomosis', 'End ileostomy'])

g = sns.FacetGrid(long_features.query('category_1 == "cd" and endoscopic_activity != "not collected"'), col='name', col_wrap=1, aspect=2,
                  sharey=False, sharex=False)
g = g.map(swarmbox, 'intensity', 'category_2', 'endoscopic_activity').add_legend()

g.savefig('cd-bile-acids-type-with-endoscopic-activity.pdf')
def swarmbox(x, y, hue, **kwargs):
    ax = plt.gca()
    order = ['Control', 'Pouchitis',
             'Subsequent dev of CD', 'Subsequent normal pouch']
    
    sns.boxplot(x=x, y=y, hue=hue, ax=ax, order=order, **kwargs)
    sns.swarmplot(x=x, y=y, hue=hue, ax=ax, order=order, dodge=True, edgecolor="gray", linewidth=1, **kwargs)
    
    ax.ticklabel_format(axis='x', style='scientific', scilimits=(0, 1))
    ax.set_yticklabels(['UC no surgery', 'Pouchitis',
                        'Subsequent dev of CD',
                        'Subsequent normal pouch'])

g = sns.FacetGrid(long_features.query('category_1 == "uc" and endoscopic_activity != "not collected"'), col='name', col_wrap=1, aspect=2, legend_out=True,
                  sharey=False, sharex=False)
g = g.map(swarmbox, 'intensity', 'category_3', 'endoscopic_activity').add_legend()
g.savefig('uc-bile-acids-type-with-endoscopic-activity.pdf')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1637616937469227, 'fraction_numerical': 0.010635155096011817, 'mean_word_length': 4.171079429735234, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '218269', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3793, 'n_tokens_neox': 3545, 'n_words': 714, 'path': 'metabolomics/6.0.0-primary-and-secondary-bile-acids.ipynb', 'repo_name': 'knightlab-analyses/ibd-surgery', 'size': '10155'}