Skip to content

Commit f49ea23

Browse files
committed
init
0 parents  commit f49ea23

21 files changed

+1243
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*pyc
2+
.vscode
3+
__pycache__
4+
test

MANIFEST.in

Whitespace-only changes.

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# NCBI Pubmed Toolkits
2+
---
3+
## Requirements
4+
- Python3
5+
6+
---
7+
## Installation
8+
```bash
9+
pip isntall pypubmed
10+
```
11+
12+
---
13+
## Usage
14+
### `search`
15+
> search Pubmed with term
16+
```bash
17+
```
18+
19+
### `citation`
20+
> generate citation for given PMID
21+
```bash
22+
```
23+
24+
### `translate`
25+
> translate text with googletrans
26+
```bash
27+
```
28+
29+
### `pdf`
30+
> download pdf if available for given PMID
31+
```bash
32+
```
33+
34+
### `config`
35+
> set api_key etc.
36+
```bash
37+
```
38+
39+
### `serve`
40+
> start a server
41+
```bash
42+
```
43+
44+
---
45+
## Documents
46+
> https://pypubmed.readthedocs.io/en/latest/
47+

help.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# **NCBI EUtils**
2+
> https://www.ncbi.nlm.nih.gov/books/NBK25497/
3+
4+
## `einfo`
5+
> https://eutils.ncbi.nlm.nih.gov/entrez/eutils/einfo.fcgi?db=pubmed
6+
7+
## `esearch`
8+
> https://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.ESearch
9+
>> https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ngs[title]&retmax=5&retstart=0
10+
>> https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ngs&retmax=5&retstart=0&field=title
11+
12+
## `efetch`
13+
> https://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.EFetch
14+
>> https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=17284678,9997&retmode=xml
15+
16+
17+
## `elink`
18+
> https://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.ELink
19+
> - get cited (`{"linkname": "pubmed_pubmed_citedin"}`)
20+
>> https://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?dbfrom=pubmed&db=pubmed&id=20210808&cmd=neighbor_score&retmode=json
21+
> - get pdf url
22+
>> https://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?dbfrom=pubmed&db=pubmed&cmd=prlinks&id=10210801
23+
24+
25+
26+
### `retmode` and `rettype`
27+
> https://www.ncbi.nlm.nih.gov/books/NBK25499/table/chapter4.T._valid_values_of__retmode_and/?report=objectonly
28+
29+
### `API KEY`
30+
> - E-utils users are allowed 3 requests/second without an API key.
31+
> - Create an API key to increase your e-utils limit to 10 requests/second.
32+
> - use with `api_key=API_KEY `
33+
34+
35+
---
36+
# **Journals**
37+
> https://www.ncbi.nlm.nih.gov/nlmcatalog/journals
38+
> - https://ftp.ncbi.nlm.nih.gov/pubmed/J_Entrez.gz
39+
> - https://ftp.ncbi.nlm.nih.gov/pubmed/J_Medline.gz
40+
41+
---
42+
# **Impact Factor**
43+
> http://www.greensci.net/
44+
45+
46+
- https://dataguide.nlm.nih.gov/eutilities/utilities.html
47+
- https://www.nlm.nih.gov/bsd/licensee/elements_article_source.html
48+
- https://www.nlm.nih.gov/bsd/licensee/elements_alphabetical.html

pypubmed/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""====================\n
2+
NCBI Pubmed ToolKits\n
3+
====================
4+
"""
5+
6+
__version__ = '1.0.0'
7+
8+
__author__ = 'suqingdong'
9+
__author_email__ = '[email protected]'

pypubmed/bin/__init__.py

Whitespace-only changes.

pypubmed/bin/cli.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
import sys
3+
import click
4+
5+
6+
from pypubmed.core.search import search, advance_search
7+
from pypubmed.core.citations import citations
8+
from pypubmed import __version__, __author__, __author_email__, __doc__
9+
10+
11+
PY3 = sys.version_info.major == 3
12+
if not PY3:
13+
reload(sys)
14+
sys.setdefaultencoding('utf-8')
15+
16+
17+
18+
__epilog__ = click.style('''
19+
contact: {__author__} <{__author_email__}>
20+
'''.format(**locals()), fg='bright_black')
21+
22+
@click.group(epilog=__epilog__, help=click.style(__doc__, fg='bright_blue', bold=True))
23+
@click.option('-d', '--debug', help='logging in debug mode', default=False, is_flag=True)
24+
@click.version_option(version=__version__, prog_name='pypubmed')
25+
def cli(debug):
26+
if debug:
27+
os.putenv('DEBUG', True)
28+
29+
30+
def main():
31+
cli.add_command(search)
32+
cli.add_command(advance_search)
33+
cli.add_command(citations)
34+
cli()
35+
36+
37+
if __name__ == '__main__':
38+
main()

pypubmed/core/__init__.py

Whitespace-only changes.

pypubmed/core/article.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
3+
4+
class Article(object):
5+
def __init__(self, **kwargs):
6+
for k, v in kwargs.items():
7+
setattr(self, k, v)
8+
9+
def to_dict(self):
10+
return self.__dict__
11+
12+
def to_json(self, **kwargs):
13+
return json.dumps(self.__dict__, ensure_ascii=False, **kwargs)
14+
15+
@property
16+
def fields(self):
17+
return list(self.__dict__.keys())
18+
19+
def __repr__(self):
20+
return 'Article[{pmid} - {title}]'.format(**self.__dict__)
21+
22+
23+
if __name__ == '__main__':
24+
25+
p = Article(pmid=1, issn='1234-5678', title='test')
26+
print(p)
27+
print(p.to_dict())
28+
print(p.to_json())

pypubmed/core/citations.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""
2+
NCBI Citations:
3+
- https://pubmed.ncbi.nlm.nih.gov/help/#cite
4+
- Is this limited? IP blocking with too much requests?
5+
- https://pubmed.ncbi.nlm.nih.gov/{PMID}/citations/
6+
7+
Related Website:
8+
- https://www.pmid2cite.com/
9+
- https://www.plagiarism.org/article/citation-styles
10+
- https://ozzz.org/citation-generator/
11+
12+
Format:
13+
- AMA: American Medical Association
14+
- MLA: Modern Language Association
15+
- APA: American Psychological Association
16+
- NLM: National Library of Medicine
17+
"""
18+
import os
19+
import re
20+
import json
21+
22+
import click
23+
24+
from pypubmed.core.eutils import Eutils
25+
from pypubmed.util.web_request import WebRequest
26+
27+
28+
def ncbi_citations(pmid, fmt=None):
29+
url = 'https://pubmed.ncbi.nlm.nih.gov/{}/citations/'.format(pmid)
30+
data = WebRequest.get_response(url).json()
31+
if fmt in data:
32+
return data[fmt]
33+
return data
34+
35+
36+
def manual_citations(article, fmt='ama'):
37+
38+
doi = ' doi:{}'.format(article.doi) if article.doi else ''
39+
issue = '({})'.format(article.issue) if article.issue else ''
40+
41+
# ========================================
42+
# # merge the pagination?
43+
# # - S1-22; quiz S23-4 => S1-S24
44+
# # - 548-554 => 548-54
45+
# ========================================
46+
# pagination_merge = article.pagination
47+
48+
if fmt == 'ama':
49+
citation = '{authors}. {title} {med_abbr}. {year};{volume}{issue}:{pagination}.{doi}'
50+
if len(article.authors) > 5:
51+
authors = ', '.join('{} {}'.format(author[0], author[2]) for author in article.authors[:3]) + ' et al'
52+
else:
53+
authors = ', '.join('{} {}'.format(author[0], author[2]) for author in article.authors)
54+
elif fmt == 'mla':
55+
first_author = '{}, {}'.format(*article.authors[0][:2])
56+
issue = ',{}'.format(article.issue) if article.issue else ''
57+
citation = '{first_author} et al. \u201c{title}\u201d {journal} vol. {volume}{issue} ({year}): {pagination}.{doi}'
58+
elif fmt == 'apa':
59+
doi = ' https://doi.org/{}'.format(article.doi) if article.doi else ''
60+
author_all = ', '.join('{}, {}.'.format(each[0], '. '.join(each[2])) for each in article.authors[:-1])
61+
last_author = article.authors[-1]
62+
if len(last_author) == 1:
63+
last_author = last_author[0]
64+
else:
65+
last_author = '{}, {}.'.format(last_author[0], '. '.join(last_author[2]))
66+
citation = '{author_all}, & {last_author} ({year}). {title} {journal}, {volume}{issue}, {pagination}.{doi}'
67+
elif fmt == 'nlm':
68+
author_list = ', '.join('{} {}'.format(each[0], each[2]) if len(each) >= 3 else ';{}'.format(each[0]) for each in article.authors)
69+
doi = doi + '. ' if doi else ' '
70+
citation = '{author_list}. {title} {med_abbr}. {pubdate};{volume}{issue}:{pagination}.{doi}PMID:{pmid}'
71+
if article.pmc:
72+
citation += '; PMCID: {pmc}.'
73+
74+
citation = citation.format(**dict(article.to_dict(), **locals()))
75+
76+
return citation
77+
78+
79+
__epilog__ = '''
80+
examples:\n
81+
citations 1 2 3\n
82+
citations 1 2 3 -f nlm\n
83+
citations 1 2 3 -m -f apa\n
84+
'''
85+
86+
@click.command(epilog=__epilog__,
87+
no_args_is_help=True,
88+
short_help='generate citations for given pmids',
89+
help='Citations Tools')
90+
@click.option('-m', '--manual', help='cite with manual citations, default with ncbi citations', default=False, is_flag=True)
91+
@click.option('-f', '--fmt', help='the format of citation', type=click.Choice('ama mla apa nlm'.split()), default='ama')
92+
@click.option('-k', '--api-key', help='the API_KEY of NCIB', envvar='NCBI_API_KEY')
93+
@click.argument('pmids', nargs=-1)
94+
def citations(pmids, manual, fmt, api_key):
95+
if not pmids:
96+
click.secho('please supply pmids or a file', fg='green')
97+
exit(1)
98+
99+
if os.path.isfile(pmids[0]):
100+
pmid_list = open(pmids[0]).read().strip().split()
101+
else:
102+
pmid_list = pmids
103+
104+
if manual:
105+
e = Eutils(api_key=api_key)
106+
articles = e.efetch(pmid_list)
107+
for article in articles:
108+
citation = manual_citations(article, fmt=fmt)
109+
click.secho(citation, fg='yellow')
110+
else:
111+
for pmid in pmid_list:
112+
citation = ncbi_citations(pmid, fmt=fmt)
113+
click.secho(json.dumps(citation, indent=2), fg='green')
114+
115+
116+
117+
118+
119+
if __name__ == '__main__':
120+
citations()

0 commit comments

Comments
 (0)