Skip to content

Commit

Permalink
Added a couple of extra tests and exception handling for the uniprot …
Browse files Browse the repository at this point in the history
…functions if a bad uniprot ID is passed or the internet is down
  • Loading branch information
alexholehouse committed May 12, 2021
1 parent 89fcf7e commit 0305fc8
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ __pycache__/
# C extensions
*.so
.DS_Store
*~

# Distribution / packaging
.Python
Expand Down
5 changes: 2 additions & 3 deletions metapredict/backend/domain_definition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import scipy
from scipy.signal import savgol_filter
from metapredict.metapredict_exceptions import DomainError
"""
Functions for extracting out discrete disordered domains based on the linear disorder score
calculated by metapredict.
Expand All @@ -9,8 +10,6 @@



class DomainException(Exception):
pass


## ------------------------------------------------------------------------
Expand Down Expand Up @@ -114,7 +113,7 @@ def binerize_function(idr_score):

# add to help debugging
if len(B) != len(values):
raise DomainException('Error with binerize function. This is a bug. Please raise an issue on GitHub')
raise DomainError('Error with binerize function. This is a bug. Please raise an issue on GitHub')


## Part 1 - remove gaps
Expand Down
16 changes: 16 additions & 0 deletions metapredict/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# stuff for uniprot from backend
import metapredict.backend.uniprot_predictions
from metapredict.backend.uniprot_predictions import fetch_sequence
from metapredict.metapredict_exceptions import MetapredictError

def predict_disorder_domains(sequence,
disorder_threshold=0.42,
Expand Down Expand Up @@ -490,6 +491,12 @@ def predict_disorder_uniprot(uniprot_id, normalized=True):

# fetch sequence from Uniprot
sequence = fetch_sequence(uniprot_id)

# raise error if the webcall fails
if sequence is None:
raise MetapredictError('Error: unable to fetch UniProt with accession %s'%(uniprot_id))


# return predicted values of disorder for sequence
return meta_predict(sequence, normalized)

Expand Down Expand Up @@ -520,6 +527,11 @@ def graph_disorder_uniprot(uniprot_id, line_intervals=[], name = " ", DPI=150, s
"""
#make all residues upper case
sequence = fetch_sequence(uniprot_id)

# raise error if the webcall fails
if sequence is None:
raise MetapredictError('Error: unable to fetch UniProt with accession %s'%(uniprot_id))

#graph sequence
graph(sequence = sequence, name = name, cutoffLines=line_intervals, DPI=DPI, save_fig=save, output_file=output)

Expand Down Expand Up @@ -598,6 +610,10 @@ def predict_disorder_domains_uniprot(uniprot_id,
"""
sequence = fetch_sequence(uniprot_id)

# raise error if the webcall fails
if sequence is None:
raise MetapredictError('Error: unable to fetch UniProt with accession %s'%(uniprot_id))

disorder = predict_disorder(sequence, normalized=normalized)

return_tuple = domain_definition.get_domains(sequence,
Expand Down
8 changes: 8 additions & 0 deletions metapredict/metapredict_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


class DomainError(Exception):
pass


class MetapredictError(Exception):
pass
95 changes: 95 additions & 0 deletions metapredict/tests/test_uniprot_functionality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
Unit and regression test for the metapredict package.
This is extremely underdone at this point... Sorry about that :'(
"""

# Import package, test suite, and other packages as needed
from metapredict import meta
from metapredict.metapredict_exceptions import MetapredictError
import pytest
import sys
import os
import numpy as np


current_filepath = os.getcwd()
fasta_filepath = "{}/testing.fasta".format(current_filepath)

P53_UID = 'P04637'

def test_metapredict_imported():
"""Sample test, will always pass so long as import statement worked"""
assert "metapredict" in sys.modules


# ....................................................................................
#
def test_predict_disorder_uniprot():

# checks that this fails when an invalid uniprot accession is passed
with pytest.raises(MetapredictError):
meta.predict_disorder_uniprot('aaaa')


# checks that when we pull p53 we get 393 residues of sweet,
# sweet disorder prediction
assert len(meta.predict_disorder_uniprot(P53_UID)) == 393

# check summed disorder is right
assert np.sum(meta.predict_disorder_uniprot(P53_UID)) == 172.965


# check summed disorder is right when we don't normalize (these are not magic values,
# just the expected 'truth' for the 1.0 release
assert np.sum(meta.predict_disorder_uniprot(P53_UID,normalized=False)) == 173.524


# ....................................................................................
#
def test_graph_disorder_uniprot_():

# checks that this fails when an invalid uniprot accession is passed
with pytest.raises(MetapredictError):
meta.graph_disorder_uniprot('aaaa')


# probably should have some tests here...?


# ....................................................................................
#
def test_predict_disorder_domains_uniprot_():

# checks that this fails when an invalid uniprot accession is passed
with pytest.raises(MetapredictError):
meta.predict_disorder_domains_uniprot('aaaa')


# checks that when we pull p53 we get 393 residues of sweet,
# sweet disorder prediction

dis_domains = meta.predict_disorder_domains_uniprot(P53_UID)
assert len(dis_domains[0]) == 393
assert len(dis_domains[1]) == 393
assert np.sum(dis_domains[0]) == 172.965
assert np.sum(dis_domains[1]) == 173.04537763974946

# did we find 2 IDRs
assert len(dis_domains[2]) == 2

# IDR1
assert dis_domains[2][0][2] == 'MEEPQSDPSVEPPLSQETFSDLWKLLPENNVLSPLPSQAMDDLMLSPDDIEQWFTEDPGPDEAPRMPEAAPPVAPAPAAPTPAAPAPAPSWPLSSSVPSQKT'
assert dis_domains[2][0][0] == 0
assert dis_domains[2][0][1] == 102

# IDR2
assert dis_domains[2][1][2] == 'PGRDRRTEEENLRKKGEPHHELPPGSTKRALPNNTSSSPQPKKKPLDGEYFTLQIRGRERFEMFRELNEALELKDAQAGKEPGGSRAHSSHLKSKKGQSTSRHKKLMFKTEGPDSD'
assert dis_domains[2][1][0] == 277
assert dis_domains[2][1][1] == 393

# FD1
assert dis_domains[3][0][2] == 'YQGSYGFRLGFLHSGTAKSVTCTYSPALNKMFCQLAKTCPVQLWVDSTPPPGTRVRAMAIYKQSQHMTEVVRRCPHHERCSDSDGLAPPQHLIRVEGNLRVEYLDDRNTFRHSVVVPYEPPEVGSDCTTIHYNYMCNSSCMGGMNRRPILTIITLEDSSGNLLGRNSFEVRVCAC'
assert dis_domains[3][0][0] == 102
assert dis_domains[3][0][1] == 277

0 comments on commit 0305fc8

Please sign in to comment.