From 27c1176b51c921053dc042b111e4abfbf7c4728b Mon Sep 17 00:00:00 2001 From: Chiara Rasi Date: Fri, 6 Oct 2023 14:21:33 +0200 Subject: [PATCH] Reformat with black --- schug/cli/fetch.py | 13 +++++++++---- schug/cli/load.py | 13 +++++++++---- schug/demo/__init__.py | 16 ++++++++++++---- schug/endpoints/exons.py | 4 ++-- schug/endpoints/genes.py | 4 +++- schug/endpoints/http_exceptions.py | 4 +++- schug/endpoints/transcripts.py | 4 +++- schug/load/biomart.py | 4 +++- schug/load/demo.py | 14 ++++++++++++-- schug/load/ensembl.py | 24 ++++++++---------------- schug/load/fetch_resource.py | 4 +++- schug/models/link_tables.py | 4 +++- schug/models/transcript.py | 10 ++++++++-- tests/conftest.py | 8 +++++++- 14 files changed, 85 insertions(+), 41 deletions(-) diff --git a/schug/cli/fetch.py b/schug/cli/fetch.py index 9440463..eace607 100644 --- a/schug/cli/fetch.py +++ b/schug/cli/fetch.py @@ -3,7 +3,6 @@ import typer from schug.load.biomart import EnsemblBiomartClient from schug.load.ensembl import ( - fetch_ensembl_exon_lines, fetch_ensembl_exons, fetch_ensembl_genes, fetch_ensembl_transcripts, @@ -22,7 +21,9 @@ @app.command() def ensembl_genes(build: str = typer.Option("37", "-b", "--build")): """Fetch genes from ensembl""" - ensembl_client: EnsemblBiomartClient = fetch_ensembl_genes(build=build, chromosomes=["Y"]) + ensembl_client: EnsemblBiomartClient = fetch_ensembl_genes( + build=build, chromosomes=["Y"] + ) for line in ensembl_client: typer.echo(line) @@ -30,7 +31,9 @@ def ensembl_genes(build: str = typer.Option("37", "-b", "--build")): @app.command() def ensembl_transcripts(build: str = typer.Option("37", "-b", "--build")): """Fetch genes from ensembl""" - ensembl_client: EnsemblBiomartClient = fetch_ensembl_transcripts(build=build, chromosomes=["Y"]) + ensembl_client: EnsemblBiomartClient = fetch_ensembl_transcripts( + build=build, chromosomes=["Y"] + ) for line in ensembl_client: typer.echo(line) @@ -38,7 +41,9 @@ def ensembl_transcripts(build: str = typer.Option("37", "-b", "--build")): @app.command() def ensembl_exons(build: str = typer.Option("37", "-b", "--build")): """Fetch genes from ensembl""" - ensembl_client: EnsemblBiomartClient = fetch_ensembl_exon_lines(build=build, chromosomes=["Y"]) + ensembl_client: EnsemblBiomartClient = fetch_ensembl_exons( + build=build, chromosomes=["Y"] + ) for line in ensembl_client: typer.echo(line) diff --git a/schug/cli/load.py b/schug/cli/load.py index dd3fac0..aecd56d 100644 --- a/schug/cli/load.py +++ b/schug/cli/load.py @@ -5,7 +5,7 @@ from pydantic import parse_obj_as from schug.database.genes import create_gene_item from schug.load.ensembl import ( - fetch_ensembl_exon_lines, + fetch_ensembl_exons, fetch_ensembl_genes, fetch_ensembl_transcripts, ) @@ -24,7 +24,7 @@ def exons( """Load exon data""" typer.echo("Loading exons") if not exons_file: - exons_file = fetch_ensembl_exon_lines(build=build, chromosomes=["Y"]) + exons_file = fetch_ensembl_exons(build=build, chromosomes=["Y"]) parsed_exons = parse_obj_as( List[EnsemblExon], [parsed_line for parsed_line in csv.DictReader(exons_file, delimiter="\t")], @@ -36,14 +36,19 @@ def exons( @app.command() -def transcripts(transcripts_file: typer.FileText = typer.Option(None, "--infile", "-i")): +def transcripts( + transcripts_file: typer.FileText = typer.Option(None, "--infile", "-i") +): """Load transcript data""" typer.echo("Loading transcripts") if not transcripts_file: transcripts_file = fetch_ensembl_transcripts(build="37", chromosomes=["Y"]) parsed_transcripts = parse_obj_as( List[EnsemblTranscript], - [parsed_line for parsed_line in csv.DictReader(transcripts_file, delimiter="\t")], + [ + parsed_line + for parsed_line in csv.DictReader(transcripts_file, delimiter="\t") + ], ) for i, tx in enumerate(parsed_transcripts): if i == 5: diff --git a/schug/demo/__init__.py b/schug/demo/__init__.py index aeac1e6..2e43179 100644 --- a/schug/demo/__init__.py +++ b/schug/demo/__init__.py @@ -3,10 +3,18 @@ import importlib_resources # Paths -EXONS_37_FILE_PATH: Path = Path(importlib_resources.files("schug"), "demo", "exons_37.tsv") -EXONS_38_FILE_PATH: Path = Path(importlib_resources.files("schug"), "demo", "exons_38.tsv") -GENES_37_FILE_PATH: Path = Path(importlib_resources.files("schug"), "demo", "genes_37.tsv") -GENES_38_FILE_PATH: Path = Path(importlib_resources.files("schug"), "demo", "genes_38.tsv") +EXONS_37_FILE_PATH: Path = Path( + importlib_resources.files("schug"), "demo", "exons_37.tsv" +) +EXONS_38_FILE_PATH: Path = Path( + importlib_resources.files("schug"), "demo", "exons_38.tsv" +) +GENES_37_FILE_PATH: Path = Path( + importlib_resources.files("schug"), "demo", "genes_37.tsv" +) +GENES_38_FILE_PATH: Path = Path( + importlib_resources.files("schug"), "demo", "genes_38.tsv" +) TRANSCRIPTS_37_FILE_PATH: Path = Path( importlib_resources.files("schug"), "demo", "transcripts_37.tsv" ) diff --git a/schug/endpoints/exons.py b/schug/endpoints/exons.py index 4933c07..6452331 100644 --- a/schug/endpoints/exons.py +++ b/schug/endpoints/exons.py @@ -3,7 +3,7 @@ from fastapi import APIRouter, Depends, HTTPException, Query, status from fastapi.responses import StreamingResponse from schug.database.session import get_session -from schug.load.ensembl import fetch_ensembl_exon_lines +from schug.load.ensembl import fetch_ensembl_exons from schug.load.fetch_resource import stream_resource from schug.models import Exon, ExonRead from schug.models.common import Build @@ -27,7 +27,7 @@ def read_exons( async def ensembl_exons(build: Build): """A proxy to the Ensembl Biomart that retrieves exons in a specific genome build.""" - ensembl_client: EnsemblBiomartClient = fetch_ensembl_exon_lines(build=build) + ensembl_client: EnsemblBiomartClient = fetch_ensembl_exons(build=build) url: str = ensembl_client.build_url(xml=ensembl_client.xml) return StreamingResponse(stream_resource(url), media_type="text/tsv") diff --git a/schug/endpoints/genes.py b/schug/endpoints/genes.py index 150b7ef..7435286 100644 --- a/schug/endpoints/genes.py +++ b/schug/endpoints/genes.py @@ -95,7 +95,9 @@ def read_gene_hgnc_symbol( session: Session = Depends(get_session), ): try: - gene = session.exec(select(Gene).where(Gene.primary_symbol == hgnc_symbol)).one() + gene = session.exec( + select(Gene).where(Gene.primary_symbol == hgnc_symbol) + ).one() except NoResultFound: SchugHttpException.error_404(result=None, query=hgnc_symbol) return gene diff --git a/schug/endpoints/http_exceptions.py b/schug/endpoints/http_exceptions.py index 1fcf817..5556cf2 100644 --- a/schug/endpoints/http_exceptions.py +++ b/schug/endpoints/http_exceptions.py @@ -8,5 +8,7 @@ class SchugHttpException(Exception): def error_404(result: Any, query: Any) -> Optional[HTTPException]: """Handle exception http 404 error not found""" if not result: - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"{query} not found") + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, detail=f"{query} not found" + ) return None diff --git a/schug/endpoints/transcripts.py b/schug/endpoints/transcripts.py index 353ed3f..1dadeba 100644 --- a/schug/endpoints/transcripts.py +++ b/schug/endpoints/transcripts.py @@ -33,7 +33,9 @@ def read_transcript_db_id( ): transcript = session.get(Transcript, db_id) if not transcript: - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Transcript not found") + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, detail="Transcript not found" + ) return transcript diff --git a/schug/load/biomart.py b/schug/load/biomart.py index 87ba13f..15a9ab9 100644 --- a/schug/load/biomart.py +++ b/schug/load/biomart.py @@ -69,7 +69,9 @@ def xml_filters(filters: dict) -> List[str]: value = filters[filter_name] if not isinstance(value, str): value = ",".join(value) - formatted_lines.append(f'') + formatted_lines.append( + f'' + ) return formatted_lines diff --git a/schug/load/demo.py b/schug/load/demo.py index a633ae2..49c2e53 100644 --- a/schug/load/demo.py +++ b/schug/load/demo.py @@ -8,8 +8,18 @@ def load_demo(): """Load some dummy data into a test instance of schug""" with get_session() as session: exons = [ - Exon(chromosome="1", start=210111576, end=210111622, exon_name="ENSE00001443254"), - Exon(chromosome="1", start=210126054, end=210126101, exon_name="ENSE00003523023"), + Exon( + chromosome="1", + start=210111576, + end=210111622, + exon_name="ENSE00001443254", + ), + Exon( + chromosome="1", + start=210126054, + end=210126101, + exon_name="ENSE00003523023", + ), ] transcripts = [ diff --git a/schug/load/ensembl.py b/schug/load/ensembl.py index b385e1c..316bcbc 100644 --- a/schug/load/ensembl.py +++ b/schug/load/ensembl.py @@ -18,7 +18,9 @@ CHROMOSOMES_38 = AUTOSOMES + ["X", "Y", "M"] -def fetch_ensembl_biomart(attributes: List[str], filters: dict, build=None) -> EnsemblBiomartClient: +def fetch_ensembl_biomart( + attributes: List[str], filters: dict, build=None +) -> EnsemblBiomartClient: """Fetch data from ensembl biomart Args: attributes(list): List of selected attributes @@ -36,7 +38,9 @@ def fetch_ensembl_biomart(attributes: List[str], filters: dict, build=None) -> E return client -def fetch_ensembl_genes(build: str, chromosomes: List[str] = None) -> EnsemblBiomartClient: +def fetch_ensembl_genes( + build: str, chromosomes: List[str] = None +) -> EnsemblBiomartClient: """Fetch genes from ensembl""" chromosomes: List[str] = chromosomes or CHROMOSOMES LOG.info("Fetching ensembl genes") @@ -80,10 +84,10 @@ def fetch_ensembl_transcripts( return fetch_ensembl_biomart(attributes=attributes, filters=filters, build=build) -def fetch_ensembl_exon_lines( +def fetch_ensembl_exons( build: str, chromosomes: Optional[List[str]] = None ) -> EnsemblBiomartClient: - """Fetch the ensembl exons""" + """Fetch the ensembl exons.""" chromosomes = chromosomes or CHROMOSOMES LOG.info("Fetching ensembl exons") @@ -105,15 +109,3 @@ def fetch_ensembl_exon_lines( filters = {"chromosome_name": chromosomes} return fetch_ensembl_biomart(attributes=attributes, filters=filters, build=build) - - -def fetch_ensembl_exons(build: str, chromosomes: Optional[List[str]] = None) -> List[EnsemblExon]: - """Fetch ensembl exon objects""" - exon_lines: EnsemblBiomartClient = fetch_ensembl_exon_lines( - build=build, chromosomes=chromosomes - ) - - parsed_exons = parse_obj_as( - List[EnsemblExon], [exon_info for exon_info in csv.DictReader(exon_lines, delimiter="\t")] - ) - return parsed_exons diff --git a/schug/load/fetch_resource.py b/schug/load/fetch_resource.py index 0d457b2..958d457 100644 --- a/schug/load/fetch_resource.py +++ b/schug/load/fetch_resource.py @@ -53,7 +53,9 @@ def fetch_resource(url: str) -> List[str]: content: str = response.text if response.url.endswith(".gz"): LOG.info("gzipped!") - encoded_content = b"".join(chunk for chunk in response.iter_content(chunk_size=128)) + encoded_content = b"".join( + chunk for chunk in response.iter_content(chunk_size=128) + ) content = zlib.decompress(encoded_content, 16 + zlib.MAX_WBITS).decode("utf-8") data = content.split("\n") diff --git a/schug/models/link_tables.py b/schug/models/link_tables.py index cb41d05..6d35052 100644 --- a/schug/models/link_tables.py +++ b/schug/models/link_tables.py @@ -4,7 +4,9 @@ class ExonTranscriptLink(SQLModel, table=True): - exon_id: Optional[int] = Field(default=None, foreign_key="exon.id", primary_key=True) + exon_id: Optional[int] = Field( + default=None, foreign_key="exon.id", primary_key=True + ) transcript_id: Optional[int] = Field( default=None, foreign_key="transcript.id", primary_key=True ) diff --git a/schug/models/transcript.py b/schug/models/transcript.py index 48d88b8..2759d23 100644 --- a/schug/models/transcript.py +++ b/schug/models/transcript.py @@ -29,7 +29,9 @@ class Transcript(TranscriptBase, table=True): id: Optional[int] = Field(default=None, primary_key=True) gene: Optional["Gene"] = Relationship(back_populates="transcripts") - exons: List["Exon"] = Relationship(back_populates="transcripts", link_model=ExonTranscriptLink) + exons: List["Exon"] = Relationship( + back_populates="transcripts", link_model=ExonTranscriptLink + ) class TranscriptRead(TranscriptBase): @@ -55,7 +57,11 @@ class EnsemblTranscript(BaseModel): @validator("refseq_id", always=True) def set_refseq_id(cls, _, values: dict) -> Optional[str]: - order: List[str] = ["refseq_mrna", "refseq_mrna_predicted", "refseq_ncrna_predicted"] + order: List[str] = [ + "refseq_mrna", + "refseq_mrna_predicted", + "refseq_ncrna_predicted", + ] for keyword in order: if values[keyword]: return values[keyword] diff --git a/tests/conftest.py b/tests/conftest.py index 8f441e5..423be94 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -62,7 +62,13 @@ def endpoints() -> Endpoints: @pytest.fixture(name="schug_gene") def fixture_gene_id() -> Gene: """Return a Gene object.""" - return Gene(start=1, end=2, chromosome="1", genome_build=Build.build_38, ensembl_id="ENSG123") + return Gene( + start=1, + end=2, + chromosome="1", + genome_build=Build.build_38, + ensembl_id="ENSG123", + ) @pytest.fixture(name="file_handler")