import NotebookImport
from HNSCC_Imports import *

"""
Explanation: Global Imports
Importing a bunch of globals from this notebook.
End of explanation
"""


maf = FH.get_submaf(run.data_path, 'HNSC', fields='All')
maf = maf[maf.patient.map(lambda s: s[13:16] == '01A')]
maf.patient = maf.patient.map(lambda s: s[:12])
maf = maf.reset_index()
maf['t_alt_count'] = maf['t_alt_count'].replace('---', nan).astype(float)
maf['t_ref_count'] = maf['t_ref_count'].replace('---', nan).astype(float)

frac = maf.t_alt_count / maf[['t_alt_count','t_ref_count']].sum(1)
frac = frac[frac > .075]

get_mad = lambda s: (s - s.median()).abs().median() * 1.826 
med = frac.groupby(maf.patient).median()
mad = frac.groupby(maf.patient).mad()
math = (mad / med) * 100
math.name = 'MATH'

"""
Explanation: Calculate MATH Score
End of explanation
"""


math.describe()

math.hist()

"""
Explanation: My numbers are a bit off... I should try and reconsile this.
End of explanation
"""


(math > 32).value_counts()

"""
Explanation: I'm going to tweak their threshold as my calculation is a bit miscalibrated.
End of explanation
"""


(math > 31.5).value_counts()

math_t = (math > 31.5).map({True:'MATH High', False:'MATH Low'})

"""
Explanation: This is a bit closer but its hard to tell where the missing samples are.
End of explanation
"""


survival_and_stats(math_t, surv)

"""
Explanation: Survival Analysis
Curves look a little off, maybe I'm using more recent data?
End of explanation
"""


violin_plot_pandas(hpv, math)

survival_and_stats(combine(math_t=='MATH High', hpv), surv)

"""
Explanation: Combination of HPV and MATH
Not sure why they cut off survival at 4 years
End of explanation
"""


draw_survival_curves(math_t, surv, hpv)

"""
Explanation: From another angle, I don't see the HPV effect in my data.
End of explanation
"""


survival_and_stats(combine(del_3p<0, mut.features.ix['TP53']>0).ix[keepers_o].dropna(), 
                   clinical.survival.survival_5y)

"""
Explanation: Combination Analysis in HPV-

TP53-3p (Our finding from this paper).
I use a different working set with some old patients filtered
End of explanation
"""


survival_and_stats(combine(del_3p<0, mut.features.ix['TP53']>0).ix[ti(hpv==False)].dropna(), 
                   clinical.survival.survival_5y)

"""
Explanation: This is with the old patients back in.
End of explanation
"""


violin_plot_pandas(mut.df.ix['TP53'], math)

survival_and_stats(combine(math_t=='MATH High', mut.features.ix['TP53']>0).ix[ti(hpv==False)].dropna(), 
                   clinical.survival.survival_5y)

"""
Explanation: TP53-MATH (Figure 6b)
End of explanation
"""


violin_plot_pandas(del_3p, math, order=[-2,-1,0,1])

survival_and_stats(combine(math_t=='MATH High', del_3p < 0).ix[ti(hpv==False)].dropna(), 
                   clinical.survival.survival_5y)

"""
Explanation: 3p Deletion-MATH
End of explanation
"""


combo = combine(mut.features.ix['TP53']>0, del_3p < 0)

violin_plot_pandas(combo, math, order=['neither','3p_deletion',
                                       'TP53','both'])

survival_and_stats(combine(math_t=='MATH High', combo=='both').ix[ti(hpv==False)].dropna(), 
                   clinical.survival.survival_5y)

"""
Explanation: TP53-3p combination + MATH
End of explanation
"""


draw_survival_curves(combo, surv, math_t)

"""
Explanation: TP53-3p combination in in the context of MATH
End of explanation
"""


draw_survival_curves(math_t=='MATH High', surv, combo)

"""
Explanation: MATH in the context of TP53-3p
End of explanation
"""


two_hit = combine(del_3p<0, mut_new.ix['TP53']>0) == 'both'
two_hit.name = 'two_hit'
subtypes = combine(mirna.ix['hsa-mir-548k'][:,'01'] > -1, two_hit)
subtypes = subtypes.map({'hsa-mir-548k':1, 'neither':1, 'two_hit':2, 'both': 3})
subtypes.name = 'subtype'

violin_plot_pandas(subtypes, math)

draw_survival_curves(math_t=='MATH High', surv, subtypes)

draw_survival_curves(subtypes, surv, math_t=='MATH High')

"""
Explanation: Add third subtype from out paper
End of explanation
"""


