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

Delete Curation #191

Open
wants to merge 9 commits into
base: master
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
105 changes: 105 additions & 0 deletions bin/delete_existing_curation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env python

# Copyright 2023 EMBL - European Bioinformatics Institute
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 argparse

from ebi_eva_common_pyutils.config import cfg
from ebi_eva_common_pyutils.logger import logging_config as log_cfg

from eva_submission.biosample_submission.biosamples_communicators import WebinHALCommunicator, AAPHALCommunicator
from eva_submission.biosample_submission.biosamples_submitters import BioSamplesSubmitter
from eva_submission.submission_config import load_config

logger = log_cfg.get_logger(__name__)


class LastSampleCurationDeleter(BioSamplesSubmitter):

def __init__(self, communicators=None):
if not communicators:
communicators = LastSampleCurationDeleter.get_config_communicators()
super().__init__(communicators, submit_type=('curate',), allow_removal=True)


@staticmethod
def get_config_communicators():
communicators = []
# If the config has the credential for using webin with BioSamples use webin first
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is the same as here then it might make sense to refactor so as to not repeat logic.

if cfg.query('biosamples', 'webin_url') and cfg.query('biosamples', 'webin_username') and \
cfg.query('biosamples', 'webin_password'):
communicators.append(WebinHALCommunicator(
cfg.query('biosamples', 'webin_url'), cfg.query('biosamples', 'bsd_url'),
cfg.query('biosamples', 'webin_username'), cfg.query('biosamples', 'webin_password')
))
communicators.append(AAPHALCommunicator(
cfg.query('biosamples', 'aap_url'), cfg.query('biosamples', 'bsd_url'),
cfg.query('biosamples', 'username'), cfg.query('biosamples', 'password'),
cfg.query('biosamples', 'domain')
))
return communicators

def delete_last_curation_of(self, accession, delete=False):
sample_data = self.default_communicator.follows_link('samples', method='GET', join_url=accession)
curation_data = self.default_communicator.follows_link('curationLinks', json_obj=sample_data, all_pages=True)
curation_links = curation_data.get('_embedded').get('curationLinks')
curation_object = curation_links[-1]

communicator = None
# Check if we own the curation
for c in self.communicators:
if c.communicator_attributes.items() <= curation_object.items():
communicator = c
if not communicator:
self.warning(f'Curation object {curation_object["hash"]} is not owned by you: '
f'Webin: {curation_object["webinSubmissionAccountId"]} - '
f'Domain: {curation_object["domain"]}')
else:
if curation_object:
logger.info(
f'About to delete BioSamples curation {curation_object["hash"]} for accession {accession} which changed\n '
f'- attributes with {curation_object["curation"]["attributesPre"]} to {curation_object["curation"]["attributesPost"]}\n'
f'- External Ref with {curation_object["curation"]["externalReferencesPre"]} to {curation_object["curation"]["externalReferencesPost"]}\n'
f'- Relationship with {curation_object["curation"]["relationshipsPre"]} to {curation_object["curation"]["relationshipsPost"]}'
)
if delete:
self.default_communicator.follows_link('self', json_obj=curation_object, method='DELETE')
pass


def main():
arg_parser = argparse.ArgumentParser(
description='query Biosamples accessions from a file')
Copy link
Contributor

Choose a reason for hiding this comment

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

Description doesn't seem complete...

arg_parser.add_argument('--accession_file', required=True,
help='file containing the list of accession to query')
arg_parser.add_argument('--set_delete', action='store_true', default=False, help='Actually does the deletion')
# Load the config_file from default location
load_config()

args = arg_parser.parse_args()

log_cfg.add_stdout_handler()

# Load the config_file from default location
load_config()
last_sample_curation_deleter = LastSampleCurationDeleter()
with open(args.accession_file) as open_file:
for line in open_file:
accession = line.strip()
last_sample_curation_deleter.delete_last_curation_of(accession, args.set_delete)


if __name__ == "__main__":
main()
10 changes: 8 additions & 2 deletions eva_submission/biosample_submission/biosamples_communicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

import re
from json import JSONDecodeError

import requests
from cached_property import cached_property
Expand All @@ -29,7 +30,7 @@ class HALCommunicator(AppLogger):
"""
This class helps navigate through REST API that uses the HAL standard.
"""
acceptable_code = [200, 201]
acceptable_code = [200, 201, 204]

def __init__(self, auth_url, bsd_url, username, password):
self.auth_url = auth_url
Expand Down Expand Up @@ -102,7 +103,12 @@ def follows(self, query, json_obj=None, method='GET', url_template_values=None,
if join_url:
url += '/' + join_url
# Now query the url
json_response = self._req(method, url, **kwargs).json()
response = self._req(method, url, **kwargs)
try:
json_response = response.json()
except JSONDecodeError:
self.debug(f'No response available for request {method} to {url}')
return {}

# Depaginate the call if requested
if all_pages is True:
Expand Down
7 changes: 3 additions & 4 deletions tests/test_biosamples_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

import yaml

from eva_submission.biosample_submission.biosamples_communicators import HALCommunicator
from eva_submission import ROOT_DIR
from eva_submission.biosample_submission import biosamples_submitters
from eva_submission.biosample_submission.biosamples_communicators import HALCommunicator, WebinHALCommunicator, \
AAPHALCommunicator
from eva_submission.biosample_submission.biosamples_submitters import BioSamplesSubmitter, SampleMetadataSubmitter, \
SampleReferenceSubmitter
import eva_submission.biosample_submission.biosamples_submitters
SampleReferenceSubmitter, AAPHALCommunicator, WebinHALCommunicator


class BSDTestCase(TestCase):

Expand Down
Loading