#!/usr/bin/env python # coding: utf-8 # # Bgee # # Extracting data from [Bgee](http://bgee.unil.ch/). See [this Thinklab discussion](http://thinklab.com/discussion/tissue-specific-gene-expression-resources/81#278) for more information. # In[1]: import collections import pandas import seaborn import matplotlib.pyplot as plt import IPython get_ipython().run_line_magic('matplotlib', 'inline') # In[2]: def get_groupby_counts(df, columns): """Group datagrame by columns and return the number of rows for each grouping.""" grouped = df.groupby(columns) get_len = lambda df: pandas.Series({'count': len(df)}) df = grouped.apply(get_len) df = df.sort('count', ascending=False) df = df.reset_index() return df # ## Read and process presence of expression data # In[3]: # Read expression presence_df = pandas.read_table('download/Homo_sapiens_expr-simple.tsv.gz', compression='gzip') presence_df.head() # In[4]: # Apply filters for gene presence present_df = presence_df[ presence_df['Call quality'].isin({'high quality', 'low quality'}) & presence_df['Expression'].isin({'present', 'low ambiguity'}) ] # In[5]: # Find genes present per developmental stage stage_df = get_groupby_counts(present_df, ['Developmental stage ID', 'Developmental stage name']) stage_df.head(12) # In[6]: # Find genes present per anatomical entity anatomy_df = get_groupby_counts(present_df, ['Anatomical entity ID', 'Anatomical entity name']) anatomy_df.head(10) # ### Figure 1: Number of genes present by developmental stage and anatomical entity # In[7]: # Number of present genes per development stage -- anatomical entity pair pairwise_df = get_groupby_counts(present_df, ['Developmental stage name', 'Anatomical entity name']) rect_df = pairwise_df.pivot('Developmental stage name', 'Anatomical entity name', 'count').fillna(0) rect_df = rect_df.loc[stage_df['Developmental stage name'][:12], anatomy_df['Anatomical entity name'][:40]] IPython.core.pylabtools.figsize(12, 3.2) seaborn.heatmap(rect_df); # ## Read and process differential expression data # In[18]: # Read simple differential expression by anatomy diffex_df = pandas.read_table('download/Homo_sapiens_diffexpr-anatomy-simple.tsv.gz', compression='gzip') diffex_df.head() # In[20]: # filter differential expression diffex_df = diffex_df[ diffex_df['Differential expression'].isin({'over-expression', 'under-expression'}) & diffex_df['Call quality'].isin({'low quality', 'high quality'}) ] # In[21]: # Calculate counts per anatomy--DE pair de_anatomy_df = get_groupby_counts(diffex_df, ['Anatomical entity ID', 'Anatomical entity name', 'Differential expression']) de_anatomy_df.head() # ### Figure 2: Number of differntially expressed genes present by anatomical entity # In[29]: # Plot DE counts rect_df = de_anatomy_df.pivot('Anatomical entity name', 'Differential expression', 'count').fillna(0) rect_df['differential expression'] = rect_df['under-expression'] + rect_df['over-expression'] rect_df = rect_df.sort('differential expression', ascending=False) IPython.core.pylabtools.figsize(1, 16) cmap = seaborn.cubehelix_palette(15, start=2.2, rot=1, gamma=1.6, hue=1, light=0.98, dark=0, as_cmap=True) seaborn.heatmap(rect_df.drop('differential expression', axis=1), cmap=cmap); # ## Conversion from ensembl to entrez gene # # Not yet implemented # In[13]: # Read Entrez Genes url = 'https://raw.githubusercontent.com/dhimmel/entrez-gene/6e133f9ef8ce51a4c5387e58a6cc97564a66cec8/data/genes-human.tsv' entrez_df = pandas.read_table(url) coding_genes = set(entrez_df.GeneID[entrez_df.type_of_gene == 'protein-coding']) # Merge with entrez gene identifiers url = 'https://raw.githubusercontent.com/dhimmel/entrez-gene/6e133f9ef8ce51a4c5387e58a6cc97564a66cec8/data/xrefs-human.tsv' entrez_map_df = pandas.read_table(url) entrez_map_df = entrez_map_df[entrez_map_df.resource == 'Ensembl'] entrez_map_df = entrez_map_df[['GeneID', 'identifier']].rename(columns={'identifier': 'ensembl_id'}) entrez_map_df.head() # ## GNF Expression Atlas (BodyMap) expression counts # # Show the presence counts per tissue we observed in the [GNF Expression Atlas](https://dx.doi.org/10.1073/pnas.0400782101) data. # In[14]: import gzip import io import requests import numpy # Read BTO id and names url = 'https://gist.githubusercontent.com/dhimmel/1f252b674c0c75443cc1/raw/a97c3425792f2288b0369de70e1b46018832455b/bto-terms-in-gnf.tsv' bto_df = pandas.read_table(url) # Read expression url = 'http://het.io/disease-genes/downloads/files/expression.txt.gz' with gzip.open(io.BytesIO(requests.get(url).content)) as read_file: gnf_df = pandas.read_table(read_file) gnf_df = numpy.log10(gnf_df) # In[15]: # Compute expressed genes per tissue gnf_summary_df = (gnf_df >= 1.4).sum().reset_index() gnf_summary_df.columns = ['bto_id', 'count'] gnf_summary_df = bto_df.merge(gnf_summary_df, how='right') gnf_summary_df = gnf_summary_df.sort('count', ascending=False) gnf_summary_df.head(15) # ### Figure 3: Distibution of genes present per tissue in GNF Expression Atlas # In[16]: # Plot distribution of expressed genes per tissue IPython.core.pylabtools.figsize(5, 2) plt.hist(gnf_summary_df['count'], 15); # In[ ]: