saezlab / liana-py

LIANA+: an all-in-one framework for cell-cell communication
http://liana-py.readthedocs.io/
GNU General Public License v3.0
156 stars 21 forks source link

Trouble with liana: 'liana.resource' has no attribute 'explode_complexes' #77

Closed merlevede closed 9 months ago

merlevede commented 10 months ago

Hello,

To make my analysis reproducible, I am creating an environment for my decoupler analyses and I am not able to run the liana quantification anymore. I am using the code described in the doc to perform this analysis:

liana_lr = ln.resource.select_resource()
liana_lr = ln.resource.explode_complexes(liana_lr)

# Create two new DataFrames, each containing one of the pairs of columns to be concatenated
df1 = liana_lr[['interaction', 'ligand']]
df2 = liana_lr[['interaction', 'receptor']]

# Rename the columns in each new DataFrame
df1.columns = ['interaction', 'genes']
df2.columns = ['interaction', 'genes']

# Concatenate the two new DataFrames
liana_lr = pd.concat([df1, df2], axis=0)
liana_lr['weight'] = 1

# Find duplicated rows
duplicates = liana_lr.duplicated()

# Remove duplicated rows
liana_lr = liana_lr[~duplicates]

I get the following error message:

'liana.resource' has no attribute 'explode_complexes'

It seems that the code of liana select_resource was updated last month. I have the impression that the function ln.resource.explode_complexes is not accessible anymore.

Have you encountered a similar issue?

I am using the following versions: decoupler 1.4.0 liana 1.0.3

Thanks for your help.

dbdimitrov commented 10 months ago

Hi @merlevede, I also noticed it. I will make sure to have it accessible in the next update (should be very soon).

merlevede commented 10 months ago

Hi @merlevede, I also noticed it. I will make sure to have it accessible in the next update (should be very soon).

Great, thanks a lot @dbdimitrov! Just to have an idea about what you mean by "very soon", is it a couple of days? I had nice results in my former analysis that I wanted to report for a project deliverable that I have to deliver in the coming days.

dbdimitrov commented 9 months ago

Hi @merlevede,

It might take me more than a couple of days, in the meantime you can use the folllowing function to replace the one that is used in the decoupler tutorial:

def explode_complexes(resource: pd.DataFrame,
                      SOURCE='ligand',
                      TARGET='receptor') -> pd.DataFrame:
    """
    Function to explode ligand-receptor complexes

    Parameters
    ----------
    resource
        Ligand-receptor resource
    SOURCE
        Name of the source (typically ligand) column
    TARGET
        Name of the target (typically receptor) column

    Returns
    -------
    A resource with exploded complexes

    """
    resource['interaction'] = resource[SOURCE] + '&' + resource[TARGET]
    resource = (resource.set_index('interaction')
                .apply(lambda x: x.str.split('_'))
                .explode([TARGET])
                .explode(SOURCE)
                .reset_index()
                )
    resource[[f'{SOURCE}_complex', f'{TARGET}_complex']] = resource[
        'interaction'].str.split('&', expand=True)

    return resource
dbdimitrov commented 9 months ago

This should be resolved now with the latest update.