Skip to content

Commit e055f46

Browse files
authored
Merge pull request #20 from biosustain/export_apis
Add apis to fetch a list of genes/strains
2 parents ba3736c + 4582ec1 commit e055f46

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

django_project/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@
218218
path("interop-query/query-by-strain", interop_views.query_by_strain, name="query_by_strain"),
219219
path("interop-query/query-by-gene", interop_views.query_by_gene, name="query_by_gene"),
220220
path("interop-query/query-by-pair", interop_views.query_by_pair, name="query_by_pair"),
221+
path("interop-query/genes", interop_views.genes, name="genes"),
222+
path("interop-query/strains", interop_views.strains, name="strains"),
221223

222224
# path('admin/', admin.site.urls) # make the amdin panel inaccessible via its utl (the admin admin is preserved for the potential future needs)
223225
]

gene_function/models.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ def get_gene_info_and_pangenomic_class(genome_match, projection=None):
127127
class GenomeInfo:
128128
objects = database.MongoDBObjects("pankb_genome_info")
129129

130+
@staticmethod
131+
def get_all_strains():
132+
"""
133+
Return a sorted list of distinct genome_id values.
134+
"""
135+
pipeline = [
136+
{"$group": {"_id": "$genome_id"}},
137+
{"$sort": {"_id": 1}},
138+
{"$project": {"_id": 0, "genome_id": "$_id"}},
139+
]
140+
141+
cursor = GenomeInfo.objects.aggregate(pipeline)
142+
return [doc["genome_id"] for doc in cursor if doc.get("genome_id")]
143+
144+
130145
def get_genome_and_isolation_info_pipeline(genome_match):
131146
return [
132147
{"$match": genome_match},

interop_query/views.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,29 @@
1010

1111
logger = logging.getLogger(__name__)
1212

13+
@csrf_exempt
14+
@require_http_methods(["GET"])
15+
def genes(request):
16+
"""Return a flat list of all gene names."""
17+
try:
18+
genes = GeneAnnotations.get_all_genes()
19+
return JsonResponse(genes, safe=False)
20+
except Exception as e:
21+
logger.exception("list_all_genes failed")
22+
return JsonResponse({"message": f"Error: {e}"}, status=500)
23+
24+
25+
@csrf_exempt
26+
@require_http_methods(["GET"])
27+
def strains(request):
28+
"""Return all strains (genomes) with isolation info merged."""
29+
try:
30+
data = GenomeInfo.get_all_strains()
31+
return JsonResponse(data, safe=False)
32+
except Exception as e:
33+
logger.exception("list_all_strains failed")
34+
return JsonResponse({"message": f"Error: {e}"}, status=500)
35+
1336

1437
@csrf_exempt
1538
@require_http_methods(["POST"])

pangenome_analyses/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
class GeneAnnotations:
55
objects = database.MongoDBObjects('pankb_gene_annotations')
66

7+
@staticmethod
8+
def get_all_genes(projection=None):
9+
"""
10+
Return a sorted list of distinct gene names.
11+
"""
12+
cursor = GeneAnnotations.objects.find(
13+
{}, projection=projection or ["gene"]
14+
)
15+
16+
names = {doc.get("gene") for doc in cursor if "gene" in doc}
17+
return sorted(n for n in names if n)
18+
719
def get_gene_analysis_pairs(genes):
820
"""
921
Return all distinct (gene, pangenome_analysis) tuples

0 commit comments

Comments
 (0)