Skip to content

Commit

Permalink
tests + bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
khoroshevskyi committed Jul 23, 2024
1 parent 39070cc commit 9550289
Show file tree
Hide file tree
Showing 10 changed files with 594 additions and 35 deletions.
5 changes: 4 additions & 1 deletion pepdbagent/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Projects(Base):

history_mapping: Mapped[List["HistoryProjects"]] = relationship(
back_populates="project_mapping", cascade="all, delete-orphan"
) # TODO: check if cascade is correct
)

__table_args__ = (UniqueConstraint("namespace", "name", "tag"),)

Expand Down Expand Up @@ -317,6 +317,9 @@ class Schemas(Base):
default=deliver_update_date, onupdate=deliver_update_date
)

projects_mappings: Mapped[List["Projects"]] = relationship(
"Projects", back_populates="schema_mapping"
)
group_relation_mapping: Mapped[List["SchemaGroupRelations"]] = relationship(
"SchemaGroupRelations", back_populates="schema_mapping"
)
Expand Down
7 changes: 4 additions & 3 deletions pepdbagent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,10 @@ class SchemaAnnotation(BaseModel):

namespace: str
name: str
last_update_date: Optional[datetime.datetime]
submission_date: Optional[datetime.datetime]
description: Optional[str]
last_update_date: str
submission_date: str
description: Optional[str] = ""
popularity_number: Optional[int] = 0


class SchemaSearchResult(BaseModel):
Expand Down
77 changes: 50 additions & 27 deletions pepdbagent/modules/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Schemas,
SchemaGroups,
SchemaGroupRelations,
User,
)
from pepdbagent.exceptions import (
SchemaAlreadyExistsError,
Expand Down Expand Up @@ -110,9 +111,10 @@ def info(self, namespace: str, name: str) -> SchemaAnnotation:
return SchemaAnnotation(
namespace=schema_obj.namespace,
name=schema_obj.name,
last_update_date=schema_obj.last_update_date,
submission_date=schema_obj.submission_date,
last_update_date=str(schema_obj.last_update_date),
submission_date=str(schema_obj.submission_date),
description=schema_obj.description,
popularity_number=len(schema_obj.projects_mappings),
)

def search(
Expand Down Expand Up @@ -143,9 +145,10 @@ def search(
SchemaAnnotation(
namespace=result.namespace,
name=result.name,
last_update_date=result.last_update_date,
submission_date=result.submission_date,
last_update_date=str(result.last_update_date),
submission_date=str(result.submission_date),
description=result.description,
# popularity_number=sum(result.projects_mappings),
)
)

Expand Down Expand Up @@ -229,6 +232,13 @@ def create(
)

with Session(self._sa_engine) as session:
user = session.scalar(select(User).where(User.namespace == namespace))

if not user:
user = User(namespace=namespace)
session.add(user)
session.commit()

schema_obj = Schemas(
namespace=namespace,
name=name,
Expand Down Expand Up @@ -366,8 +376,8 @@ def group_get(self, namespace: str, name: str) -> SchemaGroupAnnotation:
SchemaAnnotation(
namespace=schema_annotation.namespace,
name=schema_annotation.name,
last_update_date=schema_annotation.last_update_date,
submission_date=schema_annotation.submission_date,
last_update_date=str(schema_annotation.last_update_date),
submission_date=str(schema_annotation.submission_date),
desciription=schema_annotation.description,
)
)
Expand Down Expand Up @@ -458,7 +468,7 @@ def _group_search_count(self, namespace: str = None, search_str: str = ""):
"""
statement = select(func.count(SchemaGroups.id))

statement = self._add_condition(statement, namespace, search_str)
statement = self._add_group_condition(statement, namespace, search_str)

with Session(self._sa_engine) as session:
result = session.execute(statement).one()
Expand All @@ -475,7 +485,7 @@ def group_delete(self, namespace: str, name: str) -> None:
:return: None
"""

if self.group_exist(namespace, name):
if not self.group_exist(namespace, name):
raise SchemaGroupDoesNotExistError(
f"Schema group '{name}' does not exist in the database"
)
Expand Down Expand Up @@ -559,30 +569,43 @@ def group_remove_schema(

try:
with Session(self._sa_engine) as session:
session.execute(
delete(SchemaGroupRelations).where(
and_(
SchemaGroupRelations.schema_id
== select(Schemas.id)
.where(
and_(
Schemas.namespace == schema_namespace,
Schemas.name == schema_name,
)

a = session.scalar(
select(Schemas).where(
and_(Schemas.namespace == schema_namespace, Schemas.name == schema_name)
)
)
b = session.scalar(
select(SchemaGroups).where(
and_(SchemaGroups.namespace == namespace, SchemaGroups.name == name)
)
)

delete_statement = delete(SchemaGroupRelations).where(
and_(
SchemaGroupRelations.schema_id
== select(Schemas.id)
.where(
and_(
Schemas.namespace == schema_namespace,
Schemas.name == schema_name,
)
.subquery(),
SchemaGroupRelations.group_id
== select(SchemaGroups.id)
.where(
and_(
SchemaGroups.namespace == namespace,
SchemaGroups.name == name,
)
)
.subquery(),
SchemaGroupRelations.group_id
== select(SchemaGroups.id)
.where(
and_(
SchemaGroups.namespace == namespace,
SchemaGroups.name == name,
)
.subquery(),
)
.subquery(),
)
)

session.execute(delete_statement)
session.commit()
except IntegrityError:
raise SchemaIsNotInGroupError("Schema not found in the group")

Expand Down
69 changes: 69 additions & 0 deletions tests/schemas/namespace1/2.0.0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
description: "Schema for a minimal PEP"
version: "2.0.0"
properties:
config:
properties:
name:
type: string
pattern: "^\\S*$"
description: "Project name with no whitespace"
pep_version:
description: "Version of the PEP Schema this PEP follows"
type: string
sample_table:
type: string
description: "Path to the sample annotation table with one row per sample"
subsample_table:
type: string
description: "Path to the subsample annotation table with one row per subsample and sample_name attribute matching an entry in the sample table"
sample_modifiers:
type: object
properties:
append:
type: object
duplicate:
type: object
imply:
type: array
items:
type: object
properties:
if:
type: object
then:
type: object
derive:
type: object
properties:
attributes:
type: array
items:
type: string
sources:
type: object
project_modifiers:
type: object
properties:
amend:
description: "Object overwriting original project attributes"
type: object
import:
description: "List of external PEP project config files to import"
type: array
items:
type: string
required:
- pep_version
samples:
type: array
items:
type: object
properties:
sample_name:
type: string
pattern: "^\\S*$"
description: "Unique name of the sample with no whitespace"
required:
- sample_name
required:
- samples
77 changes: 77 additions & 0 deletions tests/schemas/namespace1/2.1.0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
description: "Schema for a minimal PEP"
version: "2.1.0"
properties:
config:
properties:
name:
type: string
pattern: "^\\S*$"
description: "Project name with no whitespace"
pep_version:
description: "Version of the PEP Schema this PEP follows"
type: string
sample_table:
type: string
description: "Path to the sample annotation table"
subsample_table:
type: string
description: "Path to the subsample annotation table with one row per subsample and sample_name attribute matching an entry in the sample table"
sample_table_index:
type: string
pattern: "^\\S*$"
description: "Name of the column in sample table to use as an index. It's 'sample_name' by default"
subsample_table_index:
type: array
items:
type: string
pattern: "^\\S*$"
description: "Names of the columns in subsample table to use as an index. It's ['sample_name', 'subsample_name'] by default"
sample_modifiers:
type: object
properties:
append:
type: object
duplicate:
type: object
imply:
type: array
items:
type: object
properties:
if:
type: object
then:
type: object
derive:
type: object
properties:
attributes:
type: array
items:
type: string
sources:
type: object
project_modifiers:
type: object
properties:
amend:
description: "Object overwriting original project attributes"
type: object
import:
description: "List of external PEP project config files to import"
type: array
items:
type: string
required:
- pep_version
samples:
type: array
items:
type: object
properties:
sample_name:
type: string
pattern: "^\\S*$"
description: "Unique name of the sample with no whitespace"
required:
- samples
47 changes: 47 additions & 0 deletions tests/schemas/namespace2/bedboss.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
description: bedboss run-all pep schema

properties:
samples:
type: array
items:
type: object
properties:
sample_name:
type: string
description: "Name of the sample"
input_file:
type: string
description: "Absolute path to the input file"
input_type:
type: string
description: "file format"
enum: [ "bigWig", "bigBed", "bed", "wig", "bedGraph" ]
genome:
type: string
description: "organism genome code"
format_type:
type: string
description: "whether the regions are narrow (transcription factor implies narrow, histone mark implies broad peaks)"
enum: [ "narrowPeak", "broadPeak" ]
description:
type: string
description: "freeform description of the sample"
open_signal_matrix:
type: string
description: "A full path to the openSignalMatrix required for the tissue"
chrom_sizes:
type: string
description: "A full path to the chrom.sizes required for the bedtobigbed conversion"
treatment:
type: string
description: "freeform description of the sample treatment"
cell_type:
type: string
description: "cell type code"
required:
- sample_name
- input_file
- input_type
- genome
required:
- samples
25 changes: 25 additions & 0 deletions tests/schemas/namespace2/bedbuncher.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
description: bedbuncher PEP schema
imports:
- http://schema.databio.org/pep/2.0.0.yaml

properties:
samples:
type: array
items:
type: object
properties:
JSONquery_path:
type: string
description: "path to the JSON file with the Elasticsearch query"
bedset_name:
type: string
pattern: "^\\S*$"
description: "name of the bedset that will be created"
bbconfig_path:
type: string
description: "path to bedbase config file"
required:
- JSONquery_path
- bedset_name
required:
- samples
Loading

0 comments on commit 9550289

Please sign in to comment.