aertslab / pycisTopic

pycisTopic is a Python module to simultaneously identify cell states and cis-regulatory topics from single cell epigenomics data.
Other
58 stars 12 forks source link

TypeError: 'NoneType' object does not support item assignment - binarize_topics() function #83

Open Sebdumas opened 1 year ago

Sebdumas commented 1 year ago

Hi,

I made a cisTopic object in Python importing the fragment count matrix from a Signac object (cell x peak matrix) and a selected cisTopic model pre-computed in R.

When I try to run the binarize_topics() function, I get the following error:

>>> from pycisTopic.topic_binarization import *       
>>> region_bin_topics_otsu = binarize_topics(cistopic_obj, method='otsu')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/luna.kuleuven.be/xxxxxxx/anaconda3/lib/python3.8/site-packages/pycisTopic/topic_binarization.py", line 154, in binarize_topics
    cistopic_obj.selected_model.topic_ass["Regions_in_binarized_topic"] = [
TypeError: 'NoneType' object does not support item assignment

Python version is 3.8.8 pycisTopic version is 1.0.3.dev18+ge563fb6

Any suggestion to solve that please ?

EDIT: 9th of August: I have now calculated and selected the model using pycisTopic directly, instead of loading the selected model from cisTopic (R), and I don't get anymore the error at this step.

Thank you

Sébastien

SeppeDeWinter commented 1 year ago

Hi Sébastien

How does the precomputed cisTopic object look like once loaded in Python? And how did you add this to the cisTopic object?

Best,

Seppe

Sebdumas commented 1 year ago

Hi Seppe,

Here is the code I run in R to build and save the model

#create cistopic object
cisTopic_obj<-createcisTopicObject(
  combinedEC_ATAC_downsampled_counts, #this is sparse count matrix extracted from the Signac object
  project.name = "cisTopicProject",
  min.cells = 1,
  min.regions = 1,
  is.acc = 1,
  keepCountsMatrix = F,
)

#building models
cisTopic_obj <- runWarpLDAModels(cisTopic_obj, topic=c(2,5,10,15,20,25,30,35,40,45,50), seed=987, nCores=11, 
                                 iterations = 500, addModels=FALSE)

#select a model
par(mfrow=c(3,3))
cisTopic_obj <- selectModel(cisTopic_obj, type='maximum')
cisTopic_obj <- selectModel(cisTopic_obj, type='perplexity')
cisTopic_obj <- selectModel(cisTopic_obj, type='derivative') #selected model 45 Topics

#save
modelMat <- modelMatSelection(cisTopic_obj, 'cell', 'Probability')
modelMat <- as.data.frame(modelMat)
new_colnames <- paste(colnames(modelMat), "___cisTopic", sep = "")
colnames(modelMat) <- new_colnames
write_feather(modelMat, sink=paste0("/media/sdb1/scenicplus/Analysis_aging/data/",'cell_topic.feather'))
modelMat <- modelMatSelection(cisTopic_obj, 'region', 'Probability', all.regions=TRUE)
modelMat <- as.data.frame(modelMat)
write_feather(modelMat, sink=paste0("/media/sdb1/scenicplus/Analysis_aging/data/", 'topic_region.feather'))

I then loaded this feather files in python and added them to the python cistopic object made from the same Signac count matrix.

# Create cisTopic object
import pycisTopic
from pycisTopic.cistopic_class import *
count_matrix=pd.read_csv('/media/sdb1/scenicplus/Analysis_aging/data/count_matrix.tsv', sep='\t')
cistopic_obj = create_cistopic_object(fragment_matrix=count_matrix)

# Adding cell information
cell_data =  pd.read_csv('/media/sdb1/scenicplus/Analysis_aging/data/cell_annotations.tsv', sep='\t')
new_index = [index + "___cisTopic" for index in cell_data.index]
cell_data.index = new_index
cistopic_obj.add_cell_data(cell_data)

## 2. Add model
from pycisTopic.utils import *
def load_cisTopic_model(path_to_cisTopic_model_matrices):
        metrics = None
        coherence = None
        marg_topic = None
        topic_ass = None
        cell_topic = pd.read_feather(path_to_cisTopic_model_matrices + "cell_topic.feather")
        cell_topic.index = ["Topic" + str(x) for x in range(1, cell_topic.shape[0] + 1)]
        topic_region = pd.read_feather(
                path_to_cisTopic_model_matrices + "topic_region.feather"
        )
        topic_region.index = ["Topic" + str(x) for x in range(1, topic_region.shape[0] + 1)]
        topic_region = topic_region.T
        parameters = None
        model = CistopicLDAModel(
                metrics, coherence, marg_topic, topic_ass, cell_topic, topic_region, parameters
        )
        return model

model = load_cisTopic_model('/media/sdb1/scenicplus/Analysis_aging/data/')
cistopic_obj.add_LDA_model(model)

from pycisTopic.topic_binarization import *       
region_bin_topics_otsu = binarize_topics(cistopic_obj, method='otsu')

I am not much an advanced user of Python: please, let me know the command to run for you to get an overview of the cisTopic object. Thank you.

Best,

Sébastien

SeppeDeWinter commented 1 year ago

Hi Sébastien

That should do the trick indeed, can you show the output of model after loading it from disk using

model = load_cisTopic_model('/media/sdb1/scenicplus/Analysis_aging/data/')
Sebdumas commented 1 year ago

Here is what I get:

>>> print(model)
CistopicLDAModel with 45 topics and n_cells × n_regions = 4900 × 346902
>>> vars(model)
{'metrics': None, 'coherence': None, 'marg_topic': None, 'topic_ass': None, 'cell_topic':          SI-NA-A7_AAACTCGCAATAGCGG-1___cisTopic  SI-NA-A7_AAACTGCGTCCCTAAA-1___cisTopic  ...  SI-NA-D1_TTGCAGATCCACTAGA-1___cisTopic  SI-NA-D1_TTGTCTAGTACGTAAA-1___cisTopic
Topic1                                 0.000251                                0.003061  ...                                0.000577                                0.003897
Topic2                                 0.000100                                0.015282  ...                                0.000288                                0.020428
Topic3                                 0.000067                                0.000152  ...                                0.000529                                0.004062
Topic4                                 0.025677                                0.029463  ...                                0.000625                                0.018239
Topic5                                 0.053630                                0.018342  ...                                0.001202                                0.002703
Topic6                                 0.000017                                0.000087  ...                                0.000721                                0.103117
Topic7                                 0.000125                                0.002429  ...                                0.001106                                0.006815
Topic8                                 0.022374                                0.027644  ...                                0.015288                                0.000812
Topic9                                 0.154926                                0.121653  ...                                0.251298                                0.142729
Topic10                                0.035589                                0.074665  ...                                0.001202                                0.010728
Topic11                                0.027484                                0.017656  ...                                0.000769                                0.015968
Topic12                                0.000309                                0.000599  ...                                0.001779                                0.003416
Topic13                                0.006148                                0.023614  ...                                0.033125                                0.008009
Topic14                                0.000301                                0.001993  ...                                0.001683                                0.005323
Topic15                                0.071370                                0.040889  ...                                0.000817                                0.023628
Topic16                                0.000251                                0.001133  ...                                0.000192                                0.001277
Topic17                                0.000410                                0.000359  ...                                0.050962                                0.056624
Topic18                                0.001163                                0.001460  ...                                0.002404                                0.002205
Topic19                                0.000343                                0.000109  ...                                0.000337                                0.012933
Topic20                                0.001355                                0.000065  ...                                0.000240                                0.010413
Topic21                                0.001062                                0.004705  ...                                0.000577                                0.009319
Topic22                                0.000284                                0.001405  ...                                0.000337                                0.011375
Topic23                                0.000728                                0.000251  ...                                0.001058                                0.011623
Topic24                                0.091402                                0.063011  ...                                0.014038                                0.051534
Topic25                                0.027819                                0.018070  ...                                0.001298                                0.012801
Topic26                                0.000234                                0.009639  ...                                0.028365                                0.003648
Topic27                                0.002844                                0.060375  ...                                0.000913                                0.001244
Topic28                                0.064737                                0.038351  ...                                0.060817                                0.070602
Topic29                                0.035572                                0.069905  ...                                0.001298                                0.008738
Topic30                                0.000042                                0.000185  ...                                0.112356                                0.019798
Topic31                                0.006499                                0.000501  ...                                0.009808                                0.067385
Topic32                                0.091143                                0.077399  ...                                0.031202                                0.037490
Topic33                                0.031206                                0.040203  ...                                0.017788                                0.025966
Topic34                                0.065900                                0.088520  ...                                0.004231                                0.006251
Topic35                                0.022784                                0.023538  ...                                0.002163                                0.009435
Topic36                                0.000719                                0.000730  ...                                0.005577                                0.011955
Topic37                                0.001447                                0.010772  ...                                0.000817                                0.002835
Topic38                                0.034192                                0.008561  ...                                0.001202                                0.012535
Topic39                                0.000209                                0.001786  ...                                0.000577                                0.006118
Topic40                                0.000059                                0.000545  ...                                0.004327                                0.003150
Topic41                                0.115390                                0.084370  ...                                0.292692                                0.136578
Topic42                                0.000493                                0.006067  ...                                0.005385                                0.019217
Topic43                                0.003070                                0.009727  ...                                0.015048                                0.001459
Topic44                                0.000142                                0.000294  ...                                0.022452                                0.005024
Topic45                                0.000134                                0.000436  ...                                0.000529                                0.000597

[45 rows x 4900 columns], 'cell_topic_harmony': [], 'topic_region':                             Topic1        Topic2        Topic3        Topic4        Topic5        Topic6  ...       Topic40       Topic41       Topic42       Topic43       Topic44       Topic45
chr1:3012578-3012777  1.383725e-07  1.573430e-07  1.565552e-07  6.781890e-08  1.082175e-07  2.366896e-07  ...  1.457989e-07  4.039188e-08  1.026014e-07  1.338731e-07  1.423757e-07  1.747460e-06
chr1:3071720-3072010  1.383725e-07  1.573430e-07  1.565552e-07  6.781890e-08  1.082175e-07  2.366896e-07  ...  1.457989e-07  4.039188e-08  1.026014e-07  1.338731e-07  1.566132e-06  3.336059e-06
chr1:3083575-3083860  1.383725e-07  1.573430e-07  1.565552e-07  6.781890e-08  1.082175e-07  2.366896e-07  ...  1.603788e-06  4.039188e-08  1.026014e-07  1.338731e-07  1.423757e-07  9.690458e-06
chr1:3119361-3119845  1.383725e-07  1.573430e-07  1.565552e-07  6.781890e-08  1.082175e-07  7.337379e-06  ...  1.457989e-07  4.039188e-08  1.026014e-07  1.338731e-07  1.423757e-07  1.747460e-06
chr1:3121220-3121842  1.383725e-07  1.573430e-07  1.565552e-07  6.781890e-08  1.082175e-07  2.603586e-06  ...  1.457989e-07  4.039188e-08  1.026014e-07  1.338731e-07  1.423757e-07  1.588600e-07
...                            ...           ...           ...           ...           ...           ...  ...           ...           ...           ...           ...           ...           ...
chrY:1273605-1273891  1.383725e-07  3.304203e-06  1.565552e-07  6.781890e-08  1.082175e-07  2.366896e-07  ...  1.603788e-06  4.039188e-08  1.026014e-07  1.338731e-07  1.423757e-07  1.588600e-07
chrY:2087159-2087581  1.383725e-07  1.573430e-07  1.565552e-07  6.781890e-08  1.082175e-07  2.366896e-07  ...  1.457989e-07  4.039188e-08  1.026014e-07  1.338731e-07  1.423757e-07  1.588600e-07
chrY:2342911-2343325  1.383725e-07  1.573430e-07  1.565552e-07  6.781890e-08  1.082175e-07  2.366896e-07  ...  1.457989e-07  4.039188e-08  1.026014e-07  1.338731e-07  1.423757e-07  1.588600e-07
chrY:2415224-2415423  1.383725e-07  1.573430e-07  1.565552e-07  6.781890e-08  2.272567e-06  2.366896e-07  ...  1.457989e-07  4.039188e-08  1.026014e-07  1.338731e-07  1.423757e-07  1.588600e-07
chrY:5361269-5361869  1.383725e-07  1.573430e-07  1.565552e-07  7.460079e-07  1.082175e-07  2.366896e-07  ...  1.457989e-07  4.039188e-08  1.026014e-07  1.338731e-07  1.423757e-07  1.588600e-07

[346902 rows x 45 columns], 'parameters': None, 'n_cells': 4900, 'n_regions': 346902, 'n_topic': 45}
>>> 
foxberrysquash commented 10 months ago

I had the same problem. I just added a line right after loading the model cisTopic_obj.selected_model.topic_ass = pd.DataFrame() That worked