Samad14 commited on
Commit
a0ea38d
·
verified ·
1 Parent(s): 9e5e349

Upload folder using huggingface_hub (part 2)

Browse files
tests/test_geo_retrieval_fallback.py CHANGED
@@ -298,3 +298,102 @@ def test_gse336901_count_download_falls_back_to_canonical_series_supplement_path
298
  assert source == ftp_url
299
  assert matrix["genes"] == 2
300
  assert matrix["columns"] == ["GSM9844290", "GSM9844291", "GSM9844295", "GSM9844296"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
298
  assert source == ftp_url
299
  assert matrix["genes"] == 2
300
  assert matrix["columns"] == ["GSM9844290", "GSM9844291", "GSM9844295", "GSM9844296"]
301
+
302
+
303
+
304
+ def test_ena_run_fallback_maps_fastq_mates_to_every_geo_sample():
305
+ from app.rnaseq.geo_counts import fetch_ena_read_runs
306
+
307
+ rows = [
308
+ "study_accession\trun_accession\texperiment_accession\texperiment_alias\texperiment_title\tsample_accession\tsample_alias\tsample_title\tfastq_ftp\tfastq_md5\tfastq_bytes\tlibrary_layout\tlibrary_strategy\tscientific_name",
309
+ "PRJNA1483549\tSRR100001\tSRX100001\tGSM900001_r1\tRNA-seq GSM900001\tSAMN100001\tGSM900001\tSensitive sample 1\tftp.sra.ebi.ac.uk/vol1/fastq/SRR100/001/SRR100001/SRR100001_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR100/001/SRR100001/SRR100001_2.fastq.gz\t" + "a" * 32 + ";" + "b" * 32 + "\t100;200\tPAIRED\tRNA-Seq\tHomo sapiens",
310
+ "PRJNA1483549\tSRR100002\tSRX100002\tGSM900002_r1\tRNA-seq GSM900002\tSAMN100002\tGSM900002\tResistant sample 2\tftp.sra.ebi.ac.uk/vol1/fastq/SRR100/002/SRR100002/SRR100002_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR100/002/SRR100002/SRR100002_2.fastq.gz\t" + "c" * 32 + ";" + "d" * 32 + "\t300;400\tPAIRED\tRNA-Seq\tHomo sapiens",
311
+ ]
312
+ payload = ("\n".join(rows) + "\n").encode()
313
+
314
+ def respond(request):
315
+ assert request.url.host == "www.ebi.ac.uk"
316
+ assert request.url.path == "/ena/portal/api/filereport"
317
+ assert request.url.params.get("accession") == "PRJNA1483549"
318
+ assert request.url.params.get("result") == "read_run"
319
+ return httpx.Response(200, content=payload)
320
+
321
+ async def run():
322
+ async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client:
323
+ return await fetch_ena_read_runs(client, "PRJNA1483549", ["GSM900001", "GSM900002"])
324
+
325
+ result = asyncio.run(run())
326
+ assert result["status"] == "available"
327
+ assert result["sample_count"] == 2
328
+ assert result["run_count"] == 2
329
+ assert result["fastq_file_count"] == 4
330
+ assert result["paired_end"] is True
331
+ assert result["total_fastq_bytes"] == 1000
332
+ assert result["runs"][0]["sample_accession"] == "GSM900001"
333
+ assert result["runs"][0]["fastq_files"][0]["md5"] == "a" * 32
334
+
335
+
336
+ def test_ena_run_fallback_rejects_untrusted_fastq_urls():
337
+ from app.rnaseq.geo_counts import fetch_ena_read_runs
338
+
339
+ payload = (
340
+ "study_accession\trun_accession\texperiment_accession\texperiment_alias\texperiment_title\tsample_accession\tsample_alias\tsample_title\tfastq_ftp\tfastq_md5\tfastq_bytes\tlibrary_layout\tlibrary_strategy\tscientific_name\n"
341
+ "PRJNA1483549\tSRR100001\tSRX100001\tGSM900001_r1\tRNA-seq\tSAMN100001\tGSM900001\tSensitive\tftp.sra.ebi.ac.uk/vol1/fastq/SRR100/001/SRR100001/SRR100001_1.fastq.gz;https://evil.example/SRR100001_2.fastq.gz\t" + "a" * 32 + ";" + "b" * 32 + "\t100;200\tPAIRED\tRNA-Seq\tHomo sapiens\n"
342
+ ).encode()
343
+
344
+ def respond(request):
345
+ return httpx.Response(200, content=payload)
346
+
347
+ async def run():
348
+ async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client:
349
+ return await fetch_ena_read_runs(client, "PRJNA1483549", ["GSM900001"])
350
+
351
+ with pytest.raises(GeoCountsError, match="untrusted or unexpected FASTQ URL"):
352
+ asyncio.run(run())
353
+
354
+
355
+ def test_automatic_geo_prepare_returns_ena_fallback_instead_of_404_error(monkeypatch):
356
+ from app.routers import geo_search
357
+ from app.rnaseq.geo_counts import GeoCountsError
358
+
359
+ series = {
360
+ "accession": "GSE336901",
361
+ "title": "RNA-seq FFPE response study",
362
+ "read_projects": ["PRJNA1483549"],
363
+ "samples": [{"accession": "GSM900001"}, {"accession": "GSM900002"}],
364
+ "files": [{
365
+ "name": "GSE336901_Fig3_FFPE_raw_counts_matrix_GEO.csv.gz",
366
+ "url": "https://www.ncbi.nlm.nih.gov/geo/download/?acc=GSE336901&format=file&file=GSE336901_Fig3_FFPE_raw_counts_matrix_GEO.csv.gz",
367
+ "analysis_eligible": True,
368
+ "scope": "series",
369
+ }],
370
+ }
371
+ fallback = {
372
+ "status": "available", "provider": "European Nucleotide Archive",
373
+ "study_accession": "PRJNA1483549", "run_count": 2, "sample_count": 2,
374
+ "fastq_file_count": 4, "paired_end": True, "total_fastq_bytes": 1000,
375
+ "total_fastq_gib": 0.0, "runs": [],
376
+ }
377
+
378
+ async def fake_series(client, accession):
379
+ return series
380
+
381
+ async def unavailable_matrix(client, source_series, filename):
382
+ raise GeoCountsError("NCBI returned 404 for GEO supplement")
383
+
384
+ async def fake_ena(client, study_accession, expected_samples):
385
+ assert expected_samples == ["GSM900001", "GSM900002"]
386
+ return fallback
387
+
388
+ monkeypatch.setattr(geo_search, "fetch_series", fake_series)
389
+ monkeypatch.setattr(geo_search, "fetch_matrix", unavailable_matrix)
390
+ monkeypatch.setattr(geo_search, "fetch_ena_read_runs", fake_ena)
391
+
392
+ async def run():
393
+ return await geo_search._prepare_automatic(geo_search.AutomaticSelection(accession="GSE336901"))
394
+
395
+ prepared, matrix = asyncio.run(run())
396
+ assert prepared["status"] == "raw_reads_available"
397
+ assert prepared["analysis_status"] == "NOT_EVALUATED"
398
+ assert prepared["raw_read_fallback"]["run_count"] == 2
399
+ assert matrix is None