|
15 | 15 |
|
16 | 16 | @extend_schema( |
17 | 17 | tags=["Genes"], |
18 | | - summary="List all genes", |
19 | | - description="Return all genes with species and PanKB URLs.", |
| 18 | + summary="List all genes (paginated)", |
| 19 | + description=( |
| 20 | + "Return distinct (gene, species) pairs with PanKB URLs. " |
| 21 | + "Supports cursor-based pagination via skip/limit query parameters." |
| 22 | + ), |
| 23 | + parameters=[ |
| 24 | + OpenApiParameter(name="skip", type=int, location="query", description="Number of records to skip (default 0)"), |
| 25 | + OpenApiParameter(name="limit", type=int, location="query", description="Max records to return (default 10000, max 50000)"), |
| 26 | + ], |
20 | 27 | responses={ |
21 | 28 | 200: { |
22 | | - "type": "array", |
23 | | - "items": { |
24 | | - "type": "object", |
25 | | - "properties": { |
26 | | - "gene": {"type": "string"}, |
27 | | - "species": {"type": "string"}, |
28 | | - "url": {"type": "string", "format": "uri"}, |
| 29 | + "type": "object", |
| 30 | + "properties": { |
| 31 | + "genes": { |
| 32 | + "type": "array", |
| 33 | + "items": { |
| 34 | + "type": "object", |
| 35 | + "properties": { |
| 36 | + "gene": {"type": "string"}, |
| 37 | + "species": {"type": "string"}, |
| 38 | + "url": {"type": "string", "format": "uri"}, |
| 39 | + }, |
| 40 | + }, |
29 | 41 | }, |
| 42 | + "total": {"type": "integer"}, |
| 43 | + "skip": {"type": "integer"}, |
| 44 | + "limit": {"type": "integer"}, |
| 45 | + "has_more": {"type": "boolean"}, |
30 | 46 | }, |
31 | 47 | } |
32 | 48 | }, |
33 | 49 | ) |
34 | 50 | @api_view(["GET"]) |
35 | 51 | def genes(request): |
36 | | - """Return all genes with species and PanKB URLs.""" |
| 52 | + """Return all genes with species and PanKB URLs (paginated).""" |
37 | 53 | try: |
38 | | - gene_list = GeneAnnotations.get_all_genes() |
39 | | - result = [] |
| 54 | + skip = int(request.GET.get("skip", 0)) |
| 55 | + limit = min(int(request.GET.get("limit", 10000)), 50000) |
| 56 | + |
| 57 | + result = GeneAnnotations.get_all_genes_paginated(skip=skip, limit=limit) |
| 58 | + gene_list = result["genes"] |
| 59 | + |
40 | 60 | for gene_data in gene_list: |
41 | 61 | gene = gene_data.get("gene") |
42 | 62 | species = gene_data.get("pangenome_analysis") |
43 | 63 | if gene and species: |
44 | | - result.append({ |
45 | | - "gene": gene, |
46 | | - "species": species, |
47 | | - "url": f"{settings.PANKB_BASE_URL}/gene_function/gene_info/?species={quote(species)}&gene={quote(gene)}", |
48 | | - }) |
49 | | - return Response(result) |
| 64 | + gene_data["species"] = species |
| 65 | + gene_data["url"] = f"{settings.PANKB_BASE_URL}/gene_function/gene_info/?species={quote(species)}&gene={quote(gene)}" |
| 66 | + gene_data.pop("pangenome_analysis", None) |
| 67 | + |
| 68 | + return Response({ |
| 69 | + "genes": gene_list, |
| 70 | + "total": result["total"], |
| 71 | + "skip": skip, |
| 72 | + "limit": limit, |
| 73 | + "has_more": skip + len(gene_list) < result["total"], |
| 74 | + }) |
50 | 75 | except Exception as e: |
51 | 76 | logger.exception("list_all_genes failed") |
52 | 77 | return Response({"message": f"Error: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) |
53 | 78 |
|
54 | 79 |
|
55 | 80 | @extend_schema( |
56 | 81 | tags=["Strains"], |
57 | | - summary="List all strains", |
58 | | - description="Return all strains (genome IDs) with PanKB URLs.", |
| 82 | + summary="List all strains (paginated)", |
| 83 | + description=( |
| 84 | + "Return all strains (genome IDs) with PanKB URLs. " |
| 85 | + "Supports cursor-based pagination via skip/limit query parameters." |
| 86 | + ), |
| 87 | + parameters=[ |
| 88 | + OpenApiParameter(name="skip", type=int, location="query", description="Number of records to skip (default 0)"), |
| 89 | + OpenApiParameter(name="limit", type=int, location="query", description="Max records to return (default 10000, max 50000)"), |
| 90 | + ], |
59 | 91 | responses={ |
60 | 92 | 200: { |
61 | | - "type": "array", |
62 | | - "items": { |
63 | | - "type": "object", |
64 | | - "properties": { |
65 | | - "strain": {"type": "string"}, |
66 | | - "url": {"type": "string", "format": "uri"}, |
| 93 | + "type": "object", |
| 94 | + "properties": { |
| 95 | + "strains": { |
| 96 | + "type": "array", |
| 97 | + "items": { |
| 98 | + "type": "object", |
| 99 | + "properties": { |
| 100 | + "strain": {"type": "string"}, |
| 101 | + "url": {"type": "string", "format": "uri"}, |
| 102 | + }, |
| 103 | + }, |
67 | 104 | }, |
| 105 | + "total": {"type": "integer"}, |
| 106 | + "skip": {"type": "integer"}, |
| 107 | + "limit": {"type": "integer"}, |
| 108 | + "has_more": {"type": "boolean"}, |
68 | 109 | }, |
69 | 110 | } |
70 | 111 | }, |
71 | 112 | ) |
72 | 113 | @api_view(["GET"]) |
73 | 114 | def strains(request): |
74 | | - """Return all strains (genome IDs) with PanKB URLs.""" |
| 115 | + """Return all strains (genome IDs) with PanKB URLs (paginated).""" |
75 | 116 | try: |
76 | | - strain_ids = GenomeInfo.get_all_strains() |
77 | | - result = [] |
| 117 | + skip = int(request.GET.get("skip", 0)) |
| 118 | + limit = min(int(request.GET.get("limit", 10000)), 50000) |
| 119 | + |
| 120 | + result = GenomeInfo.get_all_strains_paginated(skip=skip, limit=limit) |
| 121 | + strain_ids = result["strains"] |
| 122 | + |
| 123 | + strain_list = [] |
78 | 124 | for strain_id in strain_ids: |
79 | | - result.append({ |
| 125 | + strain_list.append({ |
80 | 126 | "strain": strain_id, |
81 | 127 | "url": f"{settings.PANKB_BASE_URL}/gene_function/genome_info/?genome_id={quote(strain_id)}", |
82 | 128 | }) |
83 | | - return Response(result) |
| 129 | + |
| 130 | + return Response({ |
| 131 | + "strains": strain_list, |
| 132 | + "total": result["total"], |
| 133 | + "skip": skip, |
| 134 | + "limit": limit, |
| 135 | + "has_more": skip + len(strain_list) < result["total"], |
| 136 | + }) |
84 | 137 | except Exception as e: |
85 | 138 | logger.exception("list_all_strains failed") |
86 | 139 | return Response({"message": f"Error: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) |
@@ -266,7 +319,7 @@ def query_by_pair(request): |
266 | 319 | examples=[ |
267 | 320 | OpenApiExample( |
268 | 321 | "Example request", |
269 | | - value={"ids": ["COQ3_1", "COQ3_2", "COQ3_3"]}, |
| 322 | + value={"ids": ["AAH1"]}, |
270 | 323 | request_only=True, |
271 | 324 | ) |
272 | 325 | ], |
|
0 commit comments