示例#1
0
def plot(l1000cds2_results, plot_counter, nr_drugs=7, height=300):

    # Check if there are results
    if not l1000cds2_results['mimic'] or not l1000cds2_results['reverse']:
        display(
            Markdown(
                '### No results were found.\n This is likely due to the fact that the gene identifiers were not recognized by L1000CDS<sup>2</sup>. Please note that L1000CDS<sup>2</sup> currently only supports HGNC gene symbols (https://www.genenames.org/). If your dataset uses other gene identifier systems, such as Ensembl IDs or Entrez IDs, consider converting them to HGNC. Automated gene identifier conversion is currently under development.'
            ))
    else:
        # Links
        if l1000cds2_results['signature_label']:
            display(
                Markdown('\n### {signature_label} signature:'.format(
                    **l1000cds2_results)))
        display(Markdown(' **L1000CDS<sup>2</sup> Links:**'))
        display(
            Markdown(' *Mimic Signature Query Results*: {url}'.format(
                **l1000cds2_results['mimic'])))
        display(
            Markdown(' *Reverse Signature Query Results*: {url}'.format(
                **l1000cds2_results['reverse'])))

        # Bar charts
        fig = tools.make_subplots(rows=1, cols=2, print_grid=False)
        for i, direction in enumerate(['mimic', 'reverse']):
            drug_counts = l1000cds2_results[direction]['table'].groupby(
                'pert_desc').size().sort_values(
                    ascending=False).iloc[:nr_drugs].iloc[::-1]

            # Get Bar
            bar = go.Bar(x=drug_counts.values,
                         y=drug_counts.index,
                         orientation='h',
                         name=direction.title(),
                         hovertext=drug_counts.index,
                         hoverinfo='text',
                         marker={
                             'color':
                             '#FF7F50' if direction == 'mimic' else '	#9370DB'
                         })
            fig.append_trace(bar, 1, i + 1)

            # Get text
            text = go.Scatter(
                x=[max(bar['x']) / 50 for x in range(len(bar['y']))],
                y=bar['y'],
                mode='text',
                hoverinfo='none',
                showlegend=False,
                text=drug_counts.index,
                textposition="middle right",
                textfont={'color': 'black'})
            fig.append_trace(text, 1, i + 1)

        fig['layout'].update(
            height=height,
            title=
            '<b>L1000CDS<sup>2</sup> | Small Molecule Query</b><br><i>Top small molecules</i>',
            hovermode='closest')
        fig['layout']['xaxis1'].update(domain=[0, 0.5])
        fig['layout']['xaxis1'].update(title='<br>Count')
        fig['layout']['xaxis2'].update(title='<br>Count')
        fig['layout']['xaxis2'].update(domain=[0.5, 1])
        fig['layout']['yaxis1'].update(showticklabels=False)
        fig['layout']['yaxis2'].update(showticklabels=False)
        fig['layout']['margin'].update(l=10, t=95, r=0, b=45, pad=5)

        if l1000cds2_results['plot_type'] == 'interactive':
            iplot(fig)
        else:
            s.static_plot(fig)

        # Download
        result_list = []
        for direction, result_dict in l1000cds2_results.items():
            if direction in ['mimic', 'reverse']:
                df = pd.DataFrame(result_dict['table'])
                df['direction'] = direction
                result_list.append(df)
        result_txt = pd.concat(result_list).to_csv(index=False, sep='\t')
        s.download_button(result_txt, 'Download Results',
                          'l1000cds2_results.txt')

        # Figure Legend
        display(
            Markdown(
                '** Figure ' + plot_counter() +
                ' | L1000CDS<sup>2</sup> Query results. **The figure contains an interactive bar chart displaying the top small molecules identified by the L1000CDS2 query. The left panel displays the small molecules which mimic the observed gene expression signature, while the right panel displays the small molecules which reverse it.  Links to the L1000CDS2 web server are additionally provided, allowing users to interactively explore the analysis results. If you are experiencing issues visualizing the plot, please visit our <a href="https://amp.pharm.mssm.edu/biojupies/help#troubleshooting" target="_blank">Troubleshooting guide</a>.'
                .format(**locals())))
示例#2
0
def plot(library_size_results, plot_counter):
    colors = [
        '#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c',
        '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928'
    ]
    color_by = library_size_results['color_by']
    sample_metadata = library_size_results['sample_metadata'].loc[
        library_size_results['library_sizes'].index]
    if color_by:
        colored_string = '<i>, colored by {}</i>'.format(color_by)
        color_column = sample_metadata[color_by]
        if str(color_column[0]).isnumeric():
            color = sample_metadata[color_by]
        else:
            unique_categories = color_column.unique()
            color_dict = {
                cat: colors[i]
                for i, cat in enumerate(unique_categories)
            }
            color = [color_dict[x] for x in color_column]
    else:
        color = 'rgb(0,119,177)'
        colored_string = ''
    text = [
        '<b>{}</b><br>'.format(index) +
        '<br>'.join('<i>{key}</i>: {value}'.format(**locals())
                    for key, value in rowData.items())
        for index, rowData in sample_metadata.iterrows()
    ]
    data = [
        go.Bar(x=library_size_results['library_sizes'],
               y=sample_metadata.index,
               orientation='h',
               text=text,
               hoverinfo='text',
               marker={'color': color})
    ]
    layout = go.Layout(
        margin={
            'l': 100,
            't': 75,
            'r': 0,
            'b': 50,
            'pad': 5
        },
        title=
        '<b>Library Size Analysis | Bar Plot</b><br><i>Million reads per sample</i>'
        + colored_string,
        xaxis={'title': 'Million Reads'})
    fig = go.Figure(data=data, layout=layout)

    # Plot
    if library_size_results['plot_type'] == 'interactive':
        iplot(fig)
    else:
        s.static_plot(fig)

    # Figure Legend
    display(
        Markdown(
            '** Figure ' + plot_counter() +
            ' | Library Size Analysis results. **The figure contains an interactive bar chart which displays the total number of reads mapped to each RNA-seq sample in the dataset. Additional information for each sample is available by hovering over the bars. If provided, sample groups are indicated using different colors, thus allowing for easier interpretation of the results. If you are experiencing issues visualizing the plot, please visit our <a href="https://maayanlab.cloud/biojupies/help#troubleshooting" target="_blank">Troubleshooting guide</a>.'
            .format(**locals())))
示例#3
0
def plot(pca_results, plot_counter):

    # Get results
    pca = pca_results['pca']
    var_explained = pca_results['var_explained']
    sample_metadata = pca_results['sample_metadata']
    color_by = pca_results.get('color_by')
    color_type = pca_results.get('color_type')
    color_column = pca_results['sample_metadata'][
        color_by] if color_by else None
    colors = [
        '#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c',
        '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928'
    ]
    sample_titles = [
        '<b>{}</b><br>'.format(index) +
        '<br>'.join('<i>{key}</i>: {value}'.format(**locals())
                    for key, value in rowData.items())
        for index, rowData in sample_metadata.iterrows()
    ]

    if color_by and color_type == 'continuous':
        marker = dict(size=15,
                      color=color_column,
                      colorscale='Viridis',
                      showscale=True)
        trace = go.Scatter3d(x=pca.components_[0],
                             y=pca.components_[1],
                             z=pca.components_[2],
                             mode='markers',
                             hoverinfo='text',
                             text=sample_titles,
                             marker=marker)
        data = [trace]
    elif color_by and color_type == 'categorical' and len(
            color_column.unique()) <= len(colors):

        # Get unique categories
        unique_categories = color_column.unique()

        # Define empty list
        data = []

        # Loop through the unique categories
        for i, category in enumerate(unique_categories):

            # Get the color corresponding to the category

            # If signature
            if color_by == 'Sample Group':
                group_A, group_B = [
                    x.split(' vs ')
                    for x in pca_results['signature_metadata'].keys()
                ][0]
                if category == group_A:
                    category_color = 'blue'
                elif category == group_B:
                    category_color = 'red'
                else:
                    category_color = 'black'
            else:
                category_color = colors[i]

            # Get the indices of the samples corresponding to the category
            category_indices = [
                i for i, sample_category in enumerate(color_column)
                if sample_category == category
            ]

            # Create new trace
            trace = go.Scatter3d(
                x=pca.components_[0][category_indices],
                y=pca.components_[1][category_indices],
                z=pca.components_[2][category_indices],
                mode='markers',
                hoverinfo='text',
                text=[sample_titles[x] for x in category_indices],
                name=category,
                marker=dict(size=15, color=category_color))

            # Append trace to data list
            data.append(trace)
    else:
        marker = dict(size=15)
        trace = go.Scatter3d(x=pca.components_[0],
                             y=pca.components_[1],
                             z=pca.components_[2],
                             mode='markers',
                             hoverinfo='text',
                             text=sample_titles,
                             marker=marker)
        data = [trace]

    colored = '' if str(color_by) == 'None' else 'Colored by {}'.format(
        color_by)
    layout = go.Layout(
        title='<b>PCA Analysis | Scatter Plot</b><br><i>{}</i>'.format(
            colored),
        hovermode='closest',
        margin=go.Margin(l=0, r=0, b=0, t=50),
        width=900,
        scene=dict(xaxis=dict(title=var_explained[0]),
                   yaxis=dict(title=var_explained[1]),
                   zaxis=dict(title=var_explained[2])))
    fig = go.Figure(data=data, layout=layout)

    if pca_results['plot_type'] == 'interactive':
        iplot(fig)
    else:
        s.static_plot(fig)
    display(
        Markdown(
            '** Figure ' + plot_counter() +
            ' | Principal Component Analysis results. ** The figure displays an interactive, three-dimensional scatter plot of the first three Principal Components (PCs) of the data. Each point represents an RNA-seq sample. Samples with similar gene expression profiles are closer in the three-dimensional space. If provided, sample groups are indicated using different colors, allowing for easier interpretation of the results. If you are experiencing issues visualizing the plot, please visit our <a href="https://maayanlab.cloud/biojupies/help#troubleshooting" target="_blank">Troubleshooting guide</a>'
            .format(**locals())))