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 interface for MHCflurry 2.0.x #54

Open
wants to merge 1 commit into
base: develop
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
161 changes: 158 additions & 3 deletions epytope/EpitopePrediction/ANN.py

Large diffs are not rendered by default.

78 changes: 46 additions & 32 deletions epytope/EpitopePrediction/PSSM.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
from epytope.Core.Result import EpitopePredictionResult
from epytope.Core.Base import AEpitopePrediction

from mhcflurry import Class1AffinityPredictor
from mhcnuggets.src.predict import predict


class APSSMEpitopePrediction(AEpitopePrediction):
"""
Abstract base class for PSSM predictions.
Expand All @@ -49,7 +45,8 @@ def predict(self, peptides, alleles=None, **kwargs):
def __load_allele_model(allele, length):
allele_model = "%s_%i" % (allele, length)
return getattr(
__import__("epytope.Data.pssms." + self.name + ".mat." + allele_model, fromlist=[allele_model]),
__import__("epytope.Data.pssms." + self.name +
".mat." + allele_model, fromlist=[allele_model]),
allele_model)

if isinstance(peptides, Peptide):
Expand All @@ -63,13 +60,15 @@ def __load_allele_model(allele, length):

if alleles is None:
al = [Allele(a) for a in self.supportedAlleles]
alleles_string = {conv_a: a for conv_a, a in zip(self.convert_alleles(al), al)}
alleles_string = {conv_a: a for conv_a,
a in zip(self.convert_alleles(al), al)}
else:
if isinstance(alleles, Allele):
alleles = [alleles]
if any(not isinstance(p, Allele) for p in alleles):
raise ValueError("Input is not of type Allele")
alleles_string = {conv_a: a for conv_a, a in zip(self.convert_alleles(alleles), alleles)}
alleles_string = {conv_a: a for conv_a, a in zip(
self.convert_alleles(alleles), alleles)}

result = {}
pep_groups = list(pep_seqs.keys())
Expand All @@ -78,29 +77,33 @@ def __load_allele_model(allele, length):
peps = list(peps)
# dynamicaly import prediction PSSMS for alleles and predict
if self.supportedLength is not None and length not in self.supportedLength:
warnings.warn("Peptide length of %i is not supported by %s" % (length, self.name))
warnings.warn(
"Peptide length of %i is not supported by %s" % (length, self.name))
continue

for a in alleles_string.keys():
try:
pssm = __load_allele_model(a, length)
except ImportError:
warnings.warn("No model found for %s with length %i" % (alleles_string[a], length))
warnings.warn("No model found for %s with length %i" % (
alleles_string[a], length))
continue

if alleles_string[a] not in result:
result[alleles_string[a]] = {'Score': {}}

for p in peps:
score = sum(pssm[i].get(p[i], 0.0) for i in range(length)) + pssm.get(-1, {}).get("con", 0)
score = sum(pssm[i].get(p[i], 0.0) for i in range(
length)) + pssm.get(-1, {}).get("con", 0)
result[alleles_string[a]]['Score'][pep_seqs[p]] = score

if not result:
raise ValueError("No predictions could be made with "
+ self.name + " for given input. Check your epitope length and HLA allele combination.")

df_result = EpitopePredictionResult.from_dict(result, pep_seqs.values(), self.name)


df_result = EpitopePredictionResult.from_dict(
result, pep_seqs.values(), self.name)

return df_result


Expand Down Expand Up @@ -587,7 +590,8 @@ def predict(self, peptides, alleles=None, **kwargs):
def __load_allele_model(allele, length):
allele_model = "%s_%i" % (allele, length)
return getattr(
__import__("epytope.Data.pssms." + self.name + ".mat." + allele_model, fromlist=[allele_model]),
__import__("epytope.Data.pssms." + self.name +
".mat." + allele_model, fromlist=[allele_model]),
allele_model)

if isinstance(peptides, Peptide):
Expand All @@ -601,33 +605,38 @@ def __load_allele_model(allele, length):

if alleles is None:
al = [Allele(a) for a in self.supportedAlleles]
alleles_string = {conv_a: a for conv_a, a in zip(self.convert_alleles(al), al)}
alleles_string = {conv_a: a for conv_a,
a in zip(self.convert_alleles(al), al)}
else:
if isinstance(alleles, Allele):
alleles = [alleles]
if any(not isinstance(p, Allele) for p in alleles):
raise ValueError("Input is not of type Allele")
alleles_string = {conv_a: a for conv_a, a in zip(self.convert_alleles(alleles), alleles)}
alleles_string = {conv_a: a for conv_a, a in zip(
self.convert_alleles(alleles), alleles)}

scores = {}
for length, peps in itertools.groupby(pep_seqs.keys(), key=lambda x: len(x)):
peps = list(peps)
# dynamicaly import prediction PSSMS for alleles and predict
if length not in self.supportedLength:
warnings.warn("Peptide length of %i is not supported by %s" % (length, self.name))
warnings.warn(
"Peptide length of %i is not supported by %s" % (length, self.name))
continue

for a in alleles_string.keys():
try:
pssm = __load_allele_model(a, length)
except ImportError:
warnings.warn("No model found for %s with length %i" % (alleles_string[a], length))
warnings.warn("No model found for %s with length %i" % (
alleles_string[a], length))
continue

scores[alleles_string[a]] = {}
##here is the prediction and result object missing##
for p in peps:
score = sum(pssm[i].get(p[i], 0.0) for i in range(length)) + pssm.get(-1, {}).get("con", 0)
score = sum(pssm[i].get(p[i], 0.0) for i in range(
length)) + pssm.get(-1, {}).get("con", 0)
score /= -length
score -= pssm[-1]["intercept"]
score /= pssm[-1]["slope"]
Expand All @@ -641,8 +650,9 @@ def __load_allele_model(allele, length):
if not scores:
raise ValueError("No predictions could be made with " + self.name + " for given input. Check your"
"epitope length and HLA allele combination.")

result = {allele: {"Score":(list(scores.values())[j])} for j, allele in enumerate(alleles)}

result = {allele: {
"Score": (list(scores.values())[j])} for j, allele in enumerate(alleles)}

df_result = EpitopePredictionResult.from_dict(result, peps, self.name)
return df_result
Expand Down Expand Up @@ -717,9 +727,7 @@ def predict(self, peptides, alleles=None, **kwargs):
:rtype: :class:`~epytope.Core.Result.EpitopePredictionResult`
"""
return EpitopePredictionResult(
super(ComblibSidney2008, self).predict(peptides,
alleles=alleles,
**kwargs).applymap(lambda x: math.pow(10, x)))
super(ComblibSidney2008, self).predict(peptides, alleles=alleles, **kwargs).applymap(lambda x: math.pow(10, x)))


class TEPITOPEpan(APSSMEpitopePrediction):
Expand Down Expand Up @@ -965,7 +973,8 @@ def predict(self, peptides, alleles=None, **kwargs):
def __load_allele_model(allele, length):
allele_model = "%s" % allele
return getattr(
__import__("epytope.Data.pssms." + self.name + ".mat." + allele_model, fromlist=[allele_model]),
__import__("epytope.Data.pssms." + self.name +
".mat." + allele_model, fromlist=[allele_model]),
allele_model)

if isinstance(peptides, Peptide):
Expand All @@ -979,21 +988,24 @@ def __load_allele_model(allele, length):

if alleles is None:
al = [Allele(a) for a in self.supportedAlleles]
alleles_string = {conv_a: a for conv_a, a in zip(self.convert_alleles(al), al)}
alleles_string = {conv_a: a for conv_a,
a in zip(self.convert_alleles(al), al)}
else:
if isinstance(alleles, Allele):
alleles = [alleles]
if any(not isinstance(p, Allele) for p in alleles):
raise ValueError("Input is not of type Allele")
alleles_string = {conv_a: a for conv_a, a in zip(self.convert_alleles(alleles), alleles)}
alleles_string = {conv_a: a for conv_a, a in zip(
self.convert_alleles(alleles), alleles)}

scores = {}
pep_groups = list(pep_seqs.keys())
pep_groups.sort(key=len)
for length, peps in itertools.groupby(pep_groups, key=len):

if self.supportedLength is not None and length not in self.supportedLength:
warnings.warn("Peptide length of %i is not supported by %s" % (length, self.name))
warnings.warn(
"Peptide length of %i is not supported by %s" % (length, self.name))
continue

peps = list(peps)
Expand All @@ -1009,7 +1021,8 @@ def __load_allele_model(allele, length):
pssm = []

importance = self.__importance if length <= 9 else \
self.__importance[:5] + ((length - 9) * [0.30]) + self.__importance[5:]
self.__importance[:5] + \
((length - 9) * [0.30]) + self.__importance[5:]

for p in peps:
score = sum(self.__log_enrichment.get(p[i], 0.0) * importance[i]
Expand All @@ -1020,7 +1033,8 @@ def __load_allele_model(allele, length):
raise ValueError("No predictions could be made with " + self.name + " for given input. Check your"
"epitope length and HLA allele combination.")

result = {allele: {"Score":(list(scores.values())[j])} for j, allele in enumerate(alleles)}
result = {allele: {
"Score": (list(scores.values())[j])} for j, allele in enumerate(alleles)}

df_result = EpitopePredictionResult.from_dict(result, peps, self.name)
return df_result
Expand Down
3 changes: 1 addition & 2 deletions epytope/EpitopePrediction/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from epytope.Core.Base import AEpitopePrediction
from epytope.EpitopePrediction.External import *
from epytope.EpitopePrediction.PSSM import *
# from epytope.EpitopePrediction.SVM import *
from epytope.EpitopePrediction.ANN import *
try:
from fred_plugin import *
Expand All @@ -26,7 +25,7 @@ def __init__(cls, name, bases, nmspc):

def __call__(self, _predictor, *args, **kwargs):
'''
If a third person wants to write a new Epitope Predictior. He/She has to name the file fred_plugin and
If a third person wants to write a new Epitope Predictor. He/She has to name the file fred_plugin and
inherit from AEpitopePrediction. That's it nothing more.
'''

Expand Down