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

chore: add unit tests for python pkg #99

Merged
merged 6 commits into from
Apr 24, 2023
Merged

chore: add unit tests for python pkg #99

merged 6 commits into from
Apr 24, 2023

Conversation

manzt
Copy link
Member

@manzt manzt commented Apr 22, 2023

  • moves "core" logic into src/chromoscope/_viewer.py
  • test: add unit tests
  • add unit tests to ci

@manzt
Copy link
Member Author

manzt commented Apr 22, 2023

@sehilyi - on the python side, it might be worth looking at https://pydantic.dev/ to validate the config objects passed in

@manzt manzt requested a review from sehilyi April 22, 2023 19:47
Copy link
Member

@sehilyi sehilyi left a comment

Choose a reason for hiding this comment

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

Thanks! I will merge this.

Would publishing this to PyPI requires adding API tokens to the GH repo?

@manzt
Copy link
Member Author

manzt commented Apr 24, 2023

Yup, maybe have a look at servir's release workflow. You'll need to publish manually once locally (hatch run build && hatch publish). But then on PyPI you can create an API token for this repo, and add a secret so that publish are made with tagged commits).

@sehilyi sehilyi merged commit 79257a8 into master Apr 24, 2023
@manzt
Copy link
Member Author

manzt commented Apr 24, 2023

Oh, the other thing to mention about pydantic is that you can generate JSON schema from the models.

from typing import Literal, Union

from pydantic import BaseModel, Field, HttpUrl


class Config(BaseModel):
    id: str
    cancer: str
    assembly: Literal["hg38", "hg19"]
    sv: HttpUrl
    cnv: HttpUrl = Field(title="CNV", description="An URL of the CNV text file (.txt).")
    drivers: Union[HttpUrl, None] = Field(
        default=None,
        title="Drivers",
        description="An URL of a file that contains drivers (.txt).",
    )
    vcf: Union[HttpUrl, None] = Field(
        default=None,
        title="VCF",
        description="An URL of the point mutation file (.vcf).",
    )
    vcfIndex: Union[HttpUrl, None] = Field(
        default=None,
        title="VCF Index",
        description="An URL of the point mutation index file (.tbi).",
    )
    vcf2: Union[HttpUrl, None] = Field(
        default=None,
        title="VCF2",
        description="An URL of the the indel file (.vcf).",
    )
    vcf2Index: Union[HttpUrl, None] = Field(
        default=None,
        title="VCF2 Index",
        description="An URL of the indel index file (.tbi).",
    )
    bam: Union[HttpUrl, None] = Field(
        default=None,
        title="BAM",
        description="An URL of the BAM file (.bam)."
    )
    bamIndex: Union[HttpUrl, None] = Field(
        default=None,
        title="BAM Index",
        description="An URL of the BAM index file (.bai)."
    )
    note: Union[str, None] = Field(
        default=None,
        title="Note",
        description="A textual annotation.",
    )



if __name__ == "__main__":
    print(Config.schema_json(indent=2))
{
  "title": "Config",
  "type": "object",
  "properties": {
    "id": {
      "title": "Id",
      "type": "string"
    },
    "cancer": {
      "title": "Cancer",
      "type": "string"
    },
    "assembly": {
      "title": "Assembly",
      "enum": [
        "hg38",
        "hg19"
      ],
      "type": "string"
    },
    "sv": {
      "title": "Sv",
      "minLength": 1,
      "maxLength": 2083,
      "format": "uri",
      "type": "string"
    },
    "cnv": {
      "title": "CNV",
      "description": "An URL of the CNV text file (.txt).",
      "minLength": 1,
      "maxLength": 2083,
      "format": "uri",
      "type": "string"
    },
    "drivers": {
      "title": "Drivers",
      "description": "An URL of a file that contains drivers (.txt).",
      "minLength": 1,
      "maxLength": 2083,
      "format": "uri",
      "type": "string"
    },
    "vcf": {
      "title": "VCF",
      "description": "An URL of the point mutation file (.vcf).",
      "minLength": 1,
      "maxLength": 2083,
      "format": "uri",
      "type": "string"
    },
    "vcfIndex": {
      "title": "VCF Index",
      "description": "An URL of the point mutation index file (.tbi).",
      "minLength": 1,
      "maxLength": 2083,
      "format": "uri",
      "type": "string"
    },
    "vcf2": {
      "title": "VCF2",
      "description": "An URL of the the indel file (.vcf).",
      "minLength": 1,
      "maxLength": 2083,
      "format": "uri",
      "type": "string"
    },
    "vcf2Index": {
      "title": "VCF2 Index",
      "description": "An URL of the indel index file (.tbi).",
      "minLength": 1,
      "maxLength": 2083,
      "format": "uri",
      "type": "string"
    },
    "bam": {
      "title": "BAM",
      "description": "An URL of the BAM file (.bam).",
      "minLength": 1,
      "maxLength": 2083,
      "format": "uri",
      "type": "string"
    },
    "bamIndex": {
      "title": "BAM Index",
      "description": "An URL of the BAM index file (.bai).",
      "minLength": 1,
      "maxLength": 2083,
      "format": "uri",
      "type": "string"
    },
    "note": {
      "title": "Note",
      "description": "A textual annotation.",
      "type": "string"
    }
  },
  "required": [
    "id",
    "cancer",
    "assembly",
    "sv",
    "cnv"
  ]
}

This means you get auto-validation/parsing on the python side:

from chromosope import Config

unknown_config = [ ... ]

parsed = [Config(**item) for item in unknown_config]
parsed # now know the config is parsed correctly

and could equally generate validators for the JS side from the JSON schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants