BjornFJohansson / pydna

Clone with Python! Data structures for double stranded DNA & simulation of homologous recombination, Gibson assembly, cut & paste cloning.
Other
163 stars 42 forks source link

Protospacer must be a string when initializing cas9 #257

Closed manulera closed 1 week ago

manulera commented 2 weeks ago

This is just to document an error that @JeffXiePL came across in this notebook:

https://github.com/JeffXiePL/pydna/blob/docs_peilun/docs/notebooks/CRISPR.ipynb

You were trying to instantiate a cas9 object by passing a seqrecord as an argument to the function cas9, that's why you were getting the error. However, the error you committed in the notebook:

TypeError                                 Traceback (most recent call last)
Cell In[12], line 2
      1 # Initializing the Cas9 protein
----> 2 enzyme = cas9(gRNA_sequence)
      4 # Simulating the CRISPR-Cas9 cut
      5 cut_products = target_sequence(enzyme)

TypeError: 'cas9' object is not callable

Is not the error you should be getting. Perhaps you re-assigned cas9 to a variable? Sometimes is good to restart the kernel and re-run the notebook. You should be getting the error below:

TypeError                                 Traceback (most recent call last)
Cell In[30], [line 2](vscode-notebook-cell:?execution_count=30&line=2)
      [1](vscode-notebook-cell:?execution_count=30&line=1) # Initializing the Cas9 protein
----> [2](vscode-notebook-cell:?execution_count=30&line=2) enzyme = cas9(gRNA_sequence)
      [4](vscode-notebook-cell:?execution_count=30&line=4) # Simulating the CRISPR-Cas9 cut
      [5](vscode-notebook-cell:?execution_count=30&line=5) cut_products = target_sequence(enzyme)

File ~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/crispr.py:32, in _cas.__init__(self, protospacer)
     [29](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/crispr.py:29) def __init__(self, protospacer):
     [30](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/crispr.py:30)     self.protospacer = protospacer.upper()
     [31](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/crispr.py:31)     self.compsite = re.compile(
---> [32](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/crispr.py:32)         f"(?=(?P<watson>{self.protospacer}{self.pam}))|(?=(?P<crick>{rc(self.pam)}{rc(self.protospacer)}))",
     [33](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/crispr.py:33)         re.UNICODE,
     [34](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/crispr.py:34)     )

File ~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/utils.py:264, in rc(sequence)
    [259](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/utils.py:259) def rc(sequence: str):
    [260](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/utils.py:260)     """Reverse complement.
    [261](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/utils.py:261) 
    [262](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/utils.py:262)     accepts mixed DNA/RNA
    [263](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/utils.py:263)     """
--> [264](https://file+.vscode-resource.vscode-cdn.net/Users/manu/Documents/OpenSource/summer_internship_2024/pydna/docs/notebooks/~/Documents/OpenSource/summer_internship_2024/pydna/src/pydna/utils.py:264)     return sequence.translate(_complement_table)[::-1]

TypeError: SeqRecord.translate() takes 1 positional argument but 2 were given

This error occurs because cas9 takes a string as an input, not a Dseqrecord. However, note that:

A good place to start and see how it works is to see the examples from the tests in tests/test_module_crispr.py, you can take that as a starting example for how the cas9 enzyme works.

Also, you were trying to cut using:

cut_products = target_sequence(enzyme)

Instead, it should be:

cut_products = target_sequence.cut(enzyme)
manulera commented 1 week ago

Hi @JeffXiePL closing this as I explain the fix. Feel free to comment if you have further questions.