Skip to content

Commit

Permalink
Added test for assay column matching
Browse files Browse the repository at this point in the history
Added 'FAILED' to LZ status that is okay for usage
Linting
  • Loading branch information
Nicolai-vKuegelgen committed Nov 20, 2023
1 parent 5d4bc85 commit a0cf1d7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
8 changes: 5 additions & 3 deletions cubi_tk/snappy/itransfer_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,13 @@ def get_landing_zone_uuid_by_path(self, lz_irods_path, project_uuid, assay_uuid=
existing_lzs = list(filter(lambda x: x.assay == assay_uuid, existing_lzs))

matching_lzs = list(filter(lambda x: x.irods_path == lz_irods_path, existing_lzs))
if matching_lzs and matching_lzs[0].status == 'ACTIVE':
if matching_lzs and matching_lzs[0].status in ("ACTIVE", "FAILED"):
lz_uuid = matching_lzs[0].sodar_uuid
else:
msg = "Could not find an active LZ with the given path. Please review input: {0}".format(
lz_irods_path
msg = (
"Could not find an active LZ with the given path. Please review input: {0}".format(
lz_irods_path
)
)
logger.error(msg)
raise ParameterException(msg)
Expand Down
23 changes: 23 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,26 @@ def my_exists(self):
def my_get_sodar_info(_self):
"""Method is used to patch cubi_tk.snappy.itransfer_common.SnappyItransferCommandBase.get_sodar_info"""
return "466ab946-ce6a-4c78-9981-19b79e7bbe86", "/irods/dest"


def my_sodar_api_export(n_assays=1):
"""Return contents for api.samplesheet.export"""
assay = textwrap.dedent(
"""
Sample Name\tProtocol REF\tParameter Value[Concentration measurement]\tPerformer\tDate\tExtract Name\tCharacteristics[Concentration]\tUnit\tTerm Source REF\tTerm Accession Number\tProtocol REF\tParameter Value[Provider name]\tParameter Value[Provider contact]\tParameter Value[Provider project ID]\tParameter Value[Provider sample ID]\tParameter Value[Provider QC status]\tParameter Value[Requestor contact]\tParameter Value[Requestor project]\tParameter Value[Requestor sample ID]\tParameter Value[Concentration measurement]\tParameter Value[Library source]\tParameter Value[Library strategy]\tParameter Value[Library selection]\tParameter Value[Library layout]\tParameter Value[Library kit]\tComment[Library kit catalogue ID]\tParameter Value[Target insert size]\tParameter Value[Wet-lab insert size]\tParameter Value[Barcode kit]\tParameter Value[Barcode kit catalogue ID]\tParameter Value[Barcode name]\tParameter Value[Barcode sequence]\tPerformer\tDate\tLibrary Name\tCharacteristics[Folder name]\tCharacteristics[Concentration]\tUnit\tTerm Source REF\tTerm Accession Number\tProtocol REF\tParameter Value[Platform]\tParameter Value[Instrument model]\tParameter Value[Base quality encoding]\tParameter Value[Center name]\tParameter Value[Center contact]\tPerformer\tDate\tRaw Data File
Sample1-N1\tNucleic acid extraction WES\t\t\t\tSample1-N1-DNA1\t\t\t\t\tLibrary construction WES\t\t\t\t\t\t\t\t\t\tGENOMIC\tWXS\tHybrid Selection\tPAIRED\tAgilent SureSelect Human All Exon V7\t\t\t\t\t\t\t\t\t\tSample1-N1-DNA1-WES1\tFolder1\t\t\t\t\tNucleic acid sequencing WES\tILLUMINA\tIllumina NovaSeq 6000\tPhred+33
Sample2-N1\tNucleic acid extraction WES\t\t\t\tSample2-N1-DNA1\t\t\t\t\tLibrary construction WES\t\t\t\t\t\t\t\t\t\tGENOMIC\tWXS\tHybrid Selection\tPAIRED\tAgilent SureSelect Human All Exon V7\t\t\t\t\t\t\t\t\t\tSample2-N1-DNA1-WES1\tFolder2\t\t\t\t\tNucleic acid sequencing WES\tILLUMINA\tIllumina NovaSeq 6000\tPhred+33
Sample3-N1\tNucleic acid extraction WES\t\t\t\tSample3-N1-DNA1\t\t\t\t\tLibrary construction WES\t\t\t\t\t\t\t\t\t\tGENOMIC\tWXS\tHybrid Selection\tPAIRED\tAgilent SureSelect Human All Exon V7\t\t\t\t\t\t\t\t\t\tSample3-N1-DNA1-WES1\tFolder3\t\t\t\t\tNucleic acid sequencing WES\tILLUMINA\tIllumina NovaSeq 6000\tPhred+33
"""
).lstrip()

isa_dict = {
"investigation": {"path": "i_Investigation.txt", "tsv": None},
"studies": {"s_Study_0.txt": {"tsv": None}},
"assays": {"a_name_0": {"tsv": assay}},
}
if n_assays > 1:
for i in range(1, n_assays):
isa_dict["assays"]["a_name_%d" % i] = {"tsv": assay}

return isa_dict
69 changes: 68 additions & 1 deletion tests/test_sodar_ingest_fastq.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
import json
import os
import re
import unittest
from unittest import mock
from unittest.mock import patch

from pyfakefs import fake_filesystem, fake_pathlib
import pytest

from cubi_tk.__main__ import main, setup_argparse
from cubi_tk.exceptions import ParameterException
from cubi_tk.sodar.ingest_fastq import SodarIngestFastq

from .conftest import my_get_sodar_info
from .conftest import my_get_sodar_info, my_sodar_api_export
from .factories import InvestigationFactory


def test_run_sodar_ingest_fastq_help(capsys):
Expand Down Expand Up @@ -57,6 +62,68 @@ def test_run_sodar_ingest_fastq_src_regex():
assert res.groupdict()["sample"] == expected_sample


@patch("cubi_tk.sodar.ingest_fastq.api.samplesheet.retrieve")
@patch("cubi_tk.sodar.ingest_fastq.api.samplesheet.export")
def test_run_sodar_ingest_fastq_get_match_to_collection_mapping(mock_api_export, mock_api_retrieve):
# Patched sodar API call
mock_api_export.return_value = my_sodar_api_export()

# Instantiate SodarIngestFastq (seems to require args?)
landing_zone_uuid = "466ab946-ce6a-4c78-9981-19b79e7bbe86"
project_uuid = "466ab946-ce6a-4c78-9981-19b79e7bbe86"
fake_base_path = "/base/path"
argv = [
"--verbose",
"sodar",
"ingest-fastq",
"--num-parallel-transfers",
"0",
"--sodar-api-token",
"XXXX",
"--yes",
fake_base_path,
landing_zone_uuid,
]

parser, _subparsers = setup_argparse()
args = parser.parse_args(argv)
ingestfastq = SodarIngestFastq(args)

# test to get expected dict
expected = {
"Folder1": "Sample1-N1-DNA1-WES1",
"Folder2": "Sample2-N1-DNA1-WES1",
"Folder3": "Sample3-N1-DNA1-WES1",
}

assert expected == ingestfastq.get_match_to_collection_mapping(project_uuid, "Folder name")
assert expected == ingestfastq.get_match_to_collection_mapping(
project_uuid, "Folder name", "Library Name"
)

# Test for alternative collection column
expected2 = {
"Folder1": "Sample1-N1-DNA1",
"Folder2": "Sample2-N1-DNA1",
"Folder3": "Sample3-N1-DNA1",
}
assert expected2 == ingestfastq.get_match_to_collection_mapping(
project_uuid, "Folder name", "Extract Name"
)

# Test for missing column
with unittest.TestCase.assertRaises(unittest.TestCase, ParameterException):
ingestfastq.get_match_to_collection_mapping(project_uuid, "Typo-Column")

# Test with additional assay
mock_api_export.return_value = my_sodar_api_export(2)
mock_api_retrieve.return_value = InvestigationFactory()
assay_uuid = list(mock_api_retrieve.return_value.studies["s_Study_0"].assays.keys())[0]
ingestfastq.args.assay = assay_uuid

assert expected == ingestfastq.get_match_to_collection_mapping(project_uuid, "Folder name")


def test_run_sodar_ingest_fastq_smoke_test(mocker, requests_mock):
# --- setup arguments
irods_path = "/irods/dest"
Expand Down

0 comments on commit a0cf1d7

Please sign in to comment.