-
Notifications
You must be signed in to change notification settings - Fork 177
WIP: Motif search #1549
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
Draft
mkonstanty
wants to merge
10
commits into
prody:main
Choose a base branch
from
mkonstanty:motif_search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP: Motif search #1549
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7933077
motif search initial commit
mkonstanty ce6e5c8
swiss-prot and refseq dbs added
mkonstanty e7541ec
save dbs to local disk
mkonstanty c62ef97
revoke style changes
mkonstanty ad3d13a
revoke style changes
mkonstanty 4fec6fc
local motif search feature added
mkonstanty 054ecb5
added pdb motif search
mkonstanty 76644cb
modified f-strings
mkonstanty 3a42183
replace f-strings
mkonstanty 07c30d4
motif search app added
mkonstanty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| #!/usr/bin/env python | ||
| # -*- coding: utf-8 -*- | ||
| """This module gets PDB code from MOTIF.""" | ||
|
|
||
| import argparse | ||
| import re | ||
|
|
||
| import requests | ||
| from prody import LOGGER | ||
|
|
||
| __author__ = "Mariusz Konstanty" | ||
|
|
||
| __all__ = ["getPdbFromMotif", "expasySearchMotif", "_expasyTextToStructure"] | ||
|
|
||
| DATABASES = { | ||
| "sp": "Swiss-Prot", | ||
| # "rs": "RefSeq", | ||
| "pdb": "PDB", | ||
| "tr": "TrEMBL", | ||
| "local": "local database", | ||
| } | ||
| MOTIF_PATTERN = ( | ||
| r"^([ARDNCEQGHILKMFPSTWYVx]*" | ||
| r"(\[[ARDNCEQGHILKMFPSTWYV]+\])*" | ||
| r"({{[{{ARDNCEQGHILKMFPSTWYV}}]+}})*" | ||
| r"([ARDNCEQGHILKMFPSTWYVx]\(\d(,\d)?\))*)+$" | ||
| ) | ||
| MOTIF_MATCHER = re.compile(MOTIF_PATTERN) | ||
|
|
||
|
|
||
| def getPdbFromMotif(motif: str, database: str) -> str: | ||
| if database not in DATABASES: | ||
| raise ValueError(f"Database must be one of: {DATABASES.keys()}.") | ||
|
mkonstanty marked this conversation as resolved.
Outdated
|
||
| if not re.match(MOTIF_MATCHER, motif.replace("-", "")): | ||
| raise ValueError(f"{motif} is not valid PROSITE motif.") | ||
| result = expasySearchMotif(motif, database) | ||
| pdb_code = "" | ||
| return pdb_code | ||
|
|
||
|
|
||
| def _expasyTextToStructure(response: str) -> list: | ||
| RESULT_PATTERN = ( | ||
| r">(sp|tr|pdb)\|" | ||
| r"(\w+)\|(\w+).*\n" | ||
| r"(.*)\n" | ||
| r"([ARDNCEQGHILKMFPSTWYVx\n]+)\W+" | ||
| r"(\d{,3}) - (\d{,3}):\W+(\w+)" | ||
| ) | ||
| motifs = re.compile(RESULT_PATTERN, re.M) | ||
| results = re.findall(motifs, response) | ||
| structure = [] | ||
| names = ("database", "accession", "id", "description", "sequence", "start", "end", "match") | ||
| for result in results: | ||
| result = map(lambda x: x.replace("\n", ""), result) | ||
| structure.append(dict(zip(names, result))) | ||
| return structure | ||
|
|
||
|
|
||
| def expasySearchMotif(motif, database): | ||
| """Search motif in remote Swiss-Prot database. | ||
|
|
||
| https://prosite.expasy.org/scanprosite/scanprosite_doc.html#rest_intro | ||
|
|
||
| Args: | ||
| motif (str): Motif in PROSITE format. | ||
| database (str): 'sp' (UniProtKB/Swiss-Prot) or 'tr' (UniProtKB/TrEMBL) or 'pdb' (PDB). | ||
|
|
||
| Returns: | ||
| str: Expasy response from motif search. | ||
| """ | ||
| headers = {"User-Agent": "Python pattern search agent", "Contact": "mkonstanty@gmail.com"} | ||
| url = "https://prosite.expasy.org/cgi-bin/prosite/PSScan.cgi" | ||
| payload = {"sig": motif, "db": database} | ||
| try: | ||
| result = requests.get(url, params=payload, headers=headers) | ||
| except Exception as exception: | ||
| print(f"Remote search for motif {motif} in Swiss-Prot database failed: {exception}") | ||
| return [] | ||
| else: | ||
| return _expasyTextToStructure(result.text) | ||
|
|
||
|
|
||
| def _argMotif(motif: str) -> str: | ||
| """Create motif type for argparse.ArgumentParser. | ||
|
|
||
| Args: | ||
| value (str): motif to be validated | ||
|
|
||
| Returns: | ||
| str: valid Motif | ||
| """ | ||
| new_motif = str(motif).replace("-", "") | ||
| if not re.match(MOTIF_MATCHER, new_motif): | ||
| msg = f"{motif} is not a valid PROSITE MOTIF." | ||
| raise argparse.ArgumentTypeError(msg) | ||
| return new_motif | ||
|
|
||
|
|
||
| def _parseArgs(): | ||
| """Parse arguments from the command line. | ||
|
|
||
| Returns: | ||
| argparse.Namespace: parsed script arguments | ||
| """ | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "-m", | ||
| "--motif", | ||
| type=_argMotif, | ||
| required=True, | ||
| help="motif to search in a databases", | ||
| ) | ||
| parser.add_argument( | ||
| "-d", | ||
| "--databases", | ||
| choices=DATABASES, | ||
| action="append", | ||
| required=True, | ||
| help="choose the databases to search from", | ||
| ) | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = _parseArgs() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| from prody import LOGGER, _expasyTextToStructure, expasySearchMotif | ||
| from prody.tests import TestCase | ||
|
|
||
| LOGGER.verbosity = None | ||
|
|
||
|
|
||
| EXPASY_SCAN_PROSITE_TOOL_RESPONSE = """<pre>include splice variants (Swiss-Prot) | ||
| Output format: Text | ||
|
|
||
| Hits for USERPAT1'P-x(2)-G-E-S-G(2)-[AS]' motif on UniProtKB/Swiss-Prot sequences: | ||
| UniProtKB/Swiss-Prot (Release 2022_01 of 23-Feb-22) contains 566,996 entries. | ||
|
|
||
| found: 11 hits in 11 sequences | ||
|
|
||
| Graphical view (graphical view with feature detection) | ||
| shaded alignment of hits | ||
|
|
||
| : | ||
| >USERPAT1 (user pattern) : | ||
| Pattern: P-x(2)-G-E-S-G(2)-[AS] | ||
| Approximate number of expected random matches in ~ 100'000 sequences (50'000'000 residues): 0.44 | ||
|
|
||
|
|
||
| >sp|Q3SYW2|CO2_BOVIN (750 aa) | ||
| Complement C2 (EC 3.4.21.43) (C3/C5 convertase) [Cleaved into: Complement C2b fragment; Complement C2a fragment]. [Bos taurus (Bovine)] | ||
| MDPLMAVLCLLPLYPGLATAALSCPKNVNISGGSFTLSNGWNPGSILTYSCPLGHYPYPVVTRLCKSNGQWQIPRSTRST | ||
| KAICKPVRCPAPVSFENGVYIPRLGSHPVGGNLSFECEDGFTLRGSAVRQCRPNGMWDGETAVCDNGASHCPNPGISVGA | ||
| VRTGSRFGLGDKVRYRCSSNLVLTGSAERECQDDGVWSGTEAICRQPYSYDFPEDVAPALGTSFSHLLATTNPIQQKKKQ | ||
| NLGRKIQIQRSGHLNLYLLLDASQSVSKDDFEIFKDSASRMVDRIFSFEIKVSVAIITFASKPKIIMSVLEDRSRDVTEV | ||
| ENSLRNINYKDHENGTGTNIYEALHAVYIMMNNQMNRPHMNPGAWQEIRHAIILLTDGKSNMGGSPKVAVDNIKEVLNIN | ||
| QKRKDYLDIYAIGVGSLHVDWKELNNLGSKKDGERHAFILKDVQALSQVFEHMLDVSQLTDPICGVGNMSANASAQERTP | ||
| WHVTIKPKSQETCRGALISDQWVLTAAHCFRNAEDRTLWRVSVGDPNFQGSKEFQIEEAVISPGFNVFSKKSQGIPEFYG | ||
| DDIALLKLTQKVKMSTHARPICLPCTVGANLALRKLPGSTCRDHEKELLNQVSIPAHFVALNGDKLNINLKTGSEWTNCV | ||
| KVVLKDKTTFPNLTDVREVVTDQFLCSGTQGDDSPCKGESGGAVFLERRLRFFQVGLVSWGLYNPCGGSSKNSRKPAPHG | ||
| KVPRDFHINLFRLQPWLRQHLEGILNFVPL | ||
| 675 - 683: PckGESGGA | ||
|
|
||
|
|
||
| >sp|Q863A0|CO2_GORGO (752 aa) | ||
| Complement C2 (EC 3.4.21.43) (C3/C5 convertase) [Cleaved into: Complement C2b fragment; Complement C2a fragment]. [Gorilla gorilla gorilla (Western lowland gorilla)] | ||
| MGPLMVLFCLLFVYTGLADSAPSCPQNVNISGGTFTLSHGWAPGSLLTYSCPQGLYPSPASRLCKSSGQWQTPGATRSLS | ||
| KAVCKPVRCPAPVSFENGIYTPRLGSYPVGGNVSFECEDGFILRGSPVRQCRPNGMWDGETAVCDNGAGHCPNPGISLGA | ||
| VRTGFRFGHGDKVRYRCSSNLVLTGSSERECQGNGVWSGTEPICRQPYSYDFPEDVAPALGTSFSHMLGATNPTQKTKES | ||
| LGRKIQIQRSGHLNLYLLLDCSQSVSENDFLIFKESASLMVDRIFSFEINVSVAIITFASKPKVLMSVLNDNSRDMTEVI | ||
| SSLENANYKDHENGTGTNTYAALNSVYLMMNNQMRILGMETMAWQEIRHAIILLTDGKSNMGGSPKTAVDRIREILNINQ | ||
| KRNDYLDIYAIGVGKLDVDWRELNELGSKKDGERHAFILQDTKALHQVFEHMLDVSKLTDTICGVGNMSANASDQERTPW | ||
| HVTIKPKSQETCRGALISDQWVLTAAHCFRDGNDHSLWRVNVGDPKSQWGKEFLIEKAVISPGFDVFAKKNQGILEFYGD | ||
| DIALLKLAQKVKMSTHARPICLPCTMEANLALRRPQGSTCRDHENELLNKQSVPAHFVALNGSKLNINLKMGVEWTSCAE | ||
| VVSQEKTMFPNLTDVREVVTDQFLCSGTQEDESPCKGESGGAVFLERRFRFFQVGLVSWGLYNPCLGSADKNSRKRAPRS | ||
| KVPPPRDFHINLFRMQPWLRQHLGDVLNFLPL | ||
| 674 - 682: PckGESGGA</pre>""" | ||
|
|
||
| EXPASY_SCAN_PROSITE_TOOL_RESULT = [ | ||
| { | ||
| "database": "sp", | ||
| "accession": "Q3SYW2", | ||
| "id": "CO2_BOVIN", | ||
| "description": "Complement C2 (EC 3.4.21.43) (C3/C5 convertase) [Cleaved into: Complement C2b fragment; Complement C2a fragment]. [Bos taurus (Bovine)]", | ||
| "sequence": "MDPLMAVLCLLPLYPGLATAALSCPKNVNISGGSFTLSNGWNPGSILTYSCPLGHYPYPVVTRLCKSNGQWQIPRSTRST" | ||
| "KAICKPVRCPAPVSFENGVYIPRLGSHPVGGNLSFECEDGFTLRGSAVRQCRPNGMWDGETAVCDNGASHCPNPGISVGA" | ||
| "VRTGSRFGLGDKVRYRCSSNLVLTGSAERECQDDGVWSGTEAICRQPYSYDFPEDVAPALGTSFSHLLATTNPIQQKKKQ" | ||
| "NLGRKIQIQRSGHLNLYLLLDASQSVSKDDFEIFKDSASRMVDRIFSFEIKVSVAIITFASKPKIIMSVLEDRSRDVTEV" | ||
| "ENSLRNINYKDHENGTGTNIYEALHAVYIMMNNQMNRPHMNPGAWQEIRHAIILLTDGKSNMGGSPKVAVDNIKEVLNIN" | ||
| "QKRKDYLDIYAIGVGSLHVDWKELNNLGSKKDGERHAFILKDVQALSQVFEHMLDVSQLTDPICGVGNMSANASAQERTP" | ||
| "WHVTIKPKSQETCRGALISDQWVLTAAHCFRNAEDRTLWRVSVGDPNFQGSKEFQIEEAVISPGFNVFSKKSQGIPEFYG" | ||
| "DDIALLKLTQKVKMSTHARPICLPCTVGANLALRKLPGSTCRDHEKELLNQVSIPAHFVALNGDKLNINLKTGSEWTNCV" | ||
| "KVVLKDKTTFPNLTDVREVVTDQFLCSGTQGDDSPCKGESGGAVFLERRLRFFQVGLVSWGLYNPCGGSSKNSRKPAPHG" | ||
| "KVPRDFHINLFRLQPWLRQHLEGILNFVPL", | ||
| "start": "675", | ||
| "end": "683", | ||
| "match": "PckGESGGA", | ||
| }, | ||
| { | ||
| "database": "sp", | ||
| "accession": "Q863A0", | ||
| "id": "CO2_GORGO", | ||
| "description": "Complement C2 (EC 3.4.21.43) (C3/C5 convertase) [Cleaved into: Complement C2b fragment; Complement C2a fragment]. [Gorilla gorilla gorilla (Western lowland gorilla)]", | ||
| "sequence": "MGPLMVLFCLLFVYTGLADSAPSCPQNVNISGGTFTLSHGWAPGSLLTYSCPQGLYPSPASRLCKSSGQWQTPGATRSLS" | ||
| "KAVCKPVRCPAPVSFENGIYTPRLGSYPVGGNVSFECEDGFILRGSPVRQCRPNGMWDGETAVCDNGAGHCPNPGISLGA" | ||
| "VRTGFRFGHGDKVRYRCSSNLVLTGSSERECQGNGVWSGTEPICRQPYSYDFPEDVAPALGTSFSHMLGATNPTQKTKES" | ||
| "LGRKIQIQRSGHLNLYLLLDCSQSVSENDFLIFKESASLMVDRIFSFEINVSVAIITFASKPKVLMSVLNDNSRDMTEVI" | ||
| "SSLENANYKDHENGTGTNTYAALNSVYLMMNNQMRILGMETMAWQEIRHAIILLTDGKSNMGGSPKTAVDRIREILNINQ" | ||
| "KRNDYLDIYAIGVGKLDVDWRELNELGSKKDGERHAFILQDTKALHQVFEHMLDVSKLTDTICGVGNMSANASDQERTPW" | ||
| "HVTIKPKSQETCRGALISDQWVLTAAHCFRDGNDHSLWRVNVGDPKSQWGKEFLIEKAVISPGFDVFAKKNQGILEFYGD" | ||
| "DIALLKLAQKVKMSTHARPICLPCTMEANLALRRPQGSTCRDHENELLNKQSVPAHFVALNGSKLNINLKMGVEWTSCAE" | ||
| "VVSQEKTMFPNLTDVREVVTDQFLCSGTQEDESPCKGESGGAVFLERRFRFFQVGLVSWGLYNPCLGSADKNSRKRAPRS" | ||
| "KVPPPRDFHINLFRMQPWLRQHLGDVLNFLPL", | ||
| "start": "674", | ||
| "end": "682", | ||
| "match": "PckGESGGA", | ||
| }, | ||
| ] | ||
|
|
||
| EXPASY_ERROR = '<p>ERROR: Invalid characters in pattern "sdfsdf".</p>' | ||
|
|
||
|
|
||
| class TestMotif(TestCase): | ||
| def testExpasyTextToStructure(self): | ||
| self.assertEqual(_expasyTextToStructure(EXPASY_SCAN_PROSITE_TOOL_RESPONSE), EXPASY_SCAN_PROSITE_TOOL_RESULT) | ||
|
|
||
| def testEmptyExpasyStructure(self): | ||
| self.assertEqual(_expasyTextToStructure(EXPASY_ERROR), []) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """This module defines functions and constants related to MOTIF.""" | ||
|
|
||
| __all__ = ["prositeToRegEx"] | ||
|
|
||
|
|
||
| def prositeToRegEx(pattern: str) -> str: | ||
| """Change PROSITE Motif to python regular expression. | ||
|
|
||
| Args: | ||
| pattern (str): PROSITE Motif | ||
|
|
||
| Returns: | ||
| str: regular expression | ||
| """ | ||
| pattern = ( | ||
| pattern.replace("{", "[^") | ||
| .replace("}", "]") | ||
| .replace("(", "{") | ||
| .replace(")", "}") | ||
| .replace("-", "") | ||
| .replace("x", ".") | ||
| .replace(">", "$") | ||
| .replace("<", "*") | ||
| ) | ||
| return pattern |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.