CoolFace
Apppublic

Rules99/Bioinformatics_Project

sourceHugging Faceupdated 5y agoView on Hugging Face
2likes
app.py808 linesDownload Raw Back to root
1import numpy as np2import pickle3import pandas as pd4# import requests5# from selenium import webdriver6import matplotlib.pyplot as plt7#Simple assignment8# from selenium.webdriver import Firefox9# from selenium.webdriver.common.keys import Keys10# from selenium.common.exceptions import NoSuchElementException11# import requests 12import os13import seaborn as sns14from collections import Counter15import plotly.express as px16import streamlit as st17 18 19 20### Scrap the cosmic id information21# ### FRAMEWORKS NEEDED22 23# def scrap():24            # #### Setting options to the driver25            # options = webdriver.FirefoxOptions()26            # options.add_argument('--headless')27            # options.add_argument('--no-sandbox')28            # options.add_argument('--disable-dev-shm-usage')29            # options.capabilities30            # ### Setting options of webdriver31            # # a) Setting the chromedriver32            # browser = Firefox(options=options,executable_path=r"C:\Users\Pablo\OneDrive\Documents\Documentos\Escuela Politécnica Superior Leganés\4 AÑO\ASIGNATURAS\1 CUATRI\WEB ANALYTICS\PART 2\Milestone3\geckodriver.exe")33            # ### Functions and execution to run the scrapping34 35 36            # def getinfofromtable(oddrows:list,score:float,headertable)->list:37            #         rows = []38            #         for row in oddrows:39            #             cols = []40            #             for (i,col) in enumerate(row.find_elements_by_css_selector("td")):41            #                 if  i==headertable.index( 'Primary Tissue') or  i==headertable.index('Primary Histology') or i==headertable.index('Zygosity'):42            #                     cols.append(col.text)43            #             cols.append(score)44            #             rows.append(cols)45            #         return rows            46            # def getinfocosmic(mutationid):47            #         import time48            #         search = browser.find_element_by_id('search-field')49            #         search = search.find_element_by_class_name("text_def")50            #         search.send_keys(mutationid)51            #         search.send_keys(Keys.RETURN)52            #         time.sleep(5)53            #         try:54            #             container = browser.find_element_by_id("section-list")55                    56            #         except NoSuchElementException:57            #             return []58                    59            #         try:60 61            #             subq1 = container.text[container.text.find("score")+len("score"):]62            #             score = float(subq1[:subq1.find(")")].strip())63            #         except ValueError:64            #             score = 0 65                    66 67 68            #         section = browser.find_element_by_id("DataTables_Table_0")69 70 71            #         headertable = [header.text for header in section.find_element_by_tag_name("thead").find_elements_by_tag_name("th")]72 73            #         oddrows = section.find_elements_by_class_name("odd")74            #         evenrows = section.find_elements_by_class_name("even")75 76            #         l1 = getinfofromtable(oddrows,score,headertable)77            #         l1.extend(getinfofromtable(evenrows,score,headertable))78                    79            #         # browser.close()80            #         return l181            #         ## Looking for cosmic id info82            #         cosl = []83            #         browser.get("https://cancer.sanger.ac.uk/cosmic")84            #         for cos in cosmicinfo.reset_index()["COSMIC_ID"].iloc[20:]:85            #                 if cos.find(",")!=-1:86            #                         cos = cos.split(",")[0]87 88            #                 cosl.append(getinfocosmic(cos))89            #                 browser.get("https://cancer.sanger.ac.uk/cosmic")90### Pieplots91def pieplot(merging,id=0):92    genecount = merging.groupby(by=["gene_name","UV_exposure_tissue","sampleID"]).count().reset_index()93    if id==0:94        gtype = genecount[genecount.UV_exposure_tissue=="Intermittently-photoexposed"]95    if id ==1 :96        gtype = genecount[genecount.UV_exposure_tissue=="Chronically-photoexposed"]97    else:98        gtype = genecount99 100    gtype = gtype.groupby("gene_name").count()["sampleID"].reset_index()101    gtype.sort_values(by="sampleID",ascending=False,inplace=True)102    #define Seaborn color palette to use103    colors = sns.color_palette('pastel')[0:len(gtype)]104    #create pie chart105    # plt.suptitle("Gene Occuring for different genes")106    plt.pie(gtype.sampleID, labels =gtype.gene_name, colors = colors, autopct='%.0f%%',radius=2,textprops={"fontsize":9})107    plt.show()108 109### Depending on what result you want you return one or another110def filterp4(dfgenes,id=0):111    if id==0 or id==1:112 113        if id==0:114            chexposed=  dfgenes[dfgenes.UV_exposure_tissue=="Intermittently-photoexposed"].sort_values(by=["mean_mut"],ascending=False)115        if id==1:116            chexposed=  dfgenes[dfgenes.UV_exposure_tissue=="Chronically-photoexposed"].sort_values(by=["mean_mut"],ascending=False)117        return px.bar(chexposed,x="gene_name",y="mean_mut",error_y="std")118    if id==2:119        return px.bar(dfgenes,x="gene_name",y="mean_mut",color="UV_exposure_tissue",barmode='group',error_y="std")120 121### Read scrapping done with cosmic ids122def read_scrap()->list:123    with open('my_pickle_file.pickle', 'rb') as f :124        cosbase = pickle.load(f)125    return cosbase126### GendfClean127def gendfclean(cosbase,cid)->pd.DataFrame:128        dfd = {"tissue": None , "histology": None,"zygosity": None, "score": None }129        for i,key in enumerate(list(dfd.keys())):130            dfd[key] = list(map(lambda x : np.array(x)[:,i].tolist() if x!=[] else [] ,cosbase))131 132        dfd["cosmic_id"] = cid.tolist()133        cosmicdb = pd.DataFrame(dfd)134        cosmicdb = cosmicdb[(cosmicdb['tissue'].map(lambda d: len(d)) > 0) & (cosmicdb['histology'].map(lambda d: len(d)) > 0) & (cosmicdb['zygosity'].map(lambda d: len(d)) > 0) & (cosmicdb['score'].map(lambda d: len(d)) > 0) ]135 136        cosmicdb["score"] = cosmicdb.score.apply(lambda x: float(x[0]))137 138        return cosmicdb139 140### Look for stats of a gene141def inputgene(lookforgene,merging,id =0)->dict:142        ### id = 0--> Intermittently exposed143        ### id = 1--> Continuously exposed144        genecount = merging.groupby(by=["gene_name","UV_exposure_tissue","sampleID"]).count().reset_index()145        tgene = genecount[genecount.gene_name==lookforgene]146        if id==0:147            ph_gene = tgene[tgene.UV_exposure_tissue=='Intermittently-photoexposed']148        else:149            ph_gene = tgene[tgene.UV_exposure_tissue=="Chronically-photoexposed"]150        ### Statistiacs about gene|samples 151        stats = ph_gene.chr.describe()152        dc = dict(stats)153        dc["gene_name"] = lookforgene154        if id==0:155            dc["UV_exposure_tissue"] = 'Intermittently-photoexposed'156        else:157            dc["UV_exposure_tissue"] = 'Chronically-photoexposed'158        return  dc159### Look for stats of all genes160def gene_exposed(merging,id=0):161    return pd.DataFrame(list(map(lambda gene: inputgene(gene,merging,id),merging.gene_name.unique())))162### Merge stats for continuous and intermittently exposed163def mergecontintinfo(merging):164        ### Continuously Exposed 165        cont_exposed_info = gene_exposed(merging,1)166        ### Intermittently Exposed167        int_exposed_info = gene_exposed(merging,0)168        return pd.concat([cont_exposed_info,int_exposed_info],axis=0)169 170#### Common tissues, zygosities and histologies171def explodecommon(bd,N,col):172        return  Counter(bd[col].apply(lambda x: list(x.keys())).explode()).most_common(N)173def pdcommon(db,col,uv:str)->pd.DataFrame:174        df = pd.DataFrame(db).rename(columns={0:col,1:"Times_{}".format(col)})175        df["UV_exposure_tissue"] = uv176        return df177def get_N_common(df,col,N=10)->pd.DataFrame:178        cosm = df.copy(True)179        cosm[col] = cosm[col].apply(lambda x: Counter(x)) 180        intcosm = cosm[cosm.UV_exposure_tissue=="Intermittently-photoexposed"]181        contcosm = cosm[cosm.UV_exposure_tissue=="Chronically-photoexposed"]182 183        infotissues = explodecommon(cosm,N,col)184        inttissues = explodecommon(intcosm,N,col)185        contissues = explodecommon(contcosm,N,col)186 187        df1 = pdcommon(infotissues,col,"Total")188        df2 = pdcommon(inttissues,col,"Intermittently-photoexposed")189        df3 = pdcommon(contissues,col,"Chronically-photoexposed")190        return pd.concat([df1,df2,df3],axis=0)191 192### Deatiled information of mutation type193def mut_type(x):194    if x.mut_type=="Indel":195        196            if len(x.ref)>len(x.mut):197                    return "Del"198            elif len(x.mut)>len(x.ref):199                    return "In"200        #     if len(x.ref)>1 and len(x.mut)>1:201            202            return x.ref+">"+x.mut203    return x.mut_type204 205 206def distribution_gene(df,hue):207    208    209    plot4 = df.groupby([hue,"mut_type_cus"]).count().reset_index().iloc[:,:3]210    plot4 = plot4.rename(columns={"sampleID":"n_mut"})211    plot4 = plot4.sort_values(by="mut_type_cus",ascending=True)212    fig = px.bar(plot4,x="mut_type_cus",y="n_mut",color=hue,barmode="group")213    return fig 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241directory = os.path.abspath("")242# from EDA_IMDb_functions import *243 244 245 246 247 248st.set_page_config(layout="wide")249st.set_option('deprecation.showPyplotGlobalUse', False)250dw,col1,wl = st.columns((1,0.5,1))251col1.image('img/descarga.jfif')252st.markdown("<h1 style='text-align:center;'>Somatic Mutations Analysis in skin</h1>",unsafe_allow_html=True)253 254st.sidebar.markdown("<h2 style='text-align:center;'>Index</h2>",unsafe_allow_html=True)255menu = st.sidebar.radio(256    "",257    ("1. Intro", "2. Analysis of somatic mutations" ,'3. Sample Analysis (IGV)'),258)259 260# Pone el radio-button en horizontal. Afecta a todos los radio button de una página.261# Por eso está puesto en este que es general a todo262# st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)263 264st.sidebar.markdown('---')265st.sidebar.markdown("<h2 style='text-align:center;'>Authors</h2>",unsafe_allow_html=True)266st.sidebar.markdown("<p style='text-align:center;'>Claudio Sotillos Peceroso</p>",unsafe_allow_html=True)267st.sidebar.markdown("<p style='text-align:center;'>Pablo Reyes Martin</p>",unsafe_allow_html=True)268 269 270 271 272@st.cache(allow_output_mutation=True)273def read_csv():274    return pd.ExcelFile('Study Results.xlsx')275 276 277### Merge two dataframes278def definemerging(df1,df2):279    280    merging = df1.merge(df2,on="sampleID",how="inner")281    return merging282#### Reading the data283df = read_csv()284df1 = pd.read_excel(df, 'Dataset_S1').copy(deep=True)285df2 = pd.read_excel(df, 'Dataset_S2').copy(deep=True)286merging = None287### functions indexed288def wrt(text,tag="h1",align="center"):289    return st.markdown(f"""290               <{tag} style='text-align:{align};'>{text}</{tag}>291                """,unsafe_allow_html=True) 292def writetxt(text,tag="h1",align="center",container=st):293    return container.markdown(f"""294               <{tag} style='text-align:{align};'>{text}</{tag}>295                """,unsafe_allow_html=True)296def space(tag="h1"):297    return wrt("\n",tag=tag)298 299### 1. Introduciton300def set_home():301    wrt("1. Introduction to the problem",tag="h2")302   303    304    par1 = """305        In this project, we have focus our study on the research paper as well as on306        getting some conclusion by ourselves. Therefore there will be some contrast we will do in relation to the paper 307        as well as other plots not related with the paper. Also, we will perform the analysis of two samples that are 308        from quite opposite individuals."""309    310    ### 1.1311    wrt(par1,tag="p style='font-size:21px;'",align="justify")312    tit1 = """313            1.1 Cancers arise as a result of somatic mutations314    """315    wrt(tit1,tag="h4",align="left")316    ___,col1,_,col2,___ = st.columns((0.4,1,0.7,1,0.4))317 318    cap1 = "Fig 1 : Types of Genomic Alterations"319    320    col1.image("img/base.png",caption=cap1,width=500,use_column_width=True)321   322    cap2 = "Fig 2 : Skin Risk Factors"323    col2.image("img/risk_factors.png",caption=cap2,width=500,use_column_width=True)324    325    326    with col2:327        col2_par = """328                      The proportion of somatic mutations in normal cells is quite similar to the mutations of tumours in the same tissue cell.329                    Only a small fraction are the ones which provokes cancer.330        """331        wrt(col2_par,tag="p style='font-size:20px;'",align="justify")332 333    space()334 335    ### 1.2336    337    tit1 = """338            1.2 State of the art of the research339    """340    wrt(tit1,tag="h4",align="left")341    ___,col1,_,col2,___ = st.columns((0.4,1.4,0.3,0.6,0.4))342 343    cap1 = "Fig 1 : Types of Genomic Alterations"344    345    col1.image("img/comboskin.JPG",caption="Fig 3 : Cutaneous Sensitivity",width=600,use_column_width=True)346   347    # cap2 = "Fig 2 : Skin Risk Factors"348    col2.image("img/s_exposure.JPG",width=600,use_column_width=True)349    350 351 352    # col1,middle,col2 = st.columns((0.2,3,0.2))353    with col2:354    355        col2_par = """356                        - About 25,50% of the normal skins acquires one driver mutation.357                        - Chronic sun exposure tends to proliferate keratinocytes (cutaneous squamous cell carcinoma)358                        - Melanomas appears more in sporadic skin sun exposure (attributable to intermittent pattern of sun exposure with recreational activities)359 360            """361        st.markdown(col2_par)362        # wrt(col2_par,tag="p",align="center")363 364    space()365 366 367 368    par2=    """369        The research studies which are the main factors that causes somatic mutations in skin. The research analyzes 46 genes per sample.370        Skin samples were collected from different body areas, classified according to the pattern of sunlight exposure as371        chronically-photoexposed (n = 44) and intermittently photoexposed (n = 79).372    """373    374 375 376 377    wrt("1.3 Skin samples analysis",tag="h4",align="left")378    379    wrt(par2,tag="p style='font-size:21px;'",align="justify")380    import plotly.graph_objects as go381    set = ["Chronically Sun Exposure","Inttermittently Sun Exposure"]382    n = [44,79]383    384    fig = go.Figure(data=[go.Pie(labels=set, values=n,textinfo=f'label+percent',385                                insidetextorientation='radial',hole=.3)])386                            387    fig.update_layout(title_text ="Samples of the dataset depending on the sun exposure",annotations=[dict(text="46 genes per sample",font_size=20, showarrow=False)])388    __,col1,__,col2,__ = st.columns((0.2,1,0.5,0.5,0.2))389    col1.plotly_chart(fig,use_container_width=True)390    val = round(df1.groupby("sampleID").count()["chr"].mean(),2)391    pnt = round(val/46,2)392    col2.header("\n\n\n")393    col2.header("\n\n\n")394    col2.header("\n\n\n")395    col2.metric("Average of mutations per sample",val)396    col2.metric("Average Percentage of genes that mutate per sample",pnt)397 398 399#### 2. Plots Dataframe visualization Conclusion400def set_chintp():401    global merging402    wrt("2. Analysis of somatic mutations",tag="h3")403    par1 = """404            We are going to discuss some plots related to the paper. Moreover,  we are going to explore mutations of genes that occured within our samples as well as go beyond our data base, exploring 405            mutations that have cosmic ids so that we can show more detailed information about the mutation. We often face samples that are intermittently photoexposed to sunlight against the ones which are continuously exposed406            to contrast two distinct populations.407    """408    wrt(par1,tag="p style='font-size:21px;",align="justify")409 410    wrt("2.1 Description of the dataset",tag="h4",align="left")411    par1 = """412            They focused on sequencing 123 samples of healthy skin (from different areas,permanently or intermittently photo-exposed) of cancer-free 413            individuals. It was taken just one skin sample per individual.414            From each of these samples, a deep sequencing of 46 genes (which are implicated in skin cancer) is carried out. 415            This means that they had to analyze 5658 genes. Out of these amount of genes they found 5214 somatic mutations, which are the ones 416            which we have in our dataset.417    """418    wrt(par1,tag="p style='font-size:21px;",align="justify")419 420    st.image("img/dataset.png",width=600,use_column_width=True)421    st.image("img/skin.png",width=600,use_column_width=True)422    wrt("2.2 Average and Standard deviation of mutations",tag="h4",align="left")423    par3 ="""424            The research tells that if the skin were exposed more to the skin, it will be more likely to suffer somatic mutations.     425    The first plot represent the average and sd of mutations that have samples which are intermittently and continuously exposed to sunlight.426    """427    wrt(par3,tag="p style='font-size:21px;",align="justify") 428    429    #### CODE PLOT 1430    merging = definemerging(df1,df2)431    plot = merging.groupby("sampleID").agg({"gene_name":"count"}).merge(df2[["sampleID","UV_exposure_tissue"]],on="sampleID",how="inner")432    plot = plot.rename(columns={"gene_name":"n_of_mutations"})433    fig = plt.figure(figsize=(5,3))434    sns.barplot(data=plot,x="UV_exposure_tissue",y="n_of_mutations")435    436    plt.suptitle("Average and standard deviation of mutations per type tissue photoexposed")437 438    st.pyplot(fig,clear_figure=True)439 440 441    442    wrt("\n")443    ### Plot 3444   445    # """Number of samples that has a mutation at least one time per  gene"""446    wrt("2.3 Number of samples per gene captured at least one time",tag="h4",align="left")447    par = """448            This takes the number of times that a gene appears at least once time in the samples intermittently and continuously exposed.449            The first plots is used for the samples that are intermittently exposed and the second plot is used for the ones which are continously exposed 450    """451    print( merging.UV_exposure_tissue.unique())452    wrt(par,tag="p style='font-size:21px;",align="justify")453    wrt("Select the exposure tissue you want",tag="h6",align="left")454    box = st.selectbox("", merging.UV_exposure_tissue.unique().tolist()+["Total"],key="key1")455    if box==merging.UV_exposure_tissue.unique()[0]:456 457          458            wrt("Gene proportion (Intermittently Exposed)",tag="h5",align="center")459            pieplot(merging,0)460            st.pyplot()461    if box==merging.UV_exposure_tissue.unique()[1]:462            wrt("Gene proportion (Continuously Exposed)",tag="h5",align="center")463            pieplot(merging,1)464            st.pyplot()465    if box=="Total":466            wrt("Gene proportion (Total)",tag="h5",align="center")467            pieplot(merging,2)468            st.pyplot()469    470 471    ### Plot 4472 473    dfgenes = mergecontintinfo(merging)474    dfgenes["mean_mut"] = dfgenes["mean"]475 476    wrt("2.4 Average and standard deviation of mutations per gene",tag="h4",align="left")477    par = """478        In this graph we will visualize the average and standard deviation of mutation per gene479    """480    wrt(par,tag="p style='font-size:21px;",align="justify")481    wrt("Select the exposure tissue you want",tag="h6",align="left")  482    box2 = st.selectbox("", merging.UV_exposure_tissue.unique().tolist()+["Total"],key="key2")483    if box2==merging.UV_exposure_tissue.unique()[0]:484 485        fig = filterp4(dfgenes)486        487    if box2==merging.UV_exposure_tissue.unique()[1]:488        fig = filterp4(dfgenes,1)489    if box2=="Total":490        fig = filterp4(dfgenes,2)491    st.plotly_chart(fig,use_container_width=True)492    493 494    #### SECTION 2.2495 496   497    wrt("2.5 Number of mutations per gene and skin phototype",tag="h4",align="left")498    par ="""499            These visualizations reflectas the average of mutations that are per age of samples and is divided per skin_phototype 500    """501    wrt(par,tag="p style='font-size:21px;",align="justify")502 503    #### CODE PLOT 2504    col1,col2 = st.columns((1,1))505    plot2= df2.merge(merging.groupby("sampleID").agg({"gene_name":"count"}).reset_index(),on="sampleID",how="inner")506    plot2 =plot2.rename(columns={"gene_name":"n_mutations"})507    plot2.n_mutations = plot2.n_mutations.astype(int)508    ### plot 2.1509    sns.set()510    plt.figure()511    sns.lmplot(data=plot2.sort_values(by="skin_phototype"),x="age",y="n_mutations",col="skin_phototype",lowess=True,col_wrap=2)512    col1.pyplot()513    514    515    ### plot 2.2516   517    sns.set()518    plt.figure()    519    sns.lmplot(data=plot2,x="age",y="n_mutations",hue="skin_phototype",lowess=True)520    plt.title("Regression of number of mutations per skin phototype")521    col2.pyplot()522    writetxt("""Fig 4:Number of mutations per gene and skin phototype""",tag="h6",container=col2 )523 524    wrt("2.6 Number of mutations per exposure tissue condition and sun damage tissue",tag="h4",align="left")525    par ="""526           We take the average of mutations that we have per type of sample (exposure_tissue type) and if it presents sun damage tissue a priori or not 527    """528    wrt(par,tag="p style='font-size:21px;",align="justify")529    530    fig = plt.figure(figsize=(5,1))531    p = sns.displot(df2, x="UV_exposure_tissue", hue="sun_damage_tissue", multiple="dodge",height=3,aspect=4)532    p.fig.set_dpi(100)533    st.pyplot(clear_figure=True)534 535 536 537    wrt("2.7 Influence of sun damage tissue",tag="h4",align="left")538    par ="""539           We can see that the sun damage tissue influence generally on the number of mutations in the gene. 540    """541    wrt(par,tag="p style='font-size:21px;",align="justify")542    543    fig = plt.figure(figsize=(5,1))544    p=sns.displot(df2, x="age", hue="sun_damage_tissue", multiple="dodge",height=3,aspect=4).set(title='Age VS Sun Damage')545 546    p.fig.set_dpi(100)547    st.pyplot(clear_figure=True)548 549 550    #### Age vs uv photexposure551    wrt("2.8 Number of mutations per year and UV_exposure tissue",tag="h4",align="left")552    par =""" We can distinguish between two sample the average of mutations that are got per year"""553    wrt(par,tag="p style='font-size:21px;",align="justify")554    fig = plt.figure(figsize=(5,2))555    # sns.set(rc={'figure.figsize':(3,1),"figure.height":2})556    ax = sns.kdeplot(data=df2, x="age", hue="UV_exposure_tissue")557    plt.setp(ax.get_legend().get_texts(), fontsize='5') # for legend text558    plt.setp(ax.get_legend().get_title(), fontsize='8') # for legend title559    st.pyplot()560    ### Mut type cus plot561    wrt("2.9 How is the mutation in comparison with the reference base ?",tag="h4",align="left")562    par ="""563        We want to know between all the mutations occured (with and without cosmic id) what are the most common mutation type occured 564        """565    566    567    568    wrt(par,tag="p style='font-size:21px;",align="justify")569    merging["mut_type_cus"] = merging.apply(mut_type,axis=1)570 571    hue = st.selectbox("Select label for you want to color: ",['sex', 'UV_exposure_tissue', 'sun_damage_tissue',572       'sun_history', 'skin_phototype'],index=1)573    fig = distribution_gene(merging,hue)574    # plot4 = merging.groupby(["UV_exposure_tissue","mut_type_cus"]).count().reset_index().iloc[:,:3]575    # plot4 = plot4.rename(columns={"sampleID":"n_mut"})576    # plot4 = plot4.sort_values(by="mut_type_cus",ascending=True)577    # fig = px.bar(plot4,x="mut_type_cus",y="n_mut",color="UV_exposure_tissue",barmode="group")578    st.plotly_chart(fig,use_container_width=True)579    580    ### Muations cosmic and non cosmic id581    wrt("2.10 Where does mutations usually occur ?",tag="h4",align="left")582    par = "In this section we visualize where the mutations usually occurs as well as tumours that may be caused these mutations. Note that we can get this information just for all the samples that contains cosmic id."583    wrt(par,tag="p style='font-size:21px;",align="justify")584    # merging = definemerging(df1,df2)585    #### Cosmicinformation586    cosmicinfo = merging[merging.COSMIC_ID.isna()==False].groupby(["gene_name","COSMIC_ID"]).count()587    #### Cosmic ids588    cinfo = cosmicinfo.reset_index() 589    cid = cinfo["COSMIC_ID"]590 591    ### Read info scrapped 592 593    cosbase = read_scrap()594    cosmicdb = gendfclean(cosbase,cid)595    cosmerge = merging.merge(cosmicdb,left_on="COSMIC_ID",right_on="cosmic_id",how="inner")596    cols = ["tissue","histology"]597    p1,p2 = st.columns((1,1))598    for col,p in zip(cols,[p1,p2]):599        dtemp = get_N_common(cosmerge,col)600        #### Intermittently exposed601        #### Continuously exposed602        fig = px.bar(dtemp,x=dtemp.columns[0],y=dtemp.columns[1],color=dtemp.columns[2],barmode="group")603        p.plotly_chart(fig)604    605    ### Percentage of VAF 606    wrt("2.11 Percentage of VAF captured in all mutations and its distribution. Depth Coverage distribution",tag="h4",align="left")607    par=""" We want to proof that somatic mutations are tough to capture . To do that we have plot the variant allele frequency608            distribution of the mutations to proof that the majority of them have a low score VAF609    """610    wrt(par,tag="p style='font-size:21px;'",align="justify")611    res = str(round(((df1.shape[0]-sum(df1['VAF']>0.05))/df1.shape[0])*100,2))+'%'612    613    # plt.figure(figsize=(5,3))614 615    fig = px.box(df1,y="VAF") 616    fig2 = px.box(df1,y="DP")617    __,col1,__,col2,__ = st.columns((0.3,1,0.3,1,0.3))618    # plt.text('Those outliers are the ones higher than 5%. There are',str(df1.shape[0]-sum(df1['VAF']<0.05)),'mutations higher than 5%.')619    col1.plotly_chart(fig,clear_figure=True,use_container_width=True)620    col2.plotly_chart(fig2,clear_figure=True,use_container_width=True)621    par =f"""622        Those outliers are the ones higher than 5%. There are {str(df1.shape[0]-sum(df1['VAF']<0.05))} mutations higher than 5%.623    """624    wrt(par,tag="p",align="center")625 626 627 628    wrt("2.12 Evolution of the C>T mutations with the age",tag="h4",align="left")629    par =f"""630        The plot shows us that the number of C>T (a UV associated mutation) mutations increases with the evolution of the age.  631    """632    wrt(par,tag="p style='font-size:21px;'",align="center")633    plts = merging[merging.mut_type=="C>T"].groupby("age").count().reset_index()[["age","VAF"]]634    plts.rename(columns={"VAF":"C>T"},inplace=True)635    __,col1,__ = st.columns((1,3,1))636    fig = plt.figure(5,2)637    sns.lmplot(638        data=plts,639        x="C>T",640        y="age",641        logx=True,642        height=3, aspect=3643    ).set(title="Logarithmic increase of number of C>T")644    col1.pyplot()645 646    wrt("2.13 Mean of pathogenic score in different genes",tag="h4",align="left")647    # par =f"""648    #     The plot shows us that the number of C>T (a UV associated mutation) mutations increases with the evolution of the age.  649    # """650    # wrt(par,tag="p style='font-size:21px;'",align="center")651    # plts = merging[merging.mut_type=="C>T"].groupby("age").count().reset_index()[["age","VAF"]]652    # plts.rename(columns={"VAF":"C>T"},inplace=True)653    # __,col1,__ = st.columns((1,3,1))654    # fig = plt.figure(5,2)655    # sns.lmplot(data=plts,x="C>T",y="age",logx=True,height=3,aspect=3).set(title="Logarithmic increase of number of C>T")656    # col1.pyplot()657    dbinfo = merging.merge(cosmicdb,left_on="COSMIC_ID",right_on="cosmic_id",how="left")658    df12 = dbinfo[dbinfo.cosmic_id.isna()==False].groupby("gene_name").mean().reset_index()[["gene_name","score"]].sort_values(by="score",ascending=False)659    ___,col,___ = st.columns((2,1,2))660    with col:661        st.dataframe(df12)662 663 664    return None665def cidvars():666    667    wrt("3. Individual gene information",tag="h2")668    par1 = """669            We are going to explore mutations of genes that occured within our samples as well as go beyond our data base, exploring 670            mutations that have cosmic ids so that we can show more detailed information about the mutation.671    """672    wrt(par1,tag="p",align="center")673    674 675 676 677    wrt("3.1 Where does mutations usually occur ?",tag="h3",align="left")678    par = "In this section we visualize where the mutations usually occurs as well as tumours that may be caused these mutations. Note that we can get this information just for all the samples that contains cosmic id"679    wrt(par,tag="p",align="justify")680    merging = definemerging(df1,df2)681    #### Cosmicinformation682    cosmicinfo = merging[merging.COSMIC_ID.isna()==False].groupby(["gene_name","COSMIC_ID"]).count()683    #### Cosmic ids684    cinfo = cosmicinfo.reset_index() 685    cid = cinfo["COSMIC_ID"]686 687    ### Read info scrapped 688 689    cosbase = read_scrap()690    cosmicdb = gendfclean(cosbase,cid)691    cosmerge = merging.merge(cosmicdb,left_on="COSMIC_ID",right_on="cosmic_id",how="inner")692    cols = ["tissue","histology"]693    p1,p2 = st.columns((1,1))694    for col,p in zip(cols,[p1,p2]):695        dtemp = get_N_common(cosmerge,col)696        #### Intermittently exposed697        #### Continuously exposed698        fig = px.bar(dtemp,x=dtemp.columns[0],y=dtemp.columns[1],color=dtemp.columns[2],barmode="group")699        p.plotly_chart(fig)700    701    ### Mut type cus plot702    wrt("3.2 How is the mutation in comparison with the reference base ?",tag="h3",align="left")703    par ="""704        We want to know between all the mutations occured (with and without cosmic id) what are the most common mutation type occured 705        """706    wrt(par,tag="p",align="justify")707    merging["mut_type_cus"] = merging.apply(mut_type,axis=1)708    709    plot4 = merging.groupby(["UV_exposure_tissue","mut_type_cus"]).count().reset_index().iloc[:,:3]710    plot4 = plot4.rename(columns={"sampleID":"n_mut"})711    fig = px.bar(plot4,x="mut_type_cus",y="n_mut",color="UV_exposure_tissue",barmode="group")712    713    col1.plotly_chart(fig,use_container_width=True)714    col2.plotly_chart()715    return None716 717## Slide in which we will explain the pipeline followed in order to analyze the two samples we have.718def set_igv():719    720    wrt("3. Sample Analysis (IGV)",tag="h2")721    par1 = """722            Explanation of the steps followed to process our raw samples into files which can be interpreted with the IGV program  (VCF files).723    """724    wrt(par1,tag="p style='font-size:21px;'",align="center")725    726 727 728 729    wrt("3.1 Which Samples do we have?",tag="h4",align="left")730    par2 = """731    We wanted two samples of very oposite individuals, with the goal of being able to appreciate noticable differences when sequencing732    the genes. 733 734    - AG0312: Sample of a Male individual of 70 years old, whose skin sample is Intermittently-photoexposed, and it has a skin phototype of I. 735    - AG0322: Sample of a Female individual of 38 years old, whose skin sample is Chronically-photoexposed, and it has a skin phototype of IV.736    737    """738    739    wrt(par2,tag="p style='font-size:21px;'",align="justify")740 741 742    wrt("3.2 Processing Steps",tag="h4",align="left")743    par3 = """744        - 1. Quality Control and Alignment745        Initially, both samples were in bam format, thus are samples of good quality (since they passed the quality step) and also are aligned. 746        Thus we started our preprocessing by refinning the Alignment. 747    """748    par4 = """749        - 2. Refinement of Alignment750        Since for performing Variant Calling we need to have our samples sorted by genomic positions. Once sorted we mark 751        the duplicates (in these exist) so that the these are ignored in the Variant Calling step.752        Finally we just index the resulting bam files for its future visualization.753    """754    par5 = """755        - 3. Variant Calling756        Once we arrived to this section we had to solve an error which initially we didn't knew why it happend. The error ocurred when 757        we tried to identify the active regions (error shown on the below image). 758        The problem consited on that, in the refined bam the chromosomes were represented just with their number, and in the reference 759        genome (hg19) chromosomes were represented as "chr N". Thus just by eliminating the "chr" part in the reference genome, the 760        variant calling could be accomplished. 761    """762 763    par6 = """764        - 4. Visualization with IGV765        With the variant calling files computed, we are ready now to display the variants in our Alignments. 766        We tried to do a simple task, which was just to try to find the mutations which the researchers of the paper found for our samples. 767        For doing this we used the chromosome and position indicators of their dataset. 768        Near the positions idicated we found the mentioned mutation in the dataset, however not in the exact position.769    """770    wrt(par3,tag="p style='font-size:21px;'",align="justify")771    wrt(par4,tag="p style='font-size:21px;'",align="justify")772    wrt(par5,tag="p style='font-size:21px;'",align="justify")773    st.image("img/error.png",use_column_width=True)774    wrt(par6,tag="p style='font-size:21px;'",align="justify")775    st.image("img/chartc.png",use_column_width=True)776    st.header("\n")777    st.header("\n")778    st.header("\n")779    780    wrt("Igv Images",tag="h1",align="center")781    st.header("\n")782    st.header("\n")783    st.header("\n")784   785    786    __,col1,__,col2,__ = st.columns((0.2,1.5,0.3,1,0.2))787    with col1:788        st.image("img/col1igv.png",use_column_width=True,caption="Fig 5: Igv rep AG0312")789    with col2:790        st.image("img/col2igv.png",use_column_width=True,caption="Fig 6: Igv representation AG0322")791    return None792 793 794if menu == '1. Intro':795    set_home()796elif menu == '2. Analysis of somatic mutations':797    set_chintp()798# elif menu == '3. Individual sample information':799#     cidvars()800 801elif menu == '3. Sample Analysis (IGV)':802    set_igv()803# elif menu == 'Otras variables':804#     set_otras_variables()805# elif menu == 'Relaciones entre variables':806#     set_relations()807# elif menu == 'Matrices de correlación':808#     set_arrays()