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

Replace alchy with sqlservice and unfreeze SQLAlchemy #255

Merged
merged 30 commits into from
May 22, 2024

Conversation

northwestwitch
Copy link
Member

@northwestwitch northwestwitch commented May 17, 2024

This PR adds | fixes:

How to prepare for test:

  • Install in a local conda env -> pip install -e . (I've tested it with python=3.8)

How to test:

  • chanjo init --auto demodata
  • Make sure it has created a directory demodata containing these 3 files:
image
  • Link sample to stats -> chanjo --config demodata/chanjo.yaml link demodata/hgnc.grch37p13.exons.bed
  • Load config stats for sample1 ->chanjo --config demodata/chanjo.yaml load chanjo/init/demo-files/sample1.coverage.bed

Expected outcome:

  • Get samples mean coverage -> chanjo --config demodata/chanjo.yaml calculate mean -s sample1

Review:

  • Code approved by DN
  • Tests executed by CR, DN
  • "Merge and deploy" approved by

This version is a:

  • MAJOR - when you make incompatible API changes
  • MINOR - when you add functionality in a backwards compatible manner
  • PATCH - when you make backwards compatible bug fixes or documentation/instructions

context.abort()
LOG.info("delete sample (%s) from database", sample_id)
store.session.delete(sample_obj)
store.save()
Copy link
Member Author

@northwestwitch northwestwitch May 17, 2024

Choose a reason for hiding this comment

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

store.save() is no longer required, read below

except IntegrityError:
LOG.exception('elements already linked?')
chanjo_db.session.rollback()
Copy link
Member Author

Choose a reason for hiding this comment

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

Rollback is handled automatically by the session, see here

@@ -105,21 +100,3 @@ def tear_down(self):
# drop/delete the tables
self.drop_all()
return self

def save(self):
Copy link
Member Author

@northwestwitch northwestwitch May 17, 2024

Choose a reason for hiding this comment

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

This is no longer required because sessions handle commits and rollbacks automatically

@@ -23,40 +22,14 @@ def test_no_dialect():
assert chanjo_db.dialect == "sqlite"


def test_save(chanjo_db):
Copy link
Member Author

Choose a reason for hiding this comment

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

This was testing a function that is now removed

@northwestwitch
Copy link
Member Author

I've tested it locally and got the same result as with master brach. Also all automatic tests pass. Marking it ready for review!

@northwestwitch northwestwitch marked this pull request as ready for review May 17, 2024 08:55
@northwestwitch northwestwitch changed the title Replace alchy with sqlservice Replace alchy with sqlservice and unfreeze SQLAlchemy May 17, 2024
Copy link
Collaborator

@dnil dnil left a comment

Choose a reason for hiding this comment

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

Well done! Looks good to me, and tested as suggested locally without issue.

@@ -2,14 +2,13 @@
from collections import namedtuple
from datetime import datetime

from alchy import ModelBase, make_declarative_base
from sqlservice import declarative_base
Copy link
Collaborator

Choose a reason for hiding this comment

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

🎉

from sqlalchemy import Column, types, ForeignKey, UniqueConstraint, orm

Exon = namedtuple('Exon', ['chrom', 'start', 'end', 'completeness'])

# base for declaring a mapping
BASE = make_declarative_base(Base=ModelBase)

BASE = declarative_base()
Copy link
Collaborator

Choose a reason for hiding this comment

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

chanjo/store/models.py Outdated Show resolved Hide resolved
@@ -89,11 +87,13 @@ class TranscriptStat(BASE):
threshold = Column(types.Integer)
_incomplete_exons = Column(types.Text)

sample_id = Column(types.String(32), ForeignKey('sample.id'),
sample_id = Column(types.String(32), ForeignKey('sample.id', ondelete='CASCADE'),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same there, nice!

Copy link
Member Author

Choose a reason for hiding this comment

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

without that param if you removed a sample it didin't remove the linked records in TranscriptStat any more. I guess something has changed in SQLAlchemy when switching to version 2?

data = json.loads(lines[0].strip())
assert data['sample_id'] == 'sample'
assert isinstance(data['mean_coverage'], float)
with popexist_db.begin() as session:
Copy link
Collaborator

Choose a reason for hiding this comment

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

🥇

@Clinical-Genomics Clinical-Genomics deleted a comment from dnil May 17, 2024
@northwestwitch
Copy link
Member Author

Testing on hasta stage:

  • First attempt failed, since SQLAlchemy 2.0 runs on python>=3.7 and the S_chanjo env had python 3.6.9

  • After updating the python version in S_chanjo to 3.11:

image image
  • This branch now installs fine

$ bash /home/proj/production/servers/resources/hasta.scilifelab.se/update-tool-stage.sh -e S_chanjo -t chanjo -b deprecate_alchy

image

@northwestwitch
Copy link
Member Author

northwestwitch commented May 22, 2024

Test loading coverage for a case:

image

One of the samples was already loaded but the other 2 got loaded.

The report looks like this:

image

@northwestwitch northwestwitch merged commit a2fb2d0 into master May 22, 2024
1 check passed
@northwestwitch northwestwitch deleted the deprecate_alchy branch May 22, 2024 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace deprecated Alchy with SQLAlchemy
2 participants