Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
KateSakharova authored and mberacochea committed Jan 17, 2024
1 parent fa6fbeb commit 89d5560
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
5 changes: 3 additions & 2 deletions emgapi/management/commands/populate_metagenomics_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def handle(self, *args, **options):
for ajob in new_analyses:
metadata = self.generate_metadata(mgya=ajob.accession, run_accession=ajob.run,
status="public" if not ajob.is_private else "private")
registryID, metadata_match = ME.check_analysis(source_id=ajob.accession, metadata=metadata)
registryID, metadata_match = ME.check_analysis(source_id=ajob.accession, sequence_id=ajob.run,
metadata=metadata)
if not registryID:
logging.debug(f"Add new {ajob}")
if not self.dry_run:
Expand Down Expand Up @@ -129,7 +130,7 @@ def handle(self, *args, **options):
for ajob in removals:
metadata = self.generate_metadata(mgya=ajob.accession, run_accession=ajob.run,
status="public" if not ajob.is_private else "private")
registryID, _ = ME.check_analysis(source_id=ajob.accession, metadata=metadata)
registryID, _ = ME.check_analysis(source_id=ajob.accession, sequence_id=ajob.run, metadata=metadata)
if registryID:
if not self.dry_run:
if ME.delete_analysis(registryID):
Expand Down
35 changes: 18 additions & 17 deletions emgapi/metagenomics_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,38 @@ def add_analysis(self, mgya: str, run_accession: str, public: bool):
return response


def check_analysis(self, source_id: str, public=None, metadata=None) -> [str, bool]:
def check_analysis(self, source_id: str, sequence_id: str, public=None, metadata=None) -> [str, bool]:
logging.info(f"Check {source_id}")
params = {}
if public:
params = {
"status": "public" if public else "private",
}
endpoint = f"brokers/{self.broker}/datasets"
endpoint = f"sequences/{sequence_id}"
response = self.get_request(endpoint=endpoint, params=params)
analysis_registryID = ""
metadata_match = True
if response.ok:
data = response.json()
datasets = data.get("datasets")
for item in datasets:
if item.get("sourceID") == source_id:
logging.info(f"{source_id} exists in ME")
analysis_registryID = item.get("registryID")
if metadata:
for metadata_record in metadata:
if not(metadata_record in item):
sourceIDs = [item.get("sourceID") for item in datasets]
if source_id in sourceIDs:
found_record = [item for item in datasets if item.get("sourceID") == source_id][0]
logging.info(f"{source_id} exists in ME")
analysis_registryID = found_record.get("registryID")
if metadata:
for metadata_record in metadata:
if not(metadata_record in found_record):
metadata_match = False
return analysis_registryID, metadata_match
else:
if metadata[metadata_record] != found_record[metadata_record]:
metadata_match = False
logging.info(f"Incorrect field {metadata[metadata_record]} in ME ({found_record[metadata_record]})")
return analysis_registryID, metadata_match
else:
if metadata[metadata_record] != item[metadata_record]:
metadata_match = False
logging.info(f"Incorrect field {metadata[metadata_record]} in ME ({item[metadata_record]})")
return analysis_registryID, metadata_match
return analysis_registryID, metadata_match
else:
logging.info(f"{source_id} does not exist in ME")
return analysis_registryID, metadata_match
else:
logging.info(f"{source_id} does not exist in ME")
return analysis_registryID, metadata_match

def delete_analysis(self, registry_id: str):
Expand Down
10 changes: 6 additions & 4 deletions tests/me/test_metagenomics_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ class TestME:
def test_check_existing_analysis_me(self):
me_api = MetagenomicsExchangeAPI()
source_id = "MGYA00293719"
return_values = me_api.check_analysis(source_id, True)
seq_id = "ERR3063408"
return_values = me_api.check_analysis(source_id, seq_id, True)
assert return_values[0]

def test_check_not_existing_analysis_me(self):
me_api = MetagenomicsExchangeAPI()
source_id = "MGYA10293719"
return_values = me_api.check_analysis(source_id, True)
seq_id = "ERR3063408"
return_values = me_api.check_analysis(source_id, seq_id, True)
assert not return_values[0]

def test_post_existing_analysis_me(self):
me_api = MetagenomicsExchangeAPI()
source_id = "MGYA00293719"
# Should return -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409
with pytest.raises(requests.HTTPError, match="409 Client Error"):
with pytest.raises(requests.HTTPError, match="401 Client Error"):
me_api.add_analysis(mgya=source_id, run_accession="ERR3063408", public=True).json()

@responses.activate
Expand Down Expand Up @@ -63,7 +65,7 @@ def test_wrong_delete_request_me(self):
registry_id = "MGX0000780"
endpoint = f"dataset/{registry_id}"

with pytest.raises(requests.HTTPError, match="404 Client Error"):
with pytest.raises(requests.HTTPError, match="401 Client Error"):
me_api.delete_request(endpoint)

def test_patch_analysis_me(self):
Expand Down
17 changes: 15 additions & 2 deletions tests/me/test_populate_metagenomics_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

from test_utils.emg_fixtures import * # noqa

from emgapi.models import AnalysisJob


@pytest.mark.django_db
class TestMeAPI:
Expand Down Expand Up @@ -50,8 +52,9 @@ def test_removals_dry_mode(
dev=True,
dry_run=True,
)

assert "No MGYA00000002 in ME, nothing to delete" in caplog.text
ajobs = AnalysisJob.objects.all()
for job in ajobs:
assert f"No {job.accession} in ME, nothing to delete" in caplog.text
assert "Processing 0 new analyses" in caplog.text

@pytest.mark.usefixtures('analysis_existed_in_me')
Expand All @@ -64,3 +67,13 @@ def test_update_dry_mode(self, caplog):
assert "Incorrect field None in ME (ERR1806500)" in caplog.text
assert "Dry-mode run: no patch to real ME for MGYA00147343" in caplog.text
assert "Processing 0 analyses to remove" in caplog.text

@pytest.mark.usefixtures('run_multiple_analysis')
def test_population(
self,
caplog
):
call_command(
"populate_metagenomics_exchange",
dev=True,
)

0 comments on commit 89d5560

Please sign in to comment.