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

Feature/explicit studies privacy check #357

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
3 changes: 2 additions & 1 deletion ci/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ emg:
celery_backend: 'redis://localhost:6379/1'
results_production_dir: '/dummy/path/results'
# metagenomics exchange
me_api: 'https://wwwdev.ebi.ac.uk/ena/registry/metagenome/api'
me_api: 'https://wwwdev.ebi.ac.uk/ena/registry/metagenome/api'
mberacochea marked this conversation as resolved.
Show resolved Hide resolved
ena_api_password: None
2 changes: 2 additions & 0 deletions config/local-lite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ emg:
ENGINE: 'django.db.backends.sqlite3'
NAME: '/opt/ci/testdbs/ena-testdb.sqlite'
ERA_TABLESPACE_PREFIX: ''
ena_api_password: None


admin: True
downloads_bypass_nginx: True
Expand Down
3 changes: 2 additions & 1 deletion config/local-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ emg:
celery_backend: 'redis://localhost:6379/1'
results_production_dir: '/dummy/path/results'
# metagenomics exchange
me_api: 'https://wwwdev.ebi.ac.uk/ena/registry/metagenome/api'
me_api: 'https://wwwdev.ebi.ac.uk/ena/registry/metagenome/api'
ena_api_password: None
63 changes: 58 additions & 5 deletions emgapianns/management/lib/create_or_update_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
# limitations under the License.

import logging

import os
from base64 import b64encode
import requests
from django.db.models import Q
from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist
Expand All @@ -27,6 +29,7 @@
update_or_create_publication,
)
from emgapianns.management.lib import utils
from emgcli.settings import EMG_CONF
from emgena import models as ena_models
from emgena.models import RunStudy, AssemblyStudy

Expand Down Expand Up @@ -122,7 +125,7 @@ def _lookup_publication_by_project_id(self, project_id):
pass

def _update_or_create_study_from_db_result(
self, ena_study, study_result_dir, lineage, ena_db, emg_db
self, ena_study, study_result_dir, lineage, ena_db, emg_db
):
"""Attributes to parse out:
- Center name (done)
Expand Down Expand Up @@ -163,7 +166,25 @@ def _update_or_create_study_from_db_result(
)

hold_date = ena_study.hold_date
is_private = bool(hold_date)
ena_api_password = EMG_CONF['emg']['ena_api_password']
if ena_api_password is None:
logging.warning("ENA API password is missing. Study ownership cannot be verified.")
MGS-sails marked this conversation as resolved.
Show resolved Hide resolved
try:
study_is_public = self.check_if_study_is_public(secondary_study_accession)
if study_is_public:
is_private = False
else:
study_ownership_verified = self.verify_study_ownership(secondary_study_accession,
ena_study.submission_account_id)
if study_ownership_verified:
is_private = True
else:
logging.error(
f"Study {secondary_study_accession} is not owned by {ena_study.submission_account_id}. Skipping.")
return
except:
logging.error(f"Could not verify if study {secondary_study_accession} is public. Skipping.")
return

# Retrieve biome object
biome = emg_models.Biome.objects.using(emg_db).get(lineage=lineage)
Expand Down Expand Up @@ -222,7 +243,7 @@ def _get_ena_project(ena_db, project_id):
return project

def _update_or_create_study_from_api_result(
self, api_study, study_result_dir, lineage, ena_db, emg_db
self, api_study, study_result_dir, lineage, ena_db, emg_db
):
secondary_study_accession = api_study.get("secondary_study_accession")
data_origination = (
Expand Down Expand Up @@ -261,10 +282,42 @@ def _update_or_create_study_from_api_result(

@staticmethod
def _update_or_create_study(
emg_db, project_id, secondary_study_accession, defaults
emg_db, project_id, secondary_study_accession, defaults
):
return emg_models.Study.objects.using(emg_db).update_or_create(
project_id=project_id,
secondary_accession=secondary_study_accession,
defaults=defaults,
)

def check_if_study_is_public(self, study_id):
url = (f"https://www.ebi.ac.uk/ena/portal/api/search?result=study&query=study_accession%3D{study_id}%20OR"
f"%20secondary_study_accession%3D{study_id}&limit=10&dataPortal=metagenome&includeMetagenomes=true&format"
"=json")
headers = {
"accept": "*/*"
}
response = requests.get(url, params=None, headers=headers)
mberacochea marked this conversation as resolved.
Show resolved Hide resolved
if response.json() == []:
is_public = False
else:
is_public = response.json()[0]['secondary_study_accession'] == study_id

return is_public

def verify_study_ownership(self, study_id, submission_account_id):
url = f"{'https://www.ebi.ac.uk/ena/submit/report/studies'}/{study_id}"
MGS-sails marked this conversation as resolved.
Show resolved Hide resolved
ena_api_password = EMG_CONF['emg']['ena_api_password']
if not ena_api_password:
logging.warning("ENA API password is missing. Study ownership cannot be verified.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logging.warning("ENA API password is missing. Study ownership cannot be verified.")
logging.error("ENA API password is missing. Study ownership cannot be verified.")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @mberacochea .
The idea here is to allow this part to continue when the CI pipeline is running.
Since in the pipeline config, the ena_api_password is kept empty

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is fine, but in case this happens by accident I would print that in the error log file

return False
auth_string = f"mg-{submission_account_id}:{ena_api_password}"
headers = {
"accept": "*/*",
"Authorization": f"Basic {b64encode(auth_string.encode()).decode()}"
}
response = requests.get(url, params=None, headers=headers)
MGS-sails marked this conversation as resolved.
Show resolved Hide resolved
ownership_verified = False
if response.status_code == 200 and study_id in response.text:
ownership_verified = True
return ownership_verified
MGS-sails marked this conversation as resolved.
Show resolved Hide resolved
Loading