Ejemplo n.º 1
0
    def test_true_positive(self):
        m = VariantCalls('Q54H 444H')
        rule = HCVR("SCORE FROM ( 54H => 0, 444H => 8 )")
        dtree = rule.dtree(m)

        expected_repr = "[Mutation('Q54H'), Mutation('444H')]"
        self.assertEqual(expected_repr, repr(sorted(dtree.residues)))
Ejemplo n.º 2
0
    def test_score_residues(self):
        rule = HCVR("SCORE FROM ( 100G => 10, 101D => 20 )")
        expected_residue = repr({Mutation('S100G')})

        result = rule.dtree(VariantCalls("S100G R101d"))

        self.assertEqual(expected_residue, repr(result.residues))
Ejemplo n.º 3
0
 def test_score_comment(self):
     rule = HCVR(
         'SCORE FROM (100G => 10, 200T => 3, 100S => "flag1 with_space")')
     self.assertEqual(rule(VariantCalls("100G 200d")), 10)
     result = rule.dtree(VariantCalls("100S 200T"))
     self.assertEqual(result.score, 3)
     self.assertIn("flag1 with_space", result.flags)
Ejemplo n.º 4
0
    def interpret(self, aaseq, region):
        result = AsiResult()
        raw_mutations = defaultdict(set)
        result.alg_name = self.alg_name
        result.alg_version = self.alg_version
        drug_classes = self.gene_def.get(region, {})
        default_level = HcvResistanceLevels.FAIL.level
        default_level_name = self.level_def[str(default_level)]

        mutations = VariantCalls(reference=self.stds[region], sample=aaseq)

        for drug_class in drug_classes:
            for drug_code in self.drug_class[drug_class]:
                drug_name, drug_rules = self.drugs[drug_code]
                drug_result = AsiDrugResult()
                drug_result.code = drug_code
                drug_result.name = drug_name
                drug_result.level = default_level
                drug_result.level_name = default_level_name
                drug_result.drug_class = drug_class

                for condition, actions in drug_rules:

                    rule = HCVR(condition)
                    try:
                        rule_result = rule.dtree(mutations)

                        score = float(rule_result.score)
                        flags = rule_result.flags
                        # rule_result.residues doesn't always have wild types.
                        m = {mutation
                             for mutation_set in mutations
                             for mutation in mutation_set
                             if mutation in rule_result.residues}
                        raw_mutations[drug_class] |= m

                        for action, comment in actions:
                            if action == 'level':
                                if int(comment) > drug_result.level:
                                    drug_result.level = int(comment)
                                    drug_result.level_name = self.level_def[comment]
                            elif action == 'comment':
                                comment, _ = self.comment_def[comment]
                                while (re.search(r'\$numberOfMutsIn{', comment) or
                                       re.search(r'\$listMutsIn{', comment)):
                                    comment = self.comment_filter(comment, aaseq, region)
                                drug_result.comments.append(comment)
                            elif action == 'scorerange':
                                drug_result.score = score
                                scorerange = comment
                                if scorerange == 'useglobalrange':
                                    scorerange = self.global_range
                                if score == 0 and flags:
                                    if 'Not available' in flags:
                                        drug_result.level = HcvResistanceLevels.NA.level
                                    elif 'Not indicated' in flags:
                                        drug_result.level = HcvResistanceLevels.NOT_INDICATED.level
                                    elif 'Effect unknown' in flags:
                                        drug_result.level = HcvResistanceLevels.UNKNOWN_MUTATIONS.level
                                else:
                                    # use score range to determine level
                                    for low_score, high_score, level in scorerange:
                                        if low_score == '-INF':
                                            low_score = -99999  # that is close enough to negative infinity.
                                        else:
                                            low_score = float(low_score)

                                        if high_score == 'INF':
                                            high_score = 99999  # that is close enough to infinity.
                                        else:
                                            high_score = float(high_score)

                                        if low_score <= drug_result.score <= high_score:
                                            if int(level) > drug_result.level:
                                                drug_result.level = int(level)
                                            break
                    except MissingPositionError:
                        drug_result.level = HcvResistanceLevels.FAIL.level

                    drug_result.level_name = self.level_def[
                        str(drug_result.level)]
                result.drugs.append(drug_result)

        for cls, cls_mutations in raw_mutations.items():
            result.mutations[cls] = [str(m) for m in cls_mutations]

        result.drugs.sort(key=lambda e: e.code)
        result.drugs.sort(key=lambda e: e.drug_class, reverse=True)
        # comments
        for target_region, results in self.mutation_comments:
            if target_region != region:
                continue
            for cond, actions in results:

                # This evaluates comment rules.
                # Previous evaluation was scoring rules.
                rule = HCVR(cond)
                try:
                    scoring = rule(mutations)

                    if scoring:
                        for _, act in actions:
                            comment_template, _ = self.comment_def[act]
                            comment = self.comment_filter(comment_template, aaseq, region)
                            result.mutation_comments.append(comment)
                except MissingPositionError:
                    pass

        return result
Ejemplo n.º 5
0
    def interpret(self, aaseq, region):
        result = AsiResult()
        raw_mutations = defaultdict(set)
        result.alg_name = self.alg_name
        result.alg_version = self.alg_version
        drug_classes = self.gene_def.get(region, {})
        default_level = ResistanceLevels.FAIL.level
        default_level_name = self.level_def[str(default_level)]

        mutations = VariantCalls(reference=self.stds[region], sample=aaseq)

        for drug_class in drug_classes:
            for drug_code in self.drug_class[drug_class]:
                drug_name, drug_rules = self.drugs[drug_code]
                drug_result = AsiDrugResult()
                drug_result.code = drug_code
                drug_result.name = drug_name
                drug_result.level = default_level
                drug_result.level_name = default_level_name
                drug_result.drug_class = drug_class

                for condition, actions in drug_rules:

                    rule = HCVR(condition)
                    try:
                        rule_result = rule.dtree(mutations)

                        score = float(rule_result.score)
                        flags = rule_result.flags
                        # rule_result.residues doesn't always have wild types.
                        m = {
                            mutation
                            for mutation_set in mutations
                            for mutation in mutation_set
                            if mutation in rule_result.residues
                        }
                        raw_mutations[drug_class] |= m

                        for action, comment in actions:
                            if action == 'level':
                                if int(comment) > drug_result.level:
                                    drug_result.level = int(comment)
                                    drug_result.level_name = self.level_def[
                                        comment]
                            elif action == 'comment':
                                comment, _ = self.comment_def[comment]
                                while (re.search('\$numberOfMutsIn{', comment)
                                       or re.search('\$listMutsIn{', comment)):
                                    comment = self.comment_filter(
                                        comment, aaseq, region)
                                drug_result.comments.append(comment)
                            elif action == 'scorerange':
                                drug_result.score = score
                                scorerange = comment
                                if scorerange == 'useglobalrange':
                                    scorerange = self.global_range
                                if score == 0 and flags:
                                    if 'Not available' in flags:
                                        drug_result.level = ResistanceLevels.NA.level
                                    elif 'Not indicated' in flags:
                                        drug_result.level = ResistanceLevels.NOT_INDICATED.level
                                    elif 'Effect unknown' in flags:
                                        drug_result.level = ResistanceLevels.UNKNOWN_MUTATIONS.level
                                else:
                                    # use score range to determine level
                                    for low_score, high_score, level in scorerange:
                                        if low_score == '-INF':
                                            low_score = -99999  # that is close enough to negative infinity.
                                        else:
                                            low_score = float(low_score)

                                        if high_score == 'INF':
                                            high_score = 99999  # that is close enough to infinity.
                                        else:
                                            high_score = float(high_score)

                                        if low_score <= drug_result.score <= high_score:
                                            if int(level) > drug_result.level:
                                                drug_result.level = int(level)
                                            break
                    except MissingPositionError:
                        drug_result.level = ResistanceLevels.FAIL.level

                    drug_result.level_name = self.level_def[str(
                        drug_result.level)]
                result.drugs.append(drug_result)

        for cls, cls_mutations in raw_mutations.items():
            result.mutations[cls] = [str(m) for m in cls_mutations]

        result.drugs.sort(key=lambda e: e.code)
        result.drugs.sort(key=lambda e: e.drug_class, reverse=True)
        # comments
        for target_region, results in self.mutation_comments:
            if target_region != region:
                continue
            for cond, actions in results:

                # This evaluates comment rules.
                # Previous evaluation was scoring rules.
                rule = HCVR(cond)
                try:
                    scoring = rule(mutations)

                    if scoring:
                        for _, act in actions:
                            comment_template, _ = self.comment_def[act]
                            comment = self.comment_filter(
                                comment_template, aaseq, region)
                            result.mutation_comments.append(comment)
                except MissingPositionError:
                    pass

        return result