Skip to content
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

Add search_term method #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 83 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,33 @@ Get CUI.
import umls_api

resp = umls_api.API(api_key='<UMLS_API_KEY_HERE>').get_cui(cui='C0007107')
print(res)
print(resp)
{
'pageCount': 1,
'pageNumber': 1,
'pageSize': 25,
'result': {
'atomCount': 247,
'atoms': 'https://uts-ws.nlm.nih.gov/rest/content/2020AB/CUI/C0007107/atoms',
'attributeCount': 0,
'classType': 'Concept',
'cvMemberCount': 0,
'dateAdded': '09-30-1990',
'defaultPreferredAtom': 'https://uts-ws.nlm.nih.gov/rest/content/2020AB/CUI/C0007107/atoms/preferred',
'definitions': 'https://uts-ws.nlm.nih.gov/rest/content/2020AB/CUI/C0007107/definitions',
'majorRevisionDate': '06-25-2020',
'name': 'Malignant neoplasm of larynx',
'relationCount': 17,
'relations': 'https://uts-ws.nlm.nih.gov/rest/content/2020AB/CUI/C0007107/relations',
'semanticTypes': [{
'name': 'Neoplastic Process',
'uri': 'https://uts-ws.nlm.nih.gov/rest/semantic-network/2020AB/TUI/T191'
}],
'status': 'R',
'suppressible': False,
'ui': 'C0007107'
}
'pageCount': 1,
'pageNumber': 1,
'pageSize': 25,
'result': {
'atomCount': 247,
'atoms': 'https://uts-ws.nlm.nih.gov/rest/content/2020AB/CUI/C0007107/atoms',
'attributeCount': 0,
'classType': 'Concept',
'cvMemberCount': 0,
'dateAdded': '09-30-1990',
'defaultPreferredAtom': 'https://uts-ws.nlm.nih.gov/rest/content/2020AB/CUI/C0007107/atoms/preferred',
'definitions': 'https://uts-ws.nlm.nih.gov/rest/content/2020AB/CUI/C0007107/definitions',
'majorRevisionDate': '06-25-2020',
'name': 'Malignant neoplasm of larynx',
'relationCount': 17,
'relations': 'https://uts-ws.nlm.nih.gov/rest/content/2020AB/CUI/C0007107/relations',
'semanticTypes': [{
'name': 'Neoplastic Process',
'uri': 'https://uts-ws.nlm.nih.gov/rest/semantic-network/2020AB/TUI/T191'
}],
'status': 'R',
'suppressible': False,
'ui': 'C0007107'
}
}
```

Get TUI.
Expand All @@ -52,39 +53,63 @@ import umls_api
resp = umls_api.API(api_key='<UMLS_API_KEY_HERE>').get_tui(cui='T047')
print(resp)
{
'pageCount': 1,
'pageNumber': 1,
'pageSize': 25,
'result': {
'abbreviation': 'dsyn',
'childCount': 2,
'classType': 'SemanticType',
'definition': 'A condition which alters or interferes with a '
'normal process, state, or activity of an organism. '
'It is usually characterized by the abnormal '
"functioning of one or more of the host's systems, "
'parts, or organs. Included here is a complex of '
'symptoms descriptive of a disorder.',
'example': 'NONE',
'name': 'Disease or Syndrome',
'nonHuman': 'NONE',
'semanticTypeGroup': {
'abbreviation': 'DISO',
'classType': 'SemanticGroup',
'expandedForm': 'Disorders',
'semanticTypeCount': 12
},
'treeNumber': 'B2.2.1.2.1',
'ui': 'T047',
'usageNote': 'Any specific disease or syndrome that is modified by '
'such modifiers as "acute", "prolonged", etc. will '
'also be assigned to this type. If an anatomic '
'abnormality has a pathologic manifestation, then it '
'will be given this type as well as a type from the '
"'Anatomical Abnormality' hierarchy, e.g., "
'"Diabetic Cataract" will be double-typed for this '
'reason.'
}
'pageCount': 1,
'pageNumber': 1,
'pageSize': 25,
'result': {
'abbreviation': 'dsyn',
'childCount': 2,
'classType': 'SemanticType',
'definition': 'A condition which alters or interferes with a '
'normal process, state, or activity of an organism. '
'It is usually characterized by the abnormal '
"functioning of one or more of the host's systems, "
'parts, or organs. Included here is a complex of '
'symptoms descriptive of a disorder.',
'example': 'NONE',
'name': 'Disease or Syndrome',
'nonHuman': 'NONE',
'semanticTypeGroup': {
'abbreviation': 'DISO',
'classType': 'SemanticGroup',
'expandedForm': 'Disorders',
'semanticTypeCount': 12
},
'treeNumber': 'B2.2.1.2.1',
'ui': 'T047',
'usageNote': 'Any specific disease or syndrome that is modified by '
'such modifiers as "acute", "prolonged", etc. will '
'also be assigned to this type. If an anatomic '
'abnormality has a pathologic manifestation, then it '
'will be given this type as well as a type from the '
"'Anatomical Abnormality' hierarchy, e.g., "
'"Diabetic Cataract" will be double-typed for this '
'reason.'
}
}
```

Search term.

```python
import umls_api

resp = umls_api.API(api_key='<UMLS_API_KEY_HERE>').search_term('Malignant neoplasm of larynx')
print(resp)
{
'pageSize': 25,
'pageNumber': 1,
'result': {
'classType': 'searchResults',
'results': [
{
'ui': 'C0007107',
'rootSource': 'MTH',
'uri': 'https://uts-ws.nlm.nih.gov/rest/content/2021AB/CUI/C0007107',
'name': 'Malignant neoplasm of larynx'
}
]
}
}
```

Expand Down
28 changes: 23 additions & 5 deletions src/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
UMLS_API_KEY = os.environ['UMLS_API_KEY']
CUI = 'C0007107'
TUI = 'T047'
TERM = 'Malignant neoplasm of larynx'


class TestAuth:
Expand Down Expand Up @@ -38,16 +39,13 @@ def test_get_cui(self):
'dateAdded': '09-30-1990',
'defaultPreferredAtom': str,
'definitions': str,
'majorRevisionDate': '06-25-2020',
'majorRevisionDate': str,
'name': 'Malignant neoplasm of larynx',
'relationCount': int,
'relations': str,
'semanticTypes': [{
'name': 'Neoplastic Process',
'uri': (
'https://uts-ws.nlm.nih.gov/rest/semantic-network/'
'2020AB/TUI/T191'
)
'uri': str,
}],
'status': str,
'suppressible': bool,
Expand Down Expand Up @@ -81,3 +79,23 @@ def test_get_tui(self):
'usageNote': str
}
})

def test_get_term(self):
result = umls_api.API(api_key=UMLS_API_KEY).term_search(TERM, umls_api.SearchType.NORMALIZED_STRING)

assert result == S({
'pageNumber': int,
'pageSize': int,
'result': {
'classType': 'searchResults',
'results': [
{
'ui': 'C0007107',
'rootSource': str,
'name': 'Malignant neoplasm of larynx',
'uri': str

}
]
}
})
22 changes: 22 additions & 0 deletions src/umls_api/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests
from lxml.html import fromstring
from cachetools import cached, TTLCache
from enum import Enum

TTL_7HRS = TTLCache(maxsize=2, ttl=25200)

Expand Down Expand Up @@ -34,6 +35,16 @@ def get_single_use_service_ticket(self):
return single_use_service_ticket


class SearchType(Enum):
EXACT = 'exact'
WORDS = 'words'
LEFT_TRUNCATION = 'leftTruncation'
RIGHT_TRUNCATION = 'rightTruncation'
APPROXIMATE = 'approximate'
NORMALIZED_STRING = 'normalizedString'
NORMALIZED_WORDS = 'normalizedWords'


class API:
BASE_URL = 'https://uts-ws.nlm.nih.gov/rest'

Expand All @@ -49,6 +60,17 @@ def get_tui(self, tui):
url = (f'{self.BASE_URL}/semantic-network/{self._version}/TUI/{tui}')
return self._get(url=url)

def term_search(self, term, searchType=None):
url = (f'{self.BASE_URL}/search/{self._version}/?string={term}')
if searchType is not None:
# Type checking
if not isinstance(searchType, SearchType):
raise ValueError('searchType must be an instance of SearchType Enum')
else:
url = url + (f'/&searchType={searchType.value}')

return self._get(url=url)

def _get(self, url):
ticket = self._auth.get_single_use_service_ticket()
resp = requests.get(url, params={'ticket': ticket})
Expand Down