Skip to content

Commit

Permalink
Modified ME dj function to add new ME records, fixed tests for ME and…
Browse files Browse the repository at this point in the history
… api
  • Loading branch information
KateSakharova committed Oct 20, 2023
1 parent c51ed5d commit 481a4d5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
19 changes: 10 additions & 9 deletions emgapi/management/commands/mgx_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
import logging

from django.core.management import BaseCommand
from django.conf import settings

from emgapi.models import AnalysisJob, MetagenomicsExchange, ME_Broker
from metagenomics_exchange import MetagenomicsExchangeAPI
from emgapi.metagenomics_exchange import MetagenomicsExchangeAPI

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -101,22 +100,24 @@ def handle(self, *args, **options):
self.assembly_accession = options.get("assembly")
self.run_accession = options.get("run")

# FIXME: this command needs adjustments
broker = options.get("broker")
url = settings.ME_API['dev']
check_url = url + f'brokers/{self.broker}/datasets'
ME = MetagenomicsExchangeAPI()
broker_obj = ME_Broker.objects.get_or_create(broker)
ME = MetagenomicsExchangeAPI(broker=broker)
analyses = self._filtering_analyses()

for analysis in analyses:
MGYA = analysis.accession
public = not analysis.is_private
# check is MGYA in ME
if ME.check_analysis(url=check_url, sourceID=MGYA, public=public, token=settings.ME_TOKEN):
if ME.check_analysis(source_id=MGYA, public=public):
logging.info(f"{MGYA} exists in ME")
else:
logging.info(f"{MGYA} does not exist in ME")
ME.add_record(url=url, mgya=MGYA, run_accession=analysis.run.accesison, public=public, broker=broker,
token=settings.ME_TOKEN)
response = ME.add_record(mgya=MGYA, run_accession=analysis.run, public=public)
if response.status_code == 201:
logging.info(f"Populating MetagenomicsExchange table with {MGYA}")
MetagenomicsExchange.objects.get_or_create(analysis, broker=broker_obj)
else:
logging.error(f"{MGYA} response exit code: {response.status_code}. No population to DB.")


8 changes: 4 additions & 4 deletions emgapi/metagenomics_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ def post_request(self, endpoint: str, data: dict):
def add_record(self, mgya: str, run_accession: str, public: bool):
data = {
"confidence": "full",
"endPoint": f"https://www.ebi.ac.uk/metagenomics/analyses/mgya",
"endPoint": f"https://www.ebi.ac.uk/metagenomics/analyses/{mgya}",
"method": ["other_metadata"],
"sourceID": mgya,
"sequenceID": run_accession,
"status": "public" if public else "private",
"brokerID": self.broker,
}
response = self.post_request(endpoint="datasets", data=data)
return response.json()
return response

def check_analysis(self, source_id: str, public: bool) -> bool:
logging.info(f"Check {source_id}")
Expand All @@ -75,7 +75,7 @@ def check_analysis(self, source_id: str, public: bool) -> bool:
datasets = data.get("datasets")
for item in datasets:
if item.get("sourceID") == source_id:
logging.info(f"{source_id} exists")
return True
logging.info(f"{source_id} exists")

logging.info(f"{source_id} does not exist")
return False
11 changes: 6 additions & 5 deletions emgapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,18 +1615,16 @@ class ME_Broker(models.Model):


class MetagenomicsExchangeManager(models.Manager):
def get_or_create(self, analysis, broker=None):
def get_or_create(self, analysis, broker):
try:
me_record = MetagenomicsExchange.objects.get(
analysis=analysis
analysis=analysis, broker=broker
)
except ObjectDoesNotExist:
logging.warning(f"{analysis.accession} not in ME db, creating.")
me_record = MetagenomicsExchange(
analysis=analysis,
analysis=analysis, broker=broker
)
if broker:
me_record.broker = broker
me_record.save()
logging.info(f"Created record ID: {me_record.id}")
return me_record
Expand All @@ -1644,6 +1642,9 @@ class MetagenomicsExchange(models.Model):
objects = MetagenomicsExchangeManager()
objects_admin = models.Manager()

def __str__(self):
return f"{self.analysis.job_id} under {self.broker}"


class StudyErrorType(models.Model):
error_id = models.IntegerField(
Expand Down
24 changes: 21 additions & 3 deletions tests/me/test_metagenomics_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

from emgapi.metagenomics_exchange import MetagenomicsExchangeAPI

from requests import HTTPError
import requests
import responses
import settings


class TestME:
Expand All @@ -24,5 +26,21 @@ def test_post_existing_analysis(self):
me_api = MetagenomicsExchangeAPI()
source_id = "MGYA00293719"
# Should return -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409
with pytest.raises(HTTPError, match="409 Client Error"):
me_api.add_record(mgya=source_id, run_accession="ERR3063408", public=True)
with pytest.raises(requests.HTTPError, match="409 Client Error"):
me_api.add_record(mgya=source_id, run_accession="ERR3063408", public=True).json()

@responses.activate
def test_mock_post_new_analysis(self):
me_api = MetagenomicsExchangeAPI()
endpoint = "datasets"
url = settings.ME_API + f"/{endpoint}"

responses.add(responses.POST, url, json={'success': True}, status=201)

response = me_api.add_record(mgya="MGYA00593709", run_accession="SRR3960575", public=True)

assert response.status_code == 201
assert response.json() == {'success': True}



27 changes: 18 additions & 9 deletions tests/me/test_api.py → tests/me/test_mgx_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""

import pytest
from unittest.mock import patch

Expand All @@ -25,18 +25,21 @@

@pytest.mark.django_db
class TestMeAPI:
@patch("emgapi.management.commands.mgx_api.Command.post_request")
@patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.post_request")
@pytest.mark.usefixtures(
"me_broker",
"metagenomics_exchange",
)
def test_new_analysis_population(
self,
mock_post_request,
run_multiple_analysis
mock_post_request
):
class MockResponse:
def __init__(self, ok, status_code):
self.ok = ok
self.status_code = status_code

mock_post_request.return_value = MockResponse(True, 200)
mock_post_request.return_value = MockResponse(True, 201)
test_run = "ABC01234"
test_pipeline_version = 5.0
test_broker = 'EMG'
Expand All @@ -49,9 +52,14 @@ def __init__(self, ok, status_code):
)
analysis = AnalysisJob.objects.filter(run__accession=test_run).filter(pipeline__pipeline_id=test_pipeline_version).first()
assert ME_Broker.objects.filter(brokerID=test_broker).count() == 1
assert MetagenomicsExchange.objects.filter(analysis=analysis).count() == 1
broker_id = ME_Broker.objects.filter(brokerID=test_broker).first().id
assert MetagenomicsExchange.objects.filter(analysis=analysis).filter(broker=broker_id).count() == 1

@patch("emgapi.management.commands.mgx_api.Command.get_request")
@pytest.mark.usefixtures(
"me_broker",
"metagenomics_exchange",
)
@patch("emgapi.metagenomics_exchange.MetagenomicsExchangeAPI.get_request")
def test_check_existing_analysis(
self,
mock_get_request,
Expand All @@ -67,6 +75,7 @@ def __init__(self, ok, status_code, json_data):

def json(self):
return self.json_data

test_run = "ABC01234"
test_pipeline_version = 1.0
test_broker = 'MAR'
Expand All @@ -83,5 +92,5 @@ def json(self):
)

assert ME_Broker.objects.filter(brokerID=test_broker).count() == 1
assert MetagenomicsExchange.objects.filter(analysis=analysis).count() == 1
"""
broker_id = ME_Broker.objects.filter(brokerID=test_broker).first().id
assert MetagenomicsExchange.objects.filter(analysis=analysis, broker=broker_id).count() == 1

0 comments on commit 481a4d5

Please sign in to comment.