Skip to content

Commit

Permalink
Change rules around missing coverage to match ruby version for #412.
Browse files Browse the repository at this point in the history
  • Loading branch information
donkirkby committed Feb 2, 2018
1 parent 90c7bac commit 1ea6d4b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 16 deletions.
59 changes: 43 additions & 16 deletions micall/hivdb/hivdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,25 +336,52 @@ def interpret(asi, amino_seq, region):
is_missing_midi = region.endswith('-NS5b') and len(amino_seq) < len(ref_seq)

if is_missing_midi:
# At least some data found, assume wild type in gaps.
new_amino_seq = []
for wild_type, old_aminos in zip_longest(ref_seq, amino_seq):
if old_aminos is not None:
new_amino_seq.append(old_aminos)
else:
new_amino_seq.append({wild_type: 1.0})
amino_seq = new_amino_seq

amino_seq += [{}] * (len(ref_seq) - len(amino_seq))
result = asi.interpret(amino_seq, region)

if not is_missing_midi:
return result
for drug_result in result.drugs:
if drug_result.level == ResistanceLevels.FAIL.level:
break
else:
# No missing coverage, we're done.
return result

# At least some data found, assume wild type in gaps.
new_amino_seq = []
for wild_type, old_aminos in zip_longest(ref_seq, amino_seq):
if old_aminos:
new_amino_seq.append(old_aminos)
else:
new_amino_seq.append({wild_type: 1.0})
amino_seq = new_amino_seq

new_result = asi.interpret(amino_seq, region)
new_drug_results = {drug_result.code: drug_result
for drug_result in new_result.drugs}
if is_missing_midi:
allowed_levels = (ResistanceLevels.FAIL.level,
ResistanceLevels.RESISTANCE_LIKELY.level,
ResistanceLevels.NOT_INDICATED.level)
else:
allowed_levels = (ResistanceLevels.FAIL.level,
ResistanceLevels.UNKNOWN_MUTATIONS.level,
ResistanceLevels.RESISTANCE_POSSIBLE.level,
ResistanceLevels.RESISTANCE_LIKELY.level,
ResistanceLevels.NOT_INDICATED.level)

for drug_result in result.drugs:
if drug_result.level not in (ResistanceLevels.FAIL.level,
ResistanceLevels.RESISTANCE_LIKELY.level,
ResistanceLevels.NOT_INDICATED.level):
drug_result.level = ResistanceLevels.FAIL.level
drug_result.level_name = ResistanceLevels.FAIL.name
drug_result.score = 0.0
if drug_result.level == ResistanceLevels.FAIL.level or is_missing_midi:
new_drug_result = new_drug_results[drug_result.code]
if new_drug_result.level in allowed_levels:
drug_result.level = new_drug_result.level
drug_result.level_name = new_drug_result.level_name
drug_result.score = new_drug_result.score
else:
drug_result.level = ResistanceLevels.FAIL.level
drug_result.level_name = ResistanceLevels.FAIL.name
drug_result.score = 0.0
result.mutations = new_result.mutations
return result


Expand Down
25 changes: 25 additions & 0 deletions micall/tests/test_hivdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,31 @@ def test_hcv_missing_midi(self):
self.assertEqual(expected_resistance, resistance_csv.getvalue())
self.assertEqual(expected_mutations, mutations_csv.getvalue())

def test_hcv_ns3_coverage_gap(self):
self.maxDiff = None
ns3_aminos = [{c: 1.0}
for c in self.algorithms[None].stds['HCV1A-H77-NS3']]
ns3_aminos[53] = {} # Coverage gap at position 54
aminos = [AminoList('HCV1A-H77-NS3', ns3_aminos, '1A')]
resistance_csv = StringIO()
mutations_csv = StringIO()
expected_resistance = """\
region,drug_class,drug,drug_name,level,level_name,score,genotype
NS3,NS3,BPV,BPV,0,Sequence does not meet quality-control standards,0.0,1A
NS3,NS3,GZR,GZR,1,Likely Susceptible,0.0,1A
NS3,NS3,PTV,PTV,1,Likely Susceptible,0.0,1A
NS3,NS3,SPV,SPV,0,Sequence does not meet quality-control standards,0.0,1A
NS3,NS3,TPV,TPV,0,Sequence does not meet quality-control standards,0.0,1A
"""
expected_mutations = """\
drug_class,mutation,prevalence,genotype
"""

write_resistance(aminos, resistance_csv, mutations_csv, self.algorithms)

self.assertEqual(expected_resistance, resistance_csv.getvalue())
self.assertEqual(expected_mutations, mutations_csv.getvalue())

def test_hcv_low_coverage(self):
aminos = []
resistance_csv = StringIO()
Expand Down

0 comments on commit 1ea6d4b

Please sign in to comment.