Skip to content

Commit b4f51d8

Browse files
committed
Fix and add test for no renaming
1 parent bee3413 commit b4f51d8

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

locidex/merge.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,9 @@ def run_merge(config):
297297
run_data['result_file'] = os.path.join(outdir,"profile.tsv")
298298

299299
#Write report of all the MLST files with profile mismatch and how MLST profiles with mismatch were modified
300-
df = pd.DataFrame(modified_MLST_file_list)
301-
df.to_csv(f'{outdir}/MLST_error_report.csv', index=False, header=False)
300+
if modified_MLST_file_list:
301+
df = pd.DataFrame(modified_MLST_file_list)
302+
df.to_csv(f'{outdir}/MLST_error_report.csv', index=False, header=False)
302303

303304

304305
run_data['analysis_end_time'] = datetime.now().strftime("%d/%m/%Y %H:%M:%S")

tests/test_merge.py

+36-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ def test_extract_profiles():
4848
def test_profile_validation():
4949
mlst_file = json.load(open(MERGE_MISMATCH_PROFILES))
5050
## Test that if the sample name matches the profile in the MLST json file nothing is changed
51-
new_mlst, mlst_report = merge.validate_profiles(mlst_file, "sampleA", "sample1.mlst.json")
51+
new_mlst, mlst_report = merge.validate_and_fix_profiles(mlst_file, "sampleA", "sample1.mlst.json")
5252
assert new_mlst["data"]["profile"] == {'sampleA': {'l1': '1', 'l2': '1', 'l3': '1'}}
5353
assert mlst_report == None
5454
## Test that a different sample than the profile in the MLST json will be changed
55-
same_mlst, mlst_report = merge.validate_profiles(mlst_file, "sample1", "sample1.mlst.json")
55+
same_mlst, mlst_report = merge.validate_and_fix_profiles(mlst_file, "sample1", "sample1.mlst.json")
5656
assert same_mlst["data"]["profile"] == {'sample1': {'l1': '1', 'l2': '1', 'l3': '1'}}
5757
assert mlst_report[2] == "sample1 ID and JSON key in sample1.mlst.json DO NOT MATCH. The 'sampleA' key in sample1.mlst.json has been forcefully changed to 'sample1': User should manually check input files to ensure correctness."
5858

@@ -77,4 +77,37 @@ def test_profile_validation_report(tmpdir):
7777
reader_obj = csv.reader(file)
7878
next(reader_obj) # Skip the first line (header)
7979
second_line = next(reader_obj)
80-
assert second_line == ['sample1',"['sampleA']", "sample1 ID and JSON key in sample1.mlst.json DO NOT MATCH. The 'sampleA' key in sample1.mlst.json has been forcefully changed to 'sample1': User should manually check input files to ensure correctness."]
80+
assert second_line == ['sample1',"['sampleA']", "sample1 ID and JSON key in sample1.mlst.json DO NOT MATCH. The 'sampleA' key in sample1.mlst.json has been forcefully changed to 'sample1': User should manually check input files to ensure correctness."]
81+
with open(f"{tmpdir}/profile.tsv", "r", newline='') as file:
82+
reader_obj = csv.reader(file, delimiter="\t",)
83+
next(reader_obj) # Skip the first line (header)
84+
sampleQ = next(reader_obj)
85+
assert sampleQ == ['sampleQ','1','2','1']
86+
sample1 = next(reader_obj)
87+
assert sample1 == ['sample1','1','1','1']
88+
89+
def test_profile_validation_noreport(tmpdir):
90+
""" Note: Similar to test_profile_validation_report, except checking that functionality stays the same without a report
91+
"""
92+
CONFIG = {'command': 'merge',
93+
'input': [[
94+
'locidex/example/merge/merge_inputassure/sampleQ.mlst.json',
95+
'locidex/example/merge/merge_inputassure/sample1.mlst.json',
96+
'locidex/example/merge/merge_inputassure/sample2.mlst.json',
97+
'locidex/example/merge/merge_inputassure/sample3.mlst.json']],
98+
'outdir': f"{tmpdir}",
99+
'strict': False,
100+
'force': True,
101+
'profile_ref': None}
102+
merge.run_merge(CONFIG)
103+
merge_output = sorted(listdir(tmpdir))
104+
assert merge_output == ["profile.tsv", "run.json"]
105+
106+
with open(f"{tmpdir}/profile.tsv", "r", newline='') as file:
107+
reader_obj = csv.reader(file, delimiter="\t",)
108+
next(reader_obj) # Skip the first line (header)
109+
sampleQ = next(reader_obj)
110+
assert sampleQ == ['sampleQ','1','2','1']
111+
sample1 = next(reader_obj)
112+
assert sample1 == ['sampleA','1','1','1']
113+

0 commit comments

Comments
 (0)