-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Have conversion from stratified biom to melted pandas table / tensor #125
Comments
Alright as promised, here is the solution for turning these function tables into microbe x gene counts from scipy.sparse import coo_matrix
import pandas as pd
import numpy as np
def get_microbe_gene_table(func_table):
""" Obtain a genes per microbe table.
Parameters
----------
func_table : path
Stratified biom table output from woltka
Returns
-------
Table of microbes by gene counts
"""
func_ids = func_table.ids(axis='observation')
func_df = pd.DataFrame(list(map(lambda x: x.split('|'), func_ids)))
# convert to sparse matrix for convenience
func_df.columns = ['OGU', 'KEGG']
func_df['count'] = 1
ogus = list(set(func_df['OGU']))
ogu_lookup = pd.Series(np.arange(0, len(ogus)), ogus)
keggs = list(set(func_df['KEGG']))
kegg_lookup = pd.Series(np.arange(0, len(keggs)), keggs)
func_df['OGU_id'] = func_df['OGU'].apply(lambda x: ogu_lookup.loc[x]).astype(np.int64)
func_df['KEGG_id'] = func_df['KEGG'].apply(lambda x: kegg_lookup.loc[x]).astype(np.int64)
c, i, j = func_df['count'].values, func_df['OGU_id'].values, func_df['KEGG_id'].values
data = coo_matrix((c, (i, j)))
# pandas conversion optional. Can convert to biom if needed
ko_ogu = pd.DataFrame(data.todense(), index=ogus, columns=keggs)
return ko_ogu |
@mortonjt Thank you for sharing thoughts and code! Will be great if you can clarify what is a microbe x gene counts table? By reading your code I have the following impression. Is my understanding correct? Before:
After:
Also pinging @droush because this question may be relevant. |
Not quite, the code that I provided loses the sample information - it also keeps track of gene copy number per microbe
Another useful format would be a sparse COO format, where the output would be 4 columns like
|
@mortonjt Very good idea! I edited your response a bit to make the tables rendering correctly. |
Right now, the biom table OGU ids consist of both taxa and KEGG ids. It would be nice if there were convenience functions to allow for conversions to gene tables or tensors -- it is nontrivial to implement this from scratch.
I'm pretty close to a working solution, will post on this thread shortly
The text was updated successfully, but these errors were encountered: