<jupyter_start><jupyter_text>Setup notebook<jupyter_code>import pandas as pd
import numpy as np
import math
#from scipy import stats

# plotting
#import matplotlib as mpl

#import pickle

import re
#from collections import Counter
from collections import defaultdict

import yaml
import builtins

# jupyter notebook amenities
#%matplotlib inline
%autosave 10<jupyter_output><empty_output><jupyter_text>Read in data  Drug information and Biomarker labels<jupyter_code>oncokb_events_file = './allActionableVariants_TCGAsamples.tsv'

oncokb_events = pd.read_csv(oncokb_events_file, sep='\t', header=0, index_col=None)


#oncokb = defaultdict(list)
#for i, row in oncokb_table.iterrows():
#    # Save desired values for a given alteration
#    v = {'Gene': row.Gene,
#         'Alteration': row.Alteration,
#         'Subtype': row['Cancer Type'],
#         'Level': row.Level}
#    # Save for each drug listed for an alteration
#    drugs = row['Drugs(s)'].split(', ')
#    for drug in drugs:
#        oncokb[drug].append(v)<jupyter_output><empty_output><jupyter_text>Functions defining binary subtype label<jupyter_code>def get_drug_label(drug, min_level = 1, events=oncokb_events):
    """
    """
    
    events_for_drug = events.loc[(drug in events['Drugs(s)'])
                                & (events['Level'] <= min_level)]
    
    
    def get_events_dict(event_row):
        set_of_samples = [x for x in list(events) if x[:5]=="TCGA-"]
        sample_dict = dict([ (sample_name, None) for sample_name in set_of_samples ])
        for sample in set_of_samples:
            sample_dict[sample] = event_row[sample]
    
    list_of_event_dicts = list()
    for index,row in events_for_drug.iterrows():
        list_of_event_dicts.append(get_event_dict(row))
        

    drug_dict = union_attribute(list_of_event_dicts)
        
    return drug_dict


def union_attribute(list_of_attribute_dicts):
    # union of keys
    keys = list()
    for attribute in list_of_attribute_dicts:
        keys.extend(attribute.keys())
        keys = list(set(keys))

    # Fill in 0s
    union_dict = dict([ (sample_name, None) for sample_name in keys ])
    for k in keys:
        for attribute in list_of_attribute_dicts:
            if k in attribute:
                if attribute[k] == 0:
                    union_dict[k] = 0
                    
    # Overwrite with NaN if not present in all dicts (not truly negative)
    for k in keys:
        for attribute in list_of_attribute_dicts:
            if k in attribute:
                if attribute[k] != attribute[k]: # checks for NaN
                    union_dict[k] = None
    
    # Overwrite with 1s (build union)
    for k in keys:
        for attribute in list_of_attribute_dicts:
            if k in attribute:
                if attribute[k] == 1:
                    union_dict[k] = 1
               
    return union_dict

def intersect_attribute(list_of_attribute_dicts):
    """
    
    """
    # union of keys
    keys = list()
    for attribute in list_of_attribute_dicts:
        keys.extend(attribute.keys())
        keys = list(set(keys))
    
    intersect_dict = dict([ (sample_name, None) for sample_name in keys ])
    
    # Fill in with 1s
    for k in keys:
        for attribute in list_of_attribute_dicts:
            if k in attribute:
                if attribute[k] == 1:
                    intersect_dict[k] = 1
                    
    # Overwrite with 0s (intersection)
    for k in keys:
        for attribute in list_of_attribute_dicts:
            if k in attribute:
                if attribute[k] == 0:
                    intersect_dict[k] = 0
                    
    # Overwrite with NaNs (truly negative)
    for k in keys:
        for attribute in list_of_attribute_dicts:
            if k in attribute:
                if attribute[k] != attribute[k]: # checks for NaN
                    intersect_dict[k] = None
                    
    return intersect_dict

def reverse_attribute(attribute):
    reverse_dict = dict([ (sample_name, None) for sample_name in attribute.keys() ])
    for k in attribute.keys():
        if attribute[k]==0:
            reverse_dict[k] = 1
        if attribute[k]==1:
            reverse_dict[k] = 0
    return reverse_dict
    

def how_many_positive_samples(attribute_dict):
    return sum(1 for v in attribute_dict.values() if v==1)

def how_many_negative_samples(attribute_dict):
    return sum(1 for v in attribute_dict.values() if v==0)<jupyter_output><empty_output><jupyter_text>Create drug labels<jupyter_code># create drug table

# create empty columns for TCGA samples

#print(oncokb_table.shape)
#for sample in set_of_tcga_samples:
#    oncokb_table[sample] = np.nan

#print(oncokb_table.shape)
#print(len(set_of_tcga_samples))


# fill the oncokb table with biomarker labels

        
# write drug table to file
#drug_table.to_csv("./oncokbDrugs_TCGAsamples.txt", sep='\t', index=False, na_rep = "NaN")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09072621202977268, 'fraction_numerical': 0.005029169181251257, 'mean_word_length': 2.5667144906743187, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '315031', 'license': 'Apache-2.0', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1564, 'n_words': 414, 'path': 'Data-Collection-and-Processing/Biomarker_and_Subtype_vectors/define_subtype_labels.ipynb', 'repo_name': 'jvivian/Actionable-Subtypes-Manuscript', 'size': '4971'}