OpenMS / pyopenms-docs

pyOpenMS readthedocs documentation, additional utilities, addons, scripts, and examples.
https://pyopenms.readthedocs.io
Other
42 stars 52 forks source link

Add tutorial how to generate modified sequences using ModifiedPeptideGenerator #289

Closed timosachsenberg closed 1 year ago

timosachsenberg commented 2 years ago

e.g., create AASequence object, getModifications "Oxidiation (M)" and then applyVariableModifications

Ayesha-Feroz commented 1 year ago

@poshul @timosachsenberg kindly provide more details about this task or provide some references so that I may obtain an understanding of how to perform that task.

timosachsenberg commented 1 year ago

Hi, We want to add a pyopenms tutorial that shows how to use the ModifiedPeptideGenerator class.

What I usually do is I first check if the python test has already some code: https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/tests/unittests/test000.py In our case it hasn't. (shame on the person that wrapped it ;) )

The C++ tests are usually the next thing I check to see how they can be used in C++ if there is no other good documentation: https://github.com/OpenMS/OpenMS/blob/develop/src/tests/class_tests/openms/source/ModifiedPeptideGenerator_test.cpp

What we want now: a pyopenms tutorial on the pyopenms.readthedocs page that shows new users of pyOpenMS how to use this class. Think about someone who never used OpenMS.

What I would do: use this C++ code https://github.com/OpenMS/OpenMS/blob/d2a64a62e870165558a8eebdf761d5bbaa71fae5/src/tests/class_tests/openms/source/ModifiedPeptideGenerator_test.cpp#L117-L123 and this https://github.com/OpenMS/OpenMS/blob/d2a64a62e870165558a8eebdf761d5bbaa71fae5/src/tests/class_tests/openms/source/ModifiedPeptideGenerator_test.cpp#L117-L123

and translate it to pyOpenMS to show how modified peptides can be generated.

How do you know how to translate the C++ code to pyOpenMS?

I usually check how we wrap the C++ functions https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/pxds/ModifiedPeptideGenerator.pxd

E.g. here you can see that there exists a wrapper called ModifiedPeptideGenerator https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/pxds/ModifiedPeptideGenerator.pxd#L11 and apparently we also wrapped the other functions of the C++ class as python functions with name: https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/pxds/ModifiedPeptideGenerator.pxd#L24-L32 so we probably can already write the python code.

So you probably can do something like:

AASequence sequence = AASequence.fromString("TESTMTESTMTESTR);
mod_names = ["Oxidation (M)"]
mods = ModifiedPeptideGenerator().getModifications(mod_names);

in python to get the map of amino acids to modification objects and then call applyFixed/VariableModifications(...) with it

I will add Arslan and Kyowon as well to this issue because it will be really useful to learn how the C++ part can be exposed in pyOpenMS.

To try this out, it should be sufficient to install pyopenms from our nightly pypi installer.

Arslan-Siraj commented 1 year ago

Thankyou Timo, its seems helpful.

KyowonJeong commented 1 year ago

This is very useful. I anyhow see the pyOpenMS deeper. Thanks Timo.

timosachsenberg commented 1 year ago

some additional details on how C++ classes are wrapped can be found here: https://pyopenms.readthedocs.io/en/latest/wrapping_workflows_new_classes.html

KyowonJeong commented 1 year ago

Unfortunately I could not contribute to this one much. I would like to continue in the next sprint though.

timosachsenberg commented 1 year ago

Yes please make this work. We really need your help now and in the future.

Ayesha-Feroz commented 1 year ago

Hi, I have come up with this code/tutorial for the sprint task

First, we need to import the necessary modules:

import pyopenms

**Defining the Amino Acid Sequence

We will use the amino acid sequence "TESTMTESTMTESTR" for this tutorial. To define the sequence using pyOpenMS, we can use the AASequence.fromString() function:**

sequence = pyopenms.AASequence.fromString("TESTMTESTMTESTR")

**Defining the Modifications

Next, we need to define the modifications we want to apply. In this tutorial, we will apply the "Oxidation (M)" modification to the methionine residues in the sequence. To define the modifications, we can use the ModifiedPeptideGenerator().getModifications() function**

mod_names = ["Oxidation (M)"] modifications = pyopenms.ModifiedPeptideGenerator().getModifications(mod_names)

**Generating Modified Peptides with Fixed Modifications

To generate modified peptides with fixed modifications, we can use the ModifiedPeptideGenerator().applyFixedModifications() function:**

fixed_peptides = pyopenms.ModifiedPeptideGenerator().applyFixedModifications(sequence, modifications)

**Generating Modified Peptides with Variable Modifications

To generate modified peptides with variable modifications, we can use the ModifiedPeptideGenerator().applyVariableModifications() function. We also need to specify the maximum number of variable modifications per peptide. In this tutorial, we will set this value to 1**

max_variable_mods = 1 variable_peptides = pyopenms.ModifiedPeptideGenerator().applyVariableModifications(sequence, modifications, max_variable_mods)

**Printing the Modified Peptides

Finally, we can print the modified peptides using a for loop:**

for peptide in fixed_peptides: print("Fixed:", peptide.toString())

for peptide in variable_peptides: print("Variable:", peptide.toString())

:--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in 1 mod_names=["Oxidation (M)"] ----> 2 modifications= pyopenms.ModifiedPeptideGenerator().getModifications(mod_names)

AttributeError: 'pyopenms.pyopenms_6.ModifiedPeptideGenerator' object has no attribute 'getModifications'

I have used Spyder. on my mac, the pyopenms version installed is 2.7.0. I have also used Colab attaching the reference pic, and getting the same Attribute error.

Image

error Also facing same error when used pyopenms 2.8 online binder

jpfeuffer commented 1 year ago

Hi! @timosachsenberg This class is wrongly wrapped: https://github.com/OpenMS/OpenMS/blob/9f4c98476dbb4d087db74ab15f1edac662f9d8b2/src/pyOpenMS/pxds/ModifiedPeptideGenerator.pxd#LL23C14-L23C14

Wrong indentation and in general deprecated way of defining static methods.

timosachsenberg commented 1 year ago

@Ayesha-Feroz can you check how other static methods are wrapped in pyOpenMS and provide a fix (including correct indentation)?

timosachsenberg commented 1 year ago

https://github.com/OpenMS/OpenMS/commit/81eb197a6e6a1785a826099c8f7b4d58972bdf7e

timosachsenberg commented 1 year ago

what is the state here @Ayesha-Feroz

Ayesha-Feroz commented 1 year ago

@timosachsenberg was busy with TAC meetings presentations reports, and research; tried to make time on Sunday but was unsuccessful; will try again next week and report here.

timosachsenberg commented 1 year ago

@jpfeuffer I just checked in binder and it seems nothing is wrapped as static function.

print(dir(ModifiedPeptideGenerator()))
['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '_init_0', '_init_1']

any obviously wrong in: https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/pxds/ModifiedPeptideGenerator.pxd

jpfeuffer commented 1 year ago

Nothing obvious. Hard to check since the build artifacts are gone.

Ayesha-Feroz commented 1 year ago

Upon testing the python code using binder pyopenms versions 3.0 (with Timo thanks for his time), it has been observed that the relevant class might be inadequately wrapped or exposed. This implies that the Python environment is not able to access certain aspects of the class's functionality, or the functionality may not operate as expected. Additional analysis is necessary to determine the specific issues with the wrapping or exposure of the class and rectify them accordingly.

jpfeuffer commented 1 year ago

@jpfeuffer I just checked in binder and it seems nothing is wrapped as static function.

print(dir(ModifiedPeptideGenerator()))
['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '_init_0', '_init_1']

any obviously wrong in: https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/pxds/ModifiedPeptideGenerator.pxd

It is generated correctly in the latest PRs according to the Jenkins artifacts. Please check that you are using an uptodate version. In this case, check that the wheels are correctly uploaded and binder fetches the latest ones.

jpfeuffer commented 1 year ago

Pyopenms.version.version in binder is 20230303. Very outdated. Please fix first.

timosachsenberg commented 1 year ago

is this because we use https://github.com/OpenMS/pyopenms-docs/blob/master/postBuild to install pyopenms and we recently renamed the package name from pyopenms-nightly to just pyopenms? I can fix that

timosachsenberg commented 1 year ago

https://github.com/OpenMS/pyopenms-docs/pull/386

timosachsenberg commented 1 year ago

k I think the version should be up to date... maybe @Ayesha-Feroz can double check in binder?

Ayesha-Feroz commented 1 year ago

method is now there thanks @timosachsenberg @jpfeuffer now testing my code.

Ayesha-Feroz commented 1 year ago
upload
timosachsenberg commented 1 year ago

update?

Ayesha-Feroz commented 1 year ago

https://github.com/OpenMS/pyopenms-docs/pull/390/commits/db4eaaeff749a246c728ab25104053307f545d66 updated code and Timo comments. trying to test code on binder and facing difficulties in accessing Binder from both my Windows and Mac machines. Specifically, I am only able to successfully connect to Binder once in a weekend, and subsequent attempts to access it have been unsuccessful. Tested it on google collab and facing an error, talked to Timo he said it might be broken because our Python server seems to be offline. Now I will manually install the wheel and test the code.

timosachsenberg commented 1 year ago

thanks for the update! yeah seems the docker that hosts our python packages went offline. Seems soe wheels are available through our nightly builds on GitHub that you can try: https://github.com/OpenMS/OpenMS/actions/workflows/pyopenms-wheels.yml just select one and scroll down to the artefacts that match your system