<jupyter_start><jupyter_text>CanDI and DESeq2Let's say I want to look at changes in RNA expression across some cell lines in CCLE. DESeq2 is my preffered tool for doing differential expression analysis, unforutantely it's written in R. CanDI makes it easy to format CCLE read counts data into the shape that DESeq2 expects.<jupyter_code>import CanDI.candi as can
import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>Object InstantiationFor this example I'm going to do differential expression analysis across male and female KRAS mutant cell lines. The cell below uses CanDI to generate the correct CellLineCluster objects for our purpose.<jupyter_code>lung = can.Cancer("Lung Cancer", subtype = "NSCLC")
lung = can.CellLineCluster(lung.mutated("KRAS", variant = "Variant_Classification", item = "Missense_Mutation"))

lung_male = can.CellLineCluster(list(lung._info.loc[lung._info.sex == "Male",].index))
lung_female = can.CellLineCluster(list(lung._info.loc[lung._info.sex == "Female"].index))<jupyter_output>mutations has not been loaded. Do you want to load, y/n?> y
Load Complete<jupyter_text>Data MungingThe follow function takes two objects that we want to compare and automatically generates the counts and coldata matrices that DESeq2 needs to run. It's typically a good idea to filter our genes/transcripts with consistently low counts prior to running DESeq2. This speeds up analysis and avoids issues related to read count scaling and multiple hypthothesis testing corrections. In this case we don't care about different splicing of the same genes so I sum counts for duplicate indeces for all samples.<jupyter_code>def make_counts_coldata(obj1, obj2, condition, factor1, factor2):
    
    counts1 = obj1.rnaseq_reads
    coldat1 = pd.Series(counts1.shape[1] * [factor1], index = counts1.columns, name = condition)
    
    counts2 = obj2.rnaseq_reads
    coldat2 = pd.Series(counts2.shape[1] * [factor2], index = counts2.columns, name = condition)
    
    #Concatenate Column Data
    coldat = pd.concat([coldat1, coldat2], axis = 0)
    #Concatenate read count data 
    counts_mat = pd.concat([counts1, counts2], axis = 1)
    #Sum duplicate indeces
    counts_mat = counts_mat.groupby(counts_mat.index).sum().astype(int)
    
    return counts_mat, coldat
    
counts, coldat = make_counts_coldata(lung_male, lung_female, "sex", "male", "female")

counts.to_csv("temp_dat/lung_sex_counts.csv")
coldat.to_csv("temp_dat/lung_sex_coldata.csv")<jupyter_output>rnaseq_reads has not been loaded. Do you want to load, y/n?> y
Load Complete<jupyter_text>Running DESeq2In the following cell I use the csvs I just saved as arguments for an r-script that runs DESeq2. The last argument in this script the filname for the results.<jupyter_code>!Rscript scripts/run_deseq.r temp_dat/lung_sex_counts.csv temp_dat/lung_sex_coldata.csv temp_dat/lung_sex_deseq.csv<jupyter_output><empty_output><jupyter_text>Analyzing ResultsNow we can read the results of the differential expression analysis back into our python enviroment and continue analysis as necessary. Looking at the genes with the lowest adjusted p-value we see that XIST is the most significant differentially expressed genes. XIST is a lncRNA responsible for X inactivation in females so this a good positive control for our analysis.<jupyter_code>res = pd.read_csv("temp_dat/lung_sex_deseq.csv", index_col = "Unnamed: 0")
res.sort_values("padj").head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07654676258992806, 'fraction_numerical': 0.00920863309352518, 'mean_word_length': 5.425138632162661, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '164038', 'license': 'MIT', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1041, 'n_words': 372, 'path': 'deseq_setup.ipynb', 'repo_name': 'GilbertLabUCSF/CanDI', 'size': '3475'}