-
Notifications
You must be signed in to change notification settings - Fork 0
Handle timeout #9
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
SolomonShorser-OICR
wants to merge
4
commits into
develop
Choose a base branch
from
handle-timeout
base: develop
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
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -12,17 +12,22 @@ | |
| import uk.ac.ebi.uniprot.dataservice.client.uniprot.UniProtService; | ||
| import uk.ac.ebi.uniprot.dataservice.query.Query; | ||
|
|
||
| import java.net.SocketTimeoutException; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.time.Duration; | ||
| import java.util.*; | ||
|
|
||
| public class UniProtGeneNamesRetriever { | ||
|
|
||
| private static final int MAX_NUM_ATTEMPTS = 10; | ||
| private static final Logger logger = LogManager.getLogger(); | ||
| private static final int MAX_UNIPROT_BATCH_QUERY_SIZE = 250; | ||
| // TODO: Make this value configurable. | ||
| private static final int MAX_UNIPROT_BATCH_QUERY_SIZE = 100; | ||
|
|
||
| /** | ||
| * Queries the UniProt mapping service through their Java API library. All Uniprot accession IDs are taken from the Panther | ||
|
|
@@ -129,25 +134,58 @@ public static Set<String> retrieveGeneNamesFromUniProt(List<Set<String>> partiti | |
| int count = 0; | ||
| Set<String> uniprotAccessionsToGeneNames = new HashSet<>(); | ||
| for (Set<String> uniprotIdentifierPartition : partitionedUniProtIds) { | ||
| int currentAttemptNum = 0; | ||
| // Build UniProt API query from Set of 250 UniProt identifiers. | ||
| Query query = UniProtQueryBuilder.accessions(uniprotIdentifierPartition); | ||
| // Perform UniProt API query to retrieve gene names associated with identifiers. | ||
| QueryResult<UniProtComponent<Gene>> uniprotEntries = uniprotService.getGenes(query); | ||
|
|
||
| while (uniprotEntries.hasNext()) { | ||
| count++; | ||
| // Get Gene object returned from UniProt. | ||
| UniProtComponent<Gene> geneObject = uniprotEntries.next(); | ||
| if (!geneObject.getComponent().isEmpty()) { | ||
| // Iterate through all Gene components in the response. | ||
| for (Gene geneComponent : geneObject.getComponent()) { | ||
| // Tab-separate UniProt accession ID and its associated gene name, and then store these in the Set that will be returned. | ||
| uniprotAccessionsToGeneNames.add(geneObject.getAccession().toString() + "\t" + geneComponent.getGeneName().toString() + "\n"); | ||
| QueryResult<UniProtComponent<Gene>> uniprotEntries = null; | ||
| while (currentAttemptNum < MAX_NUM_ATTEMPTS && uniprotEntries == null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps after this loop if |
||
| { | ||
| try | ||
| { | ||
| currentAttemptNum++; | ||
| uniprotEntries = uniprotService.getGenes(query); | ||
| while (uniprotEntries.hasNext()) { | ||
| count++; | ||
| // Get Gene object returned from UniProt. | ||
| UniProtComponent<Gene> geneObject = uniprotEntries.next(); | ||
| if (!geneObject.getComponent().isEmpty()) { | ||
| // Iterate through all Gene components in the response. | ||
| for (Gene geneComponent : geneObject.getComponent()) { | ||
| // Tab-separate UniProt accession ID and its associated gene name, and then store these in the Set that will be returned. | ||
| uniprotAccessionsToGeneNames.add(geneObject.getAccession().toString() + "\t" + geneComponent.getGeneName().toString() + "\n"); | ||
| } | ||
| } | ||
|
|
||
| if (count % 1000 == 0) { | ||
| logger.info(count + " UniProt identifiers have been queried for gene names"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (count % 1000 == 0) { | ||
| logger.info(count + " UniProt identifiers have been queried for gene names"); | ||
| catch (ServiceException e) | ||
| { | ||
| // Log the exception right away, just in case any code in the exception handler fails and we don't get a chance to log it later. | ||
| logger.error(e); | ||
| // If the exception was caused by a Timeout, then we want to retry. | ||
| boolean timeoutFound = false; | ||
| int i = 0; | ||
| while (!timeoutFound && i < e.getStackTrace().length) | ||
| { | ||
| // don't be too specific - there are other classes for timeouts. ANY type timeout should trigger a wait-retry. | ||
| timeoutFound = e.getStackTrace()[i].getClassName().toUpperCase().contains("TIMEOUT"); | ||
| i++; | ||
| } | ||
| // If a timeout was found, sleep for a bit and then retry. | ||
| if (timeoutFound) | ||
| { | ||
| long sleepAmt = Duration.ofSeconds(currentAttemptNum * 2L).toMillis(); | ||
jweiser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger.warn("A timeout exception was caught while trying to connect to the UniProt service after {} attempts. A retry will be performed after {} milliseconds", currentAttemptNum, sleepAmt); | ||
| Thread.sleep(sleepAmt); | ||
| } | ||
| else | ||
| { | ||
| logger.error("ServiceException caught while trying to communicate with UniProt: " + e.getMessage(), e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.