0

I have a pandas dataframe which looks like:

gene1   gene2        pvalue        qvalue
TP53.   FUBP1       6.381282e-09.  2.435575e-08
TP53.    CIC        1.570489e-22.  7.055298e-22
IDH1.    NF1        1.946551e-05.  7.116265e-05

I want a matrix as shown below:

Image

So I to plot a matrix which has gene names as rows and columns and colours only those cells for which gene pairs are found in the pandas dataframe above. How can I achieve this in pandas.

3
  • 2
    That looks like a diagonal correlation matrix to me: seaborn.pydata.org/examples/many_pairwise_correlations.html medium.com/@szabo.bibor/… Commented Sep 12, 2021 at 11:04
  • That's what I thought too. But it uses .corr() function. I don't want to do that. I just want to go color those cells for which pairs are found in the pandas dataframe and color them according to pvalue column. Do you have ideas how can I modify the code to do that? Commented Sep 12, 2021 at 11:32
  • You could pre-process your data to fit a diagonal form and then plot it in a heatmap manually: stackoverflow.com/questions/65670428/… Commented Sep 12, 2021 at 11:39

1 Answer 1

1

Here is a working version:

import seaborn as sns

# cleanup
df['pvalue'] = df['pvalue'].str[:-1].astype(float)
df['gene1']  = df['gene1'].str[:-1]

idx = sorted(set(df['gene1']).union(df['gene2']))
df2 = (df.pivot(index='gene1', columns='gene2', values='pvalue')
         .reindex(index=idx, columns=idx)
      )

mask = df2.isna()&np.isnan(df2.T.values) # keep track of NAs on both combinations of genes (A/B and B/A)

df2 = df2.fillna(0)
df2 += df2.values.T  # fill matrix A/B → B/A
df2 = df2.mask(mask) # restore NAs

cmap = sns.diverging_palette(323, 101, s=60, as_cmap=True)
ax = sns.heatmap(data=-np.log10(df2), mask=np.triu(df2.values), center=0, cmap=cmap)
ax.invert_yaxis()
ax.xaxis.tick_top()

output: heatmap

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.