Skip to content

Commit

Permalink
better handling of default class1 models configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
timodonnell committed Feb 5, 2018
1 parent 54b923a commit c3652f9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions mhcflurry/class1_affinity_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from .class1_neural_network import Class1NeuralNetwork
from .common import random_peptides
from .downloads import get_path
from .downloads import get_default_class1_models_dir
from .encodable_sequences import EncodableSequences
from .percent_rank_transform import PercentRankTransform
from .regression_target import to_ic50
Expand Down Expand Up @@ -283,7 +283,7 @@ def load(models_dir=None, max_models=None):
`Class1AffinityPredictor` instance
"""
if models_dir is None:
models_dir = get_path("models_class1", "models")
models_dir = get_default_class1_models_dir()

manifest_path = join(models_dir, "manifest.csv")
manifest_df = pandas.read_csv(manifest_path, nrows=max_models)
Expand Down
36 changes: 35 additions & 1 deletion mhcflurry/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)
import logging
import yaml
from os.path import join, exists
from os.path import join, exists, relpath
from pipes import quote
from os import environ
from collections import OrderedDict
Expand All @@ -20,11 +20,14 @@
"MHCFLURRY_DATA_DIR",
"MHCFLURRY_DOWNLOADS_CURRENT_RELEASE",
"MHCFLURRY_DOWNLOADS_DIR",
"MHCFLURRY_DEFAULT_CLASS1_MODELS"
]

_DOWNLOADS_DIR = None
_CURRENT_RELEASE = None
_METADATA = None
_MHCFLURRY_DEFAULT_CLASS1_MODELS_DIR = environ.get(
"MHCFLURRY_DEFAULT_CLASS1_MODELS_DIR")


def get_downloads_dir():
Expand All @@ -51,6 +54,37 @@ def get_downloads_metadata():
return _METADATA


def get_default_class1_models_dir(test_exists=True):
"""
Return the absolute path to the default class1 models dir.
If environment variable MHCFLURRY_DEFAULT_CLASS1_MODELS_DIR is set to an
absolute path, return that path. If it's set to a relative path (i.e. does
not start with /) then return that path taken to be relative to the mhcflurry
downloads dir.
If environment variable MHCFLURRY_DEFAULT_CLASS1_MODELS_DIR is NOT set,
then return the path to downloaded models in the "models_class1" download.
Parameters
----------
test_exists : boolean, optional
Whether to raise an exception of the path does not exist
Returns
-------
string : absolute path
"""
if _MHCFLURRY_DEFAULT_CLASS1_MODELS_DIR:
result = join(get_downloads_dir(), _MHCFLURRY_DEFAULT_CLASS1_MODELS_DIR)
if test_exists and not exists(result):
raise IOError("No such directory: %s" % result)
return result
else:
return get_path("models_class1", "models", test_exists=test_exists)


def get_current_release_downloads():
"""
Return a dict of all available downloads in the current release.
Expand Down
6 changes: 2 additions & 4 deletions mhcflurry/downloads_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,13 @@ def exists_string(path):

downloads = get_current_release_downloads()

format_string = "%-40s %-12s %-12s %-20s "
print(format_string % (
"DOWNLOAD NAME", "DOWNLOADED?", "DEFAULT?", "URL"))
format_string = "%-40s %-12s %-20s "
print(format_string % ("DOWNLOAD NAME", "DOWNLOADED?", "URL"))

for (item, info) in downloads.items():
print(format_string % (
item,
yes_no(info['downloaded']),
yes_no(info['metadata']['default']),
info['metadata']["url"]))


Expand Down
6 changes: 3 additions & 3 deletions mhcflurry/predict_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import pandas

from .downloads import get_path
from .downloads import get_default_class1_models_dir
from .class1_affinity_predictor import Class1AffinityPredictor


Expand Down Expand Up @@ -122,7 +122,7 @@
metavar="DIR",
default=None,
help="Directory containing models. "
"Default: %s" % get_path("models_class1", "models", test_exists=False))
"Default: %s" % get_default_class1_models_dir(test_exists=False))
model_args.add_argument(
"--include-individual-model-predictions",
action="store_true",
Expand All @@ -143,7 +143,7 @@ def run(argv=sys.argv[1:]):
# The reason we set the default here instead of in the argument parser is that
# we want to test_exists at this point, so the user gets a message instructing
# them to download the models if needed.
models_dir = get_path("models_class1", "models")
models_dir = get_default_class1_models_dir(test_exists=True)
predictor = Class1AffinityPredictor.load(models_dir)

# The following two are informative commands that can come
Expand Down

0 comments on commit c3652f9

Please sign in to comment.