-
Notifications
You must be signed in to change notification settings - Fork 0
Add BioRxiv search and asynchronous data fetching #2
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
Open
sunitj
wants to merge
5
commits into
main
Choose a base branch
from
add-biorxiv-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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a85551e
Add BioRxiv search and asynchronous data fetching
google-labs-jules[bot] 69ab668
Add BioRxiv search and asynchronous data fetching
google-labs-jules[bot] 973f504
Merge branch 'main' into add-biorxiv-search
sunitj e1cdae5
I've added BioRxiv search and asynchronous data fetching to your proj…
google-labs-jules[bot] 691b91b
I've added BioRxiv search and asynchronous data fetching.
google-labs-jules[bot] 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 was deleted.
Oops, something went wrong.
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,40 @@ | ||
| import asyncio | ||
| from . import search_pubmed, search_biorxiv | ||
|
|
||
| async def query(query_text=""): | ||
| """ | ||
| Query both PubMed and bioRxiv for a given text. | ||
|
|
||
| This function queries both PubMed and bioRxiv asynchronously and | ||
| combines the results. | ||
|
|
||
| Args: | ||
| query_text (str): The text to search for. | ||
|
|
||
| Returns: | ||
| tuple: A tuple containing three lists: combined citations, | ||
| combined abstracts, and combined IDs (PMIDs and DOIs). | ||
| """ | ||
| # We need to make sure the pubmed query is async | ||
| pubmed_task = asyncio.create_task(search_pubmed.query(query_text)) | ||
| biorxiv_task = asyncio.create_task(search_biorxiv.query(query_text)) | ||
|
|
||
| results = await asyncio.gather(pubmed_task, biorxiv_task) | ||
|
|
||
| pubmed_citations, pubmed_abstracts, pubmed_ids = results[0] | ||
| biorxiv_citations, biorxiv_abstracts, biorxiv_dois = results[1] | ||
|
|
||
| combined_citations = pubmed_citations + biorxiv_citations | ||
| combined_abstracts = pubmed_abstracts + biorxiv_abstracts | ||
| combined_ids = pubmed_ids + biorxiv_dois | ||
|
|
||
| return combined_citations, combined_abstracts, combined_ids | ||
|
|
||
| if __name__ == '__main__': | ||
| async def main(): | ||
| citations, abstracts, ids = await query("crispr") | ||
| print(f"Found {len(citations)} articles.") | ||
| for c in citations: | ||
| print(c) | ||
|
|
||
| asyncio.run(main()) |
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,81 @@ | ||
| import asyncio | ||
| import httpx | ||
| from datetime import datetime, timedelta | ||
|
|
||
| PAGE_SIZE = 100 | ||
|
|
||
| async def query(query_text=""): | ||
| """ | ||
| Search bioRxiv for a given query text. | ||
|
|
||
| This function fetches preprints from the last 30 days from the bioRxiv API | ||
| and filters them based on whether the query text appears in the title or abstract. | ||
|
|
||
| Args: | ||
| query_text (str): The text to search for. | ||
|
|
||
| Returns: | ||
| tuple: A tuple containing three lists: citations, abstracts, and DOIs. | ||
| """ | ||
| date_to = datetime.now() | ||
| date_from = date_to - timedelta(days=30) | ||
|
|
||
| date_to_str = date_to.strftime('%Y-%m-%d') | ||
| date_from_str = date_from.strftime('%Y-%m-%d') | ||
|
|
||
| url = f"https://api.biorxiv.org/details/biorxiv/{date_from_str}/{date_to_str}" | ||
|
|
||
| citations = [] | ||
| abstracts = [] | ||
| dois = [] | ||
|
|
||
| async with httpx.AsyncClient() as client: | ||
| cursor = 0 | ||
| while True: | ||
| paginated_url = f"{url}/{cursor}/json" | ||
sunitj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| response = await client.get(paginated_url) | ||
|
|
||
| if response.status_code != 200: | ||
| break | ||
|
|
||
| data = response.json() | ||
| messages = data.get('messages', [{}]) | ||
| if messages and messages[0].get('status', '') == 'no results': | ||
| break | ||
|
|
||
| for article in data.get('collection', []): | ||
| title = article.get('title', '') | ||
| abstract = article.get('abstract', '') | ||
|
|
||
| if query_text.lower() in title.lower() or query_text.lower() in abstract.lower(): | ||
| authors = article.get('authors', []) | ||
| author_str = ", ".join([f"{a.get('name', '')}" for a in authors]) | ||
| doi = article.get('doi', '') | ||
| date = article.get('date', '') | ||
|
|
||
| citation = f"{author_str}. {title}. bioRxiv {doi} ({date})" | ||
|
|
||
| citations.append(citation) | ||
| abstracts.append(abstract) | ||
| dois.append(doi) | ||
|
|
||
| # bioRxiv API returns 100 results at a time. We need to paginate. | ||
| # The 'count' in messages gives total results for the query. | ||
| # 'cursor' is the starting point of the next page. | ||
| try: | ||
| count = int(messages[0].get('count', 0)) | ||
| cursor = int(messages[0].get('cursor', 0)) + PAGE_SIZE | ||
| except (ValueError, TypeError): | ||
| break | ||
|
|
||
| if cursor >= count: | ||
| break | ||
|
|
||
| return citations, abstracts, dois | ||
|
|
||
| if __name__ == '__main__': | ||
| async def main(): | ||
| citations, abstracts, dois = await query("crispr") | ||
| for c in citations: | ||
| print(c) | ||
| asyncio.run(main()) | ||
Oops, something went wrong.
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.