<jupyter_start><jupyter_text>Labeling HBECs<jupyter_code>## Initialization
import numpy as np
import pandas as pd
import scanpy as sc
from nero import Harmonia
import matplotlib.pyplot as plt
sc.settings.verbosity = 3             # verbosity: errors (0), warnings (1), info (2), hints (3)
# sc.logging.print_versions()
#results_file = './write/h1n1pbmc_final.h5ad'  # the file that will store the analysis results
from scipy.sparse import csr_matrix
sc.settings.set_figure_params(dpi=80)
adata = sc.read_10x_h5(
    '/data_volume/ifn_hbec/preprocess/filtered_feature_bc_matrix.h5', gex_only=False)                                # write a cache file for faster subsequent reading
adata.var_names_make_unique()  # this is unnecessary if using 'gene_ids'
adata.obs.shape
#add genetic demux information
donors = dict()
adata.obs
donors['1_1'] = Harmonia.MuxOut('/data_volume/ifn_hbec/preprocess/1_1_donor_ids.tsv')

donors['1_2'] = Harmonia.MuxOut('/data_volume/ifn_hbec/preprocess/1_2_donor_ids.tsv')

donors['2_1'] = Harmonia.MuxOut('/data_volume/ifn_hbec/preprocess/2_1_donor_ids.tsv')

donors['2_2'] = Harmonia.MuxOut('/data_volume/ifn_hbec/preprocess/2_2_donor_ids.tsv')
adata = Harmonia.ann_merge(adata,donors['1_1']).concatenate(Harmonia.ann_merge(adata,donors['1_2']),
                                                            Harmonia.ann_merge(adata,donors['2_1']),
                                                            Harmonia.ann_merge(adata,donors['2_2']),index_unique=None).copy()
adata.shape
#add hashtag demux information
HTO_classification = pd.read_csv('/data_volume/ifn_hbec/preprocess/HBEC_HTO_assignments.csv', sep=' ')['x']
adata.obs['HTO_classification'] = HTO_classification
adata.shape
adata.obs.head(5)
adata.obs.batch.value_counts()
pd.set_option('display.max_rows', None)
adata.obs['batch'].value_counts()
adata.obs['BEST.GUESS'].value_counts()
adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-11')&(adata.obs['BEST.GUESS']=='donor0')]


adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-11')&(adata.obs['BEST.GUESS']=='ada')]



donor0 = 2513
donor1 = 2614

adata.obs['condition']= 'unassigned'
# get subset of cells for each condition.
d2614_control = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-1')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_alpha_3 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-1')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_alpha_6 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-2')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_alpha_9 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-2')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_alpha_24 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-3')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_alpha_48 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-3')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_beta_3 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-4')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_beta_6 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-4')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_beta_9 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-5')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_beta_24 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-5')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_beta_48 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-6')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_gamma_3 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-6')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_gamma_6 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-7')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_gamma_9 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-7')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_gamma_24 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-8')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_gamma_48 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-8')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_lambda_3 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-9')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_lambda_6 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-9')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_lambda_9 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-10')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_lambda_24 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-10')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2614_lambda_48 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-11')&(adata.obs['BEST.GUESS']=='donor1')].obs.index)
d2513_control = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-1')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_alpha_3 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-1')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_alpha_6 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-2')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_alpha_9 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-2')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_alpha_24 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-3')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_alpha_48 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-3')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_beta_3 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-4')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_beta_6 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-4')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_beta_9 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-5')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_beta_24 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-5')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_beta_48 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-6')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)

d2513_gamma_3 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-6')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_gamma_6 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-7')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_gamma_9 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-7')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_gamma_24 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-8')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_gamma_48 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-8')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)

d2513_lambda_3 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-9')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_lambda_6 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-9')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_lambda_9 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-10')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_lambda_24 = list(adata[((adata.obs['batch'] == '2') | (adata.obs['batch'] == '3')) & (adata.obs['HTO_classification'] == 'hash-10')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)
d2513_lambda_48 = list(adata[((adata.obs['batch'] == '0') | (adata.obs['batch'] == '1')) & (adata.obs['HTO_classification'] == 'hash-11')&(adata.obs['BEST.GUESS']=='donor0')].obs.index)


#assign subset to condition
adata.obs.loc[adata.obs.index.isin(d2614_control), "condition"]  = 'd2614_control'
adata.obs.loc[adata.obs.index.isin(d2614_alpha_3), "condition"]  = 'd2614_alpha_3'
adata.obs.loc[adata.obs.index.isin(d2614_alpha_6), "condition"]  = 'd2614_alpha_6'
adata.obs.loc[adata.obs.index.isin(d2614_alpha_9), "condition"]  = 'd2614_alpha_9'
adata.obs.loc[adata.obs.index.isin(d2614_alpha_24), "condition"]  = 'd2614_alpha_24'
adata.obs.loc[adata.obs.index.isin(d2614_alpha_48), "condition"]  = 'd2614_alpha_48'
adata.obs.loc[adata.obs.index.isin(d2614_beta_3), "condition"]  = 'd2614_beta_3'
adata.obs.loc[adata.obs.index.isin(d2614_beta_6), "condition"]  = 'd2614_beta_6'
adata.obs.loc[adata.obs.index.isin(d2614_beta_9), "condition"]  = 'd2614_beta_9'
adata.obs.loc[adata.obs.index.isin(d2614_beta_24), "condition"]  = 'd2614_beta_24'
adata.obs.loc[adata.obs.index.isin(d2614_beta_48), "condition"]  = 'd2614_beta_48'
adata.obs.loc[adata.obs.index.isin(d2614_gamma_3), "condition"]  = 'd2614_gamma_3'
adata.obs.loc[adata.obs.index.isin(d2614_gamma_6), "condition"]  = 'd2614_gamma_6'
adata.obs.loc[adata.obs.index.isin(d2614_gamma_9), "condition"]  = 'd2614_gamma_9'
adata.obs.loc[adata.obs.index.isin(d2614_gamma_24), "condition"]  = 'd2614_gamma_24'
adata.obs.loc[adata.obs.index.isin(d2614_gamma_48), "condition"]  = 'd2614_gamma_48'
adata.obs.loc[adata.obs.index.isin(d2614_lambda_3), "condition"]  = 'd2614_lambda_3'
adata.obs.loc[adata.obs.index.isin(d2614_lambda_6), "condition"]  = 'd2614_lambda_6'
adata.obs.loc[adata.obs.index.isin(d2614_lambda_9), "condition"]  = 'd2614_lambda_9'
adata.obs.loc[adata.obs.index.isin(d2614_lambda_24), "condition"]  = 'd2614_lambda_24'
adata.obs.loc[adata.obs.index.isin(d2614_lambda_48), "condition"]  = 'd2614_lambda_48'
#assign subset to condition
adata.obs.loc[adata.obs.index.isin(d2513_control), "condition"]  = 'd2513_control'
adata.obs.loc[adata.obs.index.isin(d2513_alpha_3), "condition"]  = 'd2513_alpha_3'
adata.obs.loc[adata.obs.index.isin(d2513_alpha_6), "condition"]  = 'd2513_alpha_6'
adata.obs.loc[adata.obs.index.isin(d2513_alpha_9), "condition"]  = 'd2513_alpha_9'
adata.obs.loc[adata.obs.index.isin(d2513_alpha_24), "condition"]  = 'd2513_alpha_24'
adata.obs.loc[adata.obs.index.isin(d2513_alpha_48), "condition"]  = 'd2513_alpha_48'
adata.obs.loc[adata.obs.index.isin(d2513_beta_3), "condition"]  = 'd2513_beta_3'
adata.obs.loc[adata.obs.index.isin(d2513_beta_6), "condition"]  = 'd2513_beta_6'
adata.obs.loc[adata.obs.index.isin(d2513_beta_9), "condition"]  = 'd2513_beta_9'
adata.obs.loc[adata.obs.index.isin(d2513_beta_24), "condition"]  = 'd2513_beta_24'
adata.obs.loc[adata.obs.index.isin(d2513_beta_48), "condition"]  = 'd2513_beta_48'
adata.obs.loc[adata.obs.index.isin(d2513_gamma_3), "condition"]  = 'd2513_gamma_3'
adata.obs.loc[adata.obs.index.isin(d2513_gamma_6), "condition"]  = 'd2513_gamma_6'
adata.obs.loc[adata.obs.index.isin(d2513_gamma_9), "condition"]  = 'd2513_gamma_9'
adata.obs.loc[adata.obs.index.isin(d2513_gamma_24), "condition"]  = 'd2513_gamma_24'
adata.obs.loc[adata.obs.index.isin(d2513_gamma_48), "condition"]  = 'd2513_gamma_48'
adata.obs.loc[adata.obs.index.isin(d2513_lambda_3), "condition"]  = 'd2513_lambda_3'
adata.obs.loc[adata.obs.index.isin(d2513_lambda_6), "condition"]  = 'd2513_lambda_6'
adata.obs.loc[adata.obs.index.isin(d2513_lambda_9), "condition"]  = 'd2513_lambda_9'
adata.obs.loc[adata.obs.index.isin(d2513_lambda_24), "condition"]  = 'd2513_lambda_24'
adata.obs.loc[adata.obs.index.isin(d2513_lambda_48), "condition"]  = 'd2513_lambda_48'
adata.obs.query('condition != "unassigned"').shape
adata.write("/data_volume/ifn_hbec/HBEC_condition_assigned_deep.h5ad")<jupyter_output>... storing 'BEST.GUESS' as categorical
... storing 'DROPLET.TYPE' as categorical
... storing 'HTO_classification' as categorical
... storing 'condition' as categorical
... storing 'feature_types' as categorical
... storing 'genome' as categorical
{'fraction_non_alphanumeric': 0.24894363675427916, 'fraction_numerical': 0.0648141516866003, 'mean_word_length': 6.753470294280955, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '523611', 'license': 'MIT', 'n_tokens_mistral': 6164, 'n_tokens_neox': 5640, 'n_words': 587, 'path': 'analysis/ifn_hbec/preprocess/HBEC_assemble_labels.ipynb', 'repo_name': 'yelabucsf/scrna-parameter-estimation', 'size': '13963'}